libstdc++
shared_ptr.h
Go to the documentation of this file.
00001 // shared_ptr and weak_ptr implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2007-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // GCC Note: Based on files from version 1.32.0 of the Boost library.
00026 
00027 //  shared_count.hpp
00028 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
00029 
00030 //  shared_ptr.hpp
00031 //  Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
00032 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
00033 
00034 //  weak_ptr.hpp
00035 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
00036 
00037 //  enable_shared_from_this.hpp
00038 //  Copyright (C) 2002 Peter Dimov
00039 
00040 // Distributed under the Boost Software License, Version 1.0. (See
00041 // accompanying file LICENSE_1_0.txt or copy at
00042 // http://www.boost.org/LICENSE_1_0.txt)
00043 
00044 /** @file
00045  *  This is an internal header file, included by other library headers.
00046  *  Do not attempt to use it directly. @headername{memory}
00047  */
00048 
00049 #ifndef _SHARED_PTR_H
00050 #define _SHARED_PTR_H 1
00051 
00052 #include <bits/shared_ptr_base.h>
00053 
00054 namespace std _GLIBCXX_VISIBILITY(default)
00055 {
00056 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00057 
00058   /**
00059    * @addtogroup pointer_abstractions
00060    * @{
00061    */
00062 
00063   /// 20.7.2.2.11 shared_ptr I/O
00064   template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
00065     inline std::basic_ostream<_Ch, _Tr>&
00066     operator<<(std::basic_ostream<_Ch, _Tr>& __os,
00067                const __shared_ptr<_Tp, _Lp>& __p)
00068     {
00069       __os << __p.get();
00070       return __os;
00071     }
00072 
00073   template<typename _Del, typename _Tp, _Lock_policy _Lp>
00074     inline _Del*
00075     get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
00076     {
00077 #if __cpp_rtti
00078       return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
00079 #else
00080       return 0;
00081 #endif
00082     }
00083 
00084   /// 20.7.2.2.10 shared_ptr get_deleter
00085   template<typename _Del, typename _Tp>
00086     inline _Del*
00087     get_deleter(const shared_ptr<_Tp>& __p) noexcept
00088     {
00089 #if __cpp_rtti
00090       return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
00091 #else
00092       return 0;
00093 #endif
00094     }
00095 
00096   /**
00097    *  @brief  A smart pointer with reference-counted copy semantics.
00098    *
00099    *  The object pointed to is deleted when the last shared_ptr pointing to
00100    *  it is destroyed or reset.
00101   */
00102   template<typename _Tp>
00103     class shared_ptr : public __shared_ptr<_Tp>
00104     {
00105       template<typename... _Args>
00106         using _Constructible = typename enable_if<
00107           is_constructible<__shared_ptr<_Tp>, _Args...>::value
00108         >::type;
00109 
00110       template<typename _Arg>
00111         using _Assignable = typename enable_if<
00112           is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
00113         >::type;
00114 
00115     public:
00116 
00117       using element_type = typename __shared_ptr<_Tp>::element_type;
00118 
00119 #if __cplusplus > 201402L
00120 # define __cpp_lib_shared_ptr_weak_type 201606
00121       using weak_type = weak_ptr<_Tp>;
00122 #endif
00123       /**
00124        *  @brief  Construct an empty %shared_ptr.
00125        *  @post   use_count()==0 && get()==0
00126        */
00127       constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
00128 
00129       shared_ptr(const shared_ptr&) noexcept = default;
00130 
00131       /**
00132        *  @brief  Construct a %shared_ptr that owns the pointer @a __p.
00133        *  @param  __p  A pointer that is convertible to element_type*.
00134        *  @post   use_count() == 1 && get() == __p
00135        *  @throw  std::bad_alloc, in which case @c delete @a __p is called.
00136        */
00137       template<typename _Yp, typename = _Constructible<_Yp*>>
00138         explicit
00139         shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
00140 
00141       /**
00142        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
00143        *          and the deleter @a __d.
00144        *  @param  __p  A pointer.
00145        *  @param  __d  A deleter.
00146        *  @post   use_count() == 1 && get() == __p
00147        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00148        *
00149        *  Requirements: _Deleter's copy constructor and destructor must
00150        *  not throw
00151        *
00152        *  __shared_ptr will release __p by calling __d(__p)
00153        */
00154       template<typename _Yp, typename _Deleter,
00155                typename = _Constructible<_Yp*, _Deleter>>
00156         shared_ptr(_Yp* __p, _Deleter __d)
00157         : __shared_ptr<_Tp>(__p, std::move(__d)) { }
00158 
00159       /**
00160        *  @brief  Construct a %shared_ptr that owns a null pointer
00161        *          and the deleter @a __d.
00162        *  @param  __p  A null pointer constant.
00163        *  @param  __d  A deleter.
00164        *  @post   use_count() == 1 && get() == __p
00165        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00166        *
00167        *  Requirements: _Deleter's copy constructor and destructor must
00168        *  not throw
00169        *
00170        *  The last owner will call __d(__p)
00171        */
00172       template<typename _Deleter>
00173         shared_ptr(nullptr_t __p, _Deleter __d)
00174         : __shared_ptr<_Tp>(__p, std::move(__d)) { }
00175 
00176       /**
00177        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
00178        *          and the deleter @a __d.
00179        *  @param  __p  A pointer.
00180        *  @param  __d  A deleter.
00181        *  @param  __a  An allocator.
00182        *  @post   use_count() == 1 && get() == __p
00183        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00184        *
00185        *  Requirements: _Deleter's copy constructor and destructor must
00186        *  not throw _Alloc's copy constructor and destructor must not
00187        *  throw.
00188        *
00189        *  __shared_ptr will release __p by calling __d(__p)
00190        */
00191       template<typename _Yp, typename _Deleter, typename _Alloc,
00192                typename = _Constructible<_Yp*, _Deleter, _Alloc>>
00193         shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
00194         : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
00195 
00196       /**
00197        *  @brief  Construct a %shared_ptr that owns a null pointer
00198        *          and the deleter @a __d.
00199        *  @param  __p  A null pointer constant.
00200        *  @param  __d  A deleter.
00201        *  @param  __a  An allocator.
00202        *  @post   use_count() == 1 && get() == __p
00203        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00204        *
00205        *  Requirements: _Deleter's copy constructor and destructor must
00206        *  not throw _Alloc's copy constructor and destructor must not
00207        *  throw.
00208        *
00209        *  The last owner will call __d(__p)
00210        */
00211       template<typename _Deleter, typename _Alloc>
00212         shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
00213         : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
00214 
00215       // Aliasing constructor
00216 
00217       /**
00218        *  @brief  Constructs a %shared_ptr instance that stores @a __p
00219        *          and shares ownership with @a __r.
00220        *  @param  __r  A %shared_ptr.
00221        *  @param  __p  A pointer that will remain valid while @a *__r is valid.
00222        *  @post   get() == __p && use_count() == __r.use_count()
00223        *
00224        *  This can be used to construct a @c shared_ptr to a sub-object
00225        *  of an object managed by an existing @c shared_ptr.
00226        *
00227        * @code
00228        * shared_ptr< pair<int,int> > pii(new pair<int,int>());
00229        * shared_ptr<int> pi(pii, &pii->first);
00230        * assert(pii.use_count() == 2);
00231        * @endcode
00232        */
00233       template<typename _Yp>
00234         shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
00235         : __shared_ptr<_Tp>(__r, __p) { }
00236 
00237       /**
00238        *  @brief  If @a __r is empty, constructs an empty %shared_ptr;
00239        *          otherwise construct a %shared_ptr that shares ownership
00240        *          with @a __r.
00241        *  @param  __r  A %shared_ptr.
00242        *  @post   get() == __r.get() && use_count() == __r.use_count()
00243        */
00244       template<typename _Yp,
00245                typename = _Constructible<const shared_ptr<_Yp>&>>
00246         shared_ptr(const shared_ptr<_Yp>& __r) noexcept
00247         : __shared_ptr<_Tp>(__r) { }
00248 
00249       /**
00250        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
00251        *  @param  __r  A %shared_ptr rvalue.
00252        *  @post   *this contains the old value of @a __r, @a __r is empty.
00253        */
00254       shared_ptr(shared_ptr&& __r) noexcept
00255       : __shared_ptr<_Tp>(std::move(__r)) { }
00256 
00257       /**
00258        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
00259        *  @param  __r  A %shared_ptr rvalue.
00260        *  @post   *this contains the old value of @a __r, @a __r is empty.
00261        */
00262       template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
00263         shared_ptr(shared_ptr<_Yp>&& __r) noexcept
00264         : __shared_ptr<_Tp>(std::move(__r)) { }
00265 
00266       /**
00267        *  @brief  Constructs a %shared_ptr that shares ownership with @a __r
00268        *          and stores a copy of the pointer stored in @a __r.
00269        *  @param  __r  A weak_ptr.
00270        *  @post   use_count() == __r.use_count()
00271        *  @throw  bad_weak_ptr when __r.expired(),
00272        *          in which case the constructor has no effect.
00273        */
00274       template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
00275         explicit shared_ptr(const weak_ptr<_Yp>& __r)
00276         : __shared_ptr<_Tp>(__r) { }
00277 
00278 #if _GLIBCXX_USE_DEPRECATED
00279 #pragma GCC diagnostic push
00280 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
00281       template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
00282         shared_ptr(auto_ptr<_Yp>&& __r);
00283 #pragma GCC diagnostic pop
00284 #endif
00285 
00286       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00287       // 2399. shared_ptr's constructor from unique_ptr should be constrained
00288       template<typename _Yp, typename _Del,
00289                typename = _Constructible<unique_ptr<_Yp, _Del>>>
00290         shared_ptr(unique_ptr<_Yp, _Del>&& __r)
00291         : __shared_ptr<_Tp>(std::move(__r)) { }
00292 
00293 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
00294       // This non-standard constructor exists to support conversions that
00295       // were possible in C++11 and C++14 but are ill-formed in C++17.
00296       // If an exception is thrown this constructor has no effect.
00297       template<typename _Yp, typename _Del,
00298                 _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
00299         shared_ptr(unique_ptr<_Yp, _Del>&& __r)
00300         : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
00301 #endif
00302 
00303       /**
00304        *  @brief  Construct an empty %shared_ptr.
00305        *  @post   use_count() == 0 && get() == nullptr
00306        */
00307       constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
00308 
00309       shared_ptr& operator=(const shared_ptr&) noexcept = default;
00310 
00311       template<typename _Yp>
00312         _Assignable<const shared_ptr<_Yp>&>
00313         operator=(const shared_ptr<_Yp>& __r) noexcept
00314         {
00315           this->__shared_ptr<_Tp>::operator=(__r);
00316           return *this;
00317         }
00318 
00319 #if _GLIBCXX_USE_DEPRECATED
00320 #pragma GCC diagnostic push
00321 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
00322       template<typename _Yp>
00323         _Assignable<auto_ptr<_Yp>>
00324         operator=(auto_ptr<_Yp>&& __r)
00325         {
00326           this->__shared_ptr<_Tp>::operator=(std::move(__r));
00327           return *this;
00328         }
00329 #pragma GCC diagnostic pop
00330 #endif
00331 
00332       shared_ptr&
00333       operator=(shared_ptr&& __r) noexcept
00334       {
00335         this->__shared_ptr<_Tp>::operator=(std::move(__r));
00336         return *this;
00337       }
00338 
00339       template<class _Yp>
00340         _Assignable<shared_ptr<_Yp>>
00341         operator=(shared_ptr<_Yp>&& __r) noexcept
00342         {
00343           this->__shared_ptr<_Tp>::operator=(std::move(__r));
00344           return *this;
00345         }
00346 
00347       template<typename _Yp, typename _Del>
00348         _Assignable<unique_ptr<_Yp, _Del>>
00349         operator=(unique_ptr<_Yp, _Del>&& __r)
00350         {
00351           this->__shared_ptr<_Tp>::operator=(std::move(__r));
00352           return *this;
00353         }
00354 
00355     private:
00356       // This constructor is non-standard, it is used by allocate_shared.
00357       template<typename _Alloc, typename... _Args>
00358         shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
00359         : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
00360         { }
00361 
00362       template<typename _Yp, typename _Alloc, typename... _Args>
00363         friend shared_ptr<_Yp>
00364         allocate_shared(const _Alloc& __a, _Args&&... __args);
00365 
00366       // This constructor is non-standard, it is used by weak_ptr::lock().
00367       shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
00368       : __shared_ptr<_Tp>(__r, std::nothrow) { }
00369 
00370       friend class weak_ptr<_Tp>;
00371     };
00372 
00373 #if __cpp_deduction_guides >= 201606
00374   template<typename _Tp>
00375     shared_ptr(weak_ptr<_Tp>) ->  shared_ptr<_Tp>;
00376   template<typename _Tp, typename _Del>
00377     shared_ptr(unique_ptr<_Tp, _Del>) ->  shared_ptr<_Tp>;
00378 #endif
00379 
00380   // 20.7.2.2.7 shared_ptr comparisons
00381   template<typename _Tp, typename _Up>
00382     _GLIBCXX_NODISCARD inline bool
00383     operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00384     { return __a.get() == __b.get(); }
00385 
00386   template<typename _Tp>
00387     _GLIBCXX_NODISCARD inline bool
00388     operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00389     { return !__a; }
00390 
00391   template<typename _Tp>
00392     _GLIBCXX_NODISCARD inline bool
00393     operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00394     { return !__a; }
00395 
00396   template<typename _Tp, typename _Up>
00397     _GLIBCXX_NODISCARD inline bool
00398     operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00399     { return __a.get() != __b.get(); }
00400 
00401   template<typename _Tp>
00402     _GLIBCXX_NODISCARD inline bool
00403     operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00404     { return (bool)__a; }
00405 
00406   template<typename _Tp>
00407     _GLIBCXX_NODISCARD inline bool
00408     operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00409     { return (bool)__a; }
00410 
00411   template<typename _Tp, typename _Up>
00412     _GLIBCXX_NODISCARD inline bool
00413     operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00414     {
00415       using _Tp_elt = typename shared_ptr<_Tp>::element_type;
00416       using _Up_elt = typename shared_ptr<_Up>::element_type;
00417       using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
00418       return less<_Vp>()(__a.get(), __b.get());
00419     }
00420 
00421   template<typename _Tp>
00422     _GLIBCXX_NODISCARD inline bool
00423     operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00424     {
00425       using _Tp_elt = typename shared_ptr<_Tp>::element_type;
00426       return less<_Tp_elt*>()(__a.get(), nullptr);
00427     }
00428 
00429   template<typename _Tp>
00430     _GLIBCXX_NODISCARD inline bool
00431     operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00432     {
00433       using _Tp_elt = typename shared_ptr<_Tp>::element_type;
00434       return less<_Tp_elt*>()(nullptr, __a.get());
00435     }
00436 
00437   template<typename _Tp, typename _Up>
00438     _GLIBCXX_NODISCARD inline bool
00439     operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00440     { return !(__b < __a); }
00441 
00442   template<typename _Tp>
00443     _GLIBCXX_NODISCARD inline bool
00444     operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00445     { return !(nullptr < __a); }
00446 
00447   template<typename _Tp>
00448     _GLIBCXX_NODISCARD inline bool
00449     operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00450     { return !(__a < nullptr); }
00451 
00452   template<typename _Tp, typename _Up>
00453     _GLIBCXX_NODISCARD inline bool
00454     operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00455     { return (__b < __a); }
00456 
00457   template<typename _Tp>
00458     _GLIBCXX_NODISCARD inline bool
00459     operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00460     { return nullptr < __a; }
00461 
00462   template<typename _Tp>
00463     _GLIBCXX_NODISCARD inline bool
00464     operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00465     { return __a < nullptr; }
00466 
00467   template<typename _Tp, typename _Up>
00468     _GLIBCXX_NODISCARD inline bool
00469     operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00470     { return !(__a < __b); }
00471 
00472   template<typename _Tp>
00473     _GLIBCXX_NODISCARD inline bool
00474     operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00475     { return !(__a < nullptr); }
00476 
00477   template<typename _Tp>
00478     _GLIBCXX_NODISCARD inline bool
00479     operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00480     { return !(nullptr < __a); }
00481 
00482   // 20.7.2.2.8 shared_ptr specialized algorithms.
00483   template<typename _Tp>
00484     inline void
00485     swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
00486     { __a.swap(__b); }
00487 
00488   // 20.7.2.2.9 shared_ptr casts.
00489   template<typename _Tp, typename _Up>
00490     inline shared_ptr<_Tp>
00491     static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00492     {
00493       using _Sp = shared_ptr<_Tp>;
00494       return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
00495     }
00496 
00497   template<typename _Tp, typename _Up>
00498     inline shared_ptr<_Tp>
00499     const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00500     {
00501       using _Sp = shared_ptr<_Tp>;
00502       return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
00503     }
00504 
00505   template<typename _Tp, typename _Up>
00506     inline shared_ptr<_Tp>
00507     dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00508     {
00509       using _Sp = shared_ptr<_Tp>;
00510       if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
00511         return _Sp(__r, __p);
00512       return _Sp();
00513     }
00514 
00515 #if __cplusplus > 201402L
00516   template<typename _Tp, typename _Up>
00517     inline shared_ptr<_Tp>
00518     reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00519     {
00520       using _Sp = shared_ptr<_Tp>;
00521       return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
00522     }
00523 #endif
00524 
00525   /**
00526    *  @brief  A smart pointer with weak semantics.
00527    *
00528    *  With forwarding constructors and assignment operators.
00529    */
00530   template<typename _Tp>
00531     class weak_ptr : public __weak_ptr<_Tp>
00532     {
00533       template<typename _Arg>
00534         using _Constructible = typename enable_if<
00535           is_constructible<__weak_ptr<_Tp>, _Arg>::value
00536         >::type;
00537 
00538       template<typename _Arg>
00539         using _Assignable = typename enable_if<
00540           is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
00541         >::type;
00542 
00543     public:
00544       constexpr weak_ptr() noexcept = default;
00545 
00546       template<typename _Yp,
00547                typename = _Constructible<const shared_ptr<_Yp>&>>
00548         weak_ptr(const shared_ptr<_Yp>& __r) noexcept
00549         : __weak_ptr<_Tp>(__r) { }
00550 
00551       weak_ptr(const weak_ptr&) noexcept = default;
00552 
00553       template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
00554         weak_ptr(const weak_ptr<_Yp>& __r) noexcept
00555         : __weak_ptr<_Tp>(__r) { }
00556 
00557       weak_ptr(weak_ptr&&) noexcept = default;
00558 
00559       template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
00560         weak_ptr(weak_ptr<_Yp>&& __r) noexcept
00561         : __weak_ptr<_Tp>(std::move(__r)) { }
00562 
00563       weak_ptr&
00564       operator=(const weak_ptr& __r) noexcept = default;
00565 
00566       template<typename _Yp>
00567         _Assignable<const weak_ptr<_Yp>&>
00568         operator=(const weak_ptr<_Yp>& __r) noexcept
00569         {
00570           this->__weak_ptr<_Tp>::operator=(__r);
00571           return *this;
00572         }
00573 
00574       template<typename _Yp>
00575         _Assignable<const shared_ptr<_Yp>&>
00576         operator=(const shared_ptr<_Yp>& __r) noexcept
00577         {
00578           this->__weak_ptr<_Tp>::operator=(__r);
00579           return *this;
00580         }
00581 
00582       weak_ptr&
00583       operator=(weak_ptr&& __r) noexcept = default;
00584 
00585       template<typename _Yp>
00586         _Assignable<weak_ptr<_Yp>>
00587         operator=(weak_ptr<_Yp>&& __r) noexcept
00588         {
00589           this->__weak_ptr<_Tp>::operator=(std::move(__r));
00590           return *this;
00591         }
00592 
00593       shared_ptr<_Tp>
00594       lock() const noexcept
00595       { return shared_ptr<_Tp>(*this, std::nothrow); }
00596     };
00597 
00598 #if __cpp_deduction_guides >= 201606
00599   template<typename _Tp>
00600     weak_ptr(shared_ptr<_Tp>) ->  weak_ptr<_Tp>;
00601 #endif
00602 
00603   // 20.7.2.3.6 weak_ptr specialized algorithms.
00604   template<typename _Tp>
00605     inline void
00606     swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
00607     { __a.swap(__b); }
00608 
00609 
00610   /// Primary template owner_less
00611   template<typename _Tp = void>
00612     struct owner_less;
00613 
00614   /// Void specialization of owner_less
00615   template<>
00616     struct owner_less<void> : _Sp_owner_less<void, void>
00617     { };
00618 
00619   /// Partial specialization of owner_less for shared_ptr.
00620   template<typename _Tp>
00621     struct owner_less<shared_ptr<_Tp>>
00622     : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
00623     { };
00624 
00625   /// Partial specialization of owner_less for weak_ptr.
00626   template<typename _Tp>
00627     struct owner_less<weak_ptr<_Tp>>
00628     : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
00629     { };
00630 
00631   /**
00632    *  @brief Base class allowing use of member function shared_from_this.
00633    */
00634   template<typename _Tp>
00635     class enable_shared_from_this
00636     {
00637     protected:
00638       constexpr enable_shared_from_this() noexcept { }
00639 
00640       enable_shared_from_this(const enable_shared_from_this&) noexcept { }
00641 
00642       enable_shared_from_this&
00643       operator=(const enable_shared_from_this&) noexcept
00644       { return *this; }
00645 
00646       ~enable_shared_from_this() { }
00647 
00648     public:
00649       shared_ptr<_Tp>
00650       shared_from_this()
00651       { return shared_ptr<_Tp>(this->_M_weak_this); }
00652 
00653       shared_ptr<const _Tp>
00654       shared_from_this() const
00655       { return shared_ptr<const _Tp>(this->_M_weak_this); }
00656 
00657 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
00658 #define __cpp_lib_enable_shared_from_this 201603
00659       weak_ptr<_Tp>
00660       weak_from_this() noexcept
00661       { return this->_M_weak_this; }
00662 
00663       weak_ptr<const _Tp>
00664       weak_from_this() const noexcept
00665       { return this->_M_weak_this; }
00666 #endif
00667 
00668     private:
00669       template<typename _Tp1>
00670         void
00671         _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
00672         { _M_weak_this._M_assign(__p, __n); }
00673 
00674       // Found by ADL when this is an associated class.
00675       friend const enable_shared_from_this*
00676       __enable_shared_from_this_base(const __shared_count<>&,
00677                                      const enable_shared_from_this* __p)
00678       { return __p; }
00679 
00680       template<typename, _Lock_policy>
00681         friend class __shared_ptr;
00682 
00683       mutable weak_ptr<_Tp>  _M_weak_this;
00684     };
00685 
00686   /**
00687    *  @brief  Create an object that is owned by a shared_ptr.
00688    *  @param  __a     An allocator.
00689    *  @param  __args  Arguments for the @a _Tp object's constructor.
00690    *  @return A shared_ptr that owns the newly created object.
00691    *  @throw  An exception thrown from @a _Alloc::allocate or from the
00692    *          constructor of @a _Tp.
00693    *
00694    *  A copy of @a __a will be used to allocate memory for the shared_ptr
00695    *  and the new object.
00696    */
00697   template<typename _Tp, typename _Alloc, typename... _Args>
00698     inline shared_ptr<_Tp>
00699     allocate_shared(const _Alloc& __a, _Args&&... __args)
00700     {
00701       return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
00702                              std::forward<_Args>(__args)...);
00703     }
00704 
00705   /**
00706    *  @brief  Create an object that is owned by a shared_ptr.
00707    *  @param  __args  Arguments for the @a _Tp object's constructor.
00708    *  @return A shared_ptr that owns the newly created object.
00709    *  @throw  std::bad_alloc, or an exception thrown from the
00710    *          constructor of @a _Tp.
00711    */
00712   template<typename _Tp, typename... _Args>
00713     inline shared_ptr<_Tp>
00714     make_shared(_Args&&... __args)
00715     {
00716       typedef typename std::remove_cv<_Tp>::type _Tp_nc;
00717       return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
00718                                        std::forward<_Args>(__args)...);
00719     }
00720 
00721   /// std::hash specialization for shared_ptr.
00722   template<typename _Tp>
00723     struct hash<shared_ptr<_Tp>>
00724     : public __hash_base<size_t, shared_ptr<_Tp>>
00725     {
00726       size_t
00727       operator()(const shared_ptr<_Tp>& __s) const noexcept
00728       {
00729         return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get());
00730       }
00731     };
00732 
00733   // @} group pointer_abstractions
00734 
00735 #if __cplusplus >= 201703L
00736   namespace __detail::__variant
00737   {
00738     template<typename> struct _Never_valueless_alt; // see <variant>
00739 
00740     // Provide the strong exception-safety guarantee when emplacing a
00741     // shared_ptr into a variant.
00742     template<typename _Tp>
00743       struct _Never_valueless_alt<std::shared_ptr<_Tp>>
00744       : std::true_type
00745       { };
00746 
00747     // Provide the strong exception-safety guarantee when emplacing a
00748     // weak_ptr into a variant.
00749     template<typename _Tp>
00750       struct _Never_valueless_alt<std::weak_ptr<_Tp>>
00751       : std::true_type
00752       { };
00753   }  // namespace __detail::__variant
00754 #endif // C++17
00755 
00756 _GLIBCXX_END_NAMESPACE_VERSION
00757 } // namespace
00758 
00759 #endif // _SHARED_PTR_H