libstdc++
bits/shared_ptr.h
Go to the documentation of this file.
1 // shared_ptr and weak_ptr implementation -*- C++ -*-
2 
3 // Copyright (C) 2007-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
26 
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29 
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
33 
34 // weak_ptr.hpp
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
36 
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
39 
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
43 
44 /** @file
45  * This is an internal header file, included by other library headers.
46  * Do not attempt to use it directly. @headername{memory}
47  */
48 
49 #ifndef _SHARED_PTR_H
50 #define _SHARED_PTR_H 1
51 
52 #include <bits/shared_ptr_base.h>
53 
54 namespace std _GLIBCXX_VISIBILITY(default)
55 {
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 
58  /**
59  * @addtogroup pointer_abstractions
60  * @{
61  */
62 
63  /// 20.7.2.2.11 shared_ptr I/O
64  template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
66  operator<<(std::basic_ostream<_Ch, _Tr>& __os,
67  const __shared_ptr<_Tp, _Lp>& __p)
68  {
69  __os << __p.get();
70  return __os;
71  }
72 
73  /// 20.7.2.2.10 shared_ptr get_deleter
74  template<typename _Del, typename _Tp, _Lock_policy _Lp>
75  inline _Del*
76  get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
77  {
78 #if __cpp_rtti
79  return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
80 #else
81  return 0;
82 #endif
83  }
84 
85 
86  /**
87  * @brief A smart pointer with reference-counted copy semantics.
88  *
89  * The object pointed to is deleted when the last shared_ptr pointing to
90  * it is destroyed or reset.
91  */
92  template<typename _Tp>
93  class shared_ptr : public __shared_ptr<_Tp>
94  {
95  template<typename... _Args>
96  using _Constructible = typename enable_if<
97  is_constructible<__shared_ptr<_Tp>, _Args...>::value
98  >::type;
99 
100  template<typename _Arg>
101  using _Assignable = typename enable_if<
102  is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
103  >::type;
104 
105  public:
106 
107  using element_type = typename __shared_ptr<_Tp>::element_type;
108 
109 #if __cplusplus > 201402L
110 # define __cpp_lib_shared_ptr_weak_type 201606
111  using weak_type = weak_ptr<_Tp>;
112 #endif
113  /**
114  * @brief Construct an empty %shared_ptr.
115  * @post use_count()==0 && get()==0
116  */
117  constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
118 
119  shared_ptr(const shared_ptr&) noexcept = default;
120 
121  /**
122  * @brief Construct a %shared_ptr that owns the pointer @a __p.
123  * @param __p A pointer that is convertible to element_type*.
124  * @post use_count() == 1 && get() == __p
125  * @throw std::bad_alloc, in which case @c delete @a __p is called.
126  */
127  template<typename _Yp, typename = _Constructible<_Yp*>>
128  explicit
129  shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
130 
131  /**
132  * @brief Construct a %shared_ptr that owns the pointer @a __p
133  * and the deleter @a __d.
134  * @param __p A pointer.
135  * @param __d A deleter.
136  * @post use_count() == 1 && get() == __p
137  * @throw std::bad_alloc, in which case @a __d(__p) is called.
138  *
139  * Requirements: _Deleter's copy constructor and destructor must
140  * not throw
141  *
142  * __shared_ptr will release __p by calling __d(__p)
143  */
144  template<typename _Yp, typename _Deleter,
145  typename = _Constructible<_Yp*, _Deleter>>
146  shared_ptr(_Yp* __p, _Deleter __d)
147  : __shared_ptr<_Tp>(__p, __d) { }
148 
149  /**
150  * @brief Construct a %shared_ptr that owns a null pointer
151  * and the deleter @a __d.
152  * @param __p A null pointer constant.
153  * @param __d A deleter.
154  * @post use_count() == 1 && get() == __p
155  * @throw std::bad_alloc, in which case @a __d(__p) is called.
156  *
157  * Requirements: _Deleter's copy constructor and destructor must
158  * not throw
159  *
160  * The last owner will call __d(__p)
161  */
162  template<typename _Deleter>
163  shared_ptr(nullptr_t __p, _Deleter __d)
164  : __shared_ptr<_Tp>(__p, __d) { }
165 
166  /**
167  * @brief Construct a %shared_ptr that owns the pointer @a __p
168  * and the deleter @a __d.
169  * @param __p A pointer.
170  * @param __d A deleter.
171  * @param __a An allocator.
172  * @post use_count() == 1 && get() == __p
173  * @throw std::bad_alloc, in which case @a __d(__p) is called.
174  *
175  * Requirements: _Deleter's copy constructor and destructor must
176  * not throw _Alloc's copy constructor and destructor must not
177  * throw.
178  *
179  * __shared_ptr will release __p by calling __d(__p)
180  */
181  template<typename _Yp, typename _Deleter, typename _Alloc,
182  typename = _Constructible<_Yp*, _Deleter, _Alloc>>
183  shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
184  : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { }
185 
186  /**
187  * @brief Construct a %shared_ptr that owns a null pointer
188  * and the deleter @a __d.
189  * @param __p A null pointer constant.
190  * @param __d A deleter.
191  * @param __a An allocator.
192  * @post use_count() == 1 && get() == __p
193  * @throw std::bad_alloc, in which case @a __d(__p) is called.
194  *
195  * Requirements: _Deleter's copy constructor and destructor must
196  * not throw _Alloc's copy constructor and destructor must not
197  * throw.
198  *
199  * The last owner will call __d(__p)
200  */
201  template<typename _Deleter, typename _Alloc>
202  shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
203  : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { }
204 
205  // Aliasing constructor
206 
207  /**
208  * @brief Constructs a %shared_ptr instance that stores @a __p
209  * and shares ownership with @a __r.
210  * @param __r A %shared_ptr.
211  * @param __p A pointer that will remain valid while @a *__r is valid.
212  * @post get() == __p && use_count() == __r.use_count()
213  *
214  * This can be used to construct a @c shared_ptr to a sub-object
215  * of an object managed by an existing @c shared_ptr.
216  *
217  * @code
218  * shared_ptr< pair<int,int> > pii(new pair<int,int>());
219  * shared_ptr<int> pi(pii, &pii->first);
220  * assert(pii.use_count() == 2);
221  * @endcode
222  */
223  template<typename _Yp>
224  shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
225  : __shared_ptr<_Tp>(__r, __p) { }
226 
227  /**
228  * @brief If @a __r is empty, constructs an empty %shared_ptr;
229  * otherwise construct a %shared_ptr that shares ownership
230  * with @a __r.
231  * @param __r A %shared_ptr.
232  * @post get() == __r.get() && use_count() == __r.use_count()
233  */
234  template<typename _Yp,
235  typename = _Constructible<const shared_ptr<_Yp>&>>
236  shared_ptr(const shared_ptr<_Yp>& __r) noexcept
237  : __shared_ptr<_Tp>(__r) { }
238 
239  /**
240  * @brief Move-constructs a %shared_ptr instance from @a __r.
241  * @param __r A %shared_ptr rvalue.
242  * @post *this contains the old value of @a __r, @a __r is empty.
243  */
244  shared_ptr(shared_ptr&& __r) noexcept
245  : __shared_ptr<_Tp>(std::move(__r)) { }
246 
247  /**
248  * @brief Move-constructs a %shared_ptr instance from @a __r.
249  * @param __r A %shared_ptr rvalue.
250  * @post *this contains the old value of @a __r, @a __r is empty.
251  */
252  template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
253  shared_ptr(shared_ptr<_Yp>&& __r) noexcept
254  : __shared_ptr<_Tp>(std::move(__r)) { }
255 
256  /**
257  * @brief Constructs a %shared_ptr that shares ownership with @a __r
258  * and stores a copy of the pointer stored in @a __r.
259  * @param __r A weak_ptr.
260  * @post use_count() == __r.use_count()
261  * @throw bad_weak_ptr when __r.expired(),
262  * in which case the constructor has no effect.
263  */
264  template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
265  explicit shared_ptr(const weak_ptr<_Yp>& __r)
266  : __shared_ptr<_Tp>(__r) { }
267 
268 #if _GLIBCXX_USE_DEPRECATED
269  template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
270  shared_ptr(auto_ptr<_Yp>&& __r);
271 #endif
272 
273  // _GLIBCXX_RESOLVE_LIB_DEFECTS
274  // 2399. shared_ptr's constructor from unique_ptr should be constrained
275  template<typename _Yp, typename _Del,
276  typename = _Constructible<unique_ptr<_Yp, _Del>>>
278  : __shared_ptr<_Tp>(std::move(__r)) { }
279 
280 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
281  // This non-standard constructor exists to support conversions that
282  // were possible in C++11 and C++14 but are ill-formed in C++17.
283  // If an exception is thrown this constructor has no effect.
284  template<typename _Yp, typename _Del,
285  _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
287  : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
288 #endif
289 
290  /**
291  * @brief Construct an empty %shared_ptr.
292  * @post use_count() == 0 && get() == nullptr
293  */
294  constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
295 
296  shared_ptr& operator=(const shared_ptr&) noexcept = default;
297 
298  template<typename _Yp>
299  _Assignable<const shared_ptr<_Yp>&>
300  operator=(const shared_ptr<_Yp>& __r) noexcept
301  {
302  this->__shared_ptr<_Tp>::operator=(__r);
303  return *this;
304  }
305 
306 #if _GLIBCXX_USE_DEPRECATED
307  template<typename _Yp>
308  _Assignable<auto_ptr<_Yp>>
309  operator=(auto_ptr<_Yp>&& __r)
310  {
311  this->__shared_ptr<_Tp>::operator=(std::move(__r));
312  return *this;
313  }
314 #endif
315 
316  shared_ptr&
317  operator=(shared_ptr&& __r) noexcept
318  {
319  this->__shared_ptr<_Tp>::operator=(std::move(__r));
320  return *this;
321  }
322 
323  template<class _Yp>
324  _Assignable<shared_ptr<_Yp>>
325  operator=(shared_ptr<_Yp>&& __r) noexcept
326  {
327  this->__shared_ptr<_Tp>::operator=(std::move(__r));
328  return *this;
329  }
330 
331  template<typename _Yp, typename _Del>
332  _Assignable<unique_ptr<_Yp, _Del>>
333  operator=(unique_ptr<_Yp, _Del>&& __r)
334  {
335  this->__shared_ptr<_Tp>::operator=(std::move(__r));
336  return *this;
337  }
338 
339  private:
340  // This constructor is non-standard, it is used by allocate_shared.
341  template<typename _Alloc, typename... _Args>
342  shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
343  _Args&&... __args)
344  : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
345  { }
346 
347  template<typename _Yp, typename _Alloc, typename... _Args>
348  friend shared_ptr<_Yp>
349  allocate_shared(const _Alloc& __a, _Args&&... __args);
350 
351  // This constructor is non-standard, it is used by weak_ptr::lock().
352  shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
353  : __shared_ptr<_Tp>(__r, std::nothrow) { }
354 
355  friend class weak_ptr<_Tp>;
356  };
357 
358  // 20.7.2.2.7 shared_ptr comparisons
359  template<typename _Tp, typename _Up>
360  inline bool
361  operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
362  { return __a.get() == __b.get(); }
363 
364  template<typename _Tp>
365  inline bool
366  operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
367  { return !__a; }
368 
369  template<typename _Tp>
370  inline bool
371  operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
372  { return !__a; }
373 
374  template<typename _Tp, typename _Up>
375  inline bool
376  operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
377  { return __a.get() != __b.get(); }
378 
379  template<typename _Tp>
380  inline bool
381  operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
382  { return (bool)__a; }
383 
384  template<typename _Tp>
385  inline bool
386  operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
387  { return (bool)__a; }
388 
389  template<typename _Tp, typename _Up>
390  inline bool
391  operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
392  {
393  using _Tp_elt = typename shared_ptr<_Tp>::element_type;
394  using _Up_elt = typename shared_ptr<_Up>::element_type;
395  using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
396  return less<_Vp>()(__a.get(), __b.get());
397  }
398 
399  template<typename _Tp>
400  inline bool
401  operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
402  {
403  using _Tp_elt = typename shared_ptr<_Tp>::element_type;
404  return less<_Tp_elt*>()(__a.get(), nullptr);
405  }
406 
407  template<typename _Tp>
408  inline bool
409  operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
410  {
411  using _Tp_elt = typename shared_ptr<_Tp>::element_type;
412  return less<_Tp_elt*>()(nullptr, __a.get());
413  }
414 
415  template<typename _Tp, typename _Up>
416  inline bool
417  operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
418  { return !(__b < __a); }
419 
420  template<typename _Tp>
421  inline bool
422  operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
423  { return !(nullptr < __a); }
424 
425  template<typename _Tp>
426  inline bool
427  operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
428  { return !(__a < nullptr); }
429 
430  template<typename _Tp, typename _Up>
431  inline bool
432  operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
433  { return (__b < __a); }
434 
435  template<typename _Tp>
436  inline bool
437  operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
438  { return nullptr < __a; }
439 
440  template<typename _Tp>
441  inline bool
442  operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
443  { return __a < nullptr; }
444 
445  template<typename _Tp, typename _Up>
446  inline bool
447  operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
448  { return !(__a < __b); }
449 
450  template<typename _Tp>
451  inline bool
452  operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
453  { return !(__a < nullptr); }
454 
455  template<typename _Tp>
456  inline bool
457  operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
458  { return !(nullptr < __a); }
459 
460  template<typename _Tp>
461  struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
462  { };
463 
464  // 20.7.2.2.8 shared_ptr specialized algorithms.
465  template<typename _Tp>
466  inline void
467  swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
468  { __a.swap(__b); }
469 
470  // 20.7.2.2.9 shared_ptr casts.
471  template<typename _Tp, typename _Up>
472  inline shared_ptr<_Tp>
473  static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
474  {
475  using _Sp = shared_ptr<_Tp>;
476  return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
477  }
478 
479  template<typename _Tp, typename _Up>
480  inline shared_ptr<_Tp>
481  const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
482  {
483  using _Sp = shared_ptr<_Tp>;
484  return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
485  }
486 
487  template<typename _Tp, typename _Up>
488  inline shared_ptr<_Tp>
489  dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
490  {
491  using _Sp = shared_ptr<_Tp>;
492  if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
493  return _Sp(__r, __p);
494  return _Sp();
495  }
496 
497 #if __cplusplus > 201402L
498  template<typename _Tp, typename _Up>
499  inline shared_ptr<_Tp>
500  reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
501  {
502  using _Sp = shared_ptr<_Tp>;
503  return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
504  }
505 #endif
506 
507  /**
508  * @brief A smart pointer with weak semantics.
509  *
510  * With forwarding constructors and assignment operators.
511  */
512  template<typename _Tp>
513  class weak_ptr : public __weak_ptr<_Tp>
514  {
515  template<typename _Arg>
516  using _Constructible = typename enable_if<
517  is_constructible<__weak_ptr<_Tp>, _Arg>::value
518  >::type;
519 
520  template<typename _Arg>
521  using _Assignable = typename enable_if<
522  is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
523  >::type;
524 
525  public:
526  constexpr weak_ptr() noexcept = default;
527 
528  template<typename _Yp,
529  typename = _Constructible<const shared_ptr<_Yp>&>>
530  weak_ptr(const shared_ptr<_Yp>& __r) noexcept
531  : __weak_ptr<_Tp>(__r) { }
532 
533  weak_ptr(const weak_ptr&) noexcept = default;
534 
535  template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
536  weak_ptr(const weak_ptr<_Yp>& __r) noexcept
537  : __weak_ptr<_Tp>(__r) { }
538 
539  weak_ptr(weak_ptr&&) noexcept = default;
540 
541  template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
542  weak_ptr(weak_ptr<_Yp>&& __r) noexcept
543  : __weak_ptr<_Tp>(std::move(__r)) { }
544 
545  weak_ptr&
546  operator=(const weak_ptr& __r) noexcept = default;
547 
548  template<typename _Yp>
549  _Assignable<const weak_ptr<_Yp>&>
550  operator=(const weak_ptr<_Yp>& __r) noexcept
551  {
552  this->__weak_ptr<_Tp>::operator=(__r);
553  return *this;
554  }
555 
556  template<typename _Yp>
557  _Assignable<const shared_ptr<_Yp>&>
558  operator=(const shared_ptr<_Yp>& __r) noexcept
559  {
560  this->__weak_ptr<_Tp>::operator=(__r);
561  return *this;
562  }
563 
564  weak_ptr&
565  operator=(weak_ptr&& __r) noexcept = default;
566 
567  template<typename _Yp>
568  _Assignable<weak_ptr<_Yp>>
569  operator=(weak_ptr<_Yp>&& __r) noexcept
570  {
571  this->__weak_ptr<_Tp>::operator=(std::move(__r));
572  return *this;
573  }
574 
576  lock() const noexcept
577  { return shared_ptr<_Tp>(*this, std::nothrow); }
578  };
579 
580  // 20.7.2.3.6 weak_ptr specialized algorithms.
581  template<typename _Tp>
582  inline void
583  swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
584  { __a.swap(__b); }
585 
586 
587  /// Primary template owner_less
588  template<typename _Tp = void>
589  struct owner_less;
590 
591  /// Void specialization of owner_less
592  template<>
593  struct owner_less<void> : _Sp_owner_less<void, void>
594  { };
595 
596  /// Partial specialization of owner_less for shared_ptr.
597  template<typename _Tp>
598  struct owner_less<shared_ptr<_Tp>>
599  : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
600  { };
601 
602  /// Partial specialization of owner_less for weak_ptr.
603  template<typename _Tp>
604  struct owner_less<weak_ptr<_Tp>>
605  : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
606  { };
607 
608  /**
609  * @brief Base class allowing use of member function shared_from_this.
610  */
611  template<typename _Tp>
613  {
614  protected:
615  constexpr enable_shared_from_this() noexcept { }
616 
618 
620  operator=(const enable_shared_from_this&) noexcept
621  { return *this; }
622 
624 
625  public:
627  shared_from_this()
628  { return shared_ptr<_Tp>(this->_M_weak_this); }
629 
631  shared_from_this() const
632  { return shared_ptr<const _Tp>(this->_M_weak_this); }
633 
634 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
635 #define __cpp_lib_enable_shared_from_this 201603
637  weak_from_this() noexcept
638  { return this->_M_weak_this; }
639 
641  weak_from_this() const noexcept
642  { return this->_M_weak_this; }
643 #endif
644 
645  private:
646  template<typename _Tp1>
647  void
648  _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
649  { _M_weak_this._M_assign(__p, __n); }
650 
651  // Found by ADL when this is an associated class.
652  friend const enable_shared_from_this*
653  __enable_shared_from_this_base(const __shared_count<>&,
654  const enable_shared_from_this* __p)
655  { return __p; }
656 
657  template<typename, _Lock_policy>
658  friend class __shared_ptr;
659 
660  mutable weak_ptr<_Tp> _M_weak_this;
661  };
662 
663  /**
664  * @brief Create an object that is owned by a shared_ptr.
665  * @param __a An allocator.
666  * @param __args Arguments for the @a _Tp object's constructor.
667  * @return A shared_ptr that owns the newly created object.
668  * @throw An exception thrown from @a _Alloc::allocate or from the
669  * constructor of @a _Tp.
670  *
671  * A copy of @a __a will be used to allocate memory for the shared_ptr
672  * and the new object.
673  */
674  template<typename _Tp, typename _Alloc, typename... _Args>
675  inline shared_ptr<_Tp>
676  allocate_shared(const _Alloc& __a, _Args&&... __args)
677  {
678  return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a,
679  std::forward<_Args>(__args)...);
680  }
681 
682  /**
683  * @brief Create an object that is owned by a shared_ptr.
684  * @param __args Arguments for the @a _Tp object's constructor.
685  * @return A shared_ptr that owns the newly created object.
686  * @throw std::bad_alloc, or an exception thrown from the
687  * constructor of @a _Tp.
688  */
689  template<typename _Tp, typename... _Args>
690  inline shared_ptr<_Tp>
691  make_shared(_Args&&... __args)
692  {
693  typedef typename std::remove_const<_Tp>::type _Tp_nc;
694  return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
695  std::forward<_Args>(__args)...);
696  }
697 
698  /// std::hash specialization for shared_ptr.
699  template<typename _Tp>
700  struct hash<shared_ptr<_Tp>>
701  : public __hash_base<size_t, shared_ptr<_Tp>>
702  {
703  size_t
704  operator()(const shared_ptr<_Tp>& __s) const noexcept
705  {
707  }
708  };
709 
710  // @} group pointer_abstractions
711 
712 _GLIBCXX_END_NAMESPACE_VERSION
713 } // namespace
714 
715 #endif // _SHARED_PTR_H
shared_ptr(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer and the deleter __d.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
Primary class template hash.
Definition: system_error:141
friend shared_ptr< _Yp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
ISO C++ entities toplevel namespace is std.
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns a null pointer and the deleter __d.
shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
Template class basic_ostream.
Definition: iosfwd:86
Primary template owner_less.
constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
A smart pointer with reference-counted copy semantics.
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
Definition: mutex:542
shared_ptr(const shared_ptr< _Yp > &__r) noexcept
If __r is empty, constructs an empty shared_ptr; otherwise construct a shared_ptr that shares ownersh...
The standard allocator, as per [20.4].
Definition: allocator.h:108
shared_ptr(_Yp *__p)
Construct a shared_ptr that owns the pointer __p.
shared_ptr(const weak_ptr< _Yp > &__r)
Constructs a shared_ptr that shares ownership with __r and stores a copy of the pointer stored in __r...
A smart pointer with weak semantics.
shared_ptr(_Yp *__p, _Deleter __d)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
shared_ptr(_Yp *__p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:87
shared_ptr(shared_ptr< _Yp > &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
Base class allowing use of member function shared_from_this.
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:157
shared_ptr(const shared_ptr< _Yp > &__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
One of the comparison functors.
Definition: stl_function.h:340
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter