1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
44 /** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
52 #include <ext/aligned_buffer.h>
54 namespace std
_GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 #if _GLIBCXX_USE_DEPRECATED
59 template<typename
> class auto_ptr
;
63 * @brief Exception possibly thrown by @c shared_ptr.
66 class bad_weak_ptr
: public std::exception
70 what() const noexcept
;
72 virtual ~bad_weak_ptr() noexcept
;
75 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
77 __throw_bad_weak_ptr()
78 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
80 using __gnu_cxx::_Lock_policy
;
81 using __gnu_cxx::__default_lock_policy
;
82 using __gnu_cxx::_S_single
;
83 using __gnu_cxx::_S_mutex
;
84 using __gnu_cxx::_S_atomic
;
86 // Empty helper class except when the template argument is _S_mutex.
87 template<_Lock_policy _Lp
>
91 // The atomic policy uses fully-fenced builtins, single doesn't care.
92 enum { _S_need_barriers
= 0 };
96 class _Mutex_base
<_S_mutex
>
97 : public __gnu_cxx::__mutex
100 // This policy is used when atomic builtins are not available.
101 // The replacement atomic operations might not have the necessary
103 enum { _S_need_barriers
= 1 };
106 template<_Lock_policy _Lp
= __default_lock_policy
>
107 class _Sp_counted_base
108 : public _Mutex_base
<_Lp
>
111 _Sp_counted_base() noexcept
112 : _M_use_count(1), _M_weak_count(1) { }
115 ~_Sp_counted_base() noexcept
118 // Called when _M_use_count drops to zero, to release the resources
121 _M_dispose() noexcept
= 0;
123 // Called when _M_weak_count drops to zero.
125 _M_destroy() noexcept
129 _M_get_deleter(const std::type_info
&) noexcept
= 0;
133 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
139 _M_release() noexcept
141 // Be race-detector-friendly. For more info see bits/c++config.
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
143 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
145 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
147 // There must be a memory barrier between dispose() and destroy()
148 // to ensure that the effects of dispose() are observed in the
149 // thread that runs destroy().
150 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
151 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
153 _GLIBCXX_READ_MEM_BARRIER
;
154 _GLIBCXX_WRITE_MEM_BARRIER
;
157 // Be race-detector-friendly. For more info see bits/c++config.
158 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
159 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
162 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
169 _M_weak_add_ref() noexcept
170 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
173 _M_weak_release() noexcept
175 // Be race-detector-friendly. For more info see bits/c++config.
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
177 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
179 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
180 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
183 // destroy() must observe results of dispose()
184 _GLIBCXX_READ_MEM_BARRIER
;
185 _GLIBCXX_WRITE_MEM_BARRIER
;
192 _M_get_use_count() const noexcept
194 // No memory barrier is used here so there is no synchronization
195 // with other threads.
196 return __atomic_load_n(&_M_use_count
, __ATOMIC_RELAXED
);
200 _Sp_counted_base(_Sp_counted_base
const&) = delete;
201 _Sp_counted_base
& operator=(_Sp_counted_base
const&) = delete;
203 _Atomic_word _M_use_count
; // #shared
204 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
209 _Sp_counted_base
<_S_single
>::
212 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
215 __throw_bad_weak_ptr();
221 _Sp_counted_base
<_S_mutex
>::
224 __gnu_cxx::__scoped_lock
sentry(*this);
225 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
228 __throw_bad_weak_ptr();
234 _Sp_counted_base
<_S_atomic
>::
237 // Perform lock-free add-if-not-zero operation.
238 _Atomic_word __count
= _M_use_count
;
242 __throw_bad_weak_ptr();
243 // Replace the current counter value with the old value + 1, as
244 // long as it's not changed meanwhile.
246 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
247 true, __ATOMIC_ACQ_REL
,
252 // Forward declarations.
253 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
256 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
259 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
260 class __enable_shared_from_this
;
262 template<typename _Tp
>
265 template<typename _Tp
>
268 template<typename _Tp
>
271 template<typename _Tp
>
272 class enable_shared_from_this
;
274 template<_Lock_policy _Lp
= __default_lock_policy
>
277 template<_Lock_policy _Lp
= __default_lock_policy
>
278 class __shared_count
;
281 // Counted ptr with no deleter or allocator support
282 template<typename _Ptr
, _Lock_policy _Lp
>
283 class _Sp_counted_ptr final
: public _Sp_counted_base
<_Lp
>
287 _Sp_counted_ptr(_Ptr __p
) noexcept
291 _M_dispose() noexcept
295 _M_destroy() noexcept
299 _M_get_deleter(const std::type_info
&) noexcept
302 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
303 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
311 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() noexcept
{ }
315 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() noexcept
{ }
319 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() noexcept
{ }
321 template<int _Nm
, typename _Tp
,
322 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
323 struct _Sp_ebo_helper
;
325 /// Specialization using EBO.
326 template<int _Nm
, typename _Tp
>
327 struct _Sp_ebo_helper
<_Nm
, _Tp
, true> : private _Tp
329 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _Tp(__tp
) { }
332 _S_get(_Sp_ebo_helper
& __eboh
) { return static_cast<_Tp
&>(__eboh
); }
335 /// Specialization not using EBO.
336 template<int _Nm
, typename _Tp
>
337 struct _Sp_ebo_helper
<_Nm
, _Tp
, false>
339 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _M_tp(__tp
) { }
342 _S_get(_Sp_ebo_helper
& __eboh
)
343 { return __eboh
._M_tp
; }
349 // Support for custom deleter and/or allocator
350 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
351 class _Sp_counted_deleter final
: public _Sp_counted_base
<_Lp
>
353 class _Impl
: _Sp_ebo_helper
<0, _Deleter
>, _Sp_ebo_helper
<1, _Alloc
>
355 typedef _Sp_ebo_helper
<0, _Deleter
> _Del_base
;
356 typedef _Sp_ebo_helper
<1, _Alloc
> _Alloc_base
;
359 _Impl(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
360 : _M_ptr(__p
), _Del_base(__d
), _Alloc_base(__a
)
363 _Deleter
& _M_del() noexcept
{ return _Del_base::_S_get(*this); }
364 _Alloc
& _M_alloc() noexcept
{ return _Alloc_base::_S_get(*this); }
370 // __d(__p) must not throw.
371 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
) noexcept
372 : _M_impl(__p
, __d
, _Alloc()) { }
374 // __d(__p) must not throw.
375 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
376 : _M_impl(__p
, __d
, __a
) { }
378 ~_Sp_counted_deleter() noexcept
{ }
381 _M_dispose() noexcept
382 { _M_impl
._M_del()(_M_impl
._M_ptr
); }
385 _M_destroy() noexcept
387 typedef typename allocator_traits
<_Alloc
>::template
388 rebind_traits
<_Sp_counted_deleter
> _Alloc_traits
;
389 typename
_Alloc_traits::allocator_type
__a(_M_impl
._M_alloc());
390 _Alloc_traits::destroy(__a
, this);
391 _Alloc_traits::deallocate(__a
, this, 1);
395 _M_get_deleter(const std::type_info
& __ti
) noexcept
398 return __ti
== typeid(_Deleter
) ? &_M_impl
._M_del() : nullptr;
408 // helpers for make_shared / allocate_shared
410 struct _Sp_make_shared_tag
{ };
412 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
413 class _Sp_counted_ptr_inplace final
: public _Sp_counted_base
<_Lp
>
415 class _Impl
: _Sp_ebo_helper
<0, _Alloc
>
417 typedef _Sp_ebo_helper
<0, _Alloc
> _A_base
;
420 explicit _Impl(_Alloc __a
) noexcept
: _A_base(__a
) { }
422 _Alloc
& _M_alloc() noexcept
{ return _A_base::_S_get(*this); }
424 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
428 template<typename
... _Args
>
429 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
432 // _GLIBCXX_RESOLVE_LIB_DEFECTS
433 // 2070. allocate_shared should use allocator_traits<A>::construct
434 allocator_traits
<_Alloc
>::construct(__a
, _M_ptr(),
435 std::forward
<_Args
>(__args
)...); // might throw
438 ~_Sp_counted_ptr_inplace() noexcept
{ }
441 _M_dispose() noexcept
443 allocator_traits
<_Alloc
>::destroy(_M_impl
._M_alloc(), _M_ptr());
446 // Override because the allocator needs to know the dynamic type
448 _M_destroy() noexcept
450 typedef typename allocator_traits
<_Alloc
>::template
451 rebind_traits
<_Sp_counted_ptr_inplace
> _Alloc_traits
;
452 typename
_Alloc_traits::allocator_type
__a(_M_impl
._M_alloc());
453 _Alloc_traits::destroy(__a
, this);
454 _Alloc_traits::deallocate(__a
, this, 1);
457 // Sneaky trick so __shared_ptr can get the managed pointer
459 _M_get_deleter(const std::type_info
& __ti
) noexcept
462 if (__ti
== typeid(_Sp_make_shared_tag
))
463 return const_cast<typename remove_cv
<_Tp
>::type
*>(_M_ptr());
469 _Tp
* _M_ptr() noexcept
{ return _M_impl
._M_storage
._M_ptr(); }
475 template<_Lock_policy _Lp
>
479 constexpr __shared_count() noexcept
: _M_pi(0)
482 template<typename _Ptr
>
484 __shared_count(_Ptr __p
) : _M_pi(0)
488 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
493 __throw_exception_again
;
497 template<typename _Ptr
, typename _Deleter
>
498 __shared_count(_Ptr __p
, _Deleter __d
)
499 : __shared_count(__p
, std::move(__d
), allocator
<void>())
502 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
503 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
505 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
506 typedef typename allocator_traits
<_Alloc
>::template
507 rebind_traits
<_Sp_cd_type
> _Alloc_traits
;
508 typename
_Alloc_traits::allocator_type
__a2(__a
);
509 _Sp_cd_type
* __mem
= 0;
512 __mem
= _Alloc_traits::allocate(__a2
, 1);
513 _Alloc_traits::construct(__a2
, __mem
,
514 __p
, std::move(__d
), std::move(__a
));
519 __d(__p
); // Call _Deleter on __p.
521 _Alloc_traits::deallocate(__a2
, __mem
, 1);
522 __throw_exception_again
;
526 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
527 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
531 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
532 typedef typename allocator_traits
<_Alloc
>::template
533 rebind_traits
<_Sp_cp_type
> _Alloc_traits
;
534 typename
_Alloc_traits::allocator_type
__a2(__a
);
535 _Sp_cp_type
* __mem
= _Alloc_traits::allocate(__a2
, 1);
538 _Alloc_traits::construct(__a2
, __mem
, std::move(__a
),
539 std::forward
<_Args
>(__args
)...);
544 _Alloc_traits::deallocate(__a2
, __mem
, 1);
545 __throw_exception_again
;
549 #if _GLIBCXX_USE_DEPRECATED
550 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
551 template<typename _Tp
>
553 __shared_count(std::auto_ptr
<_Tp
>&& __r
);
556 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
557 template<typename _Tp
, typename _Del
>
559 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
) : _M_pi(0)
561 using _Ptr
= typename unique_ptr
<_Tp
, _Del
>::pointer
;
562 using _Del2
= typename conditional
<is_reference
<_Del
>::value
,
563 reference_wrapper
<typename remove_reference
<_Del
>::type
>,
566 = _Sp_counted_deleter
<_Ptr
, _Del2
, allocator
<void>, _Lp
>;
567 using _Alloc
= allocator
<_Sp_cd_type
>;
568 using _Alloc_traits
= allocator_traits
<_Alloc
>;
570 _Sp_cd_type
* __mem
= _Alloc_traits::allocate(__a
, 1);
571 _Alloc_traits::construct(__a
, __mem
, __r
.release(),
572 __r
.get_deleter()); // non-throwing
576 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
577 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
579 ~__shared_count() noexcept
581 if (_M_pi
!= nullptr)
585 __shared_count(const __shared_count
& __r
) noexcept
589 _M_pi
->_M_add_ref_copy();
593 operator=(const __shared_count
& __r
) noexcept
595 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
599 __tmp
->_M_add_ref_copy();
608 _M_swap(__shared_count
& __r
) noexcept
610 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
616 _M_get_use_count() const noexcept
617 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
620 _M_unique() const noexcept
621 { return this->_M_get_use_count() == 1; }
624 _M_get_deleter(const std::type_info
& __ti
) const noexcept
625 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : nullptr; }
628 _M_less(const __shared_count
& __rhs
) const noexcept
629 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
632 _M_less(const __weak_count
<_Lp
>& __rhs
) const noexcept
633 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
635 // Friend function injected into enclosing namespace and found by ADL
637 operator==(const __shared_count
& __a
, const __shared_count
& __b
) noexcept
638 { return __a
._M_pi
== __b
._M_pi
; }
641 friend class __weak_count
<_Lp
>;
643 _Sp_counted_base
<_Lp
>* _M_pi
;
647 template<_Lock_policy _Lp
>
651 constexpr __weak_count() noexcept
: _M_pi(0)
654 __weak_count(const __shared_count
<_Lp
>& __r
) noexcept
658 _M_pi
->_M_weak_add_ref();
661 __weak_count(const __weak_count
<_Lp
>& __r
) noexcept
665 _M_pi
->_M_weak_add_ref();
668 ~__weak_count() noexcept
671 _M_pi
->_M_weak_release();
675 operator=(const __shared_count
<_Lp
>& __r
) noexcept
677 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
679 __tmp
->_M_weak_add_ref();
681 _M_pi
->_M_weak_release();
687 operator=(const __weak_count
<_Lp
>& __r
) noexcept
689 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
691 __tmp
->_M_weak_add_ref();
693 _M_pi
->_M_weak_release();
699 _M_swap(__weak_count
<_Lp
>& __r
) noexcept
701 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
707 _M_get_use_count() const noexcept
708 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
711 _M_less(const __weak_count
& __rhs
) const noexcept
712 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
715 _M_less(const __shared_count
<_Lp
>& __rhs
) const noexcept
716 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
718 // Friend function injected into enclosing namespace and found by ADL
720 operator==(const __weak_count
& __a
, const __weak_count
& __b
) noexcept
721 { return __a
._M_pi
== __b
._M_pi
; }
724 friend class __shared_count
<_Lp
>;
726 _Sp_counted_base
<_Lp
>* _M_pi
;
729 // Now that __weak_count is defined we can define this constructor:
730 template<_Lock_policy _Lp
>
731 inline __shared_count
<_Lp
>:: __shared_count(const __weak_count
<_Lp
>& __r
)
735 _M_pi
->_M_add_ref_lock();
737 __throw_bad_weak_ptr();
741 // Support for enable_shared_from_this.
743 // Friend of __enable_shared_from_this.
744 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
746 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
747 const __enable_shared_from_this
<_Tp1
,
748 _Lp
>*, const _Tp2
*) noexcept
;
750 // Friend of enable_shared_from_this.
751 template<typename _Tp1
, typename _Tp2
>
753 __enable_shared_from_this_helper(const __shared_count
<>&,
754 const enable_shared_from_this
<_Tp1
>*,
755 const _Tp2
*) noexcept
;
757 template<_Lock_policy _Lp
>
759 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...) noexcept
763 template<typename _Tp
, _Lock_policy _Lp
>
767 typedef _Tp element_type
;
769 constexpr __shared_ptr() noexcept
770 : _M_ptr(0), _M_refcount()
773 template<typename _Tp1
>
774 explicit __shared_ptr(_Tp1
* __p
)
775 : _M_ptr(__p
), _M_refcount(__p
)
777 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
778 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
779 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
782 template<typename _Tp1
, typename _Deleter
>
783 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
784 : _M_ptr(__p
), _M_refcount(__p
, __d
)
786 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
787 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
788 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
791 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
792 __shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
793 : _M_ptr(__p
), _M_refcount(__p
, __d
, std::move(__a
))
795 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
796 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
797 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
800 template<typename _Deleter
>
801 __shared_ptr(nullptr_t __p
, _Deleter __d
)
802 : _M_ptr(0), _M_refcount(__p
, __d
)
805 template<typename _Deleter
, typename _Alloc
>
806 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
807 : _M_ptr(0), _M_refcount(__p
, __d
, std::move(__a
))
810 template<typename _Tp1
>
811 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
) noexcept
812 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
815 __shared_ptr(const __shared_ptr
&) noexcept
= default;
816 __shared_ptr
& operator=(const __shared_ptr
&) noexcept
= default;
817 ~__shared_ptr() = default;
819 template<typename _Tp1
, typename
= typename
820 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
821 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
822 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
825 __shared_ptr(__shared_ptr
&& __r
) noexcept
826 : _M_ptr(__r
._M_ptr
), _M_refcount()
828 _M_refcount
._M_swap(__r
._M_refcount
);
832 template<typename _Tp1
, typename
= typename
833 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
834 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
835 : _M_ptr(__r
._M_ptr
), _M_refcount()
837 _M_refcount
._M_swap(__r
._M_refcount
);
841 template<typename _Tp1
>
842 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
843 : _M_refcount(__r
._M_refcount
) // may throw
845 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
847 // It is now safe to copy __r._M_ptr, as
848 // _M_refcount(__r._M_refcount) did not throw.
852 // If an exception is thrown this constructor has no effect.
853 template<typename _Tp1
, typename _Del
>
854 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
855 : _M_ptr(__r
.get()), _M_refcount()
857 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
858 auto __raw
= _S_raw_ptr(__r
.get());
859 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
860 __enable_shared_from_this_helper(_M_refcount
, __raw
, __raw
);
863 #if _GLIBCXX_USE_DEPRECATED
864 // Postcondition: use_count() == 1 and __r.get() == 0
865 template<typename _Tp1
>
866 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
);
869 /* TODO: use delegating constructor */
870 constexpr __shared_ptr(nullptr_t
) noexcept
871 : _M_ptr(0), _M_refcount()
874 template<typename _Tp1
>
876 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
879 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
883 #if _GLIBCXX_USE_DEPRECATED
884 template<typename _Tp1
>
886 operator=(std::auto_ptr
<_Tp1
>&& __r
)
888 __shared_ptr(std::move(__r
)).swap(*this);
894 operator=(__shared_ptr
&& __r
) noexcept
896 __shared_ptr(std::move(__r
)).swap(*this);
902 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
904 __shared_ptr(std::move(__r
)).swap(*this);
908 template<typename _Tp1
, typename _Del
>
910 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
912 __shared_ptr(std::move(__r
)).swap(*this);
918 { __shared_ptr().swap(*this); }
920 template<typename _Tp1
>
922 reset(_Tp1
* __p
) // _Tp1 must be complete.
924 // Catch self-reset errors.
925 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
926 __shared_ptr(__p
).swap(*this);
929 template<typename _Tp1
, typename _Deleter
>
931 reset(_Tp1
* __p
, _Deleter __d
)
932 { __shared_ptr(__p
, __d
).swap(*this); }
934 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
936 reset(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
937 { __shared_ptr(__p
, __d
, std::move(__a
)).swap(*this); }
939 // Allow class instantiation when _Tp is [cv-qual] void.
940 typename
std::add_lvalue_reference
<_Tp
>::type
941 operator*() const noexcept
943 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
948 operator->() const noexcept
950 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
958 explicit operator bool() const // never throws
959 { return _M_ptr
== 0 ? false : true; }
962 unique() const noexcept
963 { return _M_refcount
._M_unique(); }
966 use_count() const noexcept
967 { return _M_refcount
._M_get_use_count(); }
970 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) noexcept
972 std::swap(_M_ptr
, __other
._M_ptr
);
973 _M_refcount
._M_swap(__other
._M_refcount
);
976 template<typename _Tp1
>
978 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
979 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
981 template<typename _Tp1
>
983 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
984 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
988 // This constructor is non-standard, it is used by allocate_shared.
989 template<typename _Alloc
, typename
... _Args
>
990 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
992 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
993 std::forward
<_Args
>(__args
)...)
995 // _M_ptr needs to point to the newly constructed object.
996 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
997 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
998 _M_ptr
= static_cast<_Tp
*>(__p
);
999 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1002 template<typename _Alloc
>
1005 void operator()(_Tp
* __ptr
)
1007 typedef allocator_traits
<_Alloc
> _Alloc_traits
;
1008 _Alloc_traits::destroy(_M_alloc
, __ptr
);
1009 _Alloc_traits::deallocate(_M_alloc
, __ptr
, 1);
1014 template<typename _Alloc
, typename
... _Args
>
1015 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1017 : _M_ptr(), _M_refcount()
1019 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
1020 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
1021 typedef allocator_traits
<_Alloc2
> __traits
;
1022 _M_ptr
= __traits::allocate(__del
._M_alloc
, 1);
1025 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1026 // 2070. allocate_shared should use allocator_traits<A>::construct
1027 __traits::construct(__del
._M_alloc
, _M_ptr
,
1028 std::forward
<_Args
>(__args
)...);
1032 __traits::deallocate(__del
._M_alloc
, _M_ptr
, 1);
1033 __throw_exception_again
;
1035 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
1036 _M_refcount
._M_swap(__count
);
1037 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1041 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1043 friend __shared_ptr
<_Tp1
, _Lp1
>
1044 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1048 _M_get_deleter(const std::type_info
& __ti
) const noexcept
1049 { return _M_refcount
._M_get_deleter(__ti
); }
1051 template<typename _Tp1
>
1053 _S_raw_ptr(_Tp1
* __ptr
)
1056 template<typename _Tp1
>
1058 _S_raw_ptr(_Tp1 __ptr
) -> decltype(std::__addressof(*__ptr
))
1059 { return std::__addressof(*__ptr
); }
1061 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1062 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1064 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1065 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&) noexcept
;
1067 _Tp
* _M_ptr
; // Contained pointer.
1068 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1072 // 20.7.2.2.7 shared_ptr comparisons
1073 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1075 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1076 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1077 { return __a
.get() == __b
.get(); }
1079 template<typename _Tp
, _Lock_policy _Lp
>
1081 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1084 template<typename _Tp
, _Lock_policy _Lp
>
1086 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1089 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1091 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1092 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1093 { return __a
.get() != __b
.get(); }
1095 template<typename _Tp
, _Lock_policy _Lp
>
1097 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1098 { return (bool)__a
; }
1100 template<typename _Tp
, _Lock_policy _Lp
>
1102 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1103 { return (bool)__a
; }
1105 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1107 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1108 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1110 typedef typename
std::common_type
<_Tp1
*, _Tp2
*>::type _CT
;
1111 return std::less
<_CT
>()(__a
.get(), __b
.get());
1114 template<typename _Tp
, _Lock_policy _Lp
>
1116 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1117 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1119 template<typename _Tp
, _Lock_policy _Lp
>
1121 operator<(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1122 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1124 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1126 operator<=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1127 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1128 { return !(__b
< __a
); }
1130 template<typename _Tp
, _Lock_policy _Lp
>
1132 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1133 { return !(nullptr < __a
); }
1135 template<typename _Tp
, _Lock_policy _Lp
>
1137 operator<=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1138 { return !(__a
< nullptr); }
1140 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1142 operator>(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1143 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1144 { return (__b
< __a
); }
1146 template<typename _Tp
, _Lock_policy _Lp
>
1148 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1149 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1151 template<typename _Tp
, _Lock_policy _Lp
>
1153 operator>(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1154 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1156 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1158 operator>=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1159 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1160 { return !(__a
< __b
); }
1162 template<typename _Tp
, _Lock_policy _Lp
>
1164 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1165 { return !(__a
< nullptr); }
1167 template<typename _Tp
, _Lock_policy _Lp
>
1169 operator>=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1170 { return !(nullptr < __a
); }
1172 template<typename _Sp
>
1173 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1176 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const noexcept
1178 typedef typename
_Sp::element_type element_type
;
1179 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1183 template<typename _Tp
, _Lock_policy _Lp
>
1184 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1185 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1188 // 20.7.2.2.8 shared_ptr specialized algorithms.
1189 template<typename _Tp
, _Lock_policy _Lp
>
1191 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
) noexcept
1194 // 20.7.2.2.9 shared_ptr casts
1196 // The seemingly equivalent code:
1197 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1198 // will eventually result in undefined behaviour, attempting to
1199 // delete the same object twice.
1200 /// static_pointer_cast
1201 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1202 inline __shared_ptr
<_Tp
, _Lp
>
1203 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1204 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1206 // The seemingly equivalent code:
1207 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1208 // will eventually result in undefined behaviour, attempting to
1209 // delete the same object twice.
1210 /// const_pointer_cast
1211 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1212 inline __shared_ptr
<_Tp
, _Lp
>
1213 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1214 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1216 // The seemingly equivalent code:
1217 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1218 // will eventually result in undefined behaviour, attempting to
1219 // delete the same object twice.
1220 /// dynamic_pointer_cast
1221 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1222 inline __shared_ptr
<_Tp
, _Lp
>
1223 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1225 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1226 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
1227 return __shared_ptr
<_Tp
, _Lp
>();
1231 template<typename _Tp
, _Lock_policy _Lp
>
1235 typedef _Tp element_type
;
1237 constexpr __weak_ptr() noexcept
1238 : _M_ptr(0), _M_refcount()
1241 __weak_ptr(const __weak_ptr
&) noexcept
= default;
1242 __weak_ptr
& operator=(const __weak_ptr
&) noexcept
= default;
1243 ~__weak_ptr() = default;
1245 // The "obvious" converting constructor implementation:
1247 // template<typename _Tp1>
1248 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1249 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1252 // has a serious problem.
1254 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1255 // conversion may require access to *__r._M_ptr (virtual inheritance).
1257 // It is not possible to avoid spurious access violations since
1258 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1259 template<typename _Tp1
, typename
= typename
1260 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1261 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1262 : _M_refcount(__r
._M_refcount
)
1263 { _M_ptr
= __r
.lock().get(); }
1265 template<typename _Tp1
, typename
= typename
1266 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1267 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1268 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1271 template<typename _Tp1
>
1273 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1275 _M_ptr
= __r
.lock().get();
1276 _M_refcount
= __r
._M_refcount
;
1280 template<typename _Tp1
>
1282 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1284 _M_ptr
= __r
._M_ptr
;
1285 _M_refcount
= __r
._M_refcount
;
1289 __shared_ptr
<_Tp
, _Lp
>
1290 lock() const noexcept
1293 // Optimization: avoid throw overhead.
1295 return __shared_ptr
<element_type
, _Lp
>();
1299 return __shared_ptr
<element_type
, _Lp
>(*this);
1301 __catch(const bad_weak_ptr
&)
1303 // Q: How can we get here?
1304 // A: Another thread may have invalidated r after the
1305 // use_count test above.
1306 return __shared_ptr
<element_type
, _Lp
>();
1310 // Optimization: avoid try/catch overhead when single threaded.
1311 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1312 : __shared_ptr
<element_type
, _Lp
>(*this);
1318 use_count() const noexcept
1319 { return _M_refcount
._M_get_use_count(); }
1322 expired() const noexcept
1323 { return _M_refcount
._M_get_use_count() == 0; }
1325 template<typename _Tp1
>
1327 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1328 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1330 template<typename _Tp1
>
1332 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1333 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1337 { __weak_ptr().swap(*this); }
1340 swap(__weak_ptr
& __s
) noexcept
1342 std::swap(_M_ptr
, __s
._M_ptr
);
1343 _M_refcount
._M_swap(__s
._M_refcount
);
1347 // Used by __enable_shared_from_this.
1349 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
) noexcept
1352 _M_refcount
= __refcount
;
1355 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1356 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1357 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1358 friend class enable_shared_from_this
<_Tp
>;
1360 _Tp
* _M_ptr
; // Contained pointer.
1361 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1364 // 20.7.2.3.6 weak_ptr specialized algorithms.
1365 template<typename _Tp
, _Lock_policy _Lp
>
1367 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
) noexcept
1370 template<typename _Tp
, typename _Tp1
>
1371 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1374 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1375 { return __lhs
.owner_before(__rhs
); }
1378 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1379 { return __lhs
.owner_before(__rhs
); }
1382 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1383 { return __lhs
.owner_before(__rhs
); }
1386 template<typename _Tp
, _Lock_policy _Lp
>
1387 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1388 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1391 template<typename _Tp
, _Lock_policy _Lp
>
1392 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1393 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1397 template<typename _Tp
, _Lock_policy _Lp
>
1398 class __enable_shared_from_this
1401 constexpr __enable_shared_from_this() noexcept
{ }
1403 __enable_shared_from_this(const __enable_shared_from_this
&) noexcept
{ }
1405 __enable_shared_from_this
&
1406 operator=(const __enable_shared_from_this
&) noexcept
1409 ~__enable_shared_from_this() { }
1412 __shared_ptr
<_Tp
, _Lp
>
1414 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1416 __shared_ptr
<const _Tp
, _Lp
>
1417 shared_from_this() const
1418 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1421 template<typename _Tp1
>
1423 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const noexcept
1424 { _M_weak_this
._M_assign(__p
, __n
); }
1426 template<typename _Tp1
>
1428 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1429 const __enable_shared_from_this
* __pe
,
1430 const _Tp1
* __px
) noexcept
1433 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1436 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1440 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1441 inline __shared_ptr
<_Tp
, _Lp
>
1442 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1444 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1445 std::forward
<_Args
>(__args
)...);
1448 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1449 inline __shared_ptr
<_Tp
, _Lp
>
1450 __make_shared(_Args
&&... __args
)
1452 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1453 return std::__allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1454 std::forward
<_Args
>(__args
)...);
1457 /// std::hash specialization for __shared_ptr.
1458 template<typename _Tp
, _Lock_policy _Lp
>
1459 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1460 : public __hash_base
<size_t, __shared_ptr
<_Tp
, _Lp
>>
1463 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const noexcept
1464 { return std::hash
<_Tp
*>()(__s
.get()); }
1467 _GLIBCXX_END_NAMESPACE_VERSION
1470 #endif // _SHARED_PTR_BASE_H