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>
128 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
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...>
550 template<
typename,
typename,
typename...>
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__)
855 template<
typename _Tp,
typename _Dp>
862 template<
typename _Tp,
typename _Dp,
863 typename _Up,
typename _Ep>
864 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
868 {
return __x.
get() == __y.
get(); }
871 template<
typename _Tp,
typename _Dp>
872 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
877#ifndef __cpp_lib_three_way_comparison
879 template<
typename _Tp,
typename _Dp>
882 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
886 template<
typename _Tp,
typename _Dp,
887 typename _Up,
typename _Ep>
892 {
return __x.get() != __y.get(); }
895 template<
typename _Tp,
typename _Dp>
899 {
return (
bool)__x; }
902 template<
typename _Tp,
typename _Dp>
906 {
return (
bool)__x; }
910 template<
typename _Tp,
typename _Dp,
911 typename _Up,
typename _Ep>
912 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
919 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
924 template<
typename _Tp,
typename _Dp>
925 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
934 template<
typename _Tp,
typename _Dp>
935 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
944 template<
typename _Tp,
typename _Dp,
945 typename _Up,
typename _Ep>
946 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
950 {
return !(__y < __x); }
953 template<
typename _Tp,
typename _Dp>
954 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
957 {
return !(
nullptr < __x); }
960 template<
typename _Tp,
typename _Dp>
961 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
964 {
return !(__x <
nullptr); }
967 template<
typename _Tp,
typename _Dp,
968 typename _Up,
typename _Ep>
969 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
973 {
return (__y < __x); }
976 template<
typename _Tp,
typename _Dp>
977 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
986 template<
typename _Tp,
typename _Dp>
987 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
996 template<
typename _Tp,
typename _Dp,
997 typename _Up,
typename _Ep>
998 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
1002 {
return !(__x < __y); }
1005 template<
typename _Tp,
typename _Dp>
1006 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
1009 {
return !(__x <
nullptr); }
1012 template<
typename _Tp,
typename _Dp>
1013 _GLIBCXX_NODISCARD
inline bool
1015 {
return !(
nullptr < __x); }
1017#ifdef __cpp_lib_three_way_comparison
1018 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
1019 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1020 typename unique_ptr<_Up, _Ep>::pointer>
1021 _GLIBCXX23_CONSTEXPR
1023 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1024 typename unique_ptr<_Up, _Ep>::pointer>
1025 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
1026 const unique_ptr<_Up, _Ep>& __y)
1027 {
return compare_three_way()(__x.get(), __y.get()); }
1029 template<
typename _Tp,
typename _Dp>
1030 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1031 _GLIBCXX23_CONSTEXPR
1036 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1037 return compare_three_way()(__x.
get(),
static_cast<pointer
>(
nullptr));
1043 template<
typename _Up,
typename _Ptr =
typename _Up::po
inter>
1044 struct __uniq_ptr_hash
1045 :
public __hash_base<size_t, _Up>
1046#if ! _GLIBCXX_INLINE_VERSION
1047 ,
private __hash_empty_base<_Ptr>
1051 operator()(
const _Up& __u)
const
1056 template<
typename _Up>
1057 using __uniq_ptr_hash_base
1058 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1059 __uniq_ptr_hash<_Up>,
1060 __hash_not_enabled<typename _Up::pointer>>;
1064 template<
typename _Tp,
typename _Dp>
1066 :
public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1069#ifdef __glibcxx_make_unique
1073 template<
typename _Tp>
1077 template<
typename _Tp>
1078 struct _MakeUniq<_Tp[]>
1079 {
typedef unique_ptr<_Tp[]> __array; };
1081 template<
typename _Tp,
size_t _Bound>
1082 struct _MakeUniq<_Tp[_Bound]>
1083 {
struct __invalid_type { }; };
1085 template<
typename _Tp>
1086 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1087 template<
typename _Tp>
1088 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1089 template<
typename _Tp>
1090 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1101 template<
typename _Tp,
typename... _Args>
1102 _GLIBCXX23_CONSTEXPR
1103 inline __detail::__unique_ptr_t<_Tp>
1104 make_unique(_Args&&... __args)
1116 template<
typename _Tp>
1117 _GLIBCXX23_CONSTEXPR
1118 inline __detail::__unique_ptr_array_t<_Tp>
1119 make_unique(
size_t __num)
1127 template<
typename _Tp,
typename... _Args>
1128 __detail::__invalid_make_unique_t<_Tp>
1129 make_unique(_Args&&...) =
delete;
1131#if __cplusplus > 201703L
1138 template<
typename _Tp>
1139 _GLIBCXX23_CONSTEXPR
1140 inline __detail::__unique_ptr_t<_Tp>
1141 make_unique_for_overwrite()
1151 template<
typename _Tp>
1152 _GLIBCXX23_CONSTEXPR
1153 inline __detail::__unique_ptr_array_t<_Tp>
1154 make_unique_for_overwrite(
size_t __num)
1162 template<
typename _Tp,
typename... _Args>
1163 __detail::__invalid_make_unique_t<_Tp>
1164 make_unique_for_overwrite(_Args&&...) =
delete;
1169#if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1175 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1179 requires requires { __os << __p.get(); }
1186#if __cpp_variable_templates
1187 template<
typename _Tp>
1188 constexpr bool __is_unique_ptr =
false;
1189 template<
typename _Tp,
typename _Del>
1190 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> =
true;
1195#if __cplusplus >= 201703L
1196 namespace __detail::__variant
1198 template<
typename>
struct _Never_valueless_alt;
1202 template<
typename _Tp,
typename _Del>
1203 struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1209_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.
Smart pointer adaptor for functions taking an output pointer parameter.
Smart pointer adaptor for functions taking an inout pointer parameter.
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(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 ~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
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.