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"
58 template<
typename>
class auto_ptr;
59#pragma GCC diagnostic pop
67 template<
typename _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>
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)
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,
230 bool = is_move_constructible<_Dp>::value,
231 bool = is_move_assignable<_Dp>::value>
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 =
280 using element_type = _Tp;
286 template<
typename _Up,
typename _Ep>
296 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
307 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
321 template<
typename _Del = deleter_type,
334 template<
typename _Del = deleter_type,
339 _Del&&> __d) noexcept
343 template<
typename _Del = deleter_type,
344 typename _DelUnref =
typename remove_reference<_Del>::type>
351 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
367 template<
typename _Up,
typename _Ep,
typename =
_Require<
377#if _GLIBCXX_USE_DEPRECATED
378#pragma GCC diagnostic push
379#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
381 template<
typename _Up,
385#pragma GCC diagnostic pop
389#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
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>
447#if _GLIBCXX_USE_BUILTIN_TRAIT(__reference_converts_from_temporary)
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
510 "unique_ptr's deleter must be invocable with a pointer");
528#ifdef __glibcxx_out_ptr
529 template<
typename,
typename,
typename...>
531 template<
typename,
typename,
typename...>
544 template<
typename _Tp,
typename _Dp>
547 template <
typename _Up>
548 using _DeleterConstraint =
554 template<
typename _Up>
561 using element_type = _Tp;
566 template<
typename _Up,
typename _Ep,
578 template<
typename _Up>
585 typename remove_pointer<_Up>::type(*)[],
594 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
606 template<
typename _Up,
610 __safe_conversion_raw<_Up>::value,
bool>::type>
625 template<
typename _Up,
typename _Del = deleter_type,
640 template<
typename _Up,
typename _Del = deleter_type,
646 _Del&&> __d) noexcept
650 template<
typename _Up,
typename _Del = deleter_type,
651 typename _DelUnref =
typename remove_reference<_Del>::type,
661 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
666 template<
typename _Up,
typename _Ep,
typename =
_Require<
677#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
682 auto&
__ptr = _M_t._M_ptr();
683 if (
__ptr !=
nullptr)
684 get_deleter()(
__ptr);
704 template<
typename _Up,
typename _Ep>
713 reset(
__u.release());
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,
781 typename remove_pointer<_Up>::type(*)[],
793 void reset(nullptr_t =
nullptr) noexcept
794 { reset(pointer()); }
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__)
834 template<
typename _Tp,
typename _Dp>
841 template<
typename _Tp,
typename _Dp,
842 typename _Up,
typename _Ep>
847 {
return __x.get() == __y.get(); }
850 template<
typename _Tp,
typename _Dp>
851 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
856#ifndef __cpp_lib_three_way_comparison
858 template<
typename _Tp,
typename _Dp>
861 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
865 template<
typename _Tp,
typename _Dp,
866 typename _Up,
typename _Ep>
869 operator!=(
const unique_ptr<_Tp, _Dp>& __x,
870 const unique_ptr<_Up, _Ep>& __y)
871 {
return __x.get() != __y.get(); }
874 template<
typename _Tp,
typename _Dp>
877 operator!=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
noexcept
878 {
return (
bool)__x; }
881 template<
typename _Tp,
typename _Dp>
884 operator!=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
885 {
return (
bool)__x; }
889 template<
typename _Tp,
typename _Dp,
890 typename _Up,
typename _Ep>
891 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
898 typename unique_ptr<_Up, _Ep>::pointer>::type
_CT;
903 template<
typename _Tp,
typename _Dp>
904 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
913 template<
typename _Tp,
typename _Dp>
914 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
923 template<
typename _Tp,
typename _Dp,
924 typename _Up,
typename _Ep>
925 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
929 {
return !(__y < __x); }
932 template<
typename _Tp,
typename _Dp>
933 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
936 {
return !(
nullptr < __x); }
939 template<
typename _Tp,
typename _Dp>
940 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
943 {
return !(__x <
nullptr); }
946 template<
typename _Tp,
typename _Dp,
947 typename _Up,
typename _Ep>
948 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
952 {
return (__y < __x); }
955 template<
typename _Tp,
typename _Dp>
956 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
965 template<
typename _Tp,
typename _Dp>
966 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
975 template<
typename _Tp,
typename _Dp,
976 typename _Up,
typename _Ep>
977 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
981 {
return !(__x < __y); }
984 template<
typename _Tp,
typename _Dp>
985 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
988 {
return !(__x <
nullptr); }
991 template<
typename _Tp,
typename _Dp>
992 _GLIBCXX_NODISCARD
inline bool
994 {
return !(
nullptr < __x); }
996#ifdef __cpp_lib_three_way_comparison
997 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
998 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
999 typename unique_ptr<_Up, _Ep>::pointer>
1000 _GLIBCXX23_CONSTEXPR
1002 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1003 typename unique_ptr<_Up, _Ep>::pointer>
1004 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
1005 const unique_ptr<_Up, _Ep>& __y)
1006 {
return compare_three_way()(__x.get(), __y.get()); }
1008 template<
typename _Tp,
typename _Dp>
1009 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1010 _GLIBCXX23_CONSTEXPR
1012 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
1013 operator<=>(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1015 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1016 return compare_three_way()(__x.get(),
static_cast<pointer
>(
nullptr));
1022 template<
typename _Up,
typename _Ptr =
typename _Up::po
inter>
1023 struct __uniq_ptr_hash
1024 :
public __hash_base<size_t, _Up>
1025#if ! _GLIBCXX_INLINE_VERSION
1026 ,
private __hash_empty_base<_Ptr>
1030 operator()(
const _Up& __u)
const
1032 {
return hash<_Ptr>()(__u.get()); }
1035 template<
typename _Up>
1036 using __uniq_ptr_hash_base
1037 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1038 __uniq_ptr_hash<_Up>,
1039 __hash_not_enabled<typename _Up::pointer>>;
1043 template<
typename _Tp,
typename _Dp>
1045 :
public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1048#ifdef __glibcxx_make_unique
1052 template<
typename _Tp>
1056 template<
typename _Tp>
1057 struct _MakeUniq<_Tp[]>
1058 {
typedef unique_ptr<_Tp[]> __array; };
1060 template<
typename _Tp,
size_t _Bound>
1061 struct _MakeUniq<_Tp[_Bound]>
1062 {
struct __invalid_type { }; };
1064 template<
typename _Tp>
1065 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1066 template<
typename _Tp>
1067 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1068 template<
typename _Tp>
1069 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1080 template<
typename _Tp,
typename... _Args>
1081 _GLIBCXX23_CONSTEXPR
1082 inline __detail::__unique_ptr_t<_Tp>
1095 template<
typename _Tp>
1097 inline __detail::__unique_ptr_array_t<_Tp>
1106 template<
typename _Tp,
typename...
_Args>
1107 __detail::__invalid_make_unique_t<_Tp>
1110#if __cplusplus > 201703L
1117 template<
typename _Tp>
1119 inline __detail::__unique_ptr_t<_Tp>
1130 template<
typename _Tp>
1132 inline __detail::__unique_ptr_array_t<_Tp>
1141 template<
typename _Tp,
typename...
_Args>
1142 __detail::__invalid_make_unique_t<_Tp>
1148#if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1154 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1158 requires requires {
__os << __p.get(); }
1165#if __cpp_variable_templates
1166 template<
typename _Tp>
1167 constexpr bool __is_unique_ptr =
false;
1168 template<
typename _Tp,
typename _Del>
1169 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> =
true;
1174#if __cplusplus >= 201703L
1175 namespace __detail::__variant
1177 template<
typename>
struct _Never_valueless_alt;
1181 template<
typename _Tp,
typename _Del>
1182 struct _Never_valueless_alt<
std::unique_ptr<_Tp, _Del>>
1188_GLIBCXX_END_NAMESPACE_VERSION
__detail::__invalid_make_unique_t< _Tp > make_unique_for_overwrite(_Args &&...)=delete
constexpr __detail::__unique_ptr_array_t< _Tp > make_unique_for_overwrite(size_t __num)
constexpr __detail::__unique_ptr_array_t< _Tp > make_unique(size_t __num)
constexpr enable_if< __is_swappable< _Dp >::value >::type swap(unique_ptr< _Tp, _Dp > &__x, unique_ptr< _Tp, _Dp > &__y) noexcept
__detail::__invalid_make_unique_t< _Tp > make_unique(_Args &&...)=delete
constexpr __detail::__unique_ptr_t< _Tp > make_unique_for_overwrite()
constexpr __detail::__unique_ptr_t< _Tp > make_unique(_Args &&... __args)
__bool_constant< true > true_type
The type used as a compile-time boolean with true 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.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Primary class template hash.
Define a member typedef type only if a boolean constant is true.
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.
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 unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
constexpr void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
constexpr const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
constexpr unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
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.
constexpr unique_ptr(_Up __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __d) noexcept
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.
constexpr unique_ptr(_Up __p) noexcept
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
unique_ptr(unique_ptr &&)=default
Move constructor.
constexpr unique_ptr(_Up __p, const deleter_type &__d) noexcept
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
constexpr std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.