31#define _UNIQUE_PTR_H 1
39#if __cplusplus >= 202002L
46namespace std _GLIBCXX_VISIBILITY(default)
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
55#if _GLIBCXX_USE_DEPRECATED
56#pragma GCC diagnostic push
57#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59#pragma GCC diagnostic pop
67 template<
typename _Tp>
78 template<typename _Up,
79 typename = _Require<is_convertible<_Up*, _Tp*>>>
89 "can't delete pointer to incomplete type");
90 static_assert(
sizeof(_Tp)>0,
91 "can't delete pointer to incomplete type");
104 template<
typename _Tp>
120 template<typename _Up,
121 typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>>
126 template<
typename _Up>
131 static_assert(
sizeof(_Tp)>0,
132 "can't delete pointer to incomplete type");
140 template <
typename _Tp,
typename _Dp>
141 class __uniq_ptr_impl
143 template <
typename _Up,
typename _Ep,
typename =
void>
149 template <
typename _Up,
typename _Ep>
151 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
153 using type =
typename remove_reference<_Ep>::type::pointer;
157 using _DeleterConstraint = enable_if<
158 __and_<__not_<is_pointer<_Dp>>,
159 is_default_constructible<_Dp>>::value>;
161 using pointer =
typename _Ptr<_Tp, _Dp>::type;
163 static_assert( !is_rvalue_reference<_Dp>::value,
164 "unique_ptr's deleter type must be a function object type"
165 " or an lvalue reference type" );
167 __uniq_ptr_impl() =
default;
169 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
171 template<
typename _Del>
173 __uniq_ptr_impl(pointer __p, _Del&& __d)
174 : _M_t(__p, std::
forward<_Del>(__d)) { }
177 __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
179 { __u._M_ptr() =
nullptr; }
182 __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u)
noexcept
184 reset(__u.release());
190 pointer& _M_ptr() noexcept {
return std::get<0>(_M_t); }
192 pointer _M_ptr() const noexcept {
return std::get<0>(_M_t); }
194 _Dp& _M_deleter() noexcept {
return std::get<1>(_M_t); }
196 const _Dp& _M_deleter() const noexcept {
return std::get<1>(_M_t); }
199 void reset(pointer __p)
noexcept
201 const pointer __old_p = _M_ptr();
204 _M_deleter()(__old_p);
208 pointer release() noexcept
210 pointer __p = _M_ptr();
217 swap(__uniq_ptr_impl& __rhs)
noexcept
220 swap(this->_M_ptr(), __rhs._M_ptr());
221 swap(this->_M_deleter(), __rhs._M_deleter());
225 tuple<pointer, _Dp> _M_t;
229 template <
typename _Tp,
typename _Dp,
232 struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
234 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
235 __uniq_ptr_data(__uniq_ptr_data&&) =
default;
236 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
default;
239 template <
typename _Tp,
typename _Dp>
240 struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
242 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
243 __uniq_ptr_data(__uniq_ptr_data&&) =
default;
244 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
delete;
247 template <
typename _Tp,
typename _Dp>
248 struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
250 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
251 __uniq_ptr_data(__uniq_ptr_data&&) =
delete;
252 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
default;
255 template <
typename _Tp,
typename _Dp>
256 struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
258 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
259 __uniq_ptr_data(__uniq_ptr_data&&) =
delete;
260 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
delete;
269 template <
typename _Tp,
typename _Dp = default_delete<_Tp>>
272 template <
typename _Up>
273 using _DeleterConstraint =
274 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
276 __uniq_ptr_data<_Tp, _Dp> _M_t;
279 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
280 using element_type = _Tp;
281 using deleter_type = _Dp;
286 template<
typename _Up,
typename _Ep>
287 using __safe_conversion_up = __and_<
288 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
289 __not_<is_array<_Up>>
293 template<
typename _Ptr,
typename =
void>
294 struct _Nothrow_deref
297 template<
typename _Ptr>
299 : __bool_constant<noexcept(*std::declval<_Ptr>())> { };
306 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
317 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
331 template<
typename _Del = deleter_type,
332 typename = _Require<is_copy_constructible<_Del>>>
344 template<
typename _Del = deleter_type,
345 typename = _Require<is_move_constructible<_Del>>>
349 _Del&&> __d) noexcept
353 template<
typename _Del = deleter_type,
354 typename _DelUnref =
typename remove_reference<_Del>::type>
358 _DelUnref&&>) =
delete;
361 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
377 template<
typename _Up,
typename _Ep,
typename = _Require<
378 __safe_conversion_up<_Up, _Ep>,
379 __conditional_t<is_reference<_Dp>::value,
381 is_convertible<_Ep, _Dp>>>>
387#if _GLIBCXX_USE_DEPRECATED
388#pragma GCC diagnostic push
389#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
391 template<
typename _Up,
392 typename = _Require<is_convertible<_Up*, pointer>,
395#pragma GCC diagnostic pop
399#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
404 static_assert(__is_invocable<deleter_type&, pointer>::value,
405 "unique_ptr's deleter must be invocable with a pointer");
406 auto& __ptr = _M_t._M_ptr();
407 if (__ptr !=
nullptr)
427 template<
typename _Up,
typename _Ep>
430 __safe_conversion_up<_Up, _Ep>,
436 reset(__u.release());
454 typename add_lvalue_reference<element_type>::type
463 noexcept(_Nothrow_deref<pointer>::value)
466#if _GLIBCXX_USE_BUILTIN_TRAIT(__reference_converts_from_temporary)
469 using _ResT =
typename add_lvalue_reference<element_type>::type;
470 using _DerefT =
decltype(*
get());
471 static_assert(!__reference_converts_from_temporary(_ResT, _DerefT),
472 "operator* must not return a dangling reference");
474 __glibcxx_assert(
get() != pointer());
483 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
491 {
return _M_t._M_ptr(); }
497 {
return _M_t._M_deleter(); }
503 {
return _M_t._M_deleter(); }
507 explicit operator bool() const noexcept
508 {
return get() == pointer() ? false :
true; }
516 {
return _M_t.release(); }
526 reset(pointer __p = pointer()) noexcept
528 static_assert(__is_invocable<deleter_type&, pointer>::value,
529 "unique_ptr's deleter must be invocable with a pointer");
538 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
547#ifdef __glibcxx_out_ptr
548 template<
typename,
typename,
typename...>
549 friend class out_ptr_t;
550 template<
typename,
typename,
typename...>
551 friend class inout_ptr_t;
563 template<
typename _Tp,
typename _Dp>
566 template <
typename _Up>
567 using _DeleterConstraint =
568 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
570 __uniq_ptr_data<_Tp, _Dp> _M_t;
573 template<
typename _Up>
574 using __is_derived_Tp
575 = __and_< is_base_of<_Tp, _Up>,
576 __not_<is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up>>> >;
579 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
580 using element_type = _Tp;
581 using deleter_type = _Dp;
585 template<
typename _Up,
typename _Ep,
587 typename _UP_pointer =
typename _UPtr::pointer,
588 typename _UP_element_type =
typename _UPtr::element_type>
589 using __safe_conversion_up = __and_<
593 is_convertible<_UP_element_type(*)[], element_type(*)[]>
597 template<
typename _Up>
598 using __safe_conversion_raw = __and_<
599 __or_<__or_<is_same<_Up, pointer>,
601 __and_<is_pointer<_Up>,
604 typename remove_pointer<_Up>::type(*)[],
613 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
625 template<
typename _Up,
627 typename = _DeleterConstraint<_Vp>,
629 __safe_conversion_raw<_Up>::value,
bool>
::type>
644 template<
typename _Up,
typename _Del = deleter_type,
645 typename = _Require<__safe_conversion_raw<_Up>,
648 unique_ptr(_Up __p,
const deleter_type& __d) noexcept
659 template<
typename _Up,
typename _Del = deleter_type,
660 typename = _Require<__safe_conversion_raw<_Up>,
665 _Del&&> __d) noexcept
669 template<
typename _Up,
typename _Del = deleter_type,
670 typename _DelUnref =
typename remove_reference<_Del>::type,
671 typename = _Require<__safe_conversion_raw<_Up>>>
674 _DelUnref&&>) =
delete;
680 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
685 template<
typename _Up,
typename _Ep,
typename = _Require<
686 __safe_conversion_up<_Up, _Ep>,
687 __conditional_t<is_reference<_Dp>::value,
689 is_convertible<_Ep, _Dp>>>>
696#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
701 auto& __ptr = _M_t._M_ptr();
702 if (__ptr !=
nullptr)
723 template<
typename _Up,
typename _Ep>
732 reset(__u.release());
750 typename std::add_lvalue_reference<element_type>::type
753 __glibcxx_assert(
get() != pointer());
761 {
return _M_t._M_ptr(); }
767 {
return _M_t._M_deleter(); }
773 {
return _M_t._M_deleter(); }
777 explicit operator bool() const noexcept
778 {
return get() == pointer() ? false :
true; }
786 {
return _M_t.release(); }
794 template <
typename _Up,
796 __or_<is_same<_Up, pointer>,
797 __and_<is_same<pointer, element_type*>,
800 typename remove_pointer<_Up>::type(*)[],
808 reset(_Up __p)
noexcept
812 void reset(nullptr_t =
nullptr) noexcept
813 { reset(pointer()); }
820 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
829#ifdef __glibcxx_out_ptr
830 template<
typename,
typename,
typename...>
friend class out_ptr_t;
831 template<
typename,
typename,
typename...>
friend class inout_ptr_t;
839 template<
typename _Tp,
typename _Dp>
841#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
844 typename enable_if<__is_swappable<_Dp>::value>::type
852#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
853 template<
typename _Tp,
typename _Dp>
860 template<
typename _Tp,
typename _Dp,
861 typename _Up,
typename _Ep>
862 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
866 {
return __x.
get() == __y.
get(); }
869 template<
typename _Tp,
typename _Dp>
870 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
875#ifndef __cpp_lib_three_way_comparison
877 template<
typename _Tp,
typename _Dp>
880 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
884 template<
typename _Tp,
typename _Dp,
885 typename _Up,
typename _Ep>
890 {
return __x.get() != __y.get(); }
893 template<
typename _Tp,
typename _Dp>
897 {
return (
bool)__x; }
900 template<
typename _Tp,
typename _Dp>
904 {
return (
bool)__x; }
908 template<
typename _Tp,
typename _Dp,
909 typename _Up,
typename _Ep>
910 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
917 typename unique_ptr<_Up, _Ep>::pointer>
::type _CT;
922 template<
typename _Tp,
typename _Dp>
923 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
932 template<
typename _Tp,
typename _Dp>
933 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
942 template<
typename _Tp,
typename _Dp,
943 typename _Up,
typename _Ep>
944 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
948 {
return !(__y < __x); }
951 template<
typename _Tp,
typename _Dp>
952 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
955 {
return !(
nullptr < __x); }
958 template<
typename _Tp,
typename _Dp>
959 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
962 {
return !(__x <
nullptr); }
965 template<
typename _Tp,
typename _Dp,
966 typename _Up,
typename _Ep>
967 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
971 {
return (__y < __x); }
974 template<
typename _Tp,
typename _Dp>
975 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
984 template<
typename _Tp,
typename _Dp>
985 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
994 template<
typename _Tp,
typename _Dp,
995 typename _Up,
typename _Ep>
996 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
1000 {
return !(__x < __y); }
1003 template<
typename _Tp,
typename _Dp>
1004 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
1007 {
return !(__x <
nullptr); }
1010 template<
typename _Tp,
typename _Dp>
1011 _GLIBCXX_NODISCARD
inline bool
1013 {
return !(
nullptr < __x); }
1015#ifdef __cpp_lib_three_way_comparison
1016 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
1017 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1018 typename unique_ptr<_Up, _Ep>::pointer>
1019 _GLIBCXX23_CONSTEXPR
1021 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1022 typename unique_ptr<_Up, _Ep>::pointer>
1023 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
1024 const unique_ptr<_Up, _Ep>& __y)
1025 {
return compare_three_way()(__x.get(), __y.get()); }
1027 template<
typename _Tp,
typename _Dp>
1028 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1029 _GLIBCXX23_CONSTEXPR
1034 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1035 return compare_three_way()(__x.
get(),
static_cast<pointer
>(
nullptr));
1041 template<
typename _Up,
typename _Ptr =
typename _Up::po
inter>
1042 struct __uniq_ptr_hash
1043 :
public __hash_base<size_t, _Up>
1044#if ! _GLIBCXX_INLINE_VERSION
1045 ,
private __hash_empty_base<_Ptr>
1049 operator()(
const _Up& __u)
const
1054 template<
typename _Up>
1055 using __uniq_ptr_hash_base
1056 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1057 __uniq_ptr_hash<_Up>,
1058 __hash_not_enabled<typename _Up::pointer>>;
1062 template<
typename _Tp,
typename _Dp>
1064 :
public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1067#ifdef __glibcxx_make_unique
1071 template<
typename _Tp>
1075 template<
typename _Tp>
1076 struct _MakeUniq<_Tp[]>
1077 {
typedef unique_ptr<_Tp[]> __array; };
1079 template<
typename _Tp,
size_t _Bound>
1080 struct _MakeUniq<_Tp[_Bound]>
1081 {
struct __invalid_type { }; };
1083 template<
typename _Tp>
1084 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1085 template<
typename _Tp>
1086 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1087 template<
typename _Tp>
1088 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1099 template<
typename _Tp,
typename... _Args>
1100 _GLIBCXX23_CONSTEXPR
1101 inline __detail::__unique_ptr_t<_Tp>
1102 make_unique(_Args&&... __args)
1114 template<
typename _Tp>
1115 _GLIBCXX23_CONSTEXPR
1116 inline __detail::__unique_ptr_array_t<_Tp>
1117 make_unique(
size_t __num)
1125 template<
typename _Tp,
typename... _Args>
1126 __detail::__invalid_make_unique_t<_Tp>
1127 make_unique(_Args&&...) =
delete;
1129#if __cplusplus > 201703L
1136 template<
typename _Tp>
1137 _GLIBCXX23_CONSTEXPR
1138 inline __detail::__unique_ptr_t<_Tp>
1139 make_unique_for_overwrite()
1149 template<
typename _Tp>
1150 _GLIBCXX23_CONSTEXPR
1151 inline __detail::__unique_ptr_array_t<_Tp>
1152 make_unique_for_overwrite(
size_t __num)
1160 template<
typename _Tp,
typename... _Args>
1161 __detail::__invalid_make_unique_t<_Tp>
1162 make_unique_for_overwrite(_Args&&...) =
delete;
1167#if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1173 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1177 requires requires { __os << __p.get(); }
1184#if __cpp_variable_templates
1185 template<
typename _Tp>
1186 constexpr bool __is_unique_ptr =
false;
1187 template<
typename _Tp,
typename _Del>
1188 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> =
true;
1193#if __cplusplus >= 201703L
1194 namespace __detail::__variant
1196 template<
typename>
struct _Never_valueless_alt;
1200 template<
typename _Tp,
typename _Del>
1201 struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1207_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.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
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 add_lvalue_reference< element_type >::type operator*() const noexcept(noexcept(*std::declval< pointer >()))
Dereference 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 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.