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 return __ti
== typeid(_Sp_make_shared_tag
) ? _M_ptr() : nullptr;
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
) : _M_pi(0)
500 // The allocator's value_type doesn't matter, will rebind it anyway.
501 typedef std::allocator
<int> _Alloc
;
502 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
503 typedef typename allocator_traits
<_Alloc
>::template
504 rebind_traits
<_Sp_cd_type
> _Alloc_traits
;
505 typename
_Alloc_traits::allocator_type __a
;
506 _Sp_cd_type
* __mem
= 0;
509 __mem
= _Alloc_traits::allocate(__a
, 1);
510 _Alloc_traits::construct(__a
, __mem
, __p
, std::move(__d
));
515 __d(__p
); // Call _Deleter on __p.
517 _Alloc_traits::deallocate(__a
, __mem
, 1);
518 __throw_exception_again
;
522 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
523 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
525 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
526 typedef typename allocator_traits
<_Alloc
>::template
527 rebind_traits
<_Sp_cd_type
> _Alloc_traits
;
528 typename
_Alloc_traits::allocator_type
__a2(__a
);
529 _Sp_cd_type
* __mem
= 0;
532 __mem
= _Alloc_traits::allocate(__a2
, 1);
533 _Alloc_traits::construct(__a2
, __mem
,
534 __p
, std::move(__d
), std::move(__a
));
539 __d(__p
); // Call _Deleter on __p.
541 _Alloc_traits::deallocate(__a2
, __mem
, 1);
542 __throw_exception_again
;
546 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
547 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
551 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
552 typedef typename allocator_traits
<_Alloc
>::template
553 rebind_traits
<_Sp_cp_type
> _Alloc_traits
;
554 typename
_Alloc_traits::allocator_type
__a2(__a
);
555 _Sp_cp_type
* __mem
= _Alloc_traits::allocate(__a2
, 1);
558 _Alloc_traits::construct(__a2
, __mem
, std::move(__a
),
559 std::forward
<_Args
>(__args
)...);
564 _Alloc_traits::deallocate(__a2
, __mem
, 1);
565 __throw_exception_again
;
569 #if _GLIBCXX_USE_DEPRECATED
570 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
571 template<typename _Tp
>
573 __shared_count(std::auto_ptr
<_Tp
>&& __r
);
576 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
577 template<typename _Tp
, typename _Del
>
579 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
580 : _M_pi(_S_create_from_up(std::move(__r
)))
583 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
584 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
586 ~__shared_count() noexcept
592 __shared_count(const __shared_count
& __r
) noexcept
596 _M_pi
->_M_add_ref_copy();
600 operator=(const __shared_count
& __r
) noexcept
602 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
606 __tmp
->_M_add_ref_copy();
615 _M_swap(__shared_count
& __r
) noexcept
617 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
623 _M_get_use_count() const noexcept
624 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
627 _M_unique() const noexcept
628 { return this->_M_get_use_count() == 1; }
631 _M_get_deleter(const std::type_info
& __ti
) const noexcept
632 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : nullptr; }
635 _M_less(const __shared_count
& __rhs
) const noexcept
636 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
639 _M_less(const __weak_count
<_Lp
>& __rhs
) const noexcept
640 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
642 // Friend function injected into enclosing namespace and found by ADL
644 operator==(const __shared_count
& __a
, const __shared_count
& __b
) noexcept
645 { return __a
._M_pi
== __b
._M_pi
; }
648 friend class __weak_count
<_Lp
>;
650 template<typename _Tp
, typename _Del
>
651 static _Sp_counted_base
<_Lp
>*
652 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
653 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
655 typedef typename unique_ptr
<_Tp
, _Del
>::pointer _Ptr
;
656 return new _Sp_counted_deleter
<_Ptr
, _Del
, std::allocator
<void>,
657 _Lp
>(__r
.get(), __r
.get_deleter());
660 template<typename _Tp
, typename _Del
>
661 static _Sp_counted_base
<_Lp
>*
662 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
663 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
665 typedef typename unique_ptr
<_Tp
, _Del
>::pointer _Ptr
;
666 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
667 typedef std::reference_wrapper
<_Del1
> _Del2
;
668 return new _Sp_counted_deleter
<_Ptr
, _Del2
, std::allocator
<void>,
669 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
672 _Sp_counted_base
<_Lp
>* _M_pi
;
676 template<_Lock_policy _Lp
>
680 constexpr __weak_count() noexcept
: _M_pi(0)
683 __weak_count(const __shared_count
<_Lp
>& __r
) noexcept
687 _M_pi
->_M_weak_add_ref();
690 __weak_count(const __weak_count
<_Lp
>& __r
) noexcept
694 _M_pi
->_M_weak_add_ref();
697 ~__weak_count() noexcept
700 _M_pi
->_M_weak_release();
704 operator=(const __shared_count
<_Lp
>& __r
) noexcept
706 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
708 __tmp
->_M_weak_add_ref();
710 _M_pi
->_M_weak_release();
716 operator=(const __weak_count
<_Lp
>& __r
) noexcept
718 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
720 __tmp
->_M_weak_add_ref();
722 _M_pi
->_M_weak_release();
728 _M_swap(__weak_count
<_Lp
>& __r
) noexcept
730 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
736 _M_get_use_count() const noexcept
737 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
740 _M_less(const __weak_count
& __rhs
) const noexcept
741 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
744 _M_less(const __shared_count
<_Lp
>& __rhs
) const noexcept
745 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
747 // Friend function injected into enclosing namespace and found by ADL
749 operator==(const __weak_count
& __a
, const __weak_count
& __b
) noexcept
750 { return __a
._M_pi
== __b
._M_pi
; }
753 friend class __shared_count
<_Lp
>;
755 _Sp_counted_base
<_Lp
>* _M_pi
;
758 // Now that __weak_count is defined we can define this constructor:
759 template<_Lock_policy _Lp
>
760 inline __shared_count
<_Lp
>:: __shared_count(const __weak_count
<_Lp
>& __r
)
764 _M_pi
->_M_add_ref_lock();
766 __throw_bad_weak_ptr();
770 // Support for enable_shared_from_this.
772 // Friend of __enable_shared_from_this.
773 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
775 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
776 const __enable_shared_from_this
<_Tp1
,
777 _Lp
>*, const _Tp2
*) noexcept
;
779 // Friend of enable_shared_from_this.
780 template<typename _Tp1
, typename _Tp2
>
782 __enable_shared_from_this_helper(const __shared_count
<>&,
783 const enable_shared_from_this
<_Tp1
>*,
784 const _Tp2
*) noexcept
;
786 template<_Lock_policy _Lp
>
788 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...) noexcept
792 template<typename _Tp
, _Lock_policy _Lp
>
796 typedef _Tp element_type
;
798 constexpr __shared_ptr() noexcept
799 : _M_ptr(0), _M_refcount()
802 template<typename _Tp1
>
803 explicit __shared_ptr(_Tp1
* __p
)
804 : _M_ptr(__p
), _M_refcount(__p
)
806 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
807 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
808 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
811 template<typename _Tp1
, typename _Deleter
>
812 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
813 : _M_ptr(__p
), _M_refcount(__p
, __d
)
815 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
816 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
817 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
820 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
821 __shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
822 : _M_ptr(__p
), _M_refcount(__p
, __d
, std::move(__a
))
824 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
825 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
826 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
829 template<typename _Deleter
>
830 __shared_ptr(nullptr_t __p
, _Deleter __d
)
831 : _M_ptr(0), _M_refcount(__p
, __d
)
834 template<typename _Deleter
, typename _Alloc
>
835 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
836 : _M_ptr(0), _M_refcount(__p
, __d
, std::move(__a
))
839 template<typename _Tp1
>
840 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
) noexcept
841 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
844 __shared_ptr(const __shared_ptr
&) noexcept
= default;
845 __shared_ptr
& operator=(const __shared_ptr
&) noexcept
= default;
846 ~__shared_ptr() = default;
848 template<typename _Tp1
, typename
= typename
849 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
850 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
851 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
854 __shared_ptr(__shared_ptr
&& __r
) noexcept
855 : _M_ptr(__r
._M_ptr
), _M_refcount()
857 _M_refcount
._M_swap(__r
._M_refcount
);
861 template<typename _Tp1
, typename
= typename
862 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
863 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
864 : _M_ptr(__r
._M_ptr
), _M_refcount()
866 _M_refcount
._M_swap(__r
._M_refcount
);
870 template<typename _Tp1
>
871 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
872 : _M_refcount(__r
._M_refcount
) // may throw
874 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
876 // It is now safe to copy __r._M_ptr, as
877 // _M_refcount(__r._M_refcount) did not throw.
881 // If an exception is thrown this constructor has no effect.
882 template<typename _Tp1
, typename _Del
>
883 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
884 : _M_ptr(__r
.get()), _M_refcount()
886 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
887 auto __tmp
= std::__addressof(*__r
.get());
888 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
889 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
892 #if _GLIBCXX_USE_DEPRECATED
893 // Postcondition: use_count() == 1 and __r.get() == 0
894 template<typename _Tp1
>
895 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
);
898 /* TODO: use delegating constructor */
899 constexpr __shared_ptr(nullptr_t
) noexcept
900 : _M_ptr(0), _M_refcount()
903 template<typename _Tp1
>
905 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
908 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
912 #if _GLIBCXX_USE_DEPRECATED
913 template<typename _Tp1
>
915 operator=(std::auto_ptr
<_Tp1
>&& __r
)
917 __shared_ptr(std::move(__r
)).swap(*this);
923 operator=(__shared_ptr
&& __r
) noexcept
925 __shared_ptr(std::move(__r
)).swap(*this);
931 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
933 __shared_ptr(std::move(__r
)).swap(*this);
937 template<typename _Tp1
, typename _Del
>
939 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
941 __shared_ptr(std::move(__r
)).swap(*this);
947 { __shared_ptr().swap(*this); }
949 template<typename _Tp1
>
951 reset(_Tp1
* __p
) // _Tp1 must be complete.
953 // Catch self-reset errors.
954 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
955 __shared_ptr(__p
).swap(*this);
958 template<typename _Tp1
, typename _Deleter
>
960 reset(_Tp1
* __p
, _Deleter __d
)
961 { __shared_ptr(__p
, __d
).swap(*this); }
963 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
965 reset(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
966 { __shared_ptr(__p
, __d
, std::move(__a
)).swap(*this); }
968 // Allow class instantiation when _Tp is [cv-qual] void.
969 typename
std::add_lvalue_reference
<_Tp
>::type
970 operator*() const noexcept
972 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
977 operator->() const noexcept
979 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
987 explicit operator bool() const // never throws
988 { return _M_ptr
== 0 ? false : true; }
991 unique() const noexcept
992 { return _M_refcount
._M_unique(); }
995 use_count() const noexcept
996 { return _M_refcount
._M_get_use_count(); }
999 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) noexcept
1001 std::swap(_M_ptr
, __other
._M_ptr
);
1002 _M_refcount
._M_swap(__other
._M_refcount
);
1005 template<typename _Tp1
>
1007 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
1008 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1010 template<typename _Tp1
>
1012 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
1013 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1017 // This constructor is non-standard, it is used by allocate_shared.
1018 template<typename _Alloc
, typename
... _Args
>
1019 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1021 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
1022 std::forward
<_Args
>(__args
)...)
1024 // _M_ptr needs to point to the newly constructed object.
1025 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1026 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
1027 _M_ptr
= static_cast<_Tp
*>(__p
);
1028 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1031 template<typename _Alloc
>
1034 void operator()(_Tp
* __ptr
)
1036 typedef allocator_traits
<_Alloc
> _Alloc_traits
;
1037 _Alloc_traits::destroy(_M_alloc
, __ptr
);
1038 _Alloc_traits::deallocate(_M_alloc
, __ptr
, 1);
1043 template<typename _Alloc
, typename
... _Args
>
1044 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1046 : _M_ptr(), _M_refcount()
1048 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
1049 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
1050 typedef allocator_traits
<_Alloc2
> __traits
;
1051 _M_ptr
= __traits::allocate(__del
._M_alloc
, 1);
1054 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1055 // 2070. allocate_shared should use allocator_traits<A>::construct
1056 __traits::construct(__del
._M_alloc
, _M_ptr
,
1057 std::forward
<_Args
>(__args
)...);
1061 __traits::deallocate(__del
._M_alloc
, _M_ptr
, 1);
1062 __throw_exception_again
;
1064 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
1065 _M_refcount
._M_swap(__count
);
1066 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1070 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1072 friend __shared_ptr
<_Tp1
, _Lp1
>
1073 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1077 _M_get_deleter(const std::type_info
& __ti
) const noexcept
1078 { return _M_refcount
._M_get_deleter(__ti
); }
1080 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1081 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1083 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1084 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&) noexcept
;
1086 _Tp
* _M_ptr
; // Contained pointer.
1087 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1091 // 20.7.2.2.7 shared_ptr comparisons
1092 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1094 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1095 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1096 { return __a
.get() == __b
.get(); }
1098 template<typename _Tp
, _Lock_policy _Lp
>
1100 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1103 template<typename _Tp
, _Lock_policy _Lp
>
1105 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1108 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1110 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1111 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1112 { return __a
.get() != __b
.get(); }
1114 template<typename _Tp
, _Lock_policy _Lp
>
1116 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1117 { return (bool)__a
; }
1119 template<typename _Tp
, _Lock_policy _Lp
>
1121 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1122 { return (bool)__a
; }
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
1129 typedef typename
std::common_type
<_Tp1
*, _Tp2
*>::type _CT
;
1130 return std::less
<_CT
>()(__a
.get(), __b
.get());
1133 template<typename _Tp
, _Lock_policy _Lp
>
1135 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1136 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1138 template<typename _Tp
, _Lock_policy _Lp
>
1140 operator<(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1141 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1143 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1145 operator<=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1146 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1147 { return !(__b
< __a
); }
1149 template<typename _Tp
, _Lock_policy _Lp
>
1151 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1152 { return !(nullptr < __a
); }
1154 template<typename _Tp
, _Lock_policy _Lp
>
1156 operator<=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1157 { return !(__a
< nullptr); }
1159 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1161 operator>(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1162 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1163 { return (__b
< __a
); }
1165 template<typename _Tp
, _Lock_policy _Lp
>
1167 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1168 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1170 template<typename _Tp
, _Lock_policy _Lp
>
1172 operator>(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1173 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1175 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1177 operator>=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1178 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1179 { return !(__a
< __b
); }
1181 template<typename _Tp
, _Lock_policy _Lp
>
1183 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1184 { return !(__a
< nullptr); }
1186 template<typename _Tp
, _Lock_policy _Lp
>
1188 operator>=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1189 { return !(nullptr < __a
); }
1191 template<typename _Sp
>
1192 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1195 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const noexcept
1197 typedef typename
_Sp::element_type element_type
;
1198 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1202 template<typename _Tp
, _Lock_policy _Lp
>
1203 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1204 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1207 // 20.7.2.2.8 shared_ptr specialized algorithms.
1208 template<typename _Tp
, _Lock_policy _Lp
>
1210 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
) noexcept
1213 // 20.7.2.2.9 shared_ptr casts
1215 // The seemingly equivalent code:
1216 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1217 // will eventually result in undefined behaviour, attempting to
1218 // delete the same object twice.
1219 /// static_pointer_cast
1220 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1221 inline __shared_ptr
<_Tp
, _Lp
>
1222 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1223 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1225 // The seemingly equivalent code:
1226 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1227 // will eventually result in undefined behaviour, attempting to
1228 // delete the same object twice.
1229 /// const_pointer_cast
1230 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1231 inline __shared_ptr
<_Tp
, _Lp
>
1232 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1233 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1235 // The seemingly equivalent code:
1236 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1237 // will eventually result in undefined behaviour, attempting to
1238 // delete the same object twice.
1239 /// dynamic_pointer_cast
1240 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1241 inline __shared_ptr
<_Tp
, _Lp
>
1242 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1244 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1245 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
1246 return __shared_ptr
<_Tp
, _Lp
>();
1250 template<typename _Tp
, _Lock_policy _Lp
>
1254 typedef _Tp element_type
;
1256 constexpr __weak_ptr() noexcept
1257 : _M_ptr(0), _M_refcount()
1260 __weak_ptr(const __weak_ptr
&) noexcept
= default;
1261 __weak_ptr
& operator=(const __weak_ptr
&) noexcept
= default;
1262 ~__weak_ptr() = default;
1264 // The "obvious" converting constructor implementation:
1266 // template<typename _Tp1>
1267 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1268 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1271 // has a serious problem.
1273 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1274 // conversion may require access to *__r._M_ptr (virtual inheritance).
1276 // It is not possible to avoid spurious access violations since
1277 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1278 template<typename _Tp1
, typename
= typename
1279 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1280 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1281 : _M_refcount(__r
._M_refcount
)
1282 { _M_ptr
= __r
.lock().get(); }
1284 template<typename _Tp1
, typename
= typename
1285 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1286 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1287 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1290 template<typename _Tp1
>
1292 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1294 _M_ptr
= __r
.lock().get();
1295 _M_refcount
= __r
._M_refcount
;
1299 template<typename _Tp1
>
1301 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1303 _M_ptr
= __r
._M_ptr
;
1304 _M_refcount
= __r
._M_refcount
;
1308 __shared_ptr
<_Tp
, _Lp
>
1309 lock() const noexcept
1312 // Optimization: avoid throw overhead.
1314 return __shared_ptr
<element_type
, _Lp
>();
1318 return __shared_ptr
<element_type
, _Lp
>(*this);
1320 __catch(const bad_weak_ptr
&)
1322 // Q: How can we get here?
1323 // A: Another thread may have invalidated r after the
1324 // use_count test above.
1325 return __shared_ptr
<element_type
, _Lp
>();
1329 // Optimization: avoid try/catch overhead when single threaded.
1330 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1331 : __shared_ptr
<element_type
, _Lp
>(*this);
1337 use_count() const noexcept
1338 { return _M_refcount
._M_get_use_count(); }
1341 expired() const noexcept
1342 { return _M_refcount
._M_get_use_count() == 0; }
1344 template<typename _Tp1
>
1346 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1347 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1349 template<typename _Tp1
>
1351 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1352 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1356 { __weak_ptr().swap(*this); }
1359 swap(__weak_ptr
& __s
) noexcept
1361 std::swap(_M_ptr
, __s
._M_ptr
);
1362 _M_refcount
._M_swap(__s
._M_refcount
);
1366 // Used by __enable_shared_from_this.
1368 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
) noexcept
1371 _M_refcount
= __refcount
;
1374 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1375 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1376 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1377 friend class enable_shared_from_this
<_Tp
>;
1379 _Tp
* _M_ptr
; // Contained pointer.
1380 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1383 // 20.7.2.3.6 weak_ptr specialized algorithms.
1384 template<typename _Tp
, _Lock_policy _Lp
>
1386 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
) noexcept
1389 template<typename _Tp
, typename _Tp1
>
1390 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1393 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1394 { return __lhs
.owner_before(__rhs
); }
1397 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1398 { return __lhs
.owner_before(__rhs
); }
1401 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1402 { return __lhs
.owner_before(__rhs
); }
1405 template<typename _Tp
, _Lock_policy _Lp
>
1406 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1407 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1410 template<typename _Tp
, _Lock_policy _Lp
>
1411 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1412 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1416 template<typename _Tp
, _Lock_policy _Lp
>
1417 class __enable_shared_from_this
1420 constexpr __enable_shared_from_this() noexcept
{ }
1422 __enable_shared_from_this(const __enable_shared_from_this
&) noexcept
{ }
1424 __enable_shared_from_this
&
1425 operator=(const __enable_shared_from_this
&) noexcept
1428 ~__enable_shared_from_this() { }
1431 __shared_ptr
<_Tp
, _Lp
>
1433 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1435 __shared_ptr
<const _Tp
, _Lp
>
1436 shared_from_this() const
1437 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1440 template<typename _Tp1
>
1442 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const noexcept
1443 { _M_weak_this
._M_assign(__p
, __n
); }
1445 template<typename _Tp1
>
1447 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1448 const __enable_shared_from_this
* __pe
,
1449 const _Tp1
* __px
) noexcept
1452 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1455 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1459 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1460 inline __shared_ptr
<_Tp
, _Lp
>
1461 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1463 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1464 std::forward
<_Args
>(__args
)...);
1467 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1468 inline __shared_ptr
<_Tp
, _Lp
>
1469 __make_shared(_Args
&&... __args
)
1471 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1472 return std::__allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1473 std::forward
<_Args
>(__args
)...);
1476 /// std::hash specialization for __shared_ptr.
1477 template<typename _Tp
, _Lock_policy _Lp
>
1478 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1479 : public __hash_base
<size_t, __shared_ptr
<_Tp
, _Lp
>>
1482 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const noexcept
1483 { return std::hash
<_Tp
*>()(__s
.get()); }
1486 _GLIBCXX_END_NAMESPACE_VERSION
1489 #endif // _SHARED_PTR_BASE_H