unique_ptr.h 33 KB

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