unique_ptr.h 36 KB

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