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