unique_ptr.h 27 KB

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