unique_ptr.h 26 KB

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