32#define _UNIQUE_PTR_H 1
40#if __cplusplus >= 202002L
47namespace std _GLIBCXX_VISIBILITY(default)
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
56#if _GLIBCXX_USE_DEPRECATED
57#pragma GCC diagnostic push
58#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
60#pragma GCC diagnostic pop
68 template<
typename _Tp>
79 template<typename _Up,
80 typename = _Require<is_convertible<_Up*, _Tp*>>>
90 "can't delete pointer to incomplete type");
91 static_assert(
sizeof(_Tp)>0,
92 "can't delete pointer to incomplete type");
105 template<
typename _Tp>
121 template<typename _Up,
122 typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>>
127 template<
typename _Up>
132 static_assert(
sizeof(_Tp)>0,
133 "can't delete pointer to incomplete type");
141 template <
typename _Tp,
typename _Dp>
142 class __uniq_ptr_impl
144 template <
typename _Up,
typename _Ep,
typename =
void>
150 template <
typename _Up,
typename _Ep>
152 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
154 using type =
typename remove_reference<_Ep>::type::pointer;
158 using _DeleterConstraint = enable_if<
159 __and_<__not_<is_pointer<_Dp>>,
160 is_default_constructible<_Dp>>::value>;
162 using pointer =
typename _Ptr<_Tp, _Dp>::type;
164 static_assert( !is_rvalue_reference<_Dp>::value,
165 "unique_ptr's deleter type must be a function object type"
166 " or an lvalue reference type" );
168 __uniq_ptr_impl() =
default;
170 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
172 template<
typename _Del>
174 __uniq_ptr_impl(pointer __p, _Del&& __d)
175 : _M_t(__p, std::
forward<_Del>(__d)) { }
178 __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
180 { __u._M_ptr() =
nullptr; }
183 __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u)
noexcept
185 reset(__u.release());
191 pointer& _M_ptr() noexcept {
return std::get<0>(_M_t); }
193 pointer _M_ptr() const noexcept {
return std::get<0>(_M_t); }
195 _Dp& _M_deleter() noexcept {
return std::get<1>(_M_t); }
197 const _Dp& _M_deleter() const noexcept {
return std::get<1>(_M_t); }
200 void reset(pointer __p)
noexcept
202 const pointer __old_p = _M_ptr();
205 _M_deleter()(__old_p);
209 pointer release() noexcept
211 pointer __p = _M_ptr();
218 swap(__uniq_ptr_impl& __rhs)
noexcept
221 swap(this->_M_ptr(), __rhs._M_ptr());
222 swap(this->_M_deleter(), __rhs._M_deleter());
226 tuple<pointer, _Dp> _M_t;
230 template <
typename _Tp,
typename _Dp,
233 struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
235 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
236 __uniq_ptr_data(__uniq_ptr_data&&) =
default;
237 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
default;
240 template <
typename _Tp,
typename _Dp>
241 struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
243 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
244 __uniq_ptr_data(__uniq_ptr_data&&) =
default;
245 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
delete;
248 template <
typename _Tp,
typename _Dp>
249 struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
251 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
252 __uniq_ptr_data(__uniq_ptr_data&&) =
delete;
253 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
default;
256 template <
typename _Tp,
typename _Dp>
257 struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
259 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
260 __uniq_ptr_data(__uniq_ptr_data&&) =
delete;
261 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
delete;
270 template <
typename _Tp,
typename _Dp = default_delete<_Tp>>
273 template <
typename _Up>
274 using _DeleterConstraint =
275 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
277 __uniq_ptr_data<_Tp, _Dp> _M_t;
280 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
281 using element_type = _Tp;
282 using deleter_type = _Dp;
287 template<
typename _Up,
typename _Ep>
288 using __safe_conversion_up = __and_<
289 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
290 __not_<is_array<_Up>>
297 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
308 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
322 template<
typename _Del = deleter_type,
323 typename = _Require<is_copy_constructible<_Del>>>
335 template<
typename _Del = deleter_type,
336 typename = _Require<is_move_constructible<_Del>>>
340 _Del&&> __d) noexcept
344 template<
typename _Del = deleter_type,
345 typename _DelUnref =
typename remove_reference<_Del>::type>
349 _DelUnref&&>) =
delete;
352 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
368 template<
typename _Up,
typename _Ep,
typename = _Require<
369 __safe_conversion_up<_Up, _Ep>,
370 __conditional_t<is_reference<_Dp>::value,
372 is_convertible<_Ep, _Dp>>>>
378#if _GLIBCXX_USE_DEPRECATED
379#pragma GCC diagnostic push
380#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
382 template<
typename _Up,
383 typename = _Require<is_convertible<_Up*, pointer>,
386#pragma GCC diagnostic pop
390#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
395 static_assert(__is_invocable<deleter_type&, pointer>::value,
396 "unique_ptr's deleter must be invocable with a pointer");
397 auto& __ptr = _M_t._M_ptr();
398 if (__ptr !=
nullptr)
418 template<
typename _Up,
typename _Ep>
421 __safe_conversion_up<_Up, _Ep>,
427 reset(__u.release());
445 typename add_lvalue_reference<element_type>::type
448#if _GLIBCXX_USE_BUILTIN_TRAIT(__reference_converts_from_temporary)
451 using _ResT =
typename add_lvalue_reference<element_type>::type;
452 using _DerefT =
decltype(*
get());
453 static_assert(!__reference_converts_from_temporary(_ResT, _DerefT),
454 "operator* must not return a dangling reference");
456 __glibcxx_assert(
get() != pointer());
465 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
473 {
return _M_t._M_ptr(); }
479 {
return _M_t._M_deleter(); }
485 {
return _M_t._M_deleter(); }
489 explicit operator bool() const noexcept
490 {
return get() == pointer() ? false :
true; }
498 {
return _M_t.release(); }
508 reset(pointer __p = pointer()) noexcept
510 static_assert(__is_invocable<deleter_type&, pointer>::value,
511 "unique_ptr's deleter must be invocable with a pointer");
520 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
529#ifdef __glibcxx_out_ptr
530 template<
typename,
typename,
typename...>
531 friend class out_ptr_t;
532 template<
typename,
typename,
typename...>
533 friend class inout_ptr_t;
545 template<
typename _Tp,
typename _Dp>
548 template <
typename _Up>
549 using _DeleterConstraint =
550 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
552 __uniq_ptr_data<_Tp, _Dp> _M_t;
555 template<
typename _Up>
556 using __is_derived_Tp
557 = __and_< is_base_of<_Tp, _Up>,
558 __not_<is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up>>> >;
561 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
562 using element_type = _Tp;
563 using deleter_type = _Dp;
567 template<
typename _Up,
typename _Ep,
569 typename _UP_pointer =
typename _UPtr::pointer,
570 typename _UP_element_type =
typename _UPtr::element_type>
571 using __safe_conversion_up = __and_<
575 is_convertible<_UP_element_type(*)[], element_type(*)[]>
579 template<
typename _Up>
580 using __safe_conversion_raw = __and_<
581 __or_<__or_<is_same<_Up, pointer>,
583 __and_<is_pointer<_Up>,
586 typename remove_pointer<_Up>::type(*)[],
595 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
607 template<
typename _Up,
609 typename = _DeleterConstraint<_Vp>,
611 __safe_conversion_raw<_Up>::value,
bool>
::type>
626 template<
typename _Up,
typename _Del = deleter_type,
627 typename = _Require<__safe_conversion_raw<_Up>,
630 unique_ptr(_Up __p,
const deleter_type& __d) noexcept
641 template<
typename _Up,
typename _Del = deleter_type,
642 typename = _Require<__safe_conversion_raw<_Up>,
647 _Del&&> __d) noexcept
651 template<
typename _Up,
typename _Del = deleter_type,
652 typename _DelUnref =
typename remove_reference<_Del>::type,
653 typename = _Require<__safe_conversion_raw<_Up>>>
656 _DelUnref&&>) =
delete;
662 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
667 template<
typename _Up,
typename _Ep,
typename = _Require<
668 __safe_conversion_up<_Up, _Ep>,
669 __conditional_t<is_reference<_Dp>::value,
671 is_convertible<_Ep, _Dp>>>>
678#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
683 auto& __ptr = _M_t._M_ptr();
684 if (__ptr !=
nullptr)
705 template<
typename _Up,
typename _Ep>
714 reset(__u.release());
732 typename std::add_lvalue_reference<element_type>::type
735 __glibcxx_assert(
get() != pointer());
743 {
return _M_t._M_ptr(); }
749 {
return _M_t._M_deleter(); }
755 {
return _M_t._M_deleter(); }
759 explicit operator bool() const noexcept
760 {
return get() == pointer() ? false :
true; }
768 {
return _M_t.release(); }
776 template <
typename _Up,
778 __or_<is_same<_Up, pointer>,
779 __and_<is_same<pointer, element_type*>,
782 typename remove_pointer<_Up>::type(*)[],
790 reset(_Up __p)
noexcept
794 void reset(nullptr_t =
nullptr) noexcept
795 { reset(pointer()); }
802 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
811#ifdef __glibcxx_out_ptr
812 template<
typename,
typename,
typename...>
friend class out_ptr_t;
813 template<
typename,
typename,
typename...>
friend class inout_ptr_t;
821 template<
typename _Tp,
typename _Dp>
823#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
826 typename enable_if<__is_swappable<_Dp>::value>::type
834#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
837 template<
typename _Tp,
typename _Dp>
844 template<
typename _Tp,
typename _Dp,
845 typename _Up,
typename _Ep>
846 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
850 {
return __x.
get() == __y.
get(); }
853 template<
typename _Tp,
typename _Dp>
854 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
859#ifndef __cpp_lib_three_way_comparison
861 template<
typename _Tp,
typename _Dp>
864 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
868 template<
typename _Tp,
typename _Dp,
869 typename _Up,
typename _Ep>
874 {
return __x.get() != __y.get(); }
877 template<
typename _Tp,
typename _Dp>
881 {
return (
bool)__x; }
884 template<
typename _Tp,
typename _Dp>
888 {
return (
bool)__x; }
892 template<
typename _Tp,
typename _Dp,
893 typename _Up,
typename _Ep>
894 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
901 typename unique_ptr<_Up, _Ep>::pointer>
::type _CT;
906 template<
typename _Tp,
typename _Dp>
907 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
916 template<
typename _Tp,
typename _Dp>
917 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
926 template<
typename _Tp,
typename _Dp,
927 typename _Up,
typename _Ep>
928 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
932 {
return !(__y < __x); }
935 template<
typename _Tp,
typename _Dp>
936 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
939 {
return !(
nullptr < __x); }
942 template<
typename _Tp,
typename _Dp>
943 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
946 {
return !(__x <
nullptr); }
949 template<
typename _Tp,
typename _Dp,
950 typename _Up,
typename _Ep>
951 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
955 {
return (__y < __x); }
958 template<
typename _Tp,
typename _Dp>
959 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
968 template<
typename _Tp,
typename _Dp>
969 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
978 template<
typename _Tp,
typename _Dp,
979 typename _Up,
typename _Ep>
980 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
984 {
return !(__x < __y); }
987 template<
typename _Tp,
typename _Dp>
988 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
991 {
return !(__x <
nullptr); }
994 template<
typename _Tp,
typename _Dp>
995 _GLIBCXX_NODISCARD
inline bool
997 {
return !(
nullptr < __x); }
999#ifdef __cpp_lib_three_way_comparison
1000 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
1001 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1002 typename unique_ptr<_Up, _Ep>::pointer>
1003 _GLIBCXX23_CONSTEXPR
1005 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1006 typename unique_ptr<_Up, _Ep>::pointer>
1007 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
1008 const unique_ptr<_Up, _Ep>& __y)
1009 {
return compare_three_way()(__x.get(), __y.get()); }
1011 template<
typename _Tp,
typename _Dp>
1012 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1013 _GLIBCXX23_CONSTEXPR
1018 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1019 return compare_three_way()(__x.
get(),
static_cast<pointer
>(
nullptr));
1025 template<
typename _Up,
typename _Ptr =
typename _Up::po
inter>
1026 struct __uniq_ptr_hash
1027 :
public __hash_base<size_t, _Up>
1028#if ! _GLIBCXX_INLINE_VERSION
1029 ,
private __hash_empty_base<_Ptr>
1033 operator()(
const _Up& __u)
const
1038 template<
typename _Up>
1039 using __uniq_ptr_hash_base
1040 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1041 __uniq_ptr_hash<_Up>,
1042 __hash_not_enabled<typename _Up::pointer>>;
1046 template<
typename _Tp,
typename _Dp>
1048 :
public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1051#ifdef __glibcxx_make_unique
1055 template<
typename _Tp>
1059 template<
typename _Tp>
1060 struct _MakeUniq<_Tp[]>
1061 {
typedef unique_ptr<_Tp[]> __array; };
1063 template<
typename _Tp,
size_t _Bound>
1064 struct _MakeUniq<_Tp[_Bound]>
1065 {
struct __invalid_type { }; };
1067 template<
typename _Tp>
1068 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1069 template<
typename _Tp>
1070 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1071 template<
typename _Tp>
1072 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1083 template<
typename _Tp,
typename... _Args>
1084 _GLIBCXX23_CONSTEXPR
1085 inline __detail::__unique_ptr_t<_Tp>
1086 make_unique(_Args&&... __args)
1098 template<
typename _Tp>
1099 _GLIBCXX23_CONSTEXPR
1100 inline __detail::__unique_ptr_array_t<_Tp>
1101 make_unique(
size_t __num)
1109 template<
typename _Tp,
typename... _Args>
1110 __detail::__invalid_make_unique_t<_Tp>
1111 make_unique(_Args&&...) =
delete;
1113#if __cplusplus > 201703L
1120 template<
typename _Tp>
1121 _GLIBCXX23_CONSTEXPR
1122 inline __detail::__unique_ptr_t<_Tp>
1123 make_unique_for_overwrite()
1133 template<
typename _Tp>
1134 _GLIBCXX23_CONSTEXPR
1135 inline __detail::__unique_ptr_array_t<_Tp>
1136 make_unique_for_overwrite(
size_t __num)
1144 template<
typename _Tp,
typename... _Args>
1145 __detail::__invalid_make_unique_t<_Tp>
1146 make_unique_for_overwrite(_Args&&...) =
delete;
1151#if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1157 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1161 requires requires { __os << __p.get(); }
1168#if __cpp_variable_templates
1169 template<
typename _Tp>
1170 constexpr bool __is_unique_ptr =
false;
1171 template<
typename _Tp,
typename _Del>
1172 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> =
true;
1177#if __cplusplus >= 201703L
1178 namespace __detail::__variant
1180 template<
typename>
struct _Never_valueless_alt;
1184 template<
typename _Tp,
typename _Del>
1185 struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1191_GLIBCXX_END_NAMESPACE_VERSION
constexpr enable_if< __is_swappable< _Dp >::value >::type swap(unique_ptr< _Tp, _Dp > &__x, unique_ptr< _Tp, _Dp > &__y) noexcept
Swap overload for unique_ptr.
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
typename __detail::__cmp3way_res_impl< _Tp, _Up >::type compare_three_way_result_t
[cmp.result], result of three-way comparison
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Template class basic_ostream.
Primary class template hash.
Define a member typedef type only if a boolean constant is true.
A simple smart pointer providing strict ownership semantics.
One of the comparison functors.
constexpr void operator()(_Tp *__ptr) const
Calls delete __ptr
constexpr default_delete() noexcept=default
Default constructor.
constexpr enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr
constexpr default_delete() noexcept=default
Default constructor.
A move-only smart pointer that manages unique ownership of a resource.
constexpr pointer operator->() const noexcept
Return the stored pointer.
constexpr unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
constexpr unique_ptr(pointer __p) noexcept
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
constexpr unique_ptr(pointer __p, const deleter_type &__d) noexcept
unique_ptr(unique_ptr &&)=default
Move constructor.
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
constexpr void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
constexpr enable_if< __and_< __safe_conversion_up< _Up, _Ep >, is_assignable< deleter_type &, _Ep && > >::value, unique_ptr & >::type operator=(unique_ptr< _Up, _Ep > &&__u) noexcept
Assignment from another type.
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
unique_ptr(auto_ptr< _Up > &&__u) noexcept
Converting constructor from auto_ptr.
constexpr unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
constexpr deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
constexpr add_lvalue_reference< element_type >::type operator*() const noexcept(noexcept(*std::declval< pointer >()))
Dereference the stored pointer.
constexpr void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
constexpr pointer get() const noexcept
Return the stored pointer.
constexpr unique_ptr(pointer __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __d) noexcept
constexpr pointer release() noexcept
Release ownership of any stored pointer.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
constexpr const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
constexpr deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
constexpr pointer release() noexcept
Release ownership of any stored pointer.
constexpr void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
constexpr void reset(_Up __p) noexcept
Replace the stored pointer.
constexpr pointer get() const noexcept
Return the stored pointer.
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
constexpr std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.