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"
59 template<
typename>
class auto_ptr;
60#pragma GCC diagnostic pop
68 template<
typename _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>
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)
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,
231 bool = is_move_constructible<_Dp>::value,
232 bool = is_move_assignable<_Dp>::value>
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 =
281 using element_type = _Tp;
287 template<
typename _Up,
typename _Ep>
297 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
308 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
322 template<
typename _Del = deleter_type,
335 template<
typename _Del = deleter_type,
340 _Del&&> __d) noexcept
344 template<
typename _Del = deleter_type,
345 typename _DelUnref =
typename remove_reference<_Del>::type>
352 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
368 template<
typename _Up,
typename _Ep,
typename =
_Require<
378#if _GLIBCXX_USE_DEPRECATED
379#pragma GCC diagnostic push
380#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
382 template<
typename _Up,
386#pragma GCC diagnostic pop
390#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
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>
448#if _GLIBCXX_USE_BUILTIN_TRAIT(__reference_converts_from_temporary)
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
511 "unique_ptr's deleter must be invocable with a pointer");
529#ifdef __glibcxx_out_ptr
530 template<
typename,
typename,
typename...>
532 template<
typename,
typename,
typename...>
545 template<
typename _Tp,
typename _Dp>
548 template <
typename _Up>
549 using _DeleterConstraint =
555 template<
typename _Up>
562 using element_type = _Tp;
567 template<
typename _Up,
typename _Ep,
579 template<
typename _Up>
586 typename remove_pointer<_Up>::type(*)[],
595 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
607 template<
typename _Up,
611 __safe_conversion_raw<_Up>::value,
bool>::type>
626 template<
typename _Up,
typename _Del = deleter_type,
641 template<
typename _Up,
typename _Del = deleter_type,
647 _Del&&> __d) noexcept
651 template<
typename _Up,
typename _Del = deleter_type,
652 typename _DelUnref =
typename remove_reference<_Del>::type,
662 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
667 template<
typename _Up,
typename _Ep,
typename =
_Require<
678#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
683 auto&
__ptr = _M_t._M_ptr();
684 if (
__ptr !=
nullptr)
685 get_deleter()(
__ptr);
705 template<
typename _Up,
typename _Ep>
714 reset(
__u.release());
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,
782 typename remove_pointer<_Up>::type(*)[],
794 void reset(nullptr_t =
nullptr) noexcept
795 { reset(pointer()); }
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__)
835 template<
typename _Tp,
typename _Dp>
842 template<
typename _Tp,
typename _Dp,
843 typename _Up,
typename _Ep>
848 {
return __x.get() == __y.get(); }
851 template<
typename _Tp,
typename _Dp>
852 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
857#ifndef __cpp_lib_three_way_comparison
859 template<
typename _Tp,
typename _Dp>
862 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
866 template<
typename _Tp,
typename _Dp,
867 typename _Up,
typename _Ep>
870 operator!=(
const unique_ptr<_Tp, _Dp>& __x,
871 const unique_ptr<_Up, _Ep>& __y)
872 {
return __x.get() != __y.get(); }
875 template<
typename _Tp,
typename _Dp>
878 operator!=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
noexcept
879 {
return (
bool)__x; }
882 template<
typename _Tp,
typename _Dp>
885 operator!=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
noexcept
886 {
return (
bool)__x; }
890 template<
typename _Tp,
typename _Dp,
891 typename _Up,
typename _Ep>
892 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
899 typename unique_ptr<_Up, _Ep>::pointer>::type
_CT;
904 template<
typename _Tp,
typename _Dp>
905 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
914 template<
typename _Tp,
typename _Dp>
915 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
924 template<
typename _Tp,
typename _Dp,
925 typename _Up,
typename _Ep>
926 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
930 {
return !(__y < __x); }
933 template<
typename _Tp,
typename _Dp>
934 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
937 {
return !(
nullptr < __x); }
940 template<
typename _Tp,
typename _Dp>
941 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
944 {
return !(__x <
nullptr); }
947 template<
typename _Tp,
typename _Dp,
948 typename _Up,
typename _Ep>
949 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
953 {
return (__y < __x); }
956 template<
typename _Tp,
typename _Dp>
957 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
966 template<
typename _Tp,
typename _Dp>
967 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
976 template<
typename _Tp,
typename _Dp,
977 typename _Up,
typename _Ep>
978 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
982 {
return !(__x < __y); }
985 template<
typename _Tp,
typename _Dp>
986 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
989 {
return !(__x <
nullptr); }
992 template<
typename _Tp,
typename _Dp>
993 _GLIBCXX_NODISCARD
inline bool
995 {
return !(
nullptr < __x); }
997#ifdef __cpp_lib_three_way_comparison
998 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
999 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1000 typename unique_ptr<_Up, _Ep>::pointer>
1001 _GLIBCXX23_CONSTEXPR
1003 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1004 typename unique_ptr<_Up, _Ep>::pointer>
1005 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
1006 const unique_ptr<_Up, _Ep>& __y)
1007 {
return compare_three_way()(__x.get(), __y.get()); }
1009 template<
typename _Tp,
typename _Dp>
1010 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1011 _GLIBCXX23_CONSTEXPR
1013 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
1014 operator<=>(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1016 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1017 return compare_three_way()(__x.get(),
static_cast<pointer
>(
nullptr));
1023 template<
typename _Up,
typename _Ptr =
typename _Up::po
inter>
1024 struct __uniq_ptr_hash
1025 :
public __hash_base<size_t, _Up>
1026#if ! _GLIBCXX_INLINE_VERSION
1027 ,
private __hash_empty_base<_Ptr>
1031 operator()(
const _Up& __u)
const
1033 {
return hash<_Ptr>()(__u.get()); }
1036 template<
typename _Up>
1037 using __uniq_ptr_hash_base
1038 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1039 __uniq_ptr_hash<_Up>,
1040 __hash_not_enabled<typename _Up::pointer>>;
1044 template<
typename _Tp,
typename _Dp>
1046 :
public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1049#ifdef __glibcxx_make_unique
1053 template<
typename _Tp>
1057 template<
typename _Tp>
1058 struct _MakeUniq<_Tp[]>
1059 {
typedef unique_ptr<_Tp[]> __array; };
1061 template<
typename _Tp,
size_t _Bound>
1062 struct _MakeUniq<_Tp[_Bound]>
1063 {
struct __invalid_type { }; };
1065 template<
typename _Tp>
1066 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1067 template<
typename _Tp>
1068 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1069 template<
typename _Tp>
1070 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1081 template<
typename _Tp,
typename... _Args>
1082 _GLIBCXX23_CONSTEXPR
1083 inline __detail::__unique_ptr_t<_Tp>
1096 template<
typename _Tp>
1098 inline __detail::__unique_ptr_array_t<_Tp>
1107 template<
typename _Tp,
typename...
_Args>
1108 __detail::__invalid_make_unique_t<_Tp>
1111#if __cplusplus > 201703L
1118 template<
typename _Tp>
1120 inline __detail::__unique_ptr_t<_Tp>
1131 template<
typename _Tp>
1133 inline __detail::__unique_ptr_array_t<_Tp>
1142 template<
typename _Tp,
typename...
_Args>
1143 __detail::__invalid_make_unique_t<_Tp>
1149#if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1155 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1159 requires requires {
__os << __p.get(); }
1166#if __cpp_variable_templates
1167 template<
typename _Tp>
1168 constexpr bool __is_unique_ptr =
false;
1169 template<
typename _Tp,
typename _Del>
1170 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> =
true;
1175#if __cplusplus >= 201703L
1176 namespace __detail::__variant
1178 template<
typename>
struct _Never_valueless_alt;
1182 template<
typename _Tp,
typename _Del>
1183 struct _Never_valueless_alt<
std::unique_ptr<_Tp, _Del>>
1189_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.