unique_ptr.h 31 KB

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