libstdc++
shared_ptr_base.h
Go to the documentation of this file.
00001 // shared_ptr and weak_ptr implementation details -*- 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 bits/shared_ptr_base.h
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_BASE_H
00050 #define _SHARED_PTR_BASE_H 1
00051 
00052 #if __cpp_rtti
00053 # include <typeinfo>
00054 #endif
00055 #include <bits/allocated_ptr.h>
00056 #include <bits/refwrap.h>
00057 #include <bits/stl_function.h>
00058 #include <ext/aligned_buffer.h>
00059 
00060 namespace std _GLIBCXX_VISIBILITY(default)
00061 {
00062 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00063 
00064 #if _GLIBCXX_USE_DEPRECATED
00065   template<typename> class auto_ptr;
00066 #endif
00067 
00068  /**
00069    *  @brief  Exception possibly thrown by @c shared_ptr.
00070    *  @ingroup exceptions
00071    */
00072   class bad_weak_ptr : public std::exception
00073   {
00074   public:
00075     virtual char const* what() const noexcept;
00076 
00077     virtual ~bad_weak_ptr() noexcept;
00078   };
00079 
00080   // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
00081   inline void
00082   __throw_bad_weak_ptr()
00083   { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
00084 
00085   using __gnu_cxx::_Lock_policy;
00086   using __gnu_cxx::__default_lock_policy;
00087   using __gnu_cxx::_S_single;
00088   using __gnu_cxx::_S_mutex;
00089   using __gnu_cxx::_S_atomic;
00090 
00091   // Empty helper class except when the template argument is _S_mutex.
00092   template<_Lock_policy _Lp>
00093     class _Mutex_base
00094     {
00095     protected:
00096       // The atomic policy uses fully-fenced builtins, single doesn't care.
00097       enum { _S_need_barriers = 0 };
00098     };
00099 
00100   template<>
00101     class _Mutex_base<_S_mutex>
00102     : public __gnu_cxx::__mutex
00103     {
00104     protected:
00105       // This policy is used when atomic builtins are not available.
00106       // The replacement atomic operations might not have the necessary
00107       // memory barriers.
00108       enum { _S_need_barriers = 1 };
00109     };
00110 
00111   template<_Lock_policy _Lp = __default_lock_policy>
00112     class _Sp_counted_base
00113     : public _Mutex_base<_Lp>
00114     {
00115     public:
00116       _Sp_counted_base() noexcept
00117       : _M_use_count(1), _M_weak_count(1) { }
00118 
00119       virtual
00120       ~_Sp_counted_base() noexcept
00121       { }
00122 
00123       // Called when _M_use_count drops to zero, to release the resources
00124       // managed by *this.
00125       virtual void
00126       _M_dispose() noexcept = 0;
00127 
00128       // Called when _M_weak_count drops to zero.
00129       virtual void
00130       _M_destroy() noexcept
00131       { delete this; }
00132 
00133       virtual void*
00134       _M_get_deleter(const std::type_info&) noexcept = 0;
00135 
00136       void
00137       _M_add_ref_copy()
00138       { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
00139 
00140       void
00141       _M_add_ref_lock();
00142 
00143       bool
00144       _M_add_ref_lock_nothrow();
00145 
00146       void
00147       _M_release() noexcept
00148       {
00149         // Be race-detector-friendly.  For more info see bits/c++config.
00150         _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
00151         if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
00152           {
00153             _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
00154             _M_dispose();
00155             // There must be a memory barrier between dispose() and destroy()
00156             // to ensure that the effects of dispose() are observed in the
00157             // thread that runs destroy().
00158             // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
00159             if (_Mutex_base<_Lp>::_S_need_barriers)
00160               {
00161                 __atomic_thread_fence (__ATOMIC_ACQ_REL);
00162               }
00163 
00164             // Be race-detector-friendly.  For more info see bits/c++config.
00165             _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
00166             if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
00167                                                        -1) == 1)
00168               {
00169                 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
00170                 _M_destroy();
00171               }
00172           }
00173       }
00174 
00175       void
00176       _M_weak_add_ref() noexcept
00177       { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
00178 
00179       void
00180       _M_weak_release() noexcept
00181       {
00182         // Be race-detector-friendly. For more info see bits/c++config.
00183         _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
00184         if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
00185           {
00186             _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
00187             if (_Mutex_base<_Lp>::_S_need_barriers)
00188               {
00189                 // See _M_release(),
00190                 // destroy() must observe results of dispose()
00191                 __atomic_thread_fence (__ATOMIC_ACQ_REL);
00192               }
00193             _M_destroy();
00194           }
00195       }
00196 
00197       long
00198       _M_get_use_count() const noexcept
00199       {
00200         // No memory barrier is used here so there is no synchronization
00201         // with other threads.
00202         return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
00203       }
00204 
00205     private:
00206       _Sp_counted_base(_Sp_counted_base const&) = delete;
00207       _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
00208 
00209       _Atomic_word  _M_use_count;     // #shared
00210       _Atomic_word  _M_weak_count;    // #weak + (#shared != 0)
00211     };
00212 
00213   template<>
00214     inline void
00215     _Sp_counted_base<_S_single>::
00216     _M_add_ref_lock()
00217     {
00218       if (_M_use_count == 0)
00219         __throw_bad_weak_ptr();
00220       ++_M_use_count;
00221     }
00222 
00223   template<>
00224     inline void
00225     _Sp_counted_base<_S_mutex>::
00226     _M_add_ref_lock()
00227     {
00228       __gnu_cxx::__scoped_lock sentry(*this);
00229       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
00230         {
00231           _M_use_count = 0;
00232           __throw_bad_weak_ptr();
00233         }
00234     }
00235 
00236   template<>
00237     inline void
00238     _Sp_counted_base<_S_atomic>::
00239     _M_add_ref_lock()
00240     {
00241       // Perform lock-free add-if-not-zero operation.
00242       _Atomic_word __count = _M_get_use_count();
00243       do
00244         {
00245           if (__count == 0)
00246             __throw_bad_weak_ptr();
00247           // Replace the current counter value with the old value + 1, as
00248           // long as it's not changed meanwhile.
00249         }
00250       while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
00251                                           true, __ATOMIC_ACQ_REL,
00252                                           __ATOMIC_RELAXED));
00253     }
00254 
00255   template<>
00256     inline bool
00257     _Sp_counted_base<_S_single>::
00258     _M_add_ref_lock_nothrow()
00259     {
00260       if (_M_use_count == 0)
00261         return false;
00262       ++_M_use_count;
00263       return true;
00264     }
00265 
00266   template<>
00267     inline bool
00268     _Sp_counted_base<_S_mutex>::
00269     _M_add_ref_lock_nothrow()
00270     {
00271       __gnu_cxx::__scoped_lock sentry(*this);
00272       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
00273         {
00274           _M_use_count = 0;
00275           return false;
00276         }
00277       return true;
00278     }
00279 
00280   template<>
00281     inline bool
00282     _Sp_counted_base<_S_atomic>::
00283     _M_add_ref_lock_nothrow()
00284     {
00285       // Perform lock-free add-if-not-zero operation.
00286       _Atomic_word __count = _M_get_use_count();
00287       do
00288         {
00289           if (__count == 0)
00290             return false;
00291           // Replace the current counter value with the old value + 1, as
00292           // long as it's not changed meanwhile.
00293         }
00294       while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
00295                                           true, __ATOMIC_ACQ_REL,
00296                                           __ATOMIC_RELAXED));
00297       return true;
00298     }
00299 
00300   template<>
00301     inline void
00302     _Sp_counted_base<_S_single>::_M_add_ref_copy()
00303     { ++_M_use_count; }
00304 
00305   template<>
00306     inline void
00307     _Sp_counted_base<_S_single>::_M_release() noexcept
00308     {
00309       if (--_M_use_count == 0)
00310         {
00311           _M_dispose();
00312           if (--_M_weak_count == 0)
00313             _M_destroy();
00314         }
00315     }
00316 
00317   template<>
00318     inline void
00319     _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
00320     { ++_M_weak_count; }
00321 
00322   template<>
00323     inline void
00324     _Sp_counted_base<_S_single>::_M_weak_release() noexcept
00325     {
00326       if (--_M_weak_count == 0)
00327         _M_destroy();
00328     }
00329 
00330   template<>
00331     inline long
00332     _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
00333     { return _M_use_count; }
00334 
00335 
00336   // Forward declarations.
00337   template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
00338     class __shared_ptr;
00339 
00340   template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
00341     class __weak_ptr;
00342 
00343   template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
00344     class __enable_shared_from_this;
00345 
00346   template<typename _Tp>
00347     class shared_ptr;
00348 
00349   template<typename _Tp>
00350     class weak_ptr;
00351 
00352   template<typename _Tp>
00353     struct owner_less;
00354 
00355   template<typename _Tp>
00356     class enable_shared_from_this;
00357 
00358   template<_Lock_policy _Lp = __default_lock_policy>
00359     class __weak_count;
00360 
00361   template<_Lock_policy _Lp = __default_lock_policy>
00362     class __shared_count;
00363 
00364 
00365   // Counted ptr with no deleter or allocator support
00366   template<typename _Ptr, _Lock_policy _Lp>
00367     class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
00368     {
00369     public:
00370       explicit
00371       _Sp_counted_ptr(_Ptr __p) noexcept
00372       : _M_ptr(__p) { }
00373 
00374       virtual void
00375       _M_dispose() noexcept
00376       { delete _M_ptr; }
00377 
00378       virtual void
00379       _M_destroy() noexcept
00380       { delete this; }
00381 
00382       virtual void*
00383       _M_get_deleter(const std::type_info&) noexcept
00384       { return nullptr; }
00385 
00386       _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
00387       _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
00388 
00389     private:
00390       _Ptr             _M_ptr;
00391     };
00392 
00393   template<>
00394     inline void
00395     _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
00396 
00397   template<>
00398     inline void
00399     _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
00400 
00401   template<>
00402     inline void
00403     _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
00404 
00405   template<int _Nm, typename _Tp,
00406            bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
00407     struct _Sp_ebo_helper;
00408 
00409   /// Specialization using EBO.
00410   template<int _Nm, typename _Tp>
00411     struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
00412     {
00413       explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
00414       explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { }
00415 
00416       static _Tp&
00417       _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
00418     };
00419 
00420   /// Specialization not using EBO.
00421   template<int _Nm, typename _Tp>
00422     struct _Sp_ebo_helper<_Nm, _Tp, false>
00423     {
00424       explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
00425       explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
00426 
00427       static _Tp&
00428       _S_get(_Sp_ebo_helper& __eboh)
00429       { return __eboh._M_tp; }
00430 
00431     private:
00432       _Tp _M_tp;
00433     };
00434 
00435   // Support for custom deleter and/or allocator
00436   template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
00437     class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
00438     {
00439       class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
00440       {
00441         typedef _Sp_ebo_helper<0, _Deleter>     _Del_base;
00442         typedef _Sp_ebo_helper<1, _Alloc>       _Alloc_base;
00443 
00444       public:
00445         _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
00446         : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a)
00447         { }
00448 
00449         _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
00450         _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
00451 
00452         _Ptr _M_ptr;
00453       };
00454 
00455     public:
00456       using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
00457 
00458       // __d(__p) must not throw.
00459       _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
00460       : _M_impl(__p, std::move(__d), _Alloc()) { }
00461 
00462       // __d(__p) must not throw.
00463       _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
00464       : _M_impl(__p, std::move(__d), __a) { }
00465 
00466       ~_Sp_counted_deleter() noexcept { }
00467 
00468       virtual void
00469       _M_dispose() noexcept
00470       { _M_impl._M_del()(_M_impl._M_ptr); }
00471 
00472       virtual void
00473       _M_destroy() noexcept
00474       {
00475         __allocator_type __a(_M_impl._M_alloc());
00476         __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
00477         this->~_Sp_counted_deleter();
00478       }
00479 
00480       virtual void*
00481       _M_get_deleter(const std::type_info& __ti) noexcept
00482       {
00483 #if __cpp_rtti
00484         // _GLIBCXX_RESOLVE_LIB_DEFECTS
00485         // 2400. shared_ptr's get_deleter() should use addressof()
00486         return __ti == typeid(_Deleter)
00487           ? std::__addressof(_M_impl._M_del())
00488           : nullptr;
00489 #else
00490         return nullptr;
00491 #endif
00492       }
00493 
00494     private:
00495       _Impl _M_impl;
00496     };
00497 
00498   // helpers for make_shared / allocate_shared
00499 
00500   struct _Sp_make_shared_tag { };
00501 
00502   template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
00503     class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
00504     {
00505       class _Impl : _Sp_ebo_helper<0, _Alloc>
00506       {
00507         typedef _Sp_ebo_helper<0, _Alloc>       _A_base;
00508 
00509       public:
00510         explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
00511 
00512         _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
00513 
00514         __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
00515       };
00516 
00517     public:
00518       using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
00519 
00520       template<typename... _Args>
00521         _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
00522         : _M_impl(__a)
00523         {
00524           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00525           // 2070.  allocate_shared should use allocator_traits<A>::construct
00526           allocator_traits<_Alloc>::construct(__a, _M_ptr(),
00527               std::forward<_Args>(__args)...); // might throw
00528         }
00529 
00530       ~_Sp_counted_ptr_inplace() noexcept { }
00531 
00532       virtual void
00533       _M_dispose() noexcept
00534       {
00535         allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
00536       }
00537 
00538       // Override because the allocator needs to know the dynamic type
00539       virtual void
00540       _M_destroy() noexcept
00541       {
00542         __allocator_type __a(_M_impl._M_alloc());
00543         __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
00544         this->~_Sp_counted_ptr_inplace();
00545       }
00546 
00547       // Sneaky trick so __shared_ptr can get the managed pointer
00548       virtual void*
00549       _M_get_deleter(const std::type_info& __ti) noexcept
00550       {
00551 #if __cpp_rtti
00552         if (__ti == typeid(_Sp_make_shared_tag))
00553           return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
00554 #endif
00555         return nullptr;
00556       }
00557 
00558     private:
00559       _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
00560 
00561       _Impl _M_impl;
00562     };
00563 
00564   // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
00565   struct __sp_array_delete
00566   {
00567     template<typename _Yp>
00568       void operator()(_Yp* __p) const { delete[] __p; }
00569   };
00570 
00571   template<_Lock_policy _Lp>
00572     class __shared_count
00573     {
00574     public:
00575       constexpr __shared_count() noexcept : _M_pi(0)
00576       { }
00577 
00578       template<typename _Ptr>
00579         explicit
00580         __shared_count(_Ptr __p) : _M_pi(0)
00581         {
00582           __try
00583             {
00584               _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
00585             }
00586           __catch(...)
00587             {
00588               delete __p;
00589               __throw_exception_again;
00590             }
00591         }
00592 
00593       template<typename _Ptr>
00594         __shared_count(_Ptr __p, /* is_array = */ false_type)
00595         : __shared_count(__p)
00596         { }
00597 
00598       template<typename _Ptr>
00599         __shared_count(_Ptr __p, /* is_array = */ true_type)
00600         : __shared_count(__p, __sp_array_delete{}, allocator<void>())
00601         { }
00602 
00603       template<typename _Ptr, typename _Deleter>
00604         __shared_count(_Ptr __p, _Deleter __d)
00605         : __shared_count(__p, std::move(__d), allocator<void>())
00606         { }
00607 
00608       template<typename _Ptr, typename _Deleter, typename _Alloc>
00609         __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
00610         {
00611           typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
00612           __try
00613             {
00614               typename _Sp_cd_type::__allocator_type __a2(__a);
00615               auto __guard = std::__allocate_guarded(__a2);
00616               _Sp_cd_type* __mem = __guard.get();
00617               ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
00618               _M_pi = __mem;
00619               __guard = nullptr;
00620             }
00621           __catch(...)
00622             {
00623               __d(__p); // Call _Deleter on __p.
00624               __throw_exception_again;
00625             }
00626         }
00627 
00628       template<typename _Tp, typename _Alloc, typename... _Args>
00629         __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
00630                        _Args&&... __args)
00631         : _M_pi(0)
00632         {
00633           typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
00634           typename _Sp_cp_type::__allocator_type __a2(__a);
00635           auto __guard = std::__allocate_guarded(__a2);
00636           _Sp_cp_type* __mem = __guard.get();
00637           ::new (__mem) _Sp_cp_type(std::move(__a),
00638                                     std::forward<_Args>(__args)...);
00639           _M_pi = __mem;
00640           __guard = nullptr;
00641         }
00642 
00643 #if _GLIBCXX_USE_DEPRECATED
00644       // Special case for auto_ptr<_Tp> to provide the strong guarantee.
00645       template<typename _Tp>
00646         explicit
00647         __shared_count(std::auto_ptr<_Tp>&& __r);
00648 #endif
00649 
00650       // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
00651       template<typename _Tp, typename _Del>
00652         explicit
00653         __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
00654         {
00655           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00656           // 2415. Inconsistency between unique_ptr and shared_ptr
00657           if (__r.get() == nullptr)
00658             return;
00659 
00660           using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
00661           using _Del2 = typename conditional<is_reference<_Del>::value,
00662               reference_wrapper<typename remove_reference<_Del>::type>,
00663               _Del>::type;
00664           using _Sp_cd_type
00665             = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
00666           using _Alloc = allocator<_Sp_cd_type>;
00667           using _Alloc_traits = allocator_traits<_Alloc>;
00668           _Alloc __a;
00669           _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
00670           _Alloc_traits::construct(__a, __mem, __r.release(),
00671                                    __r.get_deleter());  // non-throwing
00672           _M_pi = __mem;
00673         }
00674 
00675       // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
00676       explicit __shared_count(const __weak_count<_Lp>& __r);
00677 
00678       // Does not throw if __r._M_get_use_count() == 0, caller must check.
00679       explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
00680 
00681       ~__shared_count() noexcept
00682       {
00683         if (_M_pi != nullptr)
00684           _M_pi->_M_release();
00685       }
00686 
00687       __shared_count(const __shared_count& __r) noexcept
00688       : _M_pi(__r._M_pi)
00689       {
00690         if (_M_pi != 0)
00691           _M_pi->_M_add_ref_copy();
00692       }
00693 
00694       __shared_count&
00695       operator=(const __shared_count& __r) noexcept
00696       {
00697         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00698         if (__tmp != _M_pi)
00699           {
00700             if (__tmp != 0)
00701               __tmp->_M_add_ref_copy();
00702             if (_M_pi != 0)
00703               _M_pi->_M_release();
00704             _M_pi = __tmp;
00705           }
00706         return *this;
00707       }
00708 
00709       void
00710       _M_swap(__shared_count& __r) noexcept
00711       {
00712         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00713         __r._M_pi = _M_pi;
00714         _M_pi = __tmp;
00715       }
00716 
00717       long
00718       _M_get_use_count() const noexcept
00719       { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
00720 
00721       bool
00722       _M_unique() const noexcept
00723       { return this->_M_get_use_count() == 1; }
00724 
00725       void*
00726       _M_get_deleter(const std::type_info& __ti) const noexcept
00727       { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
00728 
00729       bool
00730       _M_less(const __shared_count& __rhs) const noexcept
00731       { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
00732 
00733       bool
00734       _M_less(const __weak_count<_Lp>& __rhs) const noexcept
00735       { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
00736 
00737       // Friend function injected into enclosing namespace and found by ADL
00738       friend inline bool
00739       operator==(const __shared_count& __a, const __shared_count& __b) noexcept
00740       { return __a._M_pi == __b._M_pi; }
00741 
00742     private:
00743       friend class __weak_count<_Lp>;
00744 
00745       _Sp_counted_base<_Lp>*  _M_pi;
00746     };
00747 
00748 
00749   template<_Lock_policy _Lp>
00750     class __weak_count
00751     {
00752     public:
00753       constexpr __weak_count() noexcept : _M_pi(nullptr)
00754       { }
00755 
00756       __weak_count(const __shared_count<_Lp>& __r) noexcept
00757       : _M_pi(__r._M_pi)
00758       {
00759         if (_M_pi != nullptr)
00760           _M_pi->_M_weak_add_ref();
00761       }
00762 
00763       __weak_count(const __weak_count& __r) noexcept
00764       : _M_pi(__r._M_pi)
00765       {
00766         if (_M_pi != nullptr)
00767           _M_pi->_M_weak_add_ref();
00768       }
00769 
00770       __weak_count(__weak_count&& __r) noexcept
00771       : _M_pi(__r._M_pi)
00772       { __r._M_pi = nullptr; }
00773 
00774       ~__weak_count() noexcept
00775       {
00776         if (_M_pi != nullptr)
00777           _M_pi->_M_weak_release();
00778       }
00779 
00780       __weak_count&
00781       operator=(const __shared_count<_Lp>& __r) noexcept
00782       {
00783         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00784         if (__tmp != nullptr)
00785           __tmp->_M_weak_add_ref();
00786         if (_M_pi != nullptr)
00787           _M_pi->_M_weak_release();
00788         _M_pi = __tmp;
00789         return *this;
00790       }
00791 
00792       __weak_count&
00793       operator=(const __weak_count& __r) noexcept
00794       {
00795         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00796         if (__tmp != nullptr)
00797           __tmp->_M_weak_add_ref();
00798         if (_M_pi != nullptr)
00799           _M_pi->_M_weak_release();
00800         _M_pi = __tmp;
00801         return *this;
00802       }
00803 
00804       __weak_count&
00805       operator=(__weak_count&& __r) noexcept
00806       {
00807         if (_M_pi != nullptr)
00808           _M_pi->_M_weak_release();
00809         _M_pi = __r._M_pi;
00810         __r._M_pi = nullptr;
00811         return *this;
00812       }
00813 
00814       void
00815       _M_swap(__weak_count& __r) noexcept
00816       {
00817         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00818         __r._M_pi = _M_pi;
00819         _M_pi = __tmp;
00820       }
00821 
00822       long
00823       _M_get_use_count() const noexcept
00824       { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
00825 
00826       bool
00827       _M_less(const __weak_count& __rhs) const noexcept
00828       { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
00829 
00830       bool
00831       _M_less(const __shared_count<_Lp>& __rhs) const noexcept
00832       { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
00833 
00834       // Friend function injected into enclosing namespace and found by ADL
00835       friend inline bool
00836       operator==(const __weak_count& __a, const __weak_count& __b) noexcept
00837       { return __a._M_pi == __b._M_pi; }
00838 
00839     private:
00840       friend class __shared_count<_Lp>;
00841 
00842       _Sp_counted_base<_Lp>*  _M_pi;
00843     };
00844 
00845   // Now that __weak_count is defined we can define this constructor:
00846   template<_Lock_policy _Lp>
00847     inline
00848     __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
00849     : _M_pi(__r._M_pi)
00850     {
00851       if (_M_pi != nullptr)
00852         _M_pi->_M_add_ref_lock();
00853       else
00854         __throw_bad_weak_ptr();
00855     }
00856 
00857   // Now that __weak_count is defined we can define this constructor:
00858   template<_Lock_policy _Lp>
00859     inline
00860     __shared_count<_Lp>::
00861     __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
00862     : _M_pi(__r._M_pi)
00863     {
00864       if (_M_pi != nullptr)
00865         if (!_M_pi->_M_add_ref_lock_nothrow())
00866           _M_pi = nullptr;
00867     }
00868 
00869 #define __cpp_lib_shared_ptr_arrays 201603
00870 
00871   // Helper traits for shared_ptr of array:
00872 
00873   // A pointer type Y* is said to be compatible with a pointer type T* when
00874   // either Y* is convertible to T* or Y is U[N] and T is U cv [].
00875   template<typename _Yp_ptr, typename _Tp_ptr>
00876     struct __sp_compatible_with
00877     : false_type
00878     { };
00879 
00880   template<typename _Yp, typename _Tp>
00881     struct __sp_compatible_with<_Yp*, _Tp*>
00882     : is_convertible<_Yp*, _Tp*>::type
00883     { };
00884 
00885   template<typename _Up, size_t _Nm>
00886     struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
00887     : true_type
00888     { };
00889 
00890   template<typename _Up, size_t _Nm>
00891     struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
00892     : true_type
00893     { };
00894 
00895   template<typename _Up, size_t _Nm>
00896     struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
00897     : true_type
00898     { };
00899 
00900   template<typename _Up, size_t _Nm>
00901     struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
00902     : true_type
00903     { };
00904 
00905   // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
00906   template<typename _Up, size_t _Nm, typename _Yp, typename = void>
00907     struct __sp_is_constructible_arrN
00908     : false_type
00909     { };
00910 
00911   template<typename _Up, size_t _Nm, typename _Yp>
00912     struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
00913     : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
00914     { };
00915 
00916   // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
00917   template<typename _Up, typename _Yp, typename = void>
00918     struct __sp_is_constructible_arr
00919     : false_type
00920     { };
00921 
00922   template<typename _Up, typename _Yp>
00923     struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
00924     : is_convertible<_Yp(*)[], _Up(*)[]>::type
00925     { };
00926 
00927   // Trait to check if shared_ptr<T> can be constructed from Y*.
00928   template<typename _Tp, typename _Yp>
00929     struct __sp_is_constructible;
00930 
00931   // When T is U[N], Y(*)[N] shall be convertible to T*;
00932   template<typename _Up, size_t _Nm, typename _Yp>
00933     struct __sp_is_constructible<_Up[_Nm], _Yp>
00934     : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
00935     { };
00936 
00937   // when T is U[], Y(*)[] shall be convertible to T*;
00938   template<typename _Up, typename _Yp>
00939     struct __sp_is_constructible<_Up[], _Yp>
00940     : __sp_is_constructible_arr<_Up, _Yp>::type
00941     { };
00942 
00943   // otherwise, Y* shall be convertible to T*.
00944   template<typename _Tp, typename _Yp>
00945     struct __sp_is_constructible
00946     : is_convertible<_Yp*, _Tp*>::type
00947     { };
00948 
00949 
00950   // Define operator* and operator-> for shared_ptr<T>.
00951   template<typename _Tp, _Lock_policy _Lp,
00952            bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
00953     class __shared_ptr_access
00954     {
00955     public:
00956       using element_type = _Tp;
00957 
00958       element_type&
00959       operator*() const noexcept
00960       {
00961         __glibcxx_assert(_M_get() != nullptr);
00962         return *_M_get();
00963       }
00964 
00965       element_type*
00966       operator->() const noexcept
00967       {
00968         _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
00969         return _M_get();
00970       }
00971 
00972     private:
00973       element_type*
00974       _M_get() const noexcept
00975       { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
00976     };
00977 
00978   // Define operator-> for shared_ptr<cv void>.
00979   template<typename _Tp, _Lock_policy _Lp>
00980     class __shared_ptr_access<_Tp, _Lp, false, true>
00981     {
00982     public:
00983       using element_type = _Tp;
00984 
00985       element_type*
00986       operator->() const noexcept
00987       {
00988         auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
00989         _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
00990         return __ptr;
00991       }
00992     };
00993 
00994   // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
00995   template<typename _Tp, _Lock_policy _Lp>
00996     class __shared_ptr_access<_Tp, _Lp, true, false>
00997     {
00998     public:
00999       using element_type = typename remove_extent<_Tp>::type;
01000 
01001 #if __cplusplus <= 201402L
01002       [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
01003       element_type&
01004       operator*() const noexcept
01005       {
01006         __glibcxx_assert(_M_get() != nullptr);
01007         return *_M_get();
01008       }
01009 
01010       [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
01011       element_type*
01012       operator->() const noexcept
01013       {
01014         _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
01015         return _M_get();
01016       }
01017 #endif
01018 
01019       element_type&
01020       operator[](ptrdiff_t __i) const
01021       {
01022         __glibcxx_assert(_M_get() != nullptr);
01023         __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
01024         return _M_get()[__i];
01025       }
01026 
01027     private:
01028       element_type*
01029       _M_get() const noexcept
01030       { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
01031     };
01032 
01033   template<typename _Tp, _Lock_policy _Lp>
01034     class __shared_ptr
01035     : public __shared_ptr_access<_Tp, _Lp>
01036     {
01037     public:
01038       using element_type = typename remove_extent<_Tp>::type;
01039 
01040     private:
01041       // Constraint for taking ownership of a pointer of type _Yp*:
01042       template<typename _Yp>
01043         using _SafeConv
01044           = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
01045 
01046       // Constraint for construction from shared_ptr and weak_ptr:
01047       template<typename _Yp, typename _Res = void>
01048         using _Compatible = typename
01049           enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
01050 
01051       // Constraint for assignment from shared_ptr and weak_ptr:
01052       template<typename _Yp>
01053         using _Assignable = _Compatible<_Yp, __shared_ptr&>;
01054 
01055       // Constraint for construction from unique_ptr:
01056       template<typename _Yp, typename _Del, typename _Res = void,
01057                typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
01058         using _UniqCompatible = typename enable_if<__and_<
01059           __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
01060           >::value, _Res>::type;
01061 
01062       // Constraint for assignment from unique_ptr:
01063       template<typename _Yp, typename _Del>
01064         using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
01065 
01066     public:
01067 
01068 #if __cplusplus > 201402L
01069       using weak_type = __weak_ptr<_Tp, _Lp>;
01070 #endif
01071 
01072       constexpr __shared_ptr() noexcept
01073       : _M_ptr(0), _M_refcount()
01074       { }
01075 
01076       template<typename _Yp, typename = _SafeConv<_Yp>>
01077         explicit
01078         __shared_ptr(_Yp* __p)
01079         : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
01080         {
01081           static_assert( !is_void<_Yp>::value, "incomplete type" );
01082           static_assert( sizeof(_Yp) > 0, "incomplete type" );
01083           _M_enable_shared_from_this_with(__p);
01084         }
01085 
01086       template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
01087         __shared_ptr(_Yp* __p, _Deleter __d)
01088         : _M_ptr(__p), _M_refcount(__p, std::move(__d))
01089         {
01090           static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
01091               "deleter expression d(p) is well-formed");
01092           _M_enable_shared_from_this_with(__p);
01093         }
01094 
01095       template<typename _Yp, typename _Deleter, typename _Alloc,
01096                typename = _SafeConv<_Yp>>
01097         __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
01098         : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
01099         {
01100           static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
01101               "deleter expression d(p) is well-formed");
01102           _M_enable_shared_from_this_with(__p);
01103         }
01104 
01105       template<typename _Deleter>
01106         __shared_ptr(nullptr_t __p, _Deleter __d)
01107         : _M_ptr(0), _M_refcount(__p, std::move(__d))
01108         { }
01109 
01110       template<typename _Deleter, typename _Alloc>
01111         __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
01112         : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
01113         { }
01114 
01115       template<typename _Yp>
01116         __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
01117                      element_type* __p) noexcept
01118         : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
01119         { }
01120 
01121       __shared_ptr(const __shared_ptr&) noexcept = default;
01122       __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
01123       ~__shared_ptr() = default;
01124 
01125       template<typename _Yp, typename = _Compatible<_Yp>>
01126         __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
01127         : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
01128         { }
01129 
01130       __shared_ptr(__shared_ptr&& __r) noexcept
01131       : _M_ptr(__r._M_ptr), _M_refcount()
01132       {
01133         _M_refcount._M_swap(__r._M_refcount);
01134         __r._M_ptr = 0;
01135       }
01136 
01137       template<typename _Yp, typename = _Compatible<_Yp>>
01138         __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
01139         : _M_ptr(__r._M_ptr), _M_refcount()
01140         {
01141           _M_refcount._M_swap(__r._M_refcount);
01142           __r._M_ptr = 0;
01143         }
01144 
01145       template<typename _Yp, typename = _Compatible<_Yp>>
01146         explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
01147         : _M_refcount(__r._M_refcount) // may throw
01148         {
01149           // It is now safe to copy __r._M_ptr, as
01150           // _M_refcount(__r._M_refcount) did not throw.
01151           _M_ptr = __r._M_ptr;
01152         }
01153 
01154       // If an exception is thrown this constructor has no effect.
01155       template<typename _Yp, typename _Del,
01156                typename = _UniqCompatible<_Yp, _Del>>
01157         __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
01158         : _M_ptr(__r.get()), _M_refcount()
01159         {
01160           auto __raw = _S_raw_ptr(__r.get());
01161           _M_refcount = __shared_count<_Lp>(std::move(__r));
01162           _M_enable_shared_from_this_with(__raw);
01163         }
01164 
01165 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
01166     protected:
01167       // If an exception is thrown this constructor has no effect.
01168       template<typename _Tp1, typename _Del,
01169                typename enable_if<__and_<
01170                  __not_<is_array<_Tp>>, is_array<_Tp1>,
01171                  is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
01172                >::value, bool>::type = true>
01173         __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
01174         : _M_ptr(__r.get()), _M_refcount()
01175         {
01176           auto __raw = _S_raw_ptr(__r.get());
01177           _M_refcount = __shared_count<_Lp>(std::move(__r));
01178           _M_enable_shared_from_this_with(__raw);
01179         }
01180     public:
01181 #endif
01182 
01183 #if _GLIBCXX_USE_DEPRECATED
01184       // Postcondition: use_count() == 1 and __r.get() == 0
01185       template<typename _Yp, typename = _Compatible<_Yp>>
01186         __shared_ptr(auto_ptr<_Yp>&& __r);
01187 #endif
01188 
01189       constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
01190 
01191       template<typename _Yp>
01192         _Assignable<_Yp>
01193         operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
01194         {
01195           _M_ptr = __r._M_ptr;
01196           _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
01197           return *this;
01198         }
01199 
01200 #if _GLIBCXX_USE_DEPRECATED
01201       template<typename _Yp>
01202         _Assignable<_Yp>
01203         operator=(auto_ptr<_Yp>&& __r)
01204         {
01205           __shared_ptr(std::move(__r)).swap(*this);
01206           return *this;
01207         }
01208 #endif
01209 
01210       __shared_ptr&
01211       operator=(__shared_ptr&& __r) noexcept
01212       {
01213         __shared_ptr(std::move(__r)).swap(*this);
01214         return *this;
01215       }
01216 
01217       template<class _Yp>
01218         _Assignable<_Yp>
01219         operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
01220         {
01221           __shared_ptr(std::move(__r)).swap(*this);
01222           return *this;
01223         }
01224 
01225       template<typename _Yp, typename _Del>
01226         _UniqAssignable<_Yp, _Del>
01227         operator=(unique_ptr<_Yp, _Del>&& __r)
01228         {
01229           __shared_ptr(std::move(__r)).swap(*this);
01230           return *this;
01231         }
01232 
01233       void
01234       reset() noexcept
01235       { __shared_ptr().swap(*this); }
01236 
01237       template<typename _Yp>
01238         _SafeConv<_Yp>
01239         reset(_Yp* __p) // _Yp must be complete.
01240         {
01241           // Catch self-reset errors.
01242           __glibcxx_assert(__p == 0 || __p != _M_ptr);
01243           __shared_ptr(__p).swap(*this);
01244         }
01245 
01246       template<typename _Yp, typename _Deleter>
01247         _SafeConv<_Yp>
01248         reset(_Yp* __p, _Deleter __d)
01249         { __shared_ptr(__p, std::move(__d)).swap(*this); }
01250 
01251       template<typename _Yp, typename _Deleter, typename _Alloc>
01252         _SafeConv<_Yp>
01253         reset(_Yp* __p, _Deleter __d, _Alloc __a)
01254         { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
01255 
01256       element_type*
01257       get() const noexcept
01258       { return _M_ptr; }
01259 
01260       explicit operator bool() const // never throws
01261       { return _M_ptr == 0 ? false : true; }
01262 
01263       bool
01264       unique() const noexcept
01265       { return _M_refcount._M_unique(); }
01266 
01267       long
01268       use_count() const noexcept
01269       { return _M_refcount._M_get_use_count(); }
01270 
01271       void
01272       swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
01273       {
01274         std::swap(_M_ptr, __other._M_ptr);
01275         _M_refcount._M_swap(__other._M_refcount);
01276       }
01277 
01278       template<typename _Tp1>
01279         bool
01280         owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
01281         { return _M_refcount._M_less(__rhs._M_refcount); }
01282 
01283       template<typename _Tp1>
01284         bool
01285         owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
01286         { return _M_refcount._M_less(__rhs._M_refcount); }
01287 
01288 #if __cpp_rtti
01289     protected:
01290       // This constructor is non-standard, it is used by allocate_shared.
01291       template<typename _Alloc, typename... _Args>
01292         __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
01293                      _Args&&... __args)
01294         : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
01295                                 std::forward<_Args>(__args)...)
01296         {
01297           // _M_ptr needs to point to the newly constructed object.
01298           // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
01299           void* __p = _M_refcount._M_get_deleter(typeid(__tag));
01300           _M_ptr = static_cast<_Tp*>(__p);
01301           _M_enable_shared_from_this_with(_M_ptr);
01302         }
01303 #else
01304       template<typename _Alloc>
01305         struct _Deleter
01306         {
01307           void operator()(typename _Alloc::value_type* __ptr)
01308           {
01309             __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr };
01310             allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get());
01311           }
01312           _Alloc _M_alloc;
01313         };
01314 
01315       template<typename _Alloc, typename... _Args>
01316         __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
01317                      _Args&&... __args)
01318         : _M_ptr(), _M_refcount()
01319         {
01320           typedef typename allocator_traits<_Alloc>::template
01321             rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
01322           _Deleter<typename __traits::allocator_type> __del = { __a };
01323           auto __guard = std::__allocate_guarded(__del._M_alloc);
01324           auto __ptr = __guard.get();
01325           // _GLIBCXX_RESOLVE_LIB_DEFECTS
01326           // 2070. allocate_shared should use allocator_traits<A>::construct
01327           __traits::construct(__del._M_alloc, __ptr,
01328                               std::forward<_Args>(__args)...);
01329           __guard = nullptr;
01330           __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
01331           _M_refcount._M_swap(__count);
01332           _M_ptr = __ptr;
01333           _M_enable_shared_from_this_with(_M_ptr);
01334         }
01335 #endif
01336 
01337       template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
01338                typename... _Args>
01339         friend __shared_ptr<_Tp1, _Lp1>
01340         __allocate_shared(const _Alloc& __a, _Args&&... __args);
01341 
01342       // This constructor is used by __weak_ptr::lock() and
01343       // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
01344       __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
01345       : _M_refcount(__r._M_refcount, std::nothrow)
01346       {
01347         _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
01348       }
01349 
01350       friend class __weak_ptr<_Tp, _Lp>;
01351 
01352     private:
01353 
01354       template<typename _Yp>
01355         using __esft_base_t = decltype(__enable_shared_from_this_base(
01356               std::declval<const __shared_count<_Lp>&>(),
01357               std::declval<_Yp*>()));
01358 
01359       // Detect an accessible and unambiguous enable_shared_from_this base.
01360       template<typename _Yp, typename = void>
01361         struct __has_esft_base
01362         : false_type { };
01363 
01364       template<typename _Yp>
01365         struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
01366         : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
01367 
01368       template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
01369         typename enable_if<__has_esft_base<_Yp2>::value>::type
01370         _M_enable_shared_from_this_with(_Yp* __p) noexcept
01371         {
01372           if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
01373             __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
01374         }
01375 
01376       template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
01377         typename enable_if<!__has_esft_base<_Yp2>::value>::type
01378         _M_enable_shared_from_this_with(_Yp*) noexcept
01379         { }
01380 
01381       void*
01382       _M_get_deleter(const std::type_info& __ti) const noexcept
01383       { return _M_refcount._M_get_deleter(__ti); }
01384 
01385       template<typename _Tp1>
01386         static _Tp1*
01387         _S_raw_ptr(_Tp1* __ptr)
01388         { return __ptr; }
01389 
01390       template<typename _Tp1>
01391         static auto
01392         _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
01393         { return std::__addressof(*__ptr); }
01394 
01395       template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
01396       template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
01397 
01398       template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
01399         friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
01400 
01401       element_type*        _M_ptr;         // Contained pointer.
01402       __shared_count<_Lp>  _M_refcount;    // Reference counter.
01403     };
01404 
01405 
01406   // 20.7.2.2.7 shared_ptr comparisons
01407   template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
01408     inline bool
01409     operator==(const __shared_ptr<_Tp1, _Lp>& __a,
01410                const __shared_ptr<_Tp2, _Lp>& __b) noexcept
01411     { return __a.get() == __b.get(); }
01412 
01413   template<typename _Tp, _Lock_policy _Lp>
01414     inline bool
01415     operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
01416     { return !__a; }
01417 
01418   template<typename _Tp, _Lock_policy _Lp>
01419     inline bool
01420     operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
01421     { return !__a; }
01422 
01423   template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
01424     inline bool
01425     operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
01426                const __shared_ptr<_Tp2, _Lp>& __b) noexcept
01427     { return __a.get() != __b.get(); }
01428 
01429   template<typename _Tp, _Lock_policy _Lp>
01430     inline bool
01431     operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
01432     { return (bool)__a; }
01433 
01434   template<typename _Tp, _Lock_policy _Lp>
01435     inline bool
01436     operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
01437     { return (bool)__a; }
01438 
01439   template<typename _Tp, typename _Up, _Lock_policy _Lp>
01440     inline bool
01441     operator<(const __shared_ptr<_Tp, _Lp>& __a,
01442               const __shared_ptr<_Up, _Lp>& __b) noexcept
01443     {
01444       using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
01445       using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
01446       using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
01447       return less<_Vp>()(__a.get(), __b.get());
01448     }
01449 
01450   template<typename _Tp, _Lock_policy _Lp>
01451     inline bool
01452     operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
01453     {
01454       using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
01455       return less<_Tp_elt*>()(__a.get(), nullptr);
01456     }
01457 
01458   template<typename _Tp, _Lock_policy _Lp>
01459     inline bool
01460     operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
01461     {
01462       using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
01463       return less<_Tp_elt*>()(nullptr, __a.get());
01464     }
01465 
01466   template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
01467     inline bool
01468     operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
01469                const __shared_ptr<_Tp2, _Lp>& __b) noexcept
01470     { return !(__b < __a); }
01471 
01472   template<typename _Tp, _Lock_policy _Lp>
01473     inline bool
01474     operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
01475     { return !(nullptr < __a); }
01476 
01477   template<typename _Tp, _Lock_policy _Lp>
01478     inline bool
01479     operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
01480     { return !(__a < nullptr); }
01481 
01482   template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
01483     inline bool
01484     operator>(const __shared_ptr<_Tp1, _Lp>& __a,
01485               const __shared_ptr<_Tp2, _Lp>& __b) noexcept
01486     { return (__b < __a); }
01487 
01488   template<typename _Tp, _Lock_policy _Lp>
01489     inline bool
01490     operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
01491     { return nullptr < __a; }
01492 
01493   template<typename _Tp, _Lock_policy _Lp>
01494     inline bool
01495     operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
01496     { return __a < nullptr; }
01497 
01498   template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
01499     inline bool
01500     operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
01501                const __shared_ptr<_Tp2, _Lp>& __b) noexcept
01502     { return !(__a < __b); }
01503 
01504   template<typename _Tp, _Lock_policy _Lp>
01505     inline bool
01506     operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
01507     { return !(__a < nullptr); }
01508 
01509   template<typename _Tp, _Lock_policy _Lp>
01510     inline bool
01511     operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
01512     { return !(nullptr < __a); }
01513 
01514   template<typename _Sp>
01515     struct _Sp_less : public binary_function<_Sp, _Sp, bool>
01516     {
01517       bool
01518       operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
01519       {
01520         typedef typename _Sp::element_type element_type;
01521         return std::less<element_type*>()(__lhs.get(), __rhs.get());
01522       }
01523     };
01524 
01525   template<typename _Tp, _Lock_policy _Lp>
01526     struct less<__shared_ptr<_Tp, _Lp>>
01527     : public _Sp_less<__shared_ptr<_Tp, _Lp>>
01528     { };
01529 
01530   // 20.7.2.2.8 shared_ptr specialized algorithms.
01531   template<typename _Tp, _Lock_policy _Lp>
01532     inline void
01533     swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
01534     { __a.swap(__b); }
01535 
01536   // 20.7.2.2.9 shared_ptr casts
01537 
01538   // The seemingly equivalent code:
01539   // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
01540   // will eventually result in undefined behaviour, attempting to
01541   // delete the same object twice.
01542   /// static_pointer_cast
01543   template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
01544     inline __shared_ptr<_Tp, _Lp>
01545     static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
01546     {
01547       using _Sp = __shared_ptr<_Tp, _Lp>;
01548       return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
01549     }
01550 
01551   // The seemingly equivalent code:
01552   // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
01553   // will eventually result in undefined behaviour, attempting to
01554   // delete the same object twice.
01555   /// const_pointer_cast
01556   template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
01557     inline __shared_ptr<_Tp, _Lp>
01558     const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
01559     {
01560       using _Sp = __shared_ptr<_Tp, _Lp>;
01561       return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
01562     }
01563 
01564   // The seemingly equivalent code:
01565   // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
01566   // will eventually result in undefined behaviour, attempting to
01567   // delete the same object twice.
01568   /// dynamic_pointer_cast
01569   template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
01570     inline __shared_ptr<_Tp, _Lp>
01571     dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
01572     {
01573       using _Sp = __shared_ptr<_Tp, _Lp>;
01574       if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
01575         return _Sp(__r, __p);
01576       return _Sp();
01577     }
01578 
01579 #if __cplusplus > 201402L
01580   template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
01581     inline __shared_ptr<_Tp, _Lp>
01582     reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
01583     {
01584       using _Sp = __shared_ptr<_Tp, _Lp>;
01585       return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
01586     }
01587 #endif
01588 
01589   template<typename _Tp, _Lock_policy _Lp>
01590     class __weak_ptr
01591     {
01592       template<typename _Yp, typename _Res = void>
01593         using _Compatible = typename
01594           enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
01595 
01596       // Constraint for assignment from shared_ptr and weak_ptr:
01597       template<typename _Yp>
01598         using _Assignable = _Compatible<_Yp, __weak_ptr&>;
01599 
01600     public:
01601       using element_type = typename remove_extent<_Tp>::type;
01602 
01603       constexpr __weak_ptr() noexcept
01604       : _M_ptr(nullptr), _M_refcount()
01605       { }
01606 
01607       __weak_ptr(const __weak_ptr&) noexcept = default;
01608 
01609       ~__weak_ptr() = default;
01610 
01611       // The "obvious" converting constructor implementation:
01612       //
01613       //  template<typename _Tp1>
01614       //    __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
01615       //    : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
01616       //    { }
01617       //
01618       // has a serious problem.
01619       //
01620       //  __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
01621       //  conversion may require access to *__r._M_ptr (virtual inheritance).
01622       //
01623       // It is not possible to avoid spurious access violations since
01624       // in multithreaded programs __r._M_ptr may be invalidated at any point.
01625       template<typename _Yp, typename = _Compatible<_Yp>>
01626         __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
01627         : _M_refcount(__r._M_refcount)
01628         { _M_ptr = __r.lock().get(); }
01629 
01630       template<typename _Yp, typename = _Compatible<_Yp>>
01631         __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
01632         : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
01633         { }
01634 
01635       __weak_ptr(__weak_ptr&& __r) noexcept
01636       : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
01637       { __r._M_ptr = nullptr; }
01638 
01639       template<typename _Yp, typename = _Compatible<_Yp>>
01640         __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
01641         : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
01642         { __r._M_ptr = nullptr; }
01643 
01644       __weak_ptr&
01645       operator=(const __weak_ptr& __r) noexcept = default;
01646 
01647       template<typename _Yp>
01648         _Assignable<_Yp>
01649         operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
01650         {
01651           _M_ptr = __r.lock().get();
01652           _M_refcount = __r._M_refcount;
01653           return *this;
01654         }
01655 
01656       template<typename _Yp>
01657         _Assignable<_Yp>
01658         operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
01659         {
01660           _M_ptr = __r._M_ptr;
01661           _M_refcount = __r._M_refcount;
01662           return *this;
01663         }
01664 
01665       __weak_ptr&
01666       operator=(__weak_ptr&& __r) noexcept
01667       {
01668         _M_ptr = __r._M_ptr;
01669         _M_refcount = std::move(__r._M_refcount);
01670         __r._M_ptr = nullptr;
01671         return *this;
01672       }
01673 
01674       template<typename _Yp>
01675         _Assignable<_Yp>
01676         operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
01677         {
01678           _M_ptr = __r.lock().get();
01679           _M_refcount = std::move(__r._M_refcount);
01680           __r._M_ptr = nullptr;
01681           return *this;
01682         }
01683 
01684       __shared_ptr<_Tp, _Lp>
01685       lock() const noexcept
01686       { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
01687 
01688       long
01689       use_count() const noexcept
01690       { return _M_refcount._M_get_use_count(); }
01691 
01692       bool
01693       expired() const noexcept
01694       { return _M_refcount._M_get_use_count() == 0; }
01695 
01696       template<typename _Tp1>
01697         bool
01698         owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
01699         { return _M_refcount._M_less(__rhs._M_refcount); }
01700 
01701       template<typename _Tp1>
01702         bool
01703         owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
01704         { return _M_refcount._M_less(__rhs._M_refcount); }
01705 
01706       void
01707       reset() noexcept
01708       { __weak_ptr().swap(*this); }
01709 
01710       void
01711       swap(__weak_ptr& __s) noexcept
01712       {
01713         std::swap(_M_ptr, __s._M_ptr);
01714         _M_refcount._M_swap(__s._M_refcount);
01715       }
01716 
01717     private:
01718       // Used by __enable_shared_from_this.
01719       void
01720       _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
01721       {
01722         if (use_count() == 0)
01723           {
01724             _M_ptr = __ptr;
01725             _M_refcount = __refcount;
01726           }
01727       }
01728 
01729       template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
01730       template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
01731       friend class __enable_shared_from_this<_Tp, _Lp>;
01732       friend class enable_shared_from_this<_Tp>;
01733 
01734       element_type*      _M_ptr;         // Contained pointer.
01735       __weak_count<_Lp>  _M_refcount;    // Reference counter.
01736     };
01737 
01738   // 20.7.2.3.6 weak_ptr specialized algorithms.
01739   template<typename _Tp, _Lock_policy _Lp>
01740     inline void
01741     swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
01742     { __a.swap(__b); }
01743 
01744   template<typename _Tp, typename _Tp1>
01745     struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
01746     {
01747       bool
01748       operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
01749       { return __lhs.owner_before(__rhs); }
01750 
01751       bool
01752       operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
01753       { return __lhs.owner_before(__rhs); }
01754 
01755       bool
01756       operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
01757       { return __lhs.owner_before(__rhs); }
01758     };
01759 
01760   template<>
01761     struct _Sp_owner_less<void, void>
01762     {
01763       template<typename _Tp, typename _Up>
01764         auto
01765         operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
01766         -> decltype(__lhs.owner_before(__rhs))
01767         { return __lhs.owner_before(__rhs); }
01768 
01769       using is_transparent = void;
01770     };
01771 
01772   template<typename _Tp, _Lock_policy _Lp>
01773     struct owner_less<__shared_ptr<_Tp, _Lp>>
01774     : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
01775     { };
01776 
01777   template<typename _Tp, _Lock_policy _Lp>
01778     struct owner_less<__weak_ptr<_Tp, _Lp>>
01779     : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
01780     { };
01781 
01782 
01783   template<typename _Tp, _Lock_policy _Lp>
01784     class __enable_shared_from_this
01785     {
01786     protected:
01787       constexpr __enable_shared_from_this() noexcept { }
01788 
01789       __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
01790 
01791       __enable_shared_from_this&
01792       operator=(const __enable_shared_from_this&) noexcept
01793       { return *this; }
01794 
01795       ~__enable_shared_from_this() { }
01796 
01797     public:
01798       __shared_ptr<_Tp, _Lp>
01799       shared_from_this()
01800       { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
01801 
01802       __shared_ptr<const _Tp, _Lp>
01803       shared_from_this() const
01804       { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
01805 
01806 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
01807       __weak_ptr<_Tp, _Lp>
01808       weak_from_this() noexcept
01809       { return this->_M_weak_this; }
01810 
01811       __weak_ptr<const _Tp, _Lp>
01812       weak_from_this() const noexcept
01813       { return this->_M_weak_this; }
01814 #endif
01815 
01816     private:
01817       template<typename _Tp1>
01818         void
01819         _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
01820         { _M_weak_this._M_assign(__p, __n); }
01821 
01822       friend const __enable_shared_from_this*
01823       __enable_shared_from_this_base(const __shared_count<_Lp>&,
01824                                      const __enable_shared_from_this* __p)
01825       { return __p; }
01826 
01827       template<typename, _Lock_policy>
01828         friend class __shared_ptr;
01829 
01830       mutable __weak_ptr<_Tp, _Lp>  _M_weak_this;
01831     };
01832 
01833   template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
01834     inline __shared_ptr<_Tp, _Lp>
01835     __allocate_shared(const _Alloc& __a, _Args&&... __args)
01836     {
01837       return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
01838                                     std::forward<_Args>(__args)...);
01839     }
01840 
01841   template<typename _Tp, _Lock_policy _Lp, typename... _Args>
01842     inline __shared_ptr<_Tp, _Lp>
01843     __make_shared(_Args&&... __args)
01844     {
01845       typedef typename std::remove_const<_Tp>::type _Tp_nc;
01846       return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
01847                                               std::forward<_Args>(__args)...);
01848     }
01849 
01850   /// std::hash specialization for __shared_ptr.
01851   template<typename _Tp, _Lock_policy _Lp>
01852     struct hash<__shared_ptr<_Tp, _Lp>>
01853     : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
01854     {
01855       size_t
01856       operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
01857       {
01858         return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
01859             __s.get());
01860       }
01861     };
01862 
01863 _GLIBCXX_END_NAMESPACE_VERSION
01864 } // namespace
01865 
01866 #endif // _SHARED_PTR_BASE_H