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>>
296 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
307 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
321 template<
typename _Del = deleter_type,
322 typename = _Require<is_copy_constructible<_Del>>>
334 template<
typename _Del = deleter_type,
335 typename = _Require<is_move_constructible<_Del>>>
339 _Del&&> __d) noexcept
343 template<
typename _Del = deleter_type,
344 typename _DelUnref =
typename remove_reference<_Del>::type>
348 _DelUnref&&>) =
delete;
351 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
367 template<
typename _Up,
typename _Ep,
typename = _Require<
368 __safe_conversion_up<_Up, _Ep>,
369 __conditional_t<is_reference<_Dp>::value,
371 is_convertible<_Ep, _Dp>>>>
377#if _GLIBCXX_USE_DEPRECATED
378#pragma GCC diagnostic push
379#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
381 template<
typename _Up,
382 typename = _Require<is_convertible<_Up*, pointer>,
385#pragma GCC diagnostic pop
389#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
394 static_assert(__is_invocable<deleter_type&, pointer>::value,
395 "unique_ptr's deleter must be invocable with a pointer");
396 auto& __ptr = _M_t._M_ptr();
397 if (__ptr !=
nullptr)
417 template<
typename _Up,
typename _Ep>
420 __safe_conversion_up<_Up, _Ep>,
426 reset(__u.release());
444 typename add_lvalue_reference<element_type>::type
447#if _GLIBCXX_USE_BUILTIN_TRAIT(__reference_converts_from_temporary)
450 using _ResT =
typename add_lvalue_reference<element_type>::type;
451 using _DerefT =
decltype(*
get());
452 static_assert(!__reference_converts_from_temporary(_ResT, _DerefT),
453 "operator* must not return a dangling reference");
455 __glibcxx_assert(
get() != pointer());
464 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
472 {
return _M_t._M_ptr(); }
478 {
return _M_t._M_deleter(); }
484 {
return _M_t._M_deleter(); }
488 explicit operator bool() const noexcept
489 {
return get() == pointer() ? false :
true; }
497 {
return _M_t.release(); }
507 reset(pointer __p = pointer()) noexcept
509 static_assert(__is_invocable<deleter_type&, pointer>::value,
510 "unique_ptr's deleter must be invocable with a pointer");
519 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
528#ifdef __glibcxx_out_ptr
529 template<
typename,
typename,
typename...>
530 friend class out_ptr_t;
531 template<
typename,
typename,
typename...>
532 friend class inout_ptr_t;
544 template<
typename _Tp,
typename _Dp>
547 template <
typename _Up>
548 using _DeleterConstraint =
549 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
551 __uniq_ptr_data<_Tp, _Dp> _M_t;
554 template<
typename _Up>
555 using __is_derived_Tp
556 = __and_< is_base_of<_Tp, _Up>,
557 __not_<is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up>>> >;
560 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
561 using element_type = _Tp;
562 using deleter_type = _Dp;
566 template<
typename _Up,
typename _Ep,
568 typename _UP_pointer =
typename _UPtr::pointer,
569 typename _UP_element_type =
typename _UPtr::element_type>
570 using __safe_conversion_up = __and_<
574 is_convertible<_UP_element_type(*)[], element_type(*)[]>
578 template<
typename _Up>
579 using __safe_conversion_raw = __and_<
580 __or_<__or_<is_same<_Up, pointer>,
582 __and_<is_pointer<_Up>,
585 typename remove_pointer<_Up>::type(*)[],
594 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
606 template<
typename _Up,
608 typename = _DeleterConstraint<_Vp>,
610 __safe_conversion_raw<_Up>::value,
bool>
::type>
625 template<
typename _Up,
typename _Del = deleter_type,
626 typename = _Require<__safe_conversion_raw<_Up>,
629 unique_ptr(_Up __p,
const deleter_type& __d) noexcept
640 template<
typename _Up,
typename _Del = deleter_type,
641 typename = _Require<__safe_conversion_raw<_Up>,
646 _Del&&> __d) noexcept
650 template<
typename _Up,
typename _Del = deleter_type,
651 typename _DelUnref =
typename remove_reference<_Del>::type,
652 typename = _Require<__safe_conversion_raw<_Up>>>
655 _DelUnref&&>) =
delete;
661 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
666 template<
typename _Up,
typename _Ep,
typename = _Require<
667 __safe_conversion_up<_Up, _Ep>,
668 __conditional_t<is_reference<_Dp>::value,
670 is_convertible<_Ep, _Dp>>>>
677#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
682 auto& __ptr = _M_t._M_ptr();
683 if (__ptr !=
nullptr)
704 template<
typename _Up,
typename _Ep>
713 reset(__u.release());
731 typename std::add_lvalue_reference<element_type>::type
734 __glibcxx_assert(
get() != pointer());
742 {
return _M_t._M_ptr(); }
748 {
return _M_t._M_deleter(); }
754 {
return _M_t._M_deleter(); }
758 explicit operator bool() const noexcept
759 {
return get() == pointer() ? false :
true; }
767 {
return _M_t.release(); }
775 template <
typename _Up,
777 __or_<is_same<_Up, pointer>,
778 __and_<is_same<pointer, element_type*>,
781 typename remove_pointer<_Up>::type(*)[],
789 reset(_Up __p)
noexcept
793 void reset(nullptr_t =
nullptr) noexcept
794 { reset(pointer()); }
801 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
810#ifdef __glibcxx_out_ptr
811 template<
typename,
typename,
typename...>
friend class out_ptr_t;
812 template<
typename,
typename,
typename...>
friend class inout_ptr_t;
820 template<
typename _Tp,
typename _Dp>
822#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
825 typename enable_if<__is_swappable<_Dp>::value>::type
833#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
836 template<
typename _Tp,
typename _Dp>
843 template<
typename _Tp,
typename _Dp,
844 typename _Up,
typename _Ep>
845 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
849 {
return __x.
get() == __y.
get(); }
852 template<
typename _Tp,
typename _Dp>
853 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
858#ifndef __cpp_lib_three_way_comparison
860 template<
typename _Tp,
typename _Dp>
863 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
867 template<
typename _Tp,
typename _Dp,
868 typename _Up,
typename _Ep>
873 {
return __x.get() != __y.get(); }
876 template<
typename _Tp,
typename _Dp>
880 {
return (
bool)__x; }
883 template<
typename _Tp,
typename _Dp>
887 {
return (
bool)__x; }
891 template<
typename _Tp,
typename _Dp,
892 typename _Up,
typename _Ep>
893 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
900 typename unique_ptr<_Up, _Ep>::pointer>
::type _CT;
905 template<
typename _Tp,
typename _Dp>
906 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
915 template<
typename _Tp,
typename _Dp>
916 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
925 template<
typename _Tp,
typename _Dp,
926 typename _Up,
typename _Ep>
927 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
931 {
return !(__y < __x); }
934 template<
typename _Tp,
typename _Dp>
935 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
938 {
return !(
nullptr < __x); }
941 template<
typename _Tp,
typename _Dp>
942 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
945 {
return !(__x <
nullptr); }
948 template<
typename _Tp,
typename _Dp,
949 typename _Up,
typename _Ep>
950 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
954 {
return (__y < __x); }
957 template<
typename _Tp,
typename _Dp>
958 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
967 template<
typename _Tp,
typename _Dp>
968 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
977 template<
typename _Tp,
typename _Dp,
978 typename _Up,
typename _Ep>
979 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
983 {
return !(__x < __y); }
986 template<
typename _Tp,
typename _Dp>
987 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
990 {
return !(__x <
nullptr); }
993 template<
typename _Tp,
typename _Dp>
994 _GLIBCXX_NODISCARD
inline bool
996 {
return !(
nullptr < __x); }
998#ifdef __cpp_lib_three_way_comparison
999 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
1000 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1001 typename unique_ptr<_Up, _Ep>::pointer>
1002 _GLIBCXX23_CONSTEXPR
1004 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1005 typename unique_ptr<_Up, _Ep>::pointer>
1006 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
1007 const unique_ptr<_Up, _Ep>& __y)
1008 {
return compare_three_way()(__x.get(), __y.get()); }
1010 template<
typename _Tp,
typename _Dp>
1011 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1012 _GLIBCXX23_CONSTEXPR
1017 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1018 return compare_three_way()(__x.
get(),
static_cast<pointer
>(
nullptr));
1024 template<
typename _Up,
typename _Ptr =
typename _Up::po
inter>
1025 struct __uniq_ptr_hash
1026 :
public __hash_base<size_t, _Up>
1027#if ! _GLIBCXX_INLINE_VERSION
1028 ,
private __hash_empty_base<_Ptr>
1032 operator()(
const _Up& __u)
const
1037 template<
typename _Up>
1038 using __uniq_ptr_hash_base
1039 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1040 __uniq_ptr_hash<_Up>,
1041 __hash_not_enabled<typename _Up::pointer>>;
1045 template<
typename _Tp,
typename _Dp>
1047 :
public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1050#ifdef __glibcxx_make_unique
1054 template<
typename _Tp>
1058 template<
typename _Tp>
1059 struct _MakeUniq<_Tp[]>
1060 {
typedef unique_ptr<_Tp[]> __array; };
1062 template<
typename _Tp,
size_t _Bound>
1063 struct _MakeUniq<_Tp[_Bound]>
1064 {
struct __invalid_type { }; };
1066 template<
typename _Tp>
1067 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1068 template<
typename _Tp>
1069 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1070 template<
typename _Tp>
1071 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1082 template<
typename _Tp,
typename... _Args>
1083 _GLIBCXX23_CONSTEXPR
1084 inline __detail::__unique_ptr_t<_Tp>
1085 make_unique(_Args&&... __args)
1097 template<
typename _Tp>
1098 _GLIBCXX23_CONSTEXPR
1099 inline __detail::__unique_ptr_array_t<_Tp>
1100 make_unique(
size_t __num)
1108 template<
typename _Tp,
typename... _Args>
1109 __detail::__invalid_make_unique_t<_Tp>
1110 make_unique(_Args&&...) =
delete;
1112#if __cplusplus > 201703L
1119 template<
typename _Tp>
1120 _GLIBCXX23_CONSTEXPR
1121 inline __detail::__unique_ptr_t<_Tp>
1122 make_unique_for_overwrite()
1132 template<
typename _Tp>
1133 _GLIBCXX23_CONSTEXPR
1134 inline __detail::__unique_ptr_array_t<_Tp>
1135 make_unique_for_overwrite(
size_t __num)
1143 template<
typename _Tp,
typename... _Args>
1144 __detail::__invalid_make_unique_t<_Tp>
1145 make_unique_for_overwrite(_Args&&...) =
delete;
1150#if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1156 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1160 requires requires { __os << __p.get(); }
1167#if __cpp_variable_templates
1168 template<
typename _Tp>
1169 constexpr bool __is_unique_ptr =
false;
1170 template<
typename _Tp,
typename _Del>
1171 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> =
true;
1176#if __cplusplus >= 201703L
1177 namespace __detail::__variant
1179 template<
typename>
struct _Never_valueless_alt;
1183 template<
typename _Tp,
typename _Del>
1184 struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1190_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.