unique_ptr.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. // unique_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2008-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/unique_ptr.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _UNIQUE_PTR_H
  25. #define _UNIQUE_PTR_H 1
  26. #include <bits/c++config.h>
  27. #include <debug/assertions.h>
  28. #include <type_traits>
  29. #include <tuple>
  30. #include <bits/stl_function.h>
  31. #include <bits/functional_hash.h>
  32. #if __cplusplus >= 202002L
  33. # include <compare>
  34. # if _GLIBCXX_HOSTED
  35. # include <ostream>
  36. # endif
  37. #endif
  38. /* Duplicate definition with ptr_traits.h. */
  39. #if __cplusplus > 202002L && defined(__cpp_constexpr_dynamic_alloc)
  40. # define __cpp_lib_constexpr_memory 202202L
  41. #elif __cplusplus > 201703L
  42. # define __cpp_lib_constexpr_memory 201811L
  43. #endif
  44. namespace std _GLIBCXX_VISIBILITY(default)
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. /**
  48. * @addtogroup pointer_abstractions
  49. * @{
  50. */
  51. #if _GLIBCXX_USE_DEPRECATED
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  54. template<typename> class auto_ptr;
  55. #pragma GCC diagnostic pop
  56. #endif
  57. /** Primary template of default_delete, used by unique_ptr for single objects
  58. *
  59. * @headerfile memory
  60. * @since C++11
  61. */
  62. template<typename _Tp>
  63. struct default_delete
  64. {
  65. /// Default constructor
  66. constexpr default_delete() noexcept = default;
  67. /** @brief Converting constructor.
  68. *
  69. * Allows conversion from a deleter for objects of another type, `_Up`,
  70. * only if `_Up*` is convertible to `_Tp*`.
  71. */
  72. template<typename _Up,
  73. typename = _Require<is_convertible<_Up*, _Tp*>>>
  74. _GLIBCXX23_CONSTEXPR
  75. default_delete(const default_delete<_Up>&) noexcept { }
  76. /// Calls `delete __ptr`
  77. _GLIBCXX23_CONSTEXPR
  78. void
  79. operator()(_Tp* __ptr) const
  80. {
  81. static_assert(!is_void<_Tp>::value,
  82. "can't delete pointer to incomplete type");
  83. static_assert(sizeof(_Tp)>0,
  84. "can't delete pointer to incomplete type");
  85. delete __ptr;
  86. }
  87. };
  88. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  89. // DR 740 - omit specialization for array objects with a compile time length
  90. /** Specialization of default_delete for arrays, used by `unique_ptr<T[]>`
  91. *
  92. * @headerfile memory
  93. * @since C++11
  94. */
  95. template<typename _Tp>
  96. struct default_delete<_Tp[]>
  97. {
  98. public:
  99. /// Default constructor
  100. constexpr default_delete() noexcept = default;
  101. /** @brief Converting constructor.
  102. *
  103. * Allows conversion from a deleter for arrays of another type, such as
  104. * a const-qualified version of `_Tp`.
  105. *
  106. * Conversions from types derived from `_Tp` are not allowed because
  107. * it is undefined to `delete[]` an array of derived types through a
  108. * pointer to the base type.
  109. */
  110. template<typename _Up,
  111. typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>>
  112. _GLIBCXX23_CONSTEXPR
  113. default_delete(const default_delete<_Up[]>&) noexcept { }
  114. /// Calls `delete[] __ptr`
  115. template<typename _Up>
  116. _GLIBCXX23_CONSTEXPR
  117. typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
  118. operator()(_Up* __ptr) const
  119. {
  120. static_assert(sizeof(_Tp)>0,
  121. "can't delete pointer to incomplete type");
  122. delete [] __ptr;
  123. }
  124. };
  125. /// @cond undocumented
  126. // Manages the pointer and deleter of a unique_ptr
  127. template <typename _Tp, typename _Dp>
  128. class __uniq_ptr_impl
  129. {
  130. template <typename _Up, typename _Ep, typename = void>
  131. struct _Ptr
  132. {
  133. using type = _Up*;
  134. };
  135. template <typename _Up, typename _Ep>
  136. struct
  137. _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
  138. {
  139. using type = typename remove_reference<_Ep>::type::pointer;
  140. };
  141. public:
  142. using _DeleterConstraint = enable_if<
  143. __and_<__not_<is_pointer<_Dp>>,
  144. is_default_constructible<_Dp>>::value>;
  145. using pointer = typename _Ptr<_Tp, _Dp>::type;
  146. static_assert( !is_rvalue_reference<_Dp>::value,
  147. "unique_ptr's deleter type must be a function object type"
  148. " or an lvalue reference type" );
  149. __uniq_ptr_impl() = default;
  150. _GLIBCXX23_CONSTEXPR
  151. __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
  152. template<typename _Del>
  153. _GLIBCXX23_CONSTEXPR
  154. __uniq_ptr_impl(pointer __p, _Del&& __d)
  155. : _M_t(__p, std::forward<_Del>(__d)) { }
  156. _GLIBCXX23_CONSTEXPR
  157. __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
  158. : _M_t(std::move(__u._M_t))
  159. { __u._M_ptr() = nullptr; }
  160. _GLIBCXX23_CONSTEXPR
  161. __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u) noexcept
  162. {
  163. reset(__u.release());
  164. _M_deleter() = std::forward<_Dp>(__u._M_deleter());
  165. return *this;
  166. }
  167. _GLIBCXX23_CONSTEXPR
  168. pointer& _M_ptr() noexcept { return std::get<0>(_M_t); }
  169. _GLIBCXX23_CONSTEXPR
  170. pointer _M_ptr() const noexcept { return std::get<0>(_M_t); }
  171. _GLIBCXX23_CONSTEXPR
  172. _Dp& _M_deleter() noexcept { return std::get<1>(_M_t); }
  173. _GLIBCXX23_CONSTEXPR
  174. const _Dp& _M_deleter() const noexcept { return std::get<1>(_M_t); }
  175. _GLIBCXX23_CONSTEXPR
  176. void reset(pointer __p) noexcept
  177. {
  178. const pointer __old_p = _M_ptr();
  179. _M_ptr() = __p;
  180. if (__old_p)
  181. _M_deleter()(__old_p);
  182. }
  183. _GLIBCXX23_CONSTEXPR
  184. pointer release() noexcept
  185. {
  186. pointer __p = _M_ptr();
  187. _M_ptr() = nullptr;
  188. return __p;
  189. }
  190. _GLIBCXX23_CONSTEXPR
  191. void
  192. swap(__uniq_ptr_impl& __rhs) noexcept
  193. {
  194. using std::swap;
  195. swap(this->_M_ptr(), __rhs._M_ptr());
  196. swap(this->_M_deleter(), __rhs._M_deleter());
  197. }
  198. private:
  199. tuple<pointer, _Dp> _M_t;
  200. };
  201. // Defines move construction + assignment as either defaulted or deleted.
  202. template <typename _Tp, typename _Dp,
  203. bool = is_move_constructible<_Dp>::value,
  204. bool = is_move_assignable<_Dp>::value>
  205. struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
  206. {
  207. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  208. __uniq_ptr_data(__uniq_ptr_data&&) = default;
  209. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
  210. };
  211. template <typename _Tp, typename _Dp>
  212. struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
  213. {
  214. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  215. __uniq_ptr_data(__uniq_ptr_data&&) = default;
  216. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
  217. };
  218. template <typename _Tp, typename _Dp>
  219. struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
  220. {
  221. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  222. __uniq_ptr_data(__uniq_ptr_data&&) = delete;
  223. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
  224. };
  225. template <typename _Tp, typename _Dp>
  226. struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
  227. {
  228. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  229. __uniq_ptr_data(__uniq_ptr_data&&) = delete;
  230. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
  231. };
  232. /// @endcond
  233. // 20.7.1.2 unique_ptr for single objects.
  234. /// A move-only smart pointer that manages unique ownership of a resource.
  235. /// @headerfile memory
  236. /// @since C++11
  237. template <typename _Tp, typename _Dp = default_delete<_Tp>>
  238. class unique_ptr
  239. {
  240. template <typename _Up>
  241. using _DeleterConstraint =
  242. typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
  243. __uniq_ptr_data<_Tp, _Dp> _M_t;
  244. public:
  245. using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
  246. using element_type = _Tp;
  247. using deleter_type = _Dp;
  248. private:
  249. // helper template for detecting a safe conversion from another
  250. // unique_ptr
  251. template<typename _Up, typename _Ep>
  252. using __safe_conversion_up = __and_<
  253. is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
  254. __not_<is_array<_Up>>
  255. >;
  256. public:
  257. // Constructors.
  258. /// Default constructor, creates a unique_ptr that owns nothing.
  259. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  260. constexpr unique_ptr() noexcept
  261. : _M_t()
  262. { }
  263. /** Takes ownership of a pointer.
  264. *
  265. * @param __p A pointer to an object of @c element_type
  266. *
  267. * The deleter will be value-initialized.
  268. */
  269. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  270. _GLIBCXX23_CONSTEXPR
  271. explicit
  272. unique_ptr(pointer __p) noexcept
  273. : _M_t(__p)
  274. { }
  275. /** Takes ownership of a pointer.
  276. *
  277. * @param __p A pointer to an object of @c element_type
  278. * @param __d A reference to a deleter.
  279. *
  280. * The deleter will be initialized with @p __d
  281. */
  282. template<typename _Del = deleter_type,
  283. typename = _Require<is_copy_constructible<_Del>>>
  284. _GLIBCXX23_CONSTEXPR
  285. unique_ptr(pointer __p, const deleter_type& __d) noexcept
  286. : _M_t(__p, __d) { }
  287. /** Takes ownership of a pointer.
  288. *
  289. * @param __p A pointer to an object of @c element_type
  290. * @param __d An rvalue reference to a (non-reference) deleter.
  291. *
  292. * The deleter will be initialized with @p std::move(__d)
  293. */
  294. template<typename _Del = deleter_type,
  295. typename = _Require<is_move_constructible<_Del>>>
  296. _GLIBCXX23_CONSTEXPR
  297. unique_ptr(pointer __p,
  298. __enable_if_t<!is_lvalue_reference<_Del>::value,
  299. _Del&&> __d) noexcept
  300. : _M_t(__p, std::move(__d))
  301. { }
  302. template<typename _Del = deleter_type,
  303. typename _DelUnref = typename remove_reference<_Del>::type>
  304. _GLIBCXX23_CONSTEXPR
  305. unique_ptr(pointer,
  306. __enable_if_t<is_lvalue_reference<_Del>::value,
  307. _DelUnref&&>) = delete;
  308. /// Creates a unique_ptr that owns nothing.
  309. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  310. constexpr unique_ptr(nullptr_t) noexcept
  311. : _M_t()
  312. { }
  313. // Move constructors.
  314. /// Move constructor.
  315. unique_ptr(unique_ptr&&) = default;
  316. /** @brief Converting constructor from another type
  317. *
  318. * Requires that the pointer owned by @p __u is convertible to the
  319. * type of pointer owned by this object, @p __u does not own an array,
  320. * and @p __u has a compatible deleter type.
  321. */
  322. template<typename _Up, typename _Ep, typename = _Require<
  323. __safe_conversion_up<_Up, _Ep>,
  324. __conditional_t<is_reference<_Dp>::value,
  325. is_same<_Ep, _Dp>,
  326. is_convertible<_Ep, _Dp>>>>
  327. _GLIBCXX23_CONSTEXPR
  328. unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
  329. : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
  330. { }
  331. #if _GLIBCXX_USE_DEPRECATED
  332. #pragma GCC diagnostic push
  333. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  334. /// Converting constructor from @c auto_ptr
  335. template<typename _Up, typename = _Require<
  336. is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
  337. unique_ptr(auto_ptr<_Up>&& __u) noexcept;
  338. #pragma GCC diagnostic pop
  339. #endif
  340. /// Destructor, invokes the deleter if the stored pointer is not null.
  341. #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
  342. constexpr
  343. #endif
  344. ~unique_ptr() noexcept
  345. {
  346. static_assert(__is_invocable<deleter_type&, pointer>::value,
  347. "unique_ptr's deleter must be invocable with a pointer");
  348. auto& __ptr = _M_t._M_ptr();
  349. if (__ptr != nullptr)
  350. get_deleter()(std::move(__ptr));
  351. __ptr = pointer();
  352. }
  353. // Assignment.
  354. /** @brief Move assignment operator.
  355. *
  356. * Invokes the deleter if this object owns a pointer.
  357. */
  358. unique_ptr& operator=(unique_ptr&&) = default;
  359. /** @brief Assignment from another type.
  360. *
  361. * @param __u The object to transfer ownership from, which owns a
  362. * convertible pointer to a non-array object.
  363. *
  364. * Invokes the deleter if this object owns a pointer.
  365. */
  366. template<typename _Up, typename _Ep>
  367. _GLIBCXX23_CONSTEXPR
  368. typename enable_if< __and_<
  369. __safe_conversion_up<_Up, _Ep>,
  370. is_assignable<deleter_type&, _Ep&&>
  371. >::value,
  372. unique_ptr&>::type
  373. operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  374. {
  375. reset(__u.release());
  376. get_deleter() = std::forward<_Ep>(__u.get_deleter());
  377. return *this;
  378. }
  379. /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
  380. _GLIBCXX23_CONSTEXPR
  381. unique_ptr&
  382. operator=(nullptr_t) noexcept
  383. {
  384. reset();
  385. return *this;
  386. }
  387. // Observers.
  388. /// Dereference the stored pointer.
  389. _GLIBCXX23_CONSTEXPR
  390. typename add_lvalue_reference<element_type>::type
  391. operator*() const noexcept(noexcept(*std::declval<pointer>()))
  392. {
  393. __glibcxx_assert(get() != pointer());
  394. return *get();
  395. }
  396. /// Return the stored pointer.
  397. _GLIBCXX23_CONSTEXPR
  398. pointer
  399. operator->() const noexcept
  400. {
  401. _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
  402. return get();
  403. }
  404. /// Return the stored pointer.
  405. _GLIBCXX23_CONSTEXPR
  406. pointer
  407. get() const noexcept
  408. { return _M_t._M_ptr(); }
  409. /// Return a reference to the stored deleter.
  410. _GLIBCXX23_CONSTEXPR
  411. deleter_type&
  412. get_deleter() noexcept
  413. { return _M_t._M_deleter(); }
  414. /// Return a reference to the stored deleter.
  415. _GLIBCXX23_CONSTEXPR
  416. const deleter_type&
  417. get_deleter() const noexcept
  418. { return _M_t._M_deleter(); }
  419. /// Return @c true if the stored pointer is not null.
  420. _GLIBCXX23_CONSTEXPR
  421. explicit operator bool() const noexcept
  422. { return get() == pointer() ? false : true; }
  423. // Modifiers.
  424. /// Release ownership of any stored pointer.
  425. _GLIBCXX23_CONSTEXPR
  426. pointer
  427. release() noexcept
  428. { return _M_t.release(); }
  429. /** @brief Replace the stored pointer.
  430. *
  431. * @param __p The new pointer to store.
  432. *
  433. * The deleter will be invoked if a pointer is already owned.
  434. */
  435. _GLIBCXX23_CONSTEXPR
  436. void
  437. reset(pointer __p = pointer()) noexcept
  438. {
  439. static_assert(__is_invocable<deleter_type&, pointer>::value,
  440. "unique_ptr's deleter must be invocable with a pointer");
  441. _M_t.reset(std::move(__p));
  442. }
  443. /// Exchange the pointer and deleter with another object.
  444. _GLIBCXX23_CONSTEXPR
  445. void
  446. swap(unique_ptr& __u) noexcept
  447. {
  448. static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
  449. _M_t.swap(__u._M_t);
  450. }
  451. // Disable copy from lvalue.
  452. unique_ptr(const unique_ptr&) = delete;
  453. unique_ptr& operator=(const unique_ptr&) = delete;
  454. };
  455. // 20.7.1.3 unique_ptr for array objects with a runtime length
  456. // [unique.ptr.runtime]
  457. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  458. // DR 740 - omit specialization for array objects with a compile time length
  459. /// A move-only smart pointer that manages unique ownership of an array.
  460. /// @headerfile memory
  461. /// @since C++11
  462. template<typename _Tp, typename _Dp>
  463. class unique_ptr<_Tp[], _Dp>
  464. {
  465. template <typename _Up>
  466. using _DeleterConstraint =
  467. typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
  468. __uniq_ptr_data<_Tp, _Dp> _M_t;
  469. // like is_base_of<_Tp, _Up> but false if unqualified types are the same
  470. template<typename _Up>
  471. using __is_derived_Tp
  472. = __and_< is_base_of<_Tp, _Up>,
  473. __not_<is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up>>> >;
  474. public:
  475. using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
  476. using element_type = _Tp;
  477. using deleter_type = _Dp;
  478. // helper template for detecting a safe conversion from another
  479. // unique_ptr
  480. template<typename _Up, typename _Ep,
  481. typename _UPtr = unique_ptr<_Up, _Ep>,
  482. typename _UP_pointer = typename _UPtr::pointer,
  483. typename _UP_element_type = typename _UPtr::element_type>
  484. using __safe_conversion_up = __and_<
  485. is_array<_Up>,
  486. is_same<pointer, element_type*>,
  487. is_same<_UP_pointer, _UP_element_type*>,
  488. is_convertible<_UP_element_type(*)[], element_type(*)[]>
  489. >;
  490. // helper template for detecting a safe conversion from a raw pointer
  491. template<typename _Up>
  492. using __safe_conversion_raw = __and_<
  493. __or_<__or_<is_same<_Up, pointer>,
  494. is_same<_Up, nullptr_t>>,
  495. __and_<is_pointer<_Up>,
  496. is_same<pointer, element_type*>,
  497. is_convertible<
  498. typename remove_pointer<_Up>::type(*)[],
  499. element_type(*)[]>
  500. >
  501. >
  502. >;
  503. // Constructors.
  504. /// Default constructor, creates a unique_ptr that owns nothing.
  505. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  506. constexpr unique_ptr() noexcept
  507. : _M_t()
  508. { }
  509. /** Takes ownership of a pointer.
  510. *
  511. * @param __p A pointer to an array of a type safely convertible
  512. * to an array of @c element_type
  513. *
  514. * The deleter will be value-initialized.
  515. */
  516. template<typename _Up,
  517. typename _Vp = _Dp,
  518. typename = _DeleterConstraint<_Vp>,
  519. typename = typename enable_if<
  520. __safe_conversion_raw<_Up>::value, bool>::type>
  521. _GLIBCXX23_CONSTEXPR
  522. explicit
  523. unique_ptr(_Up __p) noexcept
  524. : _M_t(__p)
  525. { }
  526. /** Takes ownership of a pointer.
  527. *
  528. * @param __p A pointer to an array of a type safely convertible
  529. * to an array of @c element_type
  530. * @param __d A reference to a deleter.
  531. *
  532. * The deleter will be initialized with @p __d
  533. */
  534. template<typename _Up, typename _Del = deleter_type,
  535. typename = _Require<__safe_conversion_raw<_Up>,
  536. is_copy_constructible<_Del>>>
  537. _GLIBCXX23_CONSTEXPR
  538. unique_ptr(_Up __p, const deleter_type& __d) noexcept
  539. : _M_t(__p, __d) { }
  540. /** Takes ownership of a pointer.
  541. *
  542. * @param __p A pointer to an array of a type safely convertible
  543. * to an array of @c element_type
  544. * @param __d A reference to a deleter.
  545. *
  546. * The deleter will be initialized with @p std::move(__d)
  547. */
  548. template<typename _Up, typename _Del = deleter_type,
  549. typename = _Require<__safe_conversion_raw<_Up>,
  550. is_move_constructible<_Del>>>
  551. _GLIBCXX23_CONSTEXPR
  552. unique_ptr(_Up __p,
  553. __enable_if_t<!is_lvalue_reference<_Del>::value,
  554. _Del&&> __d) noexcept
  555. : _M_t(std::move(__p), std::move(__d))
  556. { }
  557. template<typename _Up, typename _Del = deleter_type,
  558. typename _DelUnref = typename remove_reference<_Del>::type,
  559. typename = _Require<__safe_conversion_raw<_Up>>>
  560. unique_ptr(_Up,
  561. __enable_if_t<is_lvalue_reference<_Del>::value,
  562. _DelUnref&&>) = delete;
  563. /// Move constructor.
  564. unique_ptr(unique_ptr&&) = default;
  565. /// Creates a unique_ptr that owns nothing.
  566. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  567. constexpr unique_ptr(nullptr_t) noexcept
  568. : _M_t()
  569. { }
  570. template<typename _Up, typename _Ep, typename = _Require<
  571. __safe_conversion_up<_Up, _Ep>,
  572. __conditional_t<is_reference<_Dp>::value,
  573. is_same<_Ep, _Dp>,
  574. is_convertible<_Ep, _Dp>>>>
  575. _GLIBCXX23_CONSTEXPR
  576. unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
  577. : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
  578. { }
  579. /// Destructor, invokes the deleter if the stored pointer is not null.
  580. #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
  581. constexpr
  582. #endif
  583. ~unique_ptr()
  584. {
  585. auto& __ptr = _M_t._M_ptr();
  586. if (__ptr != nullptr)
  587. get_deleter()(__ptr);
  588. __ptr = pointer();
  589. }
  590. // Assignment.
  591. /** @brief Move assignment operator.
  592. *
  593. * Invokes the deleter if this object owns a pointer.
  594. */
  595. unique_ptr&
  596. operator=(unique_ptr&&) = default;
  597. /** @brief Assignment from another type.
  598. *
  599. * @param __u The object to transfer ownership from, which owns a
  600. * convertible pointer to an array object.
  601. *
  602. * Invokes the deleter if this object owns a pointer.
  603. */
  604. template<typename _Up, typename _Ep>
  605. _GLIBCXX23_CONSTEXPR
  606. typename
  607. enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
  608. is_assignable<deleter_type&, _Ep&&>
  609. >::value,
  610. unique_ptr&>::type
  611. operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  612. {
  613. reset(__u.release());
  614. get_deleter() = std::forward<_Ep>(__u.get_deleter());
  615. return *this;
  616. }
  617. /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
  618. _GLIBCXX23_CONSTEXPR
  619. unique_ptr&
  620. operator=(nullptr_t) noexcept
  621. {
  622. reset();
  623. return *this;
  624. }
  625. // Observers.
  626. /// Access an element of owned array.
  627. _GLIBCXX23_CONSTEXPR
  628. typename std::add_lvalue_reference<element_type>::type
  629. operator[](size_t __i) const
  630. {
  631. __glibcxx_assert(get() != pointer());
  632. return get()[__i];
  633. }
  634. /// Return the stored pointer.
  635. _GLIBCXX23_CONSTEXPR
  636. pointer
  637. get() const noexcept
  638. { return _M_t._M_ptr(); }
  639. /// Return a reference to the stored deleter.
  640. _GLIBCXX23_CONSTEXPR
  641. deleter_type&
  642. get_deleter() noexcept
  643. { return _M_t._M_deleter(); }
  644. /// Return a reference to the stored deleter.
  645. _GLIBCXX23_CONSTEXPR
  646. const deleter_type&
  647. get_deleter() const noexcept
  648. { return _M_t._M_deleter(); }
  649. /// Return @c true if the stored pointer is not null.
  650. _GLIBCXX23_CONSTEXPR
  651. explicit operator bool() const noexcept
  652. { return get() == pointer() ? false : true; }
  653. // Modifiers.
  654. /// Release ownership of any stored pointer.
  655. _GLIBCXX23_CONSTEXPR
  656. pointer
  657. release() noexcept
  658. { return _M_t.release(); }
  659. /** @brief Replace the stored pointer.
  660. *
  661. * @param __p The new pointer to store.
  662. *
  663. * The deleter will be invoked if a pointer is already owned.
  664. */
  665. template <typename _Up,
  666. typename = _Require<
  667. __or_<is_same<_Up, pointer>,
  668. __and_<is_same<pointer, element_type*>,
  669. is_pointer<_Up>,
  670. is_convertible<
  671. typename remove_pointer<_Up>::type(*)[],
  672. element_type(*)[]
  673. >
  674. >
  675. >
  676. >>
  677. _GLIBCXX23_CONSTEXPR
  678. void
  679. reset(_Up __p) noexcept
  680. { _M_t.reset(std::move(__p)); }
  681. _GLIBCXX23_CONSTEXPR
  682. void reset(nullptr_t = nullptr) noexcept
  683. { reset(pointer()); }
  684. /// Exchange the pointer and deleter with another object.
  685. _GLIBCXX23_CONSTEXPR
  686. void
  687. swap(unique_ptr& __u) noexcept
  688. {
  689. static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
  690. _M_t.swap(__u._M_t);
  691. }
  692. // Disable copy from lvalue.
  693. unique_ptr(const unique_ptr&) = delete;
  694. unique_ptr& operator=(const unique_ptr&) = delete;
  695. };
  696. /// @{
  697. /// @relates unique_ptr
  698. /// Swap overload for unique_ptr
  699. template<typename _Tp, typename _Dp>
  700. inline
  701. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  702. // Constrained free swap overload, see p0185r1
  703. _GLIBCXX23_CONSTEXPR
  704. typename enable_if<__is_swappable<_Dp>::value>::type
  705. #else
  706. void
  707. #endif
  708. swap(unique_ptr<_Tp, _Dp>& __x,
  709. unique_ptr<_Tp, _Dp>& __y) noexcept
  710. { __x.swap(__y); }
  711. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  712. template<typename _Tp, typename _Dp>
  713. typename enable_if<!__is_swappable<_Dp>::value>::type
  714. swap(unique_ptr<_Tp, _Dp>&,
  715. unique_ptr<_Tp, _Dp>&) = delete;
  716. #endif
  717. /// Equality operator for unique_ptr objects, compares the owned pointers
  718. template<typename _Tp, typename _Dp,
  719. typename _Up, typename _Ep>
  720. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  721. inline bool
  722. operator==(const unique_ptr<_Tp, _Dp>& __x,
  723. const unique_ptr<_Up, _Ep>& __y)
  724. { return __x.get() == __y.get(); }
  725. /// unique_ptr comparison with nullptr
  726. template<typename _Tp, typename _Dp>
  727. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  728. inline bool
  729. operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
  730. { return !__x; }
  731. #ifndef __cpp_lib_three_way_comparison
  732. /// unique_ptr comparison with nullptr
  733. template<typename _Tp, typename _Dp>
  734. _GLIBCXX_NODISCARD
  735. inline bool
  736. operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
  737. { return !__x; }
  738. /// Inequality operator for unique_ptr objects, compares the owned pointers
  739. template<typename _Tp, typename _Dp,
  740. typename _Up, typename _Ep>
  741. _GLIBCXX_NODISCARD
  742. inline bool
  743. operator!=(const unique_ptr<_Tp, _Dp>& __x,
  744. const unique_ptr<_Up, _Ep>& __y)
  745. { return __x.get() != __y.get(); }
  746. /// unique_ptr comparison with nullptr
  747. template<typename _Tp, typename _Dp>
  748. _GLIBCXX_NODISCARD
  749. inline bool
  750. operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
  751. { return (bool)__x; }
  752. /// unique_ptr comparison with nullptr
  753. template<typename _Tp, typename _Dp>
  754. _GLIBCXX_NODISCARD
  755. inline bool
  756. operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
  757. { return (bool)__x; }
  758. #endif // three way comparison
  759. /// Relational operator for unique_ptr objects, compares the owned pointers
  760. template<typename _Tp, typename _Dp,
  761. typename _Up, typename _Ep>
  762. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  763. inline bool
  764. operator<(const unique_ptr<_Tp, _Dp>& __x,
  765. const unique_ptr<_Up, _Ep>& __y)
  766. {
  767. typedef typename
  768. std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
  769. typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
  770. return std::less<_CT>()(__x.get(), __y.get());
  771. }
  772. /// unique_ptr comparison with nullptr
  773. template<typename _Tp, typename _Dp>
  774. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  775. inline bool
  776. operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  777. {
  778. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
  779. nullptr);
  780. }
  781. /// unique_ptr comparison with nullptr
  782. template<typename _Tp, typename _Dp>
  783. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  784. inline bool
  785. operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  786. {
  787. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
  788. __x.get());
  789. }
  790. /// Relational operator for unique_ptr objects, compares the owned pointers
  791. template<typename _Tp, typename _Dp,
  792. typename _Up, typename _Ep>
  793. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  794. inline bool
  795. operator<=(const unique_ptr<_Tp, _Dp>& __x,
  796. const unique_ptr<_Up, _Ep>& __y)
  797. { return !(__y < __x); }
  798. /// unique_ptr comparison with nullptr
  799. template<typename _Tp, typename _Dp>
  800. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  801. inline bool
  802. operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  803. { return !(nullptr < __x); }
  804. /// unique_ptr comparison with nullptr
  805. template<typename _Tp, typename _Dp>
  806. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  807. inline bool
  808. operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  809. { return !(__x < nullptr); }
  810. /// Relational operator for unique_ptr objects, compares the owned pointers
  811. template<typename _Tp, typename _Dp,
  812. typename _Up, typename _Ep>
  813. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  814. inline bool
  815. operator>(const unique_ptr<_Tp, _Dp>& __x,
  816. const unique_ptr<_Up, _Ep>& __y)
  817. { return (__y < __x); }
  818. /// unique_ptr comparison with nullptr
  819. template<typename _Tp, typename _Dp>
  820. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  821. inline bool
  822. operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  823. {
  824. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
  825. __x.get());
  826. }
  827. /// unique_ptr comparison with nullptr
  828. template<typename _Tp, typename _Dp>
  829. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  830. inline bool
  831. operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  832. {
  833. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
  834. nullptr);
  835. }
  836. /// Relational operator for unique_ptr objects, compares the owned pointers
  837. template<typename _Tp, typename _Dp,
  838. typename _Up, typename _Ep>
  839. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  840. inline bool
  841. operator>=(const unique_ptr<_Tp, _Dp>& __x,
  842. const unique_ptr<_Up, _Ep>& __y)
  843. { return !(__x < __y); }
  844. /// unique_ptr comparison with nullptr
  845. template<typename _Tp, typename _Dp>
  846. _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
  847. inline bool
  848. operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  849. { return !(__x < nullptr); }
  850. /// unique_ptr comparison with nullptr
  851. template<typename _Tp, typename _Dp>
  852. _GLIBCXX_NODISCARD inline bool
  853. operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  854. { return !(nullptr < __x); }
  855. #ifdef __cpp_lib_three_way_comparison
  856. template<typename _Tp, typename _Dp, typename _Up, typename _Ep>
  857. requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
  858. typename unique_ptr<_Up, _Ep>::pointer>
  859. _GLIBCXX23_CONSTEXPR
  860. inline
  861. compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
  862. typename unique_ptr<_Up, _Ep>::pointer>
  863. operator<=>(const unique_ptr<_Tp, _Dp>& __x,
  864. const unique_ptr<_Up, _Ep>& __y)
  865. { return compare_three_way()(__x.get(), __y.get()); }
  866. template<typename _Tp, typename _Dp>
  867. requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
  868. _GLIBCXX23_CONSTEXPR
  869. inline
  870. compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
  871. operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  872. {
  873. using pointer = typename unique_ptr<_Tp, _Dp>::pointer;
  874. return compare_three_way()(__x.get(), static_cast<pointer>(nullptr));
  875. }
  876. #endif
  877. /// @} relates unique_ptr
  878. /// @cond undocumented
  879. template<typename _Up, typename _Ptr = typename _Up::pointer,
  880. bool = __poison_hash<_Ptr>::__enable_hash_call>
  881. struct __uniq_ptr_hash
  882. #if ! _GLIBCXX_INLINE_VERSION
  883. : private __poison_hash<_Ptr>
  884. #endif
  885. {
  886. size_t
  887. operator()(const _Up& __u) const
  888. noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>())))
  889. { return hash<_Ptr>()(__u.get()); }
  890. };
  891. template<typename _Up, typename _Ptr>
  892. struct __uniq_ptr_hash<_Up, _Ptr, false>
  893. : private __poison_hash<_Ptr>
  894. { };
  895. /// @endcond
  896. /// std::hash specialization for unique_ptr.
  897. template<typename _Tp, typename _Dp>
  898. struct hash<unique_ptr<_Tp, _Dp>>
  899. : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
  900. public __uniq_ptr_hash<unique_ptr<_Tp, _Dp>>
  901. { };
  902. #if __cplusplus >= 201402L && _GLIBCXX_HOSTED
  903. #define __cpp_lib_make_unique 201304L
  904. /// @cond undocumented
  905. namespace __detail
  906. {
  907. template<typename _Tp>
  908. struct _MakeUniq
  909. { typedef unique_ptr<_Tp> __single_object; };
  910. template<typename _Tp>
  911. struct _MakeUniq<_Tp[]>
  912. { typedef unique_ptr<_Tp[]> __array; };
  913. template<typename _Tp, size_t _Bound>
  914. struct _MakeUniq<_Tp[_Bound]>
  915. { struct __invalid_type { }; };
  916. template<typename _Tp>
  917. using __unique_ptr_t = typename _MakeUniq<_Tp>::__single_object;
  918. template<typename _Tp>
  919. using __unique_ptr_array_t = typename _MakeUniq<_Tp>::__array;
  920. template<typename _Tp>
  921. using __invalid_make_unique_t = typename _MakeUniq<_Tp>::__invalid_type;
  922. }
  923. /// @endcond
  924. /** Create an object owned by a `unique_ptr`.
  925. * @tparam _Tp A non-array object type.
  926. * @param __args Constructor arguments for the new object.
  927. * @returns A `unique_ptr<_Tp>` that owns the new object.
  928. * @since C++14
  929. * @relates unique_ptr
  930. */
  931. template<typename _Tp, typename... _Args>
  932. _GLIBCXX23_CONSTEXPR
  933. inline __detail::__unique_ptr_t<_Tp>
  934. make_unique(_Args&&... __args)
  935. { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
  936. /** Create an array owned by a `unique_ptr`.
  937. * @tparam _Tp An array type of unknown bound, such as `U[]`.
  938. * @param __num The number of elements of type `U` in the new array.
  939. * @returns A `unique_ptr<U[]>` that owns the new array.
  940. * @since C++14
  941. * @relates unique_ptr
  942. *
  943. * The array elements are value-initialized.
  944. */
  945. template<typename _Tp>
  946. _GLIBCXX23_CONSTEXPR
  947. inline __detail::__unique_ptr_array_t<_Tp>
  948. make_unique(size_t __num)
  949. { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
  950. /** Disable std::make_unique for arrays of known bound.
  951. * @tparam _Tp An array type of known bound, such as `U[N]`.
  952. * @since C++14
  953. * @relates unique_ptr
  954. */
  955. template<typename _Tp, typename... _Args>
  956. __detail::__invalid_make_unique_t<_Tp>
  957. make_unique(_Args&&...) = delete;
  958. #if __cplusplus > 201703L
  959. /** Create a default-initialied object owned by a `unique_ptr`.
  960. * @tparam _Tp A non-array object type.
  961. * @returns A `unique_ptr<_Tp>` that owns the new object.
  962. * @since C++20
  963. * @relates unique_ptr
  964. */
  965. template<typename _Tp>
  966. _GLIBCXX23_CONSTEXPR
  967. inline __detail::__unique_ptr_t<_Tp>
  968. make_unique_for_overwrite()
  969. { return unique_ptr<_Tp>(new _Tp); }
  970. /** Create a default-initialized array owned by a `unique_ptr`.
  971. * @tparam _Tp An array type of unknown bound, such as `U[]`.
  972. * @param __num The number of elements of type `U` in the new array.
  973. * @returns A `unique_ptr<U[]>` that owns the new array.
  974. * @since C++20
  975. * @relates unique_ptr
  976. */
  977. template<typename _Tp>
  978. _GLIBCXX23_CONSTEXPR
  979. inline __detail::__unique_ptr_array_t<_Tp>
  980. make_unique_for_overwrite(size_t __num)
  981. { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]); }
  982. /** Disable std::make_unique_for_overwrite for arrays of known bound.
  983. * @tparam _Tp An array type of known bound, such as `U[N]`.
  984. * @since C++20
  985. * @relates unique_ptr
  986. */
  987. template<typename _Tp, typename... _Args>
  988. __detail::__invalid_make_unique_t<_Tp>
  989. make_unique_for_overwrite(_Args&&...) = delete;
  990. #endif // C++20
  991. #endif // C++14 && HOSTED
  992. #if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
  993. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  994. // 2948. unique_ptr does not define operator<< for stream output
  995. /// Stream output operator for unique_ptr
  996. /// @relates unique_ptr
  997. /// @since C++20
  998. template<typename _CharT, typename _Traits, typename _Tp, typename _Dp>
  999. inline basic_ostream<_CharT, _Traits>&
  1000. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1001. const unique_ptr<_Tp, _Dp>& __p)
  1002. requires requires { __os << __p.get(); }
  1003. {
  1004. __os << __p.get();
  1005. return __os;
  1006. }
  1007. #endif // C++20 && HOSTED
  1008. /// @} group pointer_abstractions
  1009. #if __cplusplus >= 201703L
  1010. namespace __detail::__variant
  1011. {
  1012. template<typename> struct _Never_valueless_alt; // see <variant>
  1013. // Provide the strong exception-safety guarantee when emplacing a
  1014. // unique_ptr into a variant.
  1015. template<typename _Tp, typename _Del>
  1016. struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
  1017. : std::true_type
  1018. { };
  1019. } // namespace __detail::__variant
  1020. #endif // C++17
  1021. _GLIBCXX_END_NAMESPACE_VERSION
  1022. } // namespace
  1023. #endif /* _UNIQUE_PTR_H */