libstdc++
shared_ptr.h
Go to the documentation of this file.
00001 // shared_ptr and weak_ptr implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2007-2017 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   /// 20.7.2.2.10 shared_ptr get_deleter
00074   template<typename _Del, typename _Tp, _Lock_policy _Lp>
00075     inline _Del*
00076     get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
00077     {
00078 #if __cpp_rtti
00079       return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
00080 #else
00081       return 0;
00082 #endif
00083     }
00084 
00085 
00086   /**
00087    *  @brief  A smart pointer with reference-counted copy semantics.
00088    *
00089    *  The object pointed to is deleted when the last shared_ptr pointing to
00090    *  it is destroyed or reset.
00091   */
00092   template<typename _Tp>
00093     class shared_ptr : public __shared_ptr<_Tp>
00094     {
00095       template<typename... _Args>
00096         using _Constructible = typename enable_if<
00097           is_constructible<__shared_ptr<_Tp>, _Args...>::value
00098         >::type;
00099 
00100       template<typename _Arg>
00101         using _Assignable = typename enable_if<
00102           is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
00103         >::type;
00104 
00105     public:
00106 
00107       using element_type = typename __shared_ptr<_Tp>::element_type;
00108 
00109 #if __cplusplus > 201402L
00110 # define __cpp_lib_shared_ptr_weak_type 201606
00111       using weak_type = weak_ptr<_Tp>;
00112 #endif
00113       /**
00114        *  @brief  Construct an empty %shared_ptr.
00115        *  @post   use_count()==0 && get()==0
00116        */
00117       constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
00118 
00119       shared_ptr(const shared_ptr&) noexcept = default;
00120 
00121       /**
00122        *  @brief  Construct a %shared_ptr that owns the pointer @a __p.
00123        *  @param  __p  A pointer that is convertible to element_type*.
00124        *  @post   use_count() == 1 && get() == __p
00125        *  @throw  std::bad_alloc, in which case @c delete @a __p is called.
00126        */
00127       template<typename _Yp, typename = _Constructible<_Yp*>>
00128         explicit
00129         shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
00130 
00131       /**
00132        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
00133        *          and the deleter @a __d.
00134        *  @param  __p  A pointer.
00135        *  @param  __d  A deleter.
00136        *  @post   use_count() == 1 && get() == __p
00137        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00138        *
00139        *  Requirements: _Deleter's copy constructor and destructor must
00140        *  not throw
00141        *
00142        *  __shared_ptr will release __p by calling __d(__p)
00143        */
00144       template<typename _Yp, typename _Deleter,
00145                typename = _Constructible<_Yp*, _Deleter>>
00146         shared_ptr(_Yp* __p, _Deleter __d)
00147         : __shared_ptr<_Tp>(__p, std::move(__d)) { }
00148 
00149       /**
00150        *  @brief  Construct a %shared_ptr that owns a null pointer
00151        *          and the deleter @a __d.
00152        *  @param  __p  A null pointer constant.
00153        *  @param  __d  A deleter.
00154        *  @post   use_count() == 1 && get() == __p
00155        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00156        *
00157        *  Requirements: _Deleter's copy constructor and destructor must
00158        *  not throw
00159        *
00160        *  The last owner will call __d(__p)
00161        */
00162       template<typename _Deleter>
00163         shared_ptr(nullptr_t __p, _Deleter __d)
00164         : __shared_ptr<_Tp>(__p, std::move(__d)) { }
00165 
00166       /**
00167        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
00168        *          and the deleter @a __d.
00169        *  @param  __p  A pointer.
00170        *  @param  __d  A deleter.
00171        *  @param  __a  An allocator.
00172        *  @post   use_count() == 1 && get() == __p
00173        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00174        *
00175        *  Requirements: _Deleter's copy constructor and destructor must
00176        *  not throw _Alloc's copy constructor and destructor must not
00177        *  throw.
00178        *
00179        *  __shared_ptr will release __p by calling __d(__p)
00180        */
00181       template<typename _Yp, typename _Deleter, typename _Alloc,
00182                typename = _Constructible<_Yp*, _Deleter, _Alloc>>
00183         shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
00184         : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
00185 
00186       /**
00187        *  @brief  Construct a %shared_ptr that owns a null pointer
00188        *          and the deleter @a __d.
00189        *  @param  __p  A null pointer constant.
00190        *  @param  __d  A deleter.
00191        *  @param  __a  An allocator.
00192        *  @post   use_count() == 1 && get() == __p
00193        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
00194        *
00195        *  Requirements: _Deleter's copy constructor and destructor must
00196        *  not throw _Alloc's copy constructor and destructor must not
00197        *  throw.
00198        *
00199        *  The last owner will call __d(__p)
00200        */
00201       template<typename _Deleter, typename _Alloc>
00202         shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
00203         : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
00204 
00205       // Aliasing constructor
00206 
00207       /**
00208        *  @brief  Constructs a %shared_ptr instance that stores @a __p
00209        *          and shares ownership with @a __r.
00210        *  @param  __r  A %shared_ptr.
00211        *  @param  __p  A pointer that will remain valid while @a *__r is valid.
00212        *  @post   get() == __p && use_count() == __r.use_count()
00213        *
00214        *  This can be used to construct a @c shared_ptr to a sub-object
00215        *  of an object managed by an existing @c shared_ptr.
00216        *
00217        * @code
00218        * shared_ptr< pair<int,int> > pii(new pair<int,int>());
00219        * shared_ptr<int> pi(pii, &pii->first);
00220        * assert(pii.use_count() == 2);
00221        * @endcode
00222        */
00223       template<typename _Yp>
00224         shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
00225         : __shared_ptr<_Tp>(__r, __p) { }
00226 
00227       /**
00228        *  @brief  If @a __r is empty, constructs an empty %shared_ptr;
00229        *          otherwise construct a %shared_ptr that shares ownership
00230        *          with @a __r.
00231        *  @param  __r  A %shared_ptr.
00232        *  @post   get() == __r.get() && use_count() == __r.use_count()
00233        */
00234       template<typename _Yp,
00235                typename = _Constructible<const shared_ptr<_Yp>&>>
00236         shared_ptr(const shared_ptr<_Yp>& __r) noexcept
00237         : __shared_ptr<_Tp>(__r) { }
00238 
00239       /**
00240        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
00241        *  @param  __r  A %shared_ptr rvalue.
00242        *  @post   *this contains the old value of @a __r, @a __r is empty.
00243        */
00244       shared_ptr(shared_ptr&& __r) noexcept
00245       : __shared_ptr<_Tp>(std::move(__r)) { }
00246 
00247       /**
00248        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
00249        *  @param  __r  A %shared_ptr rvalue.
00250        *  @post   *this contains the old value of @a __r, @a __r is empty.
00251        */
00252       template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
00253         shared_ptr(shared_ptr<_Yp>&& __r) noexcept
00254         : __shared_ptr<_Tp>(std::move(__r)) { }
00255 
00256       /**
00257        *  @brief  Constructs a %shared_ptr that shares ownership with @a __r
00258        *          and stores a copy of the pointer stored in @a __r.
00259        *  @param  __r  A weak_ptr.
00260        *  @post   use_count() == __r.use_count()
00261        *  @throw  bad_weak_ptr when __r.expired(),
00262        *          in which case the constructor has no effect.
00263        */
00264       template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
00265         explicit shared_ptr(const weak_ptr<_Yp>& __r)
00266         : __shared_ptr<_Tp>(__r) { }
00267 
00268 #if _GLIBCXX_USE_DEPRECATED
00269       template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
00270         shared_ptr(auto_ptr<_Yp>&& __r);
00271 #endif
00272 
00273       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00274       // 2399. shared_ptr's constructor from unique_ptr should be constrained
00275       template<typename _Yp, typename _Del,
00276                typename = _Constructible<unique_ptr<_Yp, _Del>>>
00277         shared_ptr(unique_ptr<_Yp, _Del>&& __r)
00278         : __shared_ptr<_Tp>(std::move(__r)) { }
00279 
00280 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
00281       // This non-standard constructor exists to support conversions that
00282       // were possible in C++11 and C++14 but are ill-formed in C++17.
00283       // If an exception is thrown this constructor has no effect.
00284       template<typename _Yp, typename _Del,
00285                 _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
00286         shared_ptr(unique_ptr<_Yp, _Del>&& __r)
00287         : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
00288 #endif
00289 
00290       /**
00291        *  @brief  Construct an empty %shared_ptr.
00292        *  @post   use_count() == 0 && get() == nullptr
00293        */
00294       constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
00295 
00296       shared_ptr& operator=(const shared_ptr&) noexcept = default;
00297 
00298       template<typename _Yp>
00299         _Assignable<const shared_ptr<_Yp>&>
00300         operator=(const shared_ptr<_Yp>& __r) noexcept
00301         {
00302           this->__shared_ptr<_Tp>::operator=(__r);
00303           return *this;
00304         }
00305 
00306 #if _GLIBCXX_USE_DEPRECATED
00307       template<typename _Yp>
00308         _Assignable<auto_ptr<_Yp>>
00309         operator=(auto_ptr<_Yp>&& __r)
00310         {
00311           this->__shared_ptr<_Tp>::operator=(std::move(__r));
00312           return *this;
00313         }
00314 #endif
00315 
00316       shared_ptr&
00317       operator=(shared_ptr&& __r) noexcept
00318       {
00319         this->__shared_ptr<_Tp>::operator=(std::move(__r));
00320         return *this;
00321       }
00322 
00323       template<class _Yp>
00324         _Assignable<shared_ptr<_Yp>>
00325         operator=(shared_ptr<_Yp>&& __r) noexcept
00326         {
00327           this->__shared_ptr<_Tp>::operator=(std::move(__r));
00328           return *this;
00329         }
00330 
00331       template<typename _Yp, typename _Del>
00332         _Assignable<unique_ptr<_Yp, _Del>>
00333         operator=(unique_ptr<_Yp, _Del>&& __r)
00334         {
00335           this->__shared_ptr<_Tp>::operator=(std::move(__r));
00336           return *this;
00337         }
00338 
00339     private:
00340       // This constructor is non-standard, it is used by allocate_shared.
00341       template<typename _Alloc, typename... _Args>
00342         shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
00343                    _Args&&... __args)
00344         : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
00345         { }
00346 
00347       template<typename _Yp, typename _Alloc, typename... _Args>
00348         friend shared_ptr<_Yp>
00349         allocate_shared(const _Alloc& __a, _Args&&... __args);
00350 
00351       // This constructor is non-standard, it is used by weak_ptr::lock().
00352       shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
00353       : __shared_ptr<_Tp>(__r, std::nothrow) { }
00354 
00355       friend class weak_ptr<_Tp>;
00356     };
00357 
00358 #if __cpp_deduction_guides >= 201606
00359   template<typename _Tp>
00360     shared_ptr(weak_ptr<_Tp>) ->  shared_ptr<_Tp>;
00361   template<typename _Tp, typename _Del>
00362     shared_ptr(unique_ptr<_Tp, _Del>) ->  shared_ptr<_Tp>;
00363 #endif
00364 
00365   // 20.7.2.2.7 shared_ptr comparisons
00366   template<typename _Tp, typename _Up>
00367     inline bool
00368     operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00369     { return __a.get() == __b.get(); }
00370 
00371   template<typename _Tp>
00372     inline bool
00373     operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00374     { return !__a; }
00375 
00376   template<typename _Tp>
00377     inline bool
00378     operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00379     { return !__a; }
00380 
00381   template<typename _Tp, typename _Up>
00382     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     inline bool
00388     operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00389     { return (bool)__a; }
00390 
00391   template<typename _Tp>
00392     inline bool
00393     operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00394     { return (bool)__a; }
00395 
00396   template<typename _Tp, typename _Up>
00397     inline bool
00398     operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00399     {
00400       using _Tp_elt = typename shared_ptr<_Tp>::element_type;
00401       using _Up_elt = typename shared_ptr<_Up>::element_type;
00402       using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
00403       return less<_Vp>()(__a.get(), __b.get());
00404     }
00405 
00406   template<typename _Tp>
00407     inline bool
00408     operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00409     {
00410       using _Tp_elt = typename shared_ptr<_Tp>::element_type;
00411       return less<_Tp_elt*>()(__a.get(), nullptr);
00412     }
00413 
00414   template<typename _Tp>
00415     inline bool
00416     operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00417     {
00418       using _Tp_elt = typename shared_ptr<_Tp>::element_type;
00419       return less<_Tp_elt*>()(nullptr, __a.get());
00420     }
00421 
00422   template<typename _Tp, typename _Up>
00423     inline bool
00424     operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00425     { return !(__b < __a); }
00426 
00427   template<typename _Tp>
00428     inline bool
00429     operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00430     { return !(nullptr < __a); }
00431 
00432   template<typename _Tp>
00433     inline bool
00434     operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00435     { return !(__a < nullptr); }
00436 
00437   template<typename _Tp, typename _Up>
00438     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     inline bool
00444     operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00445     { return nullptr < __a; }
00446 
00447   template<typename _Tp>
00448     inline bool
00449     operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00450     { return __a < nullptr; }
00451 
00452   template<typename _Tp, typename _Up>
00453     inline bool
00454     operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
00455     { return !(__a < __b); }
00456 
00457   template<typename _Tp>
00458     inline bool
00459     operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
00460     { return !(__a < nullptr); }
00461 
00462   template<typename _Tp>
00463     inline bool
00464     operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
00465     { return !(nullptr < __a); }
00466 
00467   template<typename _Tp>
00468     struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
00469     { };
00470 
00471   // 20.7.2.2.8 shared_ptr specialized algorithms.
00472   template<typename _Tp>
00473     inline void
00474     swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
00475     { __a.swap(__b); }
00476 
00477   // 20.7.2.2.9 shared_ptr casts.
00478   template<typename _Tp, typename _Up>
00479     inline shared_ptr<_Tp>
00480     static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00481     {
00482       using _Sp = shared_ptr<_Tp>;
00483       return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
00484     }
00485 
00486   template<typename _Tp, typename _Up>
00487     inline shared_ptr<_Tp>
00488     const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00489     {
00490       using _Sp = shared_ptr<_Tp>;
00491       return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
00492     }
00493 
00494   template<typename _Tp, typename _Up>
00495     inline shared_ptr<_Tp>
00496     dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00497     {
00498       using _Sp = shared_ptr<_Tp>;
00499       if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
00500         return _Sp(__r, __p);
00501       return _Sp();
00502     }
00503 
00504 #if __cplusplus > 201402L
00505   template<typename _Tp, typename _Up>
00506     inline shared_ptr<_Tp>
00507     reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
00508     {
00509       using _Sp = shared_ptr<_Tp>;
00510       return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
00511     }
00512 #endif
00513 
00514   /**
00515    *  @brief  A smart pointer with weak semantics.
00516    *
00517    *  With forwarding constructors and assignment operators.
00518    */
00519   template<typename _Tp>
00520     class weak_ptr : public __weak_ptr<_Tp>
00521     {
00522       template<typename _Arg>
00523         using _Constructible = typename enable_if<
00524           is_constructible<__weak_ptr<_Tp>, _Arg>::value
00525         >::type;
00526 
00527       template<typename _Arg>
00528         using _Assignable = typename enable_if<
00529           is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
00530         >::type;
00531 
00532     public:
00533       constexpr weak_ptr() noexcept = default;
00534 
00535       template<typename _Yp,
00536                typename = _Constructible<const shared_ptr<_Yp>&>>
00537         weak_ptr(const shared_ptr<_Yp>& __r) noexcept
00538         : __weak_ptr<_Tp>(__r) { }
00539 
00540       weak_ptr(const weak_ptr&) noexcept = default;
00541 
00542       template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
00543         weak_ptr(const weak_ptr<_Yp>& __r) noexcept
00544         : __weak_ptr<_Tp>(__r) { }
00545 
00546       weak_ptr(weak_ptr&&) noexcept = default;
00547 
00548       template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
00549         weak_ptr(weak_ptr<_Yp>&& __r) noexcept
00550         : __weak_ptr<_Tp>(std::move(__r)) { }
00551 
00552       weak_ptr&
00553       operator=(const weak_ptr& __r) noexcept = default;
00554 
00555       template<typename _Yp>
00556         _Assignable<const weak_ptr<_Yp>&>
00557         operator=(const weak_ptr<_Yp>& __r) noexcept
00558         {
00559           this->__weak_ptr<_Tp>::operator=(__r);
00560           return *this;
00561         }
00562 
00563       template<typename _Yp>
00564         _Assignable<const shared_ptr<_Yp>&>
00565         operator=(const shared_ptr<_Yp>& __r) noexcept
00566         {
00567           this->__weak_ptr<_Tp>::operator=(__r);
00568           return *this;
00569         }
00570 
00571       weak_ptr&
00572       operator=(weak_ptr&& __r) noexcept = default;
00573 
00574       template<typename _Yp>
00575         _Assignable<weak_ptr<_Yp>>
00576         operator=(weak_ptr<_Yp>&& __r) noexcept
00577         {
00578           this->__weak_ptr<_Tp>::operator=(std::move(__r));
00579           return *this;
00580         }
00581 
00582       shared_ptr<_Tp>
00583       lock() const noexcept
00584       { return shared_ptr<_Tp>(*this, std::nothrow); }
00585     };
00586 
00587 #if __cpp_deduction_guides >= 201606
00588   template<typename _Tp>
00589     weak_ptr(shared_ptr<_Tp>) ->  weak_ptr<_Tp>;
00590 #endif
00591 
00592   // 20.7.2.3.6 weak_ptr specialized algorithms.
00593   template<typename _Tp>
00594     inline void
00595     swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
00596     { __a.swap(__b); }
00597 
00598 
00599   /// Primary template owner_less
00600   template<typename _Tp = void>
00601     struct owner_less;
00602 
00603   /// Void specialization of owner_less
00604   template<>
00605     struct owner_less<void> : _Sp_owner_less<void, void>
00606     { };
00607 
00608   /// Partial specialization of owner_less for shared_ptr.
00609   template<typename _Tp>
00610     struct owner_less<shared_ptr<_Tp>>
00611     : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
00612     { };
00613 
00614   /// Partial specialization of owner_less for weak_ptr.
00615   template<typename _Tp>
00616     struct owner_less<weak_ptr<_Tp>>
00617     : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
00618     { };
00619 
00620   /**
00621    *  @brief Base class allowing use of member function shared_from_this.
00622    */
00623   template<typename _Tp>
00624     class enable_shared_from_this
00625     {
00626     protected:
00627       constexpr enable_shared_from_this() noexcept { }
00628 
00629       enable_shared_from_this(const enable_shared_from_this&) noexcept { }
00630 
00631       enable_shared_from_this&
00632       operator=(const enable_shared_from_this&) noexcept
00633       { return *this; }
00634 
00635       ~enable_shared_from_this() { }
00636 
00637     public:
00638       shared_ptr<_Tp>
00639       shared_from_this()
00640       { return shared_ptr<_Tp>(this->_M_weak_this); }
00641 
00642       shared_ptr<const _Tp>
00643       shared_from_this() const
00644       { return shared_ptr<const _Tp>(this->_M_weak_this); }
00645 
00646 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
00647 #define __cpp_lib_enable_shared_from_this 201603
00648       weak_ptr<_Tp>
00649       weak_from_this() noexcept
00650       { return this->_M_weak_this; }
00651 
00652       weak_ptr<const _Tp>
00653       weak_from_this() const noexcept
00654       { return this->_M_weak_this; }
00655 #endif
00656 
00657     private:
00658       template<typename _Tp1>
00659         void
00660         _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
00661         { _M_weak_this._M_assign(__p, __n); }
00662 
00663       // Found by ADL when this is an associated class.
00664       friend const enable_shared_from_this*
00665       __enable_shared_from_this_base(const __shared_count<>&,
00666                                      const enable_shared_from_this* __p)
00667       { return __p; }
00668 
00669       template<typename, _Lock_policy>
00670         friend class __shared_ptr;
00671 
00672       mutable weak_ptr<_Tp>  _M_weak_this;
00673     };
00674 
00675   /**
00676    *  @brief  Create an object that is owned by a shared_ptr.
00677    *  @param  __a     An allocator.
00678    *  @param  __args  Arguments for the @a _Tp object's constructor.
00679    *  @return A shared_ptr that owns the newly created object.
00680    *  @throw  An exception thrown from @a _Alloc::allocate or from the
00681    *          constructor of @a _Tp.
00682    *
00683    *  A copy of @a __a will be used to allocate memory for the shared_ptr
00684    *  and the new object.
00685    */
00686   template<typename _Tp, typename _Alloc, typename... _Args>
00687     inline shared_ptr<_Tp>
00688     allocate_shared(const _Alloc& __a, _Args&&... __args)
00689     {
00690       return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a,
00691                              std::forward<_Args>(__args)...);
00692     }
00693 
00694   /**
00695    *  @brief  Create an object that is owned by a shared_ptr.
00696    *  @param  __args  Arguments for the @a _Tp object's constructor.
00697    *  @return A shared_ptr that owns the newly created object.
00698    *  @throw  std::bad_alloc, or an exception thrown from the
00699    *          constructor of @a _Tp.
00700    */
00701   template<typename _Tp, typename... _Args>
00702     inline shared_ptr<_Tp>
00703     make_shared(_Args&&... __args)
00704     {
00705       typedef typename std::remove_const<_Tp>::type _Tp_nc;
00706       return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
00707                                        std::forward<_Args>(__args)...);
00708     }
00709 
00710   /// std::hash specialization for shared_ptr.
00711   template<typename _Tp>
00712     struct hash<shared_ptr<_Tp>>
00713     : public __hash_base<size_t, shared_ptr<_Tp>>
00714     {
00715       size_t
00716       operator()(const shared_ptr<_Tp>& __s) const noexcept
00717       {
00718         return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get());
00719       }
00720     };
00721 
00722   // @} group pointer_abstractions
00723 
00724 _GLIBCXX_END_NAMESPACE_VERSION
00725 } // namespace
00726 
00727 #endif // _SHARED_PTR_H