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-2021 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 <iosfwd> // std::basic_ostream
53 #include <bits/shared_ptr_base.h>
54 
55 namespace std _GLIBCXX_VISIBILITY(default)
56 {
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 
59  /**
60  * @addtogroup pointer_abstractions
61  * @{
62  */
63 
64  // 20.7.2.2.11 shared_ptr I/O
65 
66  /// Write the stored pointer to an ostream.
67  /// @relates shared_ptr
68  template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
71  const __shared_ptr<_Tp, _Lp>& __p)
72  {
73  __os << __p.get();
74  return __os;
75  }
76 
77  template<typename _Del, typename _Tp, _Lock_policy _Lp>
78  inline _Del*
79  get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
80  {
81 #if __cpp_rtti
82  return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
83 #else
84  return 0;
85 #endif
86  }
87 
88  /// 20.7.2.2.10 shared_ptr get_deleter
89 
90  /// If `__p` has a deleter of type `_Del`, return a pointer to it.
91  /// @relates shared_ptr
92  template<typename _Del, typename _Tp>
93  inline _Del*
94  get_deleter(const shared_ptr<_Tp>& __p) noexcept
95  {
96 #if __cpp_rtti
97  return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
98 #else
99  return 0;
100 #endif
101  }
102 
103  /**
104  * @brief A smart pointer with reference-counted copy semantics.
105  * @headerfile memory
106  * @since C++11
107  *
108  * A `shared_ptr` object is either empty or _owns_ a pointer passed
109  * to the constructor. Copies of a `shared_ptr` share ownership of
110  * the same pointer. When the last `shared_ptr` that owns the pointer
111  * is destroyed or reset, the owned pointer is freed (either by `delete`
112  * or by invoking a custom deleter that was passed to the constructor).
113  *
114  * A `shared_ptr` also stores another pointer, which is usually
115  * (but not always) the same pointer as it owns. The stored pointer
116  * can be retrieved by calling the `get()` member function.
117  *
118  * The equality and relational operators for `shared_ptr` only compare
119  * the stored pointer returned by `get()`, not the owned pointer.
120  * To test whether two `shared_ptr` objects share ownership of the same
121  * pointer see `std::shared_ptr::owner_before` and `std::owner_less`.
122  */
123  template<typename _Tp>
124  class shared_ptr : public __shared_ptr<_Tp>
125  {
126  template<typename... _Args>
127  using _Constructible = typename enable_if<
128  is_constructible<__shared_ptr<_Tp>, _Args...>::value
129  >::type;
130 
131  template<typename _Arg>
132  using _Assignable = typename enable_if<
134  >::type;
135 
136  public:
137 
138  /// The type pointed to by the stored pointer, remove_extent_t<_Tp>
139  using element_type = typename __shared_ptr<_Tp>::element_type;
140 
141 #if __cplusplus >= 201703L
142 # define __cpp_lib_shared_ptr_weak_type 201606
143  /// The corresponding weak_ptr type for this shared_ptr
144  /// @since C++17
146 #endif
147  /**
148  * @brief Construct an empty %shared_ptr.
149  * @post use_count()==0 && get()==0
150  */
151  constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
152 
153  shared_ptr(const shared_ptr&) noexcept = default; ///< Copy constructor
154 
155  /**
156  * @brief Construct a %shared_ptr that owns the pointer @a __p.
157  * @param __p A pointer that is convertible to element_type*.
158  * @post use_count() == 1 && get() == __p
159  * @throw std::bad_alloc, in which case @c delete @a __p is called.
160  */
161  template<typename _Yp, typename = _Constructible<_Yp*>>
162  explicit
163  shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
164 
165  /**
166  * @brief Construct a %shared_ptr that owns the pointer @a __p
167  * and the deleter @a __d.
168  * @param __p A pointer.
169  * @param __d A deleter.
170  * @post use_count() == 1 && get() == __p
171  * @throw std::bad_alloc, in which case @a __d(__p) is called.
172  *
173  * Requirements: _Deleter's copy constructor and destructor must
174  * not throw
175  *
176  * __shared_ptr will release __p by calling __d(__p)
177  */
178  template<typename _Yp, typename _Deleter,
179  typename = _Constructible<_Yp*, _Deleter>>
180  shared_ptr(_Yp* __p, _Deleter __d)
181  : __shared_ptr<_Tp>(__p, std::move(__d)) { }
182 
183  /**
184  * @brief Construct a %shared_ptr that owns a null pointer
185  * and the deleter @a __d.
186  * @param __p A null pointer constant.
187  * @param __d A deleter.
188  * @post use_count() == 1 && get() == __p
189  * @throw std::bad_alloc, in which case @a __d(__p) is called.
190  *
191  * Requirements: _Deleter's copy constructor and destructor must
192  * not throw
193  *
194  * The last owner will call __d(__p)
195  */
196  template<typename _Deleter>
197  shared_ptr(nullptr_t __p, _Deleter __d)
198  : __shared_ptr<_Tp>(__p, std::move(__d)) { }
199 
200  /**
201  * @brief Construct a %shared_ptr that owns the pointer @a __p
202  * and the deleter @a __d.
203  * @param __p A pointer.
204  * @param __d A deleter.
205  * @param __a An allocator.
206  * @post use_count() == 1 && get() == __p
207  * @throw std::bad_alloc, in which case @a __d(__p) is called.
208  *
209  * Requirements: _Deleter's copy constructor and destructor must
210  * not throw _Alloc's copy constructor and destructor must not
211  * throw.
212  *
213  * __shared_ptr will release __p by calling __d(__p)
214  */
215  template<typename _Yp, typename _Deleter, typename _Alloc,
216  typename = _Constructible<_Yp*, _Deleter, _Alloc>>
217  shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
218  : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
219 
220  /**
221  * @brief Construct a %shared_ptr that owns a null pointer
222  * and the deleter @a __d.
223  * @param __p A null pointer constant.
224  * @param __d A deleter.
225  * @param __a An allocator.
226  * @post use_count() == 1 && get() == __p
227  * @throw std::bad_alloc, in which case @a __d(__p) is called.
228  *
229  * Requirements: _Deleter's copy constructor and destructor must
230  * not throw _Alloc's copy constructor and destructor must not
231  * throw.
232  *
233  * The last owner will call __d(__p)
234  */
235  template<typename _Deleter, typename _Alloc>
236  shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
237  : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
238 
239  // Aliasing constructor
240 
241  /**
242  * @brief Constructs a `shared_ptr` instance that stores `__p`
243  * and shares ownership with `__r`.
244  * @param __r A `shared_ptr`.
245  * @param __p A pointer that will remain valid while `*__r` is valid.
246  * @post `get() == __p && use_count() == __r.use_count()`
247  *
248  * This can be used to construct a `shared_ptr` to a sub-object
249  * of an object managed by an existing `shared_ptr`. The complete
250  * object will remain valid while any `shared_ptr` owns it, even
251  * if they don't store a pointer to the complete object.
252  *
253  * @code
254  * shared_ptr<pair<int,int>> pii(new pair<int,int>());
255  * shared_ptr<int> pi(pii, &pii->first);
256  * assert(pii.use_count() == 2);
257  * @endcode
258  */
259  template<typename _Yp>
260  shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
261  : __shared_ptr<_Tp>(__r, __p) { }
262 
263 #if __cplusplus > 201703L
264  // _GLIBCXX_RESOLVE_LIB_DEFECTS
265  // 2996. Missing rvalue overloads for shared_ptr operations
266  /**
267  * @brief Constructs a `shared_ptr` instance that stores `__p`
268  * and shares ownership with `__r`.
269  * @param __r A `shared_ptr`.
270  * @param __p A pointer that will remain valid while `*__r` is valid.
271  * @post `get() == __p && !__r.use_count() && !__r.get()`
272  * @since C++17
273  *
274  * This can be used to construct a `shared_ptr` to a sub-object
275  * of an object managed by an existing `shared_ptr`. The complete
276  * object will remain valid while any `shared_ptr` owns it, even
277  * if they don't store a pointer to the complete object.
278  *
279  * @code
280  * shared_ptr<pair<int,int>> pii(new pair<int,int>());
281  * shared_ptr<int> pi1(pii, &pii->first);
282  * assert(pii.use_count() == 2);
283  * shared_ptr<int> pi2(std::move(pii), &pii->second);
284  * assert(pii.use_count() == 0);
285  * @endcode
286  */
287  template<typename _Yp>
289  : __shared_ptr<_Tp>(std::move(__r), __p) { }
290 #endif
291  /**
292  * @brief If @a __r is empty, constructs an empty %shared_ptr;
293  * otherwise construct a %shared_ptr that shares ownership
294  * with @a __r.
295  * @param __r A %shared_ptr.
296  * @post get() == __r.get() && use_count() == __r.use_count()
297  */
298  template<typename _Yp,
299  typename = _Constructible<const shared_ptr<_Yp>&>>
300  shared_ptr(const shared_ptr<_Yp>& __r) noexcept
301  : __shared_ptr<_Tp>(__r) { }
302 
303  /**
304  * @brief Move-constructs a %shared_ptr instance from @a __r.
305  * @param __r A %shared_ptr rvalue.
306  * @post *this contains the old value of @a __r, @a __r is empty.
307  */
308  shared_ptr(shared_ptr&& __r) noexcept
309  : __shared_ptr<_Tp>(std::move(__r)) { }
310 
311  /**
312  * @brief Move-constructs a %shared_ptr instance from @a __r.
313  * @param __r A %shared_ptr rvalue.
314  * @post *this contains the old value of @a __r, @a __r is empty.
315  */
316  template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
317  shared_ptr(shared_ptr<_Yp>&& __r) noexcept
318  : __shared_ptr<_Tp>(std::move(__r)) { }
319 
320  /**
321  * @brief Constructs a %shared_ptr that shares ownership with @a __r
322  * and stores a copy of the pointer stored in @a __r.
323  * @param __r A weak_ptr.
324  * @post use_count() == __r.use_count()
325  * @throw bad_weak_ptr when __r.expired(),
326  * in which case the constructor has no effect.
327  */
328  template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
329  explicit shared_ptr(const weak_ptr<_Yp>& __r)
330  : __shared_ptr<_Tp>(__r) { }
331 
332 #if _GLIBCXX_USE_DEPRECATED
333 #pragma GCC diagnostic push
334 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
335  template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
336  shared_ptr(auto_ptr<_Yp>&& __r);
337 #pragma GCC diagnostic pop
338 #endif
339 
340  // _GLIBCXX_RESOLVE_LIB_DEFECTS
341  // 2399. shared_ptr's constructor from unique_ptr should be constrained
342  template<typename _Yp, typename _Del,
343  typename = _Constructible<unique_ptr<_Yp, _Del>>>
345  : __shared_ptr<_Tp>(std::move(__r)) { }
346 
347 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
348  // This non-standard constructor exists to support conversions that
349  // were possible in C++11 and C++14 but are ill-formed in C++17.
350  // If an exception is thrown this constructor has no effect.
351  template<typename _Yp, typename _Del,
352  _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
353  shared_ptr(unique_ptr<_Yp, _Del>&& __r)
354  : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
355 #endif
356 
357  /**
358  * @brief Construct an empty %shared_ptr.
359  * @post use_count() == 0 && get() == nullptr
360  */
361  constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
362 
363  shared_ptr& operator=(const shared_ptr&) noexcept = default;
364 
365  template<typename _Yp>
366  _Assignable<const shared_ptr<_Yp>&>
367  operator=(const shared_ptr<_Yp>& __r) noexcept
368  {
369  this->__shared_ptr<_Tp>::operator=(__r);
370  return *this;
371  }
372 
373 #if _GLIBCXX_USE_DEPRECATED
374 #pragma GCC diagnostic push
375 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
376  template<typename _Yp>
377  _Assignable<auto_ptr<_Yp>>
378  operator=(auto_ptr<_Yp>&& __r)
379  {
380  this->__shared_ptr<_Tp>::operator=(std::move(__r));
381  return *this;
382  }
383 #pragma GCC diagnostic pop
384 #endif
385 
386  shared_ptr&
387  operator=(shared_ptr&& __r) noexcept
388  {
389  this->__shared_ptr<_Tp>::operator=(std::move(__r));
390  return *this;
391  }
392 
393  template<class _Yp>
394  _Assignable<shared_ptr<_Yp>>
395  operator=(shared_ptr<_Yp>&& __r) noexcept
396  {
397  this->__shared_ptr<_Tp>::operator=(std::move(__r));
398  return *this;
399  }
400 
401  template<typename _Yp, typename _Del>
402  _Assignable<unique_ptr<_Yp, _Del>>
403  operator=(unique_ptr<_Yp, _Del>&& __r)
404  {
405  this->__shared_ptr<_Tp>::operator=(std::move(__r));
406  return *this;
407  }
408 
409  private:
410  // This constructor is non-standard, it is used by allocate_shared.
411  template<typename _Alloc, typename... _Args>
412  shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
413  : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
414  { }
415 
416  template<typename _Yp, typename _Alloc, typename... _Args>
417  friend shared_ptr<_Yp>
418  allocate_shared(const _Alloc& __a, _Args&&... __args);
419 
420  // This constructor is non-standard, it is used by weak_ptr::lock().
421  shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) noexcept
422  : __shared_ptr<_Tp>(__r, std::nothrow) { }
423 
424  friend class weak_ptr<_Tp>;
425  };
426 
427 #if __cpp_deduction_guides >= 201606
428  template<typename _Tp>
429  shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
430  template<typename _Tp, typename _Del>
431  shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
432 #endif
433 
434  // 20.7.2.2.7 shared_ptr comparisons
435 
436  /// @relates shared_ptr @{
437 
438  /// Equality operator for shared_ptr objects, compares the stored pointers
439  template<typename _Tp, typename _Up>
440  _GLIBCXX_NODISCARD inline bool
441  operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
442  { return __a.get() == __b.get(); }
443 
444  /// shared_ptr comparison with nullptr
445  template<typename _Tp>
446  _GLIBCXX_NODISCARD inline bool
447  operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
448  { return !__a; }
449 
450 #ifdef __cpp_lib_three_way_comparison
451  template<typename _Tp, typename _Up>
452  inline strong_ordering
453  operator<=>(const shared_ptr<_Tp>& __a,
454  const shared_ptr<_Up>& __b) noexcept
455  { return compare_three_way()(__a.get(), __b.get()); }
456 
457  template<typename _Tp>
458  inline strong_ordering
459  operator<=>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
460  {
461  using pointer = typename shared_ptr<_Tp>::element_type*;
462  return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
463  }
464 #else
465  /// shared_ptr comparison with nullptr
466  template<typename _Tp>
467  _GLIBCXX_NODISCARD inline bool
468  operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
469  { return !__a; }
470 
471  /// Inequality operator for shared_ptr objects, compares the stored pointers
472  template<typename _Tp, typename _Up>
473  _GLIBCXX_NODISCARD inline bool
474  operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
475  { return __a.get() != __b.get(); }
476 
477  /// shared_ptr comparison with nullptr
478  template<typename _Tp>
479  _GLIBCXX_NODISCARD inline bool
480  operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
481  { return (bool)__a; }
482 
483  /// shared_ptr comparison with nullptr
484  template<typename _Tp>
485  _GLIBCXX_NODISCARD inline bool
486  operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
487  { return (bool)__a; }
488 
489  /// Relational operator for shared_ptr objects, compares the stored pointers
490  template<typename _Tp, typename _Up>
491  _GLIBCXX_NODISCARD inline bool
492  operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
493  {
494  using _Tp_elt = typename shared_ptr<_Tp>::element_type;
495  using _Up_elt = typename shared_ptr<_Up>::element_type;
496  using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
497  return less<_Vp>()(__a.get(), __b.get());
498  }
499 
500  /// shared_ptr comparison with nullptr
501  template<typename _Tp>
502  _GLIBCXX_NODISCARD inline bool
503  operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
504  {
505  using _Tp_elt = typename shared_ptr<_Tp>::element_type;
506  return less<_Tp_elt*>()(__a.get(), nullptr);
507  }
508 
509  /// shared_ptr comparison with nullptr
510  template<typename _Tp>
511  _GLIBCXX_NODISCARD inline bool
512  operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
513  {
514  using _Tp_elt = typename shared_ptr<_Tp>::element_type;
515  return less<_Tp_elt*>()(nullptr, __a.get());
516  }
517 
518  /// Relational operator for shared_ptr objects, compares the stored pointers
519  template<typename _Tp, typename _Up>
520  _GLIBCXX_NODISCARD inline bool
521  operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
522  { return !(__b < __a); }
523 
524  /// shared_ptr comparison with nullptr
525  template<typename _Tp>
526  _GLIBCXX_NODISCARD inline bool
527  operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
528  { return !(nullptr < __a); }
529 
530  /// shared_ptr comparison with nullptr
531  template<typename _Tp>
532  _GLIBCXX_NODISCARD inline bool
533  operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
534  { return !(__a < nullptr); }
535 
536  /// Relational operator for shared_ptr objects, compares the stored pointers
537  template<typename _Tp, typename _Up>
538  _GLIBCXX_NODISCARD inline bool
539  operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
540  { return (__b < __a); }
541 
542  /// shared_ptr comparison with nullptr
543  template<typename _Tp>
544  _GLIBCXX_NODISCARD inline bool
545  operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
546  { return nullptr < __a; }
547 
548  /// shared_ptr comparison with nullptr
549  template<typename _Tp>
550  _GLIBCXX_NODISCARD inline bool
551  operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
552  { return __a < nullptr; }
553 
554  /// Relational operator for shared_ptr objects, compares the stored pointers
555  template<typename _Tp, typename _Up>
556  _GLIBCXX_NODISCARD inline bool
557  operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
558  { return !(__a < __b); }
559 
560  /// shared_ptr comparison with nullptr
561  template<typename _Tp>
562  _GLIBCXX_NODISCARD inline bool
563  operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
564  { return !(__a < nullptr); }
565 
566  /// shared_ptr comparison with nullptr
567  template<typename _Tp>
568  _GLIBCXX_NODISCARD inline bool
569  operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
570  { return !(nullptr < __a); }
571 #endif
572 
573  // 20.7.2.2.8 shared_ptr specialized algorithms.
574 
575  /// Swap overload for shared_ptr
576  template<typename _Tp>
577  inline void
578  swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
579  { __a.swap(__b); }
580 
581  // 20.7.2.2.9 shared_ptr casts.
582 
583  /// Convert type of `shared_ptr`, via `static_cast`
584  template<typename _Tp, typename _Up>
585  inline shared_ptr<_Tp>
587  {
588  using _Sp = shared_ptr<_Tp>;
589  return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
590  }
591 
592  /// Convert type of `shared_ptr`, via `const_cast`
593  template<typename _Tp, typename _Up>
594  inline shared_ptr<_Tp>
595  const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
596  {
597  using _Sp = shared_ptr<_Tp>;
598  return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
599  }
600 
601  /// Convert type of `shared_ptr`, via `dynamic_cast`
602  template<typename _Tp, typename _Up>
603  inline shared_ptr<_Tp>
605  {
606  using _Sp = shared_ptr<_Tp>;
607  if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
608  return _Sp(__r, __p);
609  return _Sp();
610  }
611 
612 #if __cplusplus >= 201703L
613  /// Convert type of `shared_ptr`, via `reinterpret_cast`
614  /// @since C++17
615  template<typename _Tp, typename _Up>
616  inline shared_ptr<_Tp>
618  {
619  using _Sp = shared_ptr<_Tp>;
620  return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
621  }
622 
623 #if __cplusplus > 201703L
624  // _GLIBCXX_RESOLVE_LIB_DEFECTS
625  // 2996. Missing rvalue overloads for shared_ptr operations
626 
627  /// Convert type of `shared_ptr` rvalue, via `static_cast`
628  /// @since C++20
629  template<typename _Tp, typename _Up>
630  inline shared_ptr<_Tp>
632  {
633  using _Sp = shared_ptr<_Tp>;
634  return _Sp(std::move(__r),
635  static_cast<typename _Sp::element_type*>(__r.get()));
636  }
637 
638  /// Convert type of `shared_ptr` rvalue, via `const_cast`
639  /// @since C++20
640  template<typename _Tp, typename _Up>
641  inline shared_ptr<_Tp>
643  {
644  using _Sp = shared_ptr<_Tp>;
645  return _Sp(std::move(__r),
646  const_cast<typename _Sp::element_type*>(__r.get()));
647  }
648 
649  /// Convert type of `shared_ptr` rvalue, via `dynamic_cast`
650  /// @since C++20
651  template<typename _Tp, typename _Up>
652  inline shared_ptr<_Tp>
654  {
655  using _Sp = shared_ptr<_Tp>;
656  if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
657  return _Sp(std::move(__r), __p);
658  return _Sp();
659  }
660 
661  /// Convert type of `shared_ptr` rvalue, via `reinterpret_cast`
662  /// @since C++20
663  template<typename _Tp, typename _Up>
664  inline shared_ptr<_Tp>
666  {
667  using _Sp = shared_ptr<_Tp>;
668  return _Sp(std::move(__r),
669  reinterpret_cast<typename _Sp::element_type*>(__r.get()));
670  }
671 #endif // C++20
672 #endif // C++17
673 
674  /// @}
675 
676  /**
677  * @brief A non-owning observer for a pointer owned by a shared_ptr
678  * @headerfile memory
679  * @since C++11
680  *
681  * A weak_ptr provides a safe alternative to a raw pointer when you want
682  * a non-owning reference to an object that is managed by a shared_ptr.
683  *
684  * Unlike a raw pointer, a weak_ptr can be converted to a new shared_ptr
685  * that shares ownership with every other shared_ptr that already owns
686  * the pointer. In other words you can upgrade from a non-owning "weak"
687  * reference to an owning shared_ptr, without having access to any of
688  * the existing shared_ptr objects.
689  *
690  * Also unlike a raw pointer, a weak_ptr does not become "dangling" after
691  * the object it points to has been destroyed. Instead, a weak_ptr
692  * becomes _expired_ and can no longer be converted to a shared_ptr that
693  * owns the freed pointer, so you cannot accidentally access the pointed-to
694  * object after it has been destroyed.
695  */
696  template<typename _Tp>
697  class weak_ptr : public __weak_ptr<_Tp>
698  {
699  template<typename _Arg>
700  using _Constructible = typename enable_if<
702  >::type;
703 
704  template<typename _Arg>
705  using _Assignable = typename enable_if<
706  is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
707  >::type;
708 
709  public:
710  constexpr weak_ptr() noexcept = default;
711 
712  template<typename _Yp,
713  typename = _Constructible<const shared_ptr<_Yp>&>>
714  weak_ptr(const shared_ptr<_Yp>& __r) noexcept
715  : __weak_ptr<_Tp>(__r) { }
716 
717  weak_ptr(const weak_ptr&) noexcept = default;
718 
719  template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
720  weak_ptr(const weak_ptr<_Yp>& __r) noexcept
721  : __weak_ptr<_Tp>(__r) { }
722 
723  weak_ptr(weak_ptr&&) noexcept = default;
724 
725  template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
726  weak_ptr(weak_ptr<_Yp>&& __r) noexcept
727  : __weak_ptr<_Tp>(std::move(__r)) { }
728 
729  weak_ptr&
730  operator=(const weak_ptr& __r) noexcept = default;
731 
732  template<typename _Yp>
733  _Assignable<const weak_ptr<_Yp>&>
734  operator=(const weak_ptr<_Yp>& __r) noexcept
735  {
736  this->__weak_ptr<_Tp>::operator=(__r);
737  return *this;
738  }
739 
740  template<typename _Yp>
741  _Assignable<const shared_ptr<_Yp>&>
742  operator=(const shared_ptr<_Yp>& __r) noexcept
743  {
744  this->__weak_ptr<_Tp>::operator=(__r);
745  return *this;
746  }
747 
748  weak_ptr&
749  operator=(weak_ptr&& __r) noexcept = default;
750 
751  template<typename _Yp>
752  _Assignable<weak_ptr<_Yp>>
753  operator=(weak_ptr<_Yp>&& __r) noexcept
754  {
755  this->__weak_ptr<_Tp>::operator=(std::move(__r));
756  return *this;
757  }
758 
760  lock() const noexcept
761  { return shared_ptr<_Tp>(*this, std::nothrow); }
762  };
763 
764 #if __cpp_deduction_guides >= 201606
765  template<typename _Tp>
767 #endif
768 
769  // 20.7.2.3.6 weak_ptr specialized algorithms.
770  /// Swap overload for weak_ptr
771  /// @relates weak_ptr
772  template<typename _Tp>
773  inline void
774  swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
775  { __a.swap(__b); }
776 
777 
778  /// Primary template owner_less
779  template<typename _Tp = void>
780  struct owner_less;
781 
782  /// Void specialization of owner_less compares either shared_ptr or weak_ptr
783  template<>
784  struct owner_less<void> : _Sp_owner_less<void, void>
785  { };
786 
787  /// Partial specialization of owner_less for shared_ptr.
788  template<typename _Tp>
789  struct owner_less<shared_ptr<_Tp>>
790  : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
791  { };
792 
793  /// Partial specialization of owner_less for weak_ptr.
794  template<typename _Tp>
795  struct owner_less<weak_ptr<_Tp>>
796  : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
797  { };
798 
799  /**
800  * @brief Base class allowing use of the member function `shared_from_this`.
801  * @headerfile memory
802  * @since C++11
803  */
804  template<typename _Tp>
806  {
807  protected:
808  constexpr enable_shared_from_this() noexcept { }
809 
811 
813  operator=(const enable_shared_from_this&) noexcept
814  { return *this; }
815 
817 
818  public:
820  shared_from_this()
821  { return shared_ptr<_Tp>(this->_M_weak_this); }
822 
824  shared_from_this() const
825  { return shared_ptr<const _Tp>(this->_M_weak_this); }
826 
827 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
828 #define __cpp_lib_enable_shared_from_this 201603
829  /** @{
830  * Get a `weak_ptr` referring to the object that has `*this` as its base.
831  * @since C++17
832  */
834  weak_from_this() noexcept
835  { return this->_M_weak_this; }
836 
838  weak_from_this() const noexcept
839  { return this->_M_weak_this; }
840  /// @}
841 #endif
842 
843  private:
844  template<typename _Tp1>
845  void
846  _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
847  { _M_weak_this._M_assign(__p, __n); }
848 
849  // Found by ADL when this is an associated class.
850  friend const enable_shared_from_this*
851  __enable_shared_from_this_base(const __shared_count<>&,
852  const enable_shared_from_this* __p)
853  { return __p; }
854 
855  template<typename, _Lock_policy>
856  friend class __shared_ptr;
857 
858  mutable weak_ptr<_Tp> _M_weak_this;
859  };
860 
861  /// @relates shared_ptr @{
862 
863  /**
864  * @brief Create an object that is owned by a shared_ptr.
865  * @param __a An allocator.
866  * @param __args Arguments for the @a _Tp object's constructor.
867  * @return A shared_ptr that owns the newly created object.
868  * @throw An exception thrown from @a _Alloc::allocate or from the
869  * constructor of @a _Tp.
870  *
871  * A copy of @a __a will be used to allocate memory for the shared_ptr
872  * and the new object.
873  */
874  template<typename _Tp, typename _Alloc, typename... _Args>
875  inline shared_ptr<_Tp>
876  allocate_shared(const _Alloc& __a, _Args&&... __args)
877  {
878  static_assert(!is_array<_Tp>::value, "make_shared<T[]> not supported");
879 
880  return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
881  std::forward<_Args>(__args)...);
882  }
883 
884  /**
885  * @brief Create an object that is owned by a shared_ptr.
886  * @param __args Arguments for the @a _Tp object's constructor.
887  * @return A shared_ptr that owns the newly created object.
888  * @throw std::bad_alloc, or an exception thrown from the
889  * constructor of @a _Tp.
890  */
891  template<typename _Tp, typename... _Args>
892  inline shared_ptr<_Tp>
893  make_shared(_Args&&... __args)
894  {
895  typedef typename std::remove_cv<_Tp>::type _Tp_nc;
896  return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
897  std::forward<_Args>(__args)...);
898  }
899 
900  /// std::hash specialization for shared_ptr.
901  template<typename _Tp>
902  struct hash<shared_ptr<_Tp>>
903  : public __hash_base<size_t, shared_ptr<_Tp>>
904  {
905  size_t
906  operator()(const shared_ptr<_Tp>& __s) const noexcept
907  {
909  }
910  };
911 
912  /// @} relates shared_ptr
913  /// @} group pointer_abstractions
914 
915 #if __cplusplus >= 201703L
916  namespace __detail::__variant
917  {
918  template<typename> struct _Never_valueless_alt; // see <variant>
919 
920  // Provide the strong exception-safety guarantee when emplacing a
921  // shared_ptr into a variant.
922  template<typename _Tp>
923  struct _Never_valueless_alt<std::shared_ptr<_Tp>>
925  { };
926 
927  // Provide the strong exception-safety guarantee when emplacing a
928  // weak_ptr into a variant.
929  template<typename _Tp>
930  struct _Never_valueless_alt<std::weak_ptr<_Tp>>
932  { };
933  } // namespace __detail::__variant
934 #endif // C++17
935 
936 _GLIBCXX_END_NAMESPACE_VERSION
937 } // namespace
938 
939 #endif // _SHARED_PTR_H
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
bool operator!=(nullptr_t, const shared_ptr< _Tp > &__a) noexcept
shared_ptr comparison with nullptr
bool operator>(const shared_ptr< _Tp > &__a, nullptr_t) noexcept
shared_ptr comparison with nullptr
bool operator==(const shared_ptr< _Tp > &__a, nullptr_t) noexcept
shared_ptr comparison with nullptr
bool operator==(nullptr_t, const shared_ptr< _Tp > &__a) noexcept
shared_ptr comparison with nullptr
shared_ptr< _Tp > static_pointer_cast(const shared_ptr< _Up > &__r) noexcept
Convert type of shared_ptr, via static_cast
bool operator!=(const shared_ptr< _Tp > &__a, const shared_ptr< _Up > &__b) noexcept
Inequality operator for shared_ptr objects, compares the stored pointers.
shared_ptr< _Tp > const_pointer_cast(shared_ptr< _Up > &&__r) noexcept
Convert type of shared_ptr rvalue, via const_cast
_Del * get_deleter(const shared_ptr< _Tp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
bool operator==(const shared_ptr< _Tp > &__a, const shared_ptr< _Up > &__b) noexcept
shared_ptr< _Tp > static_pointer_cast(shared_ptr< _Up > &&__r) noexcept
Convert type of shared_ptr rvalue, via static_cast
shared_ptr< _Tp > reinterpret_pointer_cast(const shared_ptr< _Up > &__r) noexcept
Convert type of shared_ptr, via reinterpret_cast
bool operator>=(const shared_ptr< _Tp > &__a, nullptr_t) noexcept
shared_ptr comparison with nullptr
void swap(weak_ptr< _Tp > &__a, weak_ptr< _Tp > &__b) noexcept
Swap overload for weak_ptr.
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
shared_ptr< _Tp > reinterpret_pointer_cast(shared_ptr< _Up > &&__r) noexcept
Convert type of shared_ptr rvalue, via reinterpret_cast
bool operator>(nullptr_t, const shared_ptr< _Tp > &__a) noexcept
shared_ptr comparison with nullptr
bool operator>=(nullptr_t, const shared_ptr< _Tp > &__a) noexcept
shared_ptr comparison with nullptr
shared_ptr< _Tp > dynamic_pointer_cast(const shared_ptr< _Up > &__r) noexcept
Convert type of shared_ptr, via dynamic_cast
shared_ptr< _Tp > const_pointer_cast(const shared_ptr< _Up > &__r) noexcept
Convert type of shared_ptr, via const_cast
bool operator>(const shared_ptr< _Tp > &__a, const shared_ptr< _Up > &__b) noexcept
Relational operator for shared_ptr objects, compares the stored pointers.
bool operator>=(const shared_ptr< _Tp > &__a, const shared_ptr< _Up > &__b) noexcept
Relational operator for shared_ptr objects, compares the stored pointers.
bool operator!=(const shared_ptr< _Tp > &__a, nullptr_t) noexcept
shared_ptr comparison with nullptr
void swap(shared_ptr< _Tp > &__a, shared_ptr< _Tp > &__b) noexcept
Swap overload for shared_ptr.
shared_ptr< _Tp > dynamic_pointer_cast(shared_ptr< _Up > &&__r) noexcept
Convert type of shared_ptr rvalue, via dynamic_cast
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
ISO C++ entities toplevel namespace is std.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1540
Template class basic_ostream.
Definition: ostream:59
Primary class template hash.
integral_constant
Definition: type_traits:63
is_array
Definition: type_traits:450
is_constructible
Definition: type_traits:979
is_assignable
Definition: type_traits:1113
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:2229
common_type
Definition: type_traits:2265
The standard allocator, as per C++03 [20.4.1].
Definition: allocator.h:125
A smart pointer with reference-counted copy semantics.
typename __shared_ptr< _Tp >::element_type element_type
The type pointed to by the stored pointer, remove_extent_t<_Tp>
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns a null pointer and the deleter __d.
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...
shared_ptr(shared_ptr< _Yp > &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
weak_ptr< _Tp > weak_type
The corresponding weak_ptr type for this shared_ptr.
shared_ptr(_Yp *__p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
shared_ptr(const shared_ptr &) noexcept=default
Copy constructor.
shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
shared_ptr(_Yp *__p)
Construct a shared_ptr that owns the pointer __p.
shared_ptr(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer and the deleter __d.
shared_ptr(_Yp *__p, _Deleter __d)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
shared_ptr(const shared_ptr< _Yp > &__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
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...
constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
shared_ptr(shared_ptr< _Yp > &&__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
A non-owning observer for a pointer owned by a shared_ptr.
Primary template owner_less.
Base class allowing use of the member function shared_from_this.
weak_ptr< const _Tp > weak_from_this() const noexcept
weak_ptr< _Tp > weak_from_this() noexcept
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:90
One of the comparison functors.
Definition: stl_function.h:396
A move-only smart pointer that manages unique ownership of a resource.
Definition: unique_ptr.h:247