31#define _UNIQUE_PTR_H 1
39#if __cplusplus > 201703L
44#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
45# if __cpp_lib_constexpr_memory < 202202L
47# undef __cpp_lib_constexpr_memory
48# define __cpp_lib_constexpr_memory 202202L
52namespace std _GLIBCXX_VISIBILITY(default)
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
61#if _GLIBCXX_USE_DEPRECATED
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
64 template<
typename>
class auto_ptr;
65#pragma GCC diagnostic pop
73 template<
typename _Tp>
95 "can't delete pointer to incomplete type");
96 static_assert(
sizeof(_Tp)>0,
97 "can't delete pointer to incomplete type");
110 template<
typename _Tp>
132 template<
typename _Up>
137 static_assert(
sizeof(_Tp)>0,
138 "can't delete pointer to incomplete type");
146 template <
typename _Tp,
typename _Dp>
147 class __uniq_ptr_impl
149 template <
typename _Up,
typename _Ep,
typename =
void>
155 template <
typename _Up,
typename _Ep>
157 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
159 using type =
typename remove_reference<_Ep>::type::pointer;
163 using _DeleterConstraint = enable_if<
164 __and_<__not_<is_pointer<_Dp>>,
165 is_default_constructible<_Dp>>::value>;
167 using pointer =
typename _Ptr<_Tp, _Dp>::type;
169 static_assert( !is_rvalue_reference<_Dp>::value,
170 "unique_ptr's deleter type must be a function object type"
171 " or an lvalue reference type" );
173 __uniq_ptr_impl() =
default;
175 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
177 template<
typename _Del>
179 __uniq_ptr_impl(pointer __p, _Del&& __d)
183 __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
185 { __u._M_ptr() =
nullptr; }
188 __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u)
noexcept
190 reset(__u.release());
196 pointer& _M_ptr() noexcept {
return std::get<0>(_M_t); }
198 pointer _M_ptr() const noexcept {
return std::get<0>(_M_t); }
200 _Dp& _M_deleter() noexcept {
return std::get<1>(_M_t); }
202 const _Dp& _M_deleter() const noexcept {
return std::get<1>(_M_t); }
205 void reset(pointer __p)
noexcept
207 const pointer __old_p = _M_ptr();
210 _M_deleter()(__old_p);
214 pointer release() noexcept
216 pointer __p = _M_ptr();
223 swap(__uniq_ptr_impl& __rhs)
noexcept
226 swap(this->_M_ptr(), __rhs._M_ptr());
227 swap(this->_M_deleter(), __rhs._M_deleter());
231 tuple<pointer, _Dp> _M_t;
235 template <
typename _Tp,
typename _Dp,
236 bool = is_move_constructible<_Dp>::value,
237 bool = is_move_assignable<_Dp>::value>
238 struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
240 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
241 __uniq_ptr_data(__uniq_ptr_data&&) =
default;
242 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
default;
245 template <
typename _Tp,
typename _Dp>
246 struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
248 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
249 __uniq_ptr_data(__uniq_ptr_data&&) =
default;
250 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
delete;
253 template <
typename _Tp,
typename _Dp>
254 struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
256 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
257 __uniq_ptr_data(__uniq_ptr_data&&) =
delete;
258 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
default;
261 template <
typename _Tp,
typename _Dp>
262 struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
264 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
265 __uniq_ptr_data(__uniq_ptr_data&&) =
delete;
266 __uniq_ptr_data& operator=(__uniq_ptr_data&&) =
delete;
275 template <
typename _Tp,
typename _Dp = default_delete<_Tp>>
278 template <
typename _Up>
279 using _DeleterConstraint =
286 using element_type = _Tp;
292 template<
typename _Up,
typename _Ep>
302 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
313 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
327 template<
typename _Del = deleter_type,
340 template<
typename _Del = deleter_type,
345 _Del&&> __d) noexcept
349 template<
typename _Del = deleter_type,
350 typename _DelUnref =
typename remove_reference<_Del>::type>
357 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
373 template<
typename _Up,
typename _Ep,
typename =
_Require<
383#if _GLIBCXX_USE_DEPRECATED
384#pragma GCC diagnostic push
385#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
387 template<
typename _Up,
typename =
_Require<
390#pragma GCC diagnostic pop
394#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
400 "unique_ptr's deleter must be invocable with a pointer");
401 auto&
__ptr = _M_t._M_ptr();
402 if (
__ptr !=
nullptr)
422 template<
typename _Up,
typename _Ep>
449 typename add_lvalue_reference<element_type>::type
452 __glibcxx_assert(
get() != pointer());
461 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
469 {
return _M_t._M_ptr(); }
475 {
return _M_t._M_deleter(); }
481 {
return _M_t._M_deleter(); }
485 explicit operator bool() const noexcept
486 {
return get() == pointer() ?
false :
true; }
494 {
return _M_t.release(); }
504 reset(pointer __p = pointer()) noexcept
507 "unique_ptr's deleter must be invocable with a pointer");
533 template<
typename _Tp,
typename _Dp>
536 template <
typename _Up>
537 using _DeleterConstraint =
542 template<
typename _Up>
543 using __remove_cv =
typename remove_cv<_Up>::type;
546 template<
typename _Up>
553 using element_type = _Tp;
558 template<
typename _Up,
typename _Ep,
570 template<
typename _Up>
577 typename remove_pointer<_Up>::type(*)[],
586 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
598 template<
typename _Up,
602 __safe_conversion_raw<_Up>::value,
bool>::type>
617 template<
typename _Up,
typename _Del = deleter_type,
632 template<
typename _Up,
typename _Del = deleter_type,
638 _Del&&> __d) noexcept
642 template<
typename _Up,
typename _Del = deleter_type,
643 typename _DelUnref =
typename remove_reference<_Del>::type,
653 template<
typename _Del = _Dp,
typename = _DeleterConstra
int<_Del>>
658 template<
typename _Up,
typename _Ep,
typename =
_Require<
669#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
674 auto&
__ptr = _M_t._M_ptr();
675 if (
__ptr !=
nullptr)
696 template<
typename _Up,
typename _Ep>
723 typename std::add_lvalue_reference<element_type>::type
726 __glibcxx_assert(
get() != pointer());
734 {
return _M_t._M_ptr(); }
740 {
return _M_t._M_deleter(); }
746 {
return _M_t._M_deleter(); }
750 explicit operator bool() const noexcept
751 {
return get() == pointer() ?
false :
true; }
759 {
return _M_t.release(); }
767 template <
typename _Up,
773 typename remove_pointer<_Up>::type(*)[],
785 void reset(nullptr_t =
nullptr) noexcept
786 {
reset(pointer()); }
806 template<
typename _Tp,
typename _Dp>
808#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
811 typename enable_if<__is_swappable<_Dp>::value>::type
819#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
820 template<
typename _Tp,
typename _Dp>
827 template<
typename _Tp,
typename _Dp,
828 typename _Up,
typename _Ep>
833 {
return __x.get() == __y.get(); }
836 template<
typename _Tp,
typename _Dp>
837 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
842#ifndef __cpp_lib_three_way_comparison
844 template<
typename _Tp,
typename _Dp>
851 template<
typename _Tp,
typename _Dp,
852 typename _Up,
typename _Ep>
857 {
return __x.get() != __y.get(); }
860 template<
typename _Tp,
typename _Dp>
864 {
return (
bool)__x; }
867 template<
typename _Tp,
typename _Dp>
871 {
return (
bool)__x; }
875 template<
typename _Tp,
typename _Dp,
876 typename _Up,
typename _Ep>
877 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
884 typename unique_ptr<_Up, _Ep>::pointer>::type
_CT;
889 template<
typename _Tp,
typename _Dp>
890 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
899 template<
typename _Tp,
typename _Dp>
900 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
909 template<
typename _Tp,
typename _Dp,
910 typename _Up,
typename _Ep>
911 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
915 {
return !(__y < __x); }
918 template<
typename _Tp,
typename _Dp>
919 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
922 {
return !(
nullptr < __x); }
925 template<
typename _Tp,
typename _Dp>
926 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
929 {
return !(__x <
nullptr); }
932 template<
typename _Tp,
typename _Dp,
933 typename _Up,
typename _Ep>
934 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
938 {
return (__y < __x); }
941 template<
typename _Tp,
typename _Dp>
942 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
951 template<
typename _Tp,
typename _Dp>
952 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
961 template<
typename _Tp,
typename _Dp,
962 typename _Up,
typename _Ep>
963 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
967 {
return !(__x < __y); }
970 template<
typename _Tp,
typename _Dp>
971 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
974 {
return !(__x <
nullptr); }
977 template<
typename _Tp,
typename _Dp>
978 _GLIBCXX_NODISCARD
inline bool
980 {
return !(
nullptr < __x); }
982#ifdef __cpp_lib_three_way_comparison
983 template<
typename _Tp,
typename _Dp,
typename _Up,
typename _Ep>
984 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
985 typename unique_ptr<_Up, _Ep>::pointer>
988 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
989 typename unique_ptr<_Up, _Ep>::pointer>
990 operator<=>(
const unique_ptr<_Tp, _Dp>& __x,
991 const unique_ptr<_Up, _Ep>& __y)
992 {
return compare_three_way()(__x.get(), __y.get()); }
994 template<
typename _Tp,
typename _Dp>
995 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
998 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
999 operator<=>(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1001 using pointer =
typename unique_ptr<_Tp, _Dp>::pointer;
1002 return compare_three_way()(__x.get(),
static_cast<pointer
>(
nullptr));
1008 template<
typename _Up,
typename _Ptr =
typename _Up::pointer,
1009 bool = __poison_hash<_Ptr>::__enable_hash_call>
1010 struct __uniq_ptr_hash
1011#if ! _GLIBCXX_INLINE_VERSION
1012 :
private __poison_hash<_Ptr>
1016 operator()(
const _Up& __u)
const
1018 {
return hash<_Ptr>()(__u.get()); }
1021 template<
typename _Up,
typename _Ptr>
1022 struct __uniq_ptr_hash<_Up, _Ptr, false>
1023 :
private __poison_hash<_Ptr>
1028 template<
typename _Tp,
typename _Dp>
1030 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
1031 public __uniq_ptr_hash<unique_ptr<_Tp, _Dp>>
1034#if __cplusplus >= 201402L
1035#define __cpp_lib_make_unique 201304L
1040 template<
typename _Tp>
1044 template<
typename _Tp>
1045 struct _MakeUniq<_Tp[]>
1046 {
typedef unique_ptr<_Tp[]> __array; };
1048 template<
typename _Tp,
size_t _Bound>
1049 struct _MakeUniq<_Tp[_Bound]>
1050 {
struct __invalid_type { }; };
1052 template<
typename _Tp>
1053 using __unique_ptr_t =
typename _MakeUniq<_Tp>::__single_object;
1054 template<
typename _Tp>
1055 using __unique_ptr_array_t =
typename _MakeUniq<_Tp>::__array;
1056 template<
typename _Tp>
1057 using __invalid_make_unique_t =
typename _MakeUniq<_Tp>::__invalid_type;
1068 template<
typename _Tp,
typename... _Args>
1069 _GLIBCXX23_CONSTEXPR
1070 inline __detail::__unique_ptr_t<_Tp>
1083 template<
typename _Tp>
1085 inline __detail::__unique_ptr_array_t<_Tp>
1094 template<
typename _Tp,
typename...
_Args>
1095 __detail::__invalid_make_unique_t<_Tp>
1098#if __cplusplus > 201703L
1105 template<
typename _Tp>
1107 inline __detail::__unique_ptr_t<_Tp>
1118 template<
typename _Tp>
1120 inline __detail::__unique_ptr_array_t<_Tp>
1129 template<
typename _Tp,
typename...
_Args>
1130 __detail::__invalid_make_unique_t<_Tp>
1136#if __cplusplus > 201703L && __cpp_concepts
1142 template<
typename _CharT,
typename _Traits,
typename _Tp,
typename _Dp>
1146 requires requires {
__os << __p.get(); }
1155#if __cplusplus >= 201703L
1156 namespace __detail::__variant
1158 template<
typename>
struct _Never_valueless_alt;
1162 template<
typename _Tp,
typename _Del>
1163 struct _Never_valueless_alt<
std::unique_ptr<_Tp, _Del>>
1169_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)
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
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.