unique_ptr.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_UNIQUE_PTR_H
  10. #define _LIBCPP___MEMORY_UNIQUE_PTR_H
  11. #include <__compare/compare_three_way.h>
  12. #include <__compare/compare_three_way_result.h>
  13. #include <__compare/three_way_comparable.h>
  14. #include <__config>
  15. #include <__functional/hash.h>
  16. #include <__functional/operations.h>
  17. #include <__memory/allocator_traits.h> // __pointer
  18. #include <__memory/auto_ptr.h>
  19. #include <__memory/compressed_pair.h>
  20. #include <__type_traits/add_lvalue_reference.h>
  21. #include <__type_traits/common_type.h>
  22. #include <__type_traits/conditional.h>
  23. #include <__type_traits/dependent_type.h>
  24. #include <__type_traits/integral_constant.h>
  25. #include <__type_traits/is_array.h>
  26. #include <__type_traits/is_assignable.h>
  27. #include <__type_traits/is_constructible.h>
  28. #include <__type_traits/is_convertible.h>
  29. #include <__type_traits/is_function.h>
  30. #include <__type_traits/is_pointer.h>
  31. #include <__type_traits/is_reference.h>
  32. #include <__type_traits/is_same.h>
  33. #include <__type_traits/is_swappable.h>
  34. #include <__type_traits/is_trivially_relocatable.h>
  35. #include <__type_traits/is_void.h>
  36. #include <__type_traits/remove_extent.h>
  37. #include <__type_traits/type_identity.h>
  38. #include <__utility/forward.h>
  39. #include <__utility/move.h>
  40. #include <cstddef>
  41. #include <stlfwd>
  42. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  43. # pragma GCC system_header
  44. #endif
  45. _LIBCPP_PUSH_MACROS
  46. #include <__undef_macros>
  47. _LIBCPP_BEGIN_NAMESPACE_STD
  48. template <class _Tp>
  49. struct _LIBCPP_TEMPLATE_VIS default_delete {
  50. static_assert(!is_function<_Tp>::value, "default_delete cannot be instantiated for function types");
  51. #ifndef _LIBCPP_CXX03_LANG
  52. _LIBCPP_HIDE_FROM_ABI constexpr default_delete() _NOEXCEPT = default;
  53. #else
  54. _LIBCPP_HIDE_FROM_ABI default_delete() {}
  55. #endif
  56. template <class _Up, __enable_if_t<is_convertible<_Up*, _Tp*>::value, int> = 0>
  57. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 default_delete(const default_delete<_Up>&) _NOEXCEPT {}
  58. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator()(_Tp* __ptr) const _NOEXCEPT {
  59. static_assert(sizeof(_Tp) >= 0, "cannot delete an incomplete type");
  60. static_assert(!is_void<_Tp>::value, "cannot delete an incomplete type");
  61. delete __ptr;
  62. }
  63. };
  64. template <class _Tp>
  65. struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
  66. private:
  67. template <class _Up>
  68. struct _EnableIfConvertible : enable_if<is_convertible<_Up (*)[], _Tp (*)[]>::value> {};
  69. public:
  70. #ifndef _LIBCPP_CXX03_LANG
  71. _LIBCPP_HIDE_FROM_ABI constexpr default_delete() _NOEXCEPT = default;
  72. #else
  73. _LIBCPP_HIDE_FROM_ABI default_delete() {}
  74. #endif
  75. template <class _Up>
  76. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  77. default_delete(const default_delete<_Up[]>&, typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
  78. template <class _Up>
  79. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename _EnableIfConvertible<_Up>::type
  80. operator()(_Up* __ptr) const _NOEXCEPT {
  81. static_assert(sizeof(_Up) >= 0, "cannot delete an incomplete type");
  82. delete[] __ptr;
  83. }
  84. };
  85. template <class _Deleter>
  86. struct __unique_ptr_deleter_sfinae {
  87. static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
  88. typedef const _Deleter& __lval_ref_type;
  89. typedef _Deleter&& __good_rval_ref_type;
  90. typedef true_type __enable_rval_overload;
  91. };
  92. template <class _Deleter>
  93. struct __unique_ptr_deleter_sfinae<_Deleter const&> {
  94. typedef const _Deleter& __lval_ref_type;
  95. typedef const _Deleter&& __bad_rval_ref_type;
  96. typedef false_type __enable_rval_overload;
  97. };
  98. template <class _Deleter>
  99. struct __unique_ptr_deleter_sfinae<_Deleter&> {
  100. typedef _Deleter& __lval_ref_type;
  101. typedef _Deleter&& __bad_rval_ref_type;
  102. typedef false_type __enable_rval_overload;
  103. };
  104. #if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
  105. # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))
  106. #else
  107. # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
  108. #endif
  109. template <class _Tp, class _Dp>
  110. class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
  111. public:
  112. typedef _Tp element_type;
  113. typedef _Dp deleter_type;
  114. typedef _LIBCPP_NODEBUG typename __pointer<_Tp, deleter_type>::type pointer;
  115. static_assert(!is_rvalue_reference<deleter_type>::value, "the specified deleter type cannot be an rvalue reference");
  116. // A unique_ptr contains the following members which may be trivially relocatable:
  117. // - pointer : this may be trivially relocatable, so it's checked
  118. // - deleter_type: this may be trivially relocatable, so it's checked
  119. //
  120. // This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no
  121. // references to itself. This means that the entire structure is trivially relocatable if its members are.
  122. using __trivially_relocatable = __conditional_t<
  123. __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,
  124. unique_ptr,
  125. void>;
  126. private:
  127. __compressed_pair<pointer, deleter_type> __ptr_;
  128. typedef _LIBCPP_NODEBUG __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  129. template <bool _Dummy>
  130. using _LValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  131. template <bool _Dummy>
  132. using _GoodRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  133. template <bool _Dummy>
  134. using _BadRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  135. template <bool _Dummy, class _Deleter = typename __dependent_type< __type_identity<deleter_type>, _Dummy>::type>
  136. using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =
  137. __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value>;
  138. template <class _ArgType>
  139. using _EnableIfDeleterConstructible _LIBCPP_NODEBUG = __enable_if_t<is_constructible<deleter_type, _ArgType>::value>;
  140. template <class _UPtr, class _Up>
  141. using _EnableIfMoveConvertible _LIBCPP_NODEBUG =
  142. __enable_if_t< is_convertible<typename _UPtr::pointer, pointer>::value && !is_array<_Up>::value >;
  143. template <class _UDel>
  144. using _EnableIfDeleterConvertible _LIBCPP_NODEBUG =
  145. __enable_if_t< (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  146. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) >;
  147. template <class _UDel>
  148. using _EnableIfDeleterAssignable = __enable_if_t< is_assignable<_Dp&, _UDel&&>::value >;
  149. public:
  150. template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  151. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  152. template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  153. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
  154. : __ptr_(__value_init_tag(), __value_init_tag()) {}
  155. template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  156. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(pointer __p) _NOEXCEPT
  157. : __ptr_(__p, __value_init_tag()) {}
  158. template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
  159. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
  160. : __ptr_(__p, __d) {}
  161. template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
  162. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  163. : __ptr_(__p, std::move(__d)) {
  164. static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
  165. }
  166. template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
  167. _LIBCPP_HIDE_FROM_ABI unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
  168. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT
  169. : __ptr_(__u.release(), std::forward<deleter_type>(__u.get_deleter())) {}
  170. template <class _Up,
  171. class _Ep,
  172. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  173. class = _EnableIfDeleterConvertible<_Ep> >
  174. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
  175. : __ptr_(__u.release(), std::forward<_Ep>(__u.get_deleter())) {}
  176. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  177. template <class _Up,
  178. __enable_if_t<is_convertible<_Up*, _Tp*>::value && is_same<_Dp, default_delete<_Tp> >::value, int> = 0>
  179. _LIBCPP_HIDE_FROM_ABI unique_ptr(auto_ptr<_Up>&& __p) _NOEXCEPT : __ptr_(__p.release(), __value_init_tag()) {}
  180. #endif
  181. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
  182. reset(__u.release());
  183. __ptr_.second() = std::forward<deleter_type>(__u.get_deleter());
  184. return *this;
  185. }
  186. template <class _Up,
  187. class _Ep,
  188. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  189. class = _EnableIfDeleterAssignable<_Ep> >
  190. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
  191. reset(__u.release());
  192. __ptr_.second() = std::forward<_Ep>(__u.get_deleter());
  193. return *this;
  194. }
  195. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  196. template <class _Up,
  197. __enable_if_t<is_convertible<_Up*, _Tp*>::value && is_same<_Dp, default_delete<_Tp> >::value, int> = 0>
  198. _LIBCPP_HIDE_FROM_ABI unique_ptr& operator=(auto_ptr<_Up> __p) {
  199. reset(__p.release());
  200. return *this;
  201. }
  202. #endif
  203. #ifdef _LIBCPP_CXX03_LANG
  204. unique_ptr(unique_ptr const&) = delete;
  205. unique_ptr& operator=(unique_ptr const&) = delete;
  206. #endif
  207. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }
  208. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
  209. reset();
  210. return *this;
  211. }
  212. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator*() const {
  213. return *__ptr_.first();
  214. }
  215. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer operator->() const _NOEXCEPT { return __ptr_.first(); }
  216. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_.first(); }
  217. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT { return __ptr_.second(); }
  218. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type& get_deleter() const _NOEXCEPT {
  219. return __ptr_.second();
  220. }
  221. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {
  222. return __ptr_.first() != nullptr;
  223. }
  224. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {
  225. pointer __t = __ptr_.first();
  226. __ptr_.first() = pointer();
  227. return __t;
  228. }
  229. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(pointer __p = pointer()) _NOEXCEPT {
  230. pointer __tmp = __ptr_.first();
  231. __ptr_.first() = __p;
  232. if (__tmp)
  233. __ptr_.second()(__tmp);
  234. }
  235. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT { __ptr_.swap(__u.__ptr_); }
  236. };
  237. template <class _Tp, class _Dp>
  238. class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
  239. public:
  240. typedef _Tp element_type;
  241. typedef _Dp deleter_type;
  242. typedef typename __pointer<_Tp, deleter_type>::type pointer;
  243. // A unique_ptr contains the following members which may be trivially relocatable:
  244. // - pointer : this may be trivially relocatable, so it's checked
  245. // - deleter_type: this may be trivially relocatable, so it's checked
  246. //
  247. // This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no
  248. // references to itself. This means that the entire structure is trivially relocatable if its members are.
  249. using __trivially_relocatable = __conditional_t<
  250. __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,
  251. unique_ptr,
  252. void>;
  253. private:
  254. __compressed_pair<pointer, deleter_type> __ptr_;
  255. template <class _From>
  256. struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
  257. template <class _FromElem>
  258. struct _CheckArrayPointerConversion<_FromElem*>
  259. : integral_constant<bool,
  260. is_same<_FromElem*, pointer>::value ||
  261. (is_same<pointer, element_type*>::value &&
  262. is_convertible<_FromElem (*)[], element_type (*)[]>::value) > {};
  263. typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  264. template <bool _Dummy>
  265. using _LValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  266. template <bool _Dummy>
  267. using _GoodRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  268. template <bool _Dummy>
  269. using _BadRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  270. template <bool _Dummy, class _Deleter = typename __dependent_type< __type_identity<deleter_type>, _Dummy>::type>
  271. using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =
  272. __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value>;
  273. template <class _ArgType>
  274. using _EnableIfDeleterConstructible _LIBCPP_NODEBUG = __enable_if_t<is_constructible<deleter_type, _ArgType>::value>;
  275. template <class _Pp>
  276. using _EnableIfPointerConvertible _LIBCPP_NODEBUG = __enable_if_t< _CheckArrayPointerConversion<_Pp>::value >;
  277. template <class _UPtr, class _Up, class _ElemT = typename _UPtr::element_type>
  278. using _EnableIfMoveConvertible _LIBCPP_NODEBUG =
  279. __enable_if_t< is_array<_Up>::value && is_same<pointer, element_type*>::value &&
  280. is_same<typename _UPtr::pointer, _ElemT*>::value &&
  281. is_convertible<_ElemT (*)[], element_type (*)[]>::value >;
  282. template <class _UDel>
  283. using _EnableIfDeleterConvertible _LIBCPP_NODEBUG =
  284. __enable_if_t< (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  285. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) >;
  286. template <class _UDel>
  287. using _EnableIfDeleterAssignable _LIBCPP_NODEBUG = __enable_if_t< is_assignable<_Dp&, _UDel&&>::value >;
  288. public:
  289. template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  290. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  291. template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  292. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
  293. : __ptr_(__value_init_tag(), __value_init_tag()) {}
  294. template <class _Pp,
  295. bool _Dummy = true,
  296. class = _EnableIfDeleterDefaultConstructible<_Dummy>,
  297. class = _EnableIfPointerConvertible<_Pp> >
  298. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(_Pp __p) _NOEXCEPT
  299. : __ptr_(__p, __value_init_tag()) {}
  300. template <class _Pp,
  301. bool _Dummy = true,
  302. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
  303. class = _EnableIfPointerConvertible<_Pp> >
  304. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
  305. : __ptr_(__p, __d) {}
  306. template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
  307. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
  308. : __ptr_(nullptr, __d) {}
  309. template <class _Pp,
  310. bool _Dummy = true,
  311. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
  312. class = _EnableIfPointerConvertible<_Pp> >
  313. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  314. : __ptr_(__p, std::move(__d)) {
  315. static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
  316. }
  317. template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
  318. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  319. : __ptr_(nullptr, std::move(__d)) {
  320. static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
  321. }
  322. template <class _Pp,
  323. bool _Dummy = true,
  324. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
  325. class = _EnableIfPointerConvertible<_Pp> >
  326. _LIBCPP_HIDE_FROM_ABI unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
  327. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT
  328. : __ptr_(__u.release(), std::forward<deleter_type>(__u.get_deleter())) {}
  329. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
  330. reset(__u.release());
  331. __ptr_.second() = std::forward<deleter_type>(__u.get_deleter());
  332. return *this;
  333. }
  334. template <class _Up,
  335. class _Ep,
  336. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  337. class = _EnableIfDeleterConvertible<_Ep> >
  338. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
  339. : __ptr_(__u.release(), std::forward<_Ep>(__u.get_deleter())) {}
  340. template <class _Up,
  341. class _Ep,
  342. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  343. class = _EnableIfDeleterAssignable<_Ep> >
  344. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
  345. reset(__u.release());
  346. __ptr_.second() = std::forward<_Ep>(__u.get_deleter());
  347. return *this;
  348. }
  349. #ifdef _LIBCPP_CXX03_LANG
  350. unique_ptr(unique_ptr const&) = delete;
  351. unique_ptr& operator=(unique_ptr const&) = delete;
  352. #endif
  353. public:
  354. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }
  355. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
  356. reset();
  357. return *this;
  358. }
  359. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator[](size_t __i) const {
  360. return __ptr_.first()[__i];
  361. }
  362. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_.first(); }
  363. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT { return __ptr_.second(); }
  364. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type& get_deleter() const _NOEXCEPT {
  365. return __ptr_.second();
  366. }
  367. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {
  368. return __ptr_.first() != nullptr;
  369. }
  370. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {
  371. pointer __t = __ptr_.first();
  372. __ptr_.first() = pointer();
  373. return __t;
  374. }
  375. template <class _Pp, __enable_if_t<_CheckArrayPointerConversion<_Pp>::value, int> = 0>
  376. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(_Pp __p) _NOEXCEPT {
  377. pointer __tmp = __ptr_.first();
  378. __ptr_.first() = __p;
  379. if (__tmp)
  380. __ptr_.second()(__tmp);
  381. }
  382. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(nullptr_t = nullptr) _NOEXCEPT {
  383. pointer __tmp = __ptr_.first();
  384. __ptr_.first() = nullptr;
  385. if (__tmp)
  386. __ptr_.second()(__tmp);
  387. }
  388. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT { __ptr_.swap(__u.__ptr_); }
  389. };
  390. template <class _Tp, class _Dp, __enable_if_t<__is_swappable<_Dp>::value, int> = 0>
  391. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  392. swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {
  393. __x.swap(__y);
  394. }
  395. template <class _T1, class _D1, class _T2, class _D2>
  396. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  397. operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  398. return __x.get() == __y.get();
  399. }
  400. #if _LIBCPP_STD_VER <= 17
  401. template <class _T1, class _D1, class _T2, class _D2>
  402. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  403. return !(__x == __y);
  404. }
  405. #endif
  406. template <class _T1, class _D1, class _T2, class _D2>
  407. inline _LIBCPP_HIDE_FROM_ABI bool operator<(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  408. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  409. typedef typename unique_ptr<_T2, _D2>::pointer _P2;
  410. typedef typename common_type<_P1, _P2>::type _Vp;
  411. return less<_Vp>()(__x.get(), __y.get());
  412. }
  413. template <class _T1, class _D1, class _T2, class _D2>
  414. inline _LIBCPP_HIDE_FROM_ABI bool operator>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  415. return __y < __x;
  416. }
  417. template <class _T1, class _D1, class _T2, class _D2>
  418. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  419. return !(__y < __x);
  420. }
  421. template <class _T1, class _D1, class _T2, class _D2>
  422. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  423. return !(__x < __y);
  424. }
  425. #if _LIBCPP_STD_VER >= 20
  426. template <class _T1, class _D1, class _T2, class _D2>
  427. requires three_way_comparable_with<typename unique_ptr<_T1, _D1>::pointer, typename unique_ptr<_T2, _D2>::pointer>
  428. _LIBCPP_HIDE_FROM_ABI
  429. compare_three_way_result_t<typename unique_ptr<_T1, _D1>::pointer, typename unique_ptr<_T2, _D2>::pointer>
  430. operator<=>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
  431. return compare_three_way()(__x.get(), __y.get());
  432. }
  433. #endif
  434. template <class _T1, class _D1>
  435. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  436. operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {
  437. return !__x;
  438. }
  439. #if _LIBCPP_STD_VER <= 17
  440. template <class _T1, class _D1>
  441. inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT {
  442. return !__x;
  443. }
  444. template <class _T1, class _D1>
  445. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {
  446. return static_cast<bool>(__x);
  447. }
  448. template <class _T1, class _D1>
  449. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT {
  450. return static_cast<bool>(__x);
  451. }
  452. #endif // _LIBCPP_STD_VER <= 17
  453. template <class _T1, class _D1>
  454. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
  455. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  456. return less<_P1>()(__x.get(), nullptr);
  457. }
  458. template <class _T1, class _D1>
  459. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
  460. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  461. return less<_P1>()(nullptr, __x.get());
  462. }
  463. template <class _T1, class _D1>
  464. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
  465. return nullptr < __x;
  466. }
  467. template <class _T1, class _D1>
  468. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
  469. return __x < nullptr;
  470. }
  471. template <class _T1, class _D1>
  472. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
  473. return !(nullptr < __x);
  474. }
  475. template <class _T1, class _D1>
  476. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
  477. return !(__x < nullptr);
  478. }
  479. template <class _T1, class _D1>
  480. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
  481. return !(__x < nullptr);
  482. }
  483. template <class _T1, class _D1>
  484. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
  485. return !(nullptr < __x);
  486. }
  487. #if _LIBCPP_STD_VER >= 20
  488. template <class _T1, class _D1>
  489. requires three_way_comparable< typename unique_ptr<_T1, _D1>::pointer>
  490. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 compare_three_way_result_t<typename unique_ptr<_T1, _D1>::pointer>
  491. operator<=>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
  492. return compare_three_way()(__x.get(), static_cast<typename unique_ptr<_T1, _D1>::pointer>(nullptr));
  493. }
  494. #endif
  495. #if _LIBCPP_STD_VER >= 14
  496. template <class _Tp>
  497. struct __unique_if {
  498. typedef unique_ptr<_Tp> __unique_single;
  499. };
  500. template <class _Tp>
  501. struct __unique_if<_Tp[]> {
  502. typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
  503. };
  504. template <class _Tp, size_t _Np>
  505. struct __unique_if<_Tp[_Np]> {
  506. typedef void __unique_array_known_bound;
  507. };
  508. template <class _Tp, class... _Args>
  509. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_single
  510. make_unique(_Args&&... __args) {
  511. return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
  512. }
  513. template <class _Tp>
  514. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound
  515. make_unique(size_t __n) {
  516. typedef __remove_extent_t<_Tp> _Up;
  517. return unique_ptr<_Tp>(new _Up[__n]());
  518. }
  519. template <class _Tp, class... _Args>
  520. typename __unique_if<_Tp>::__unique_array_known_bound make_unique(_Args&&...) = delete;
  521. #endif // _LIBCPP_STD_VER >= 14
  522. #if _LIBCPP_STD_VER >= 20
  523. template <class _Tp>
  524. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_single
  525. make_unique_for_overwrite() {
  526. return unique_ptr<_Tp>(new _Tp);
  527. }
  528. template <class _Tp>
  529. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound
  530. make_unique_for_overwrite(size_t __n) {
  531. return unique_ptr<_Tp>(new __remove_extent_t<_Tp>[__n]);
  532. }
  533. template <class _Tp, class... _Args>
  534. typename __unique_if<_Tp>::__unique_array_known_bound make_unique_for_overwrite(_Args&&...) = delete;
  535. #endif // _LIBCPP_STD_VER >= 20
  536. template <class _Tp>
  537. struct _LIBCPP_TEMPLATE_VIS hash;
  538. template <class _Tp, class _Dp>
  539. #ifdef _LIBCPP_CXX03_LANG
  540. struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
  541. #else
  542. struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper< unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
  543. #endif
  544. {
  545. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  546. _LIBCPP_DEPRECATED_IN_CXX17 typedef unique_ptr<_Tp, _Dp> argument_type;
  547. _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
  548. #endif
  549. _LIBCPP_HIDE_FROM_ABI size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const {
  550. typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;
  551. return hash<pointer>()(__ptr.get());
  552. }
  553. };
  554. _LIBCPP_END_NAMESPACE_STD
  555. _LIBCPP_POP_MACROS
  556. #endif // _LIBCPP___MEMORY_UNIQUE_PTR_H