shared_ptr.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // shared_ptr and weak_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2007-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. // GCC Note: Based on files from version 1.32.0 of the Boost library.
  21. // shared_count.hpp
  22. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  23. // shared_ptr.hpp
  24. // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
  25. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  26. // weak_ptr.hpp
  27. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  28. // enable_shared_from_this.hpp
  29. // Copyright (C) 2002 Peter Dimov
  30. // Distributed under the Boost Software License, Version 1.0. (See
  31. // accompanying file LICENSE_1_0.txt or copy at
  32. // http://www.boost.org/LICENSE_1_0.txt)
  33. /** @file
  34. * This is an internal header file, included by other library headers.
  35. * Do not attempt to use it directly. @headername{memory}
  36. */
  37. #ifndef _SHARED_PTR_H
  38. #define _SHARED_PTR_H 1
  39. #include <iosfwd> // std::basic_ostream
  40. #include <bits/shared_ptr_base.h>
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. /**
  45. * @addtogroup pointer_abstractions
  46. * @{
  47. */
  48. // 20.7.2.2.11 shared_ptr I/O
  49. /// Write the stored pointer to an ostream.
  50. /// @relates shared_ptr
  51. template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
  52. inline std::basic_ostream<_Ch, _Tr>&
  53. operator<<(std::basic_ostream<_Ch, _Tr>& __os,
  54. const __shared_ptr<_Tp, _Lp>& __p)
  55. {
  56. __os << __p.get();
  57. return __os;
  58. }
  59. template<typename _Del, typename _Tp, _Lock_policy _Lp>
  60. inline _Del*
  61. get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
  62. {
  63. #if __cpp_rtti
  64. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  65. #else
  66. return 0;
  67. #endif
  68. }
  69. /// 20.7.2.2.10 shared_ptr get_deleter
  70. /// If `__p` has a deleter of type `_Del`, return a pointer to it.
  71. /// @relates shared_ptr
  72. template<typename _Del, typename _Tp>
  73. inline _Del*
  74. get_deleter(const shared_ptr<_Tp>& __p) noexcept
  75. {
  76. #if __cpp_rtti
  77. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  78. #else
  79. return 0;
  80. #endif
  81. }
  82. /// @cond undocumented
  83. // Constraint for overloads taking non-array types.
  84. #if __cpp_concepts && __cpp_lib_type_trait_variable_templates
  85. template<typename _Tp>
  86. requires (!is_array_v<_Tp>)
  87. using _NonArray = _Tp;
  88. #else
  89. template<typename _Tp>
  90. using _NonArray = __enable_if_t<!is_array<_Tp>::value, _Tp>;
  91. #endif
  92. #if __cpp_lib_shared_ptr_arrays >= 201707L
  93. // Constraint for overloads taking array types with unknown bound, U[].
  94. #if __cpp_concepts
  95. template<typename _Tp>
  96. requires is_array_v<_Tp> && (extent_v<_Tp> == 0)
  97. using _UnboundedArray = _Tp;
  98. #else
  99. template<typename _Tp>
  100. using _UnboundedArray
  101. = __enable_if_t<__is_array_unknown_bounds<_Tp>::value, _Tp>;
  102. #endif
  103. // Constraint for overloads taking array types with known bound, U[N].
  104. #if __cpp_concepts
  105. template<typename _Tp>
  106. requires (extent_v<_Tp> != 0)
  107. using _BoundedArray = _Tp;
  108. #else
  109. template<typename _Tp>
  110. using _BoundedArray
  111. = __enable_if_t<__is_array_known_bounds<_Tp>::value, _Tp>;
  112. #endif
  113. #if __cpp_lib_smart_ptr_for_overwrite
  114. // Constraint for overloads taking either non-array or bounded array, U[N].
  115. #if __cpp_concepts
  116. template<typename _Tp>
  117. requires (!is_array_v<_Tp>) || (extent_v<_Tp> != 0)
  118. using _NotUnboundedArray = _Tp;
  119. #else
  120. template<typename _Tp>
  121. using _NotUnboundedArray
  122. = __enable_if_t<!__is_array_unknown_bounds<_Tp>::value, _Tp>;
  123. #endif
  124. #endif // smart_ptr_for_overwrite
  125. #endif // shared_ptr_arrays
  126. /// @endcond
  127. /**
  128. * @brief A smart pointer with reference-counted copy semantics.
  129. * @headerfile memory
  130. * @since C++11
  131. *
  132. * A `shared_ptr` object is either empty or _owns_ a pointer passed
  133. * to the constructor. Copies of a `shared_ptr` share ownership of
  134. * the same pointer. When the last `shared_ptr` that owns the pointer
  135. * is destroyed or reset, the owned pointer is freed (either by `delete`
  136. * or by invoking a custom deleter that was passed to the constructor).
  137. *
  138. * A `shared_ptr` also stores another pointer, which is usually
  139. * (but not always) the same pointer as it owns. The stored pointer
  140. * can be retrieved by calling the `get()` member function.
  141. *
  142. * The equality and relational operators for `shared_ptr` only compare
  143. * the stored pointer returned by `get()`, not the owned pointer.
  144. * To test whether two `shared_ptr` objects share ownership of the same
  145. * pointer see `std::shared_ptr::owner_before` and `std::owner_less`.
  146. */
  147. template<typename _Tp>
  148. class shared_ptr : public __shared_ptr<_Tp>
  149. {
  150. template<typename... _Args>
  151. using _Constructible = typename enable_if<
  152. is_constructible<__shared_ptr<_Tp>, _Args...>::value
  153. >::type;
  154. template<typename _Arg>
  155. using _Assignable = typename enable_if<
  156. is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
  157. >::type;
  158. public:
  159. /// The type pointed to by the stored pointer, remove_extent_t<_Tp>
  160. using element_type = typename __shared_ptr<_Tp>::element_type;
  161. #if __cplusplus >= 201703L
  162. # define __cpp_lib_shared_ptr_weak_type 201606L
  163. /// The corresponding weak_ptr type for this shared_ptr
  164. /// @since C++17
  165. using weak_type = weak_ptr<_Tp>;
  166. #endif
  167. /**
  168. * @brief Construct an empty %shared_ptr.
  169. * @post use_count()==0 && get()==0
  170. */
  171. constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
  172. shared_ptr(const shared_ptr&) noexcept = default; ///< Copy constructor
  173. /**
  174. * @brief Construct a %shared_ptr that owns the pointer @a __p.
  175. * @param __p A pointer that is convertible to element_type*.
  176. * @post use_count() == 1 && get() == __p
  177. * @throw std::bad_alloc, in which case @c delete @a __p is called.
  178. */
  179. template<typename _Yp, typename = _Constructible<_Yp*>>
  180. explicit
  181. shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
  182. /**
  183. * @brief Construct a %shared_ptr that owns the pointer @a __p
  184. * and the deleter @a __d.
  185. * @param __p A pointer.
  186. * @param __d A deleter.
  187. * @post use_count() == 1 && get() == __p
  188. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  189. *
  190. * Requirements: _Deleter's copy constructor and destructor must
  191. * not throw
  192. *
  193. * __shared_ptr will release __p by calling __d(__p)
  194. */
  195. template<typename _Yp, typename _Deleter,
  196. typename = _Constructible<_Yp*, _Deleter>>
  197. shared_ptr(_Yp* __p, _Deleter __d)
  198. : __shared_ptr<_Tp>(__p, std::move(__d)) { }
  199. /**
  200. * @brief Construct a %shared_ptr that owns a null pointer
  201. * and the deleter @a __d.
  202. * @param __p A null pointer constant.
  203. * @param __d A deleter.
  204. * @post use_count() == 1 && get() == __p
  205. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  206. *
  207. * Requirements: _Deleter's copy constructor and destructor must
  208. * not throw
  209. *
  210. * The last owner will call __d(__p)
  211. */
  212. template<typename _Deleter>
  213. shared_ptr(nullptr_t __p, _Deleter __d)
  214. : __shared_ptr<_Tp>(__p, std::move(__d)) { }
  215. /**
  216. * @brief Construct a %shared_ptr that owns the pointer @a __p
  217. * and the deleter @a __d.
  218. * @param __p A pointer.
  219. * @param __d A deleter.
  220. * @param __a An allocator.
  221. * @post use_count() == 1 && get() == __p
  222. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  223. *
  224. * Requirements: _Deleter's copy constructor and destructor must
  225. * not throw _Alloc's copy constructor and destructor must not
  226. * throw.
  227. *
  228. * __shared_ptr will release __p by calling __d(__p)
  229. */
  230. template<typename _Yp, typename _Deleter, typename _Alloc,
  231. typename = _Constructible<_Yp*, _Deleter, _Alloc>>
  232. shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
  233. : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
  234. /**
  235. * @brief Construct a %shared_ptr that owns a null pointer
  236. * and the deleter @a __d.
  237. * @param __p A null pointer constant.
  238. * @param __d A deleter.
  239. * @param __a An allocator.
  240. * @post use_count() == 1 && get() == __p
  241. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  242. *
  243. * Requirements: _Deleter's copy constructor and destructor must
  244. * not throw _Alloc's copy constructor and destructor must not
  245. * throw.
  246. *
  247. * The last owner will call __d(__p)
  248. */
  249. template<typename _Deleter, typename _Alloc>
  250. shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
  251. : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
  252. // Aliasing constructor
  253. /**
  254. * @brief Constructs a `shared_ptr` instance that stores `__p`
  255. * and shares ownership with `__r`.
  256. * @param __r A `shared_ptr`.
  257. * @param __p A pointer that will remain valid while `*__r` is valid.
  258. * @post `get() == __p && use_count() == __r.use_count()`
  259. *
  260. * This can be used to construct a `shared_ptr` to a sub-object
  261. * of an object managed by an existing `shared_ptr`. The complete
  262. * object will remain valid while any `shared_ptr` owns it, even
  263. * if they don't store a pointer to the complete object.
  264. *
  265. * @code
  266. * shared_ptr<pair<int,int>> pii(new pair<int,int>());
  267. * shared_ptr<int> pi(pii, &pii->first);
  268. * assert(pii.use_count() == 2);
  269. * @endcode
  270. */
  271. template<typename _Yp>
  272. shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
  273. : __shared_ptr<_Tp>(__r, __p) { }
  274. #if __cplusplus > 201703L
  275. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  276. // 2996. Missing rvalue overloads for shared_ptr operations
  277. /**
  278. * @brief Constructs a `shared_ptr` instance that stores `__p`
  279. * and shares ownership with `__r`.
  280. * @param __r A `shared_ptr`.
  281. * @param __p A pointer that will remain valid while `*__r` is valid.
  282. * @post `get() == __p && !__r.use_count() && !__r.get()`
  283. * @since C++17
  284. *
  285. * This can be used to construct a `shared_ptr` to a sub-object
  286. * of an object managed by an existing `shared_ptr`. The complete
  287. * object will remain valid while any `shared_ptr` owns it, even
  288. * if they don't store a pointer to the complete object.
  289. *
  290. * @code
  291. * shared_ptr<pair<int,int>> pii(new pair<int,int>());
  292. * shared_ptr<int> pi1(pii, &pii->first);
  293. * assert(pii.use_count() == 2);
  294. * shared_ptr<int> pi2(std::move(pii), &pii->second);
  295. * assert(pii.use_count() == 0);
  296. * @endcode
  297. */
  298. template<typename _Yp>
  299. shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept
  300. : __shared_ptr<_Tp>(std::move(__r), __p) { }
  301. #endif
  302. /**
  303. * @brief If @a __r is empty, constructs an empty %shared_ptr;
  304. * otherwise construct a %shared_ptr that shares ownership
  305. * with @a __r.
  306. * @param __r A %shared_ptr.
  307. * @post get() == __r.get() && use_count() == __r.use_count()
  308. */
  309. template<typename _Yp,
  310. typename = _Constructible<const shared_ptr<_Yp>&>>
  311. shared_ptr(const shared_ptr<_Yp>& __r) noexcept
  312. : __shared_ptr<_Tp>(__r) { }
  313. /**
  314. * @brief Move-constructs a %shared_ptr instance from @a __r.
  315. * @param __r A %shared_ptr rvalue.
  316. * @post *this contains the old value of @a __r, @a __r is empty.
  317. */
  318. shared_ptr(shared_ptr&& __r) noexcept
  319. : __shared_ptr<_Tp>(std::move(__r)) { }
  320. /**
  321. * @brief Move-constructs a %shared_ptr instance from @a __r.
  322. * @param __r A %shared_ptr rvalue.
  323. * @post *this contains the old value of @a __r, @a __r is empty.
  324. */
  325. template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
  326. shared_ptr(shared_ptr<_Yp>&& __r) noexcept
  327. : __shared_ptr<_Tp>(std::move(__r)) { }
  328. /**
  329. * @brief Constructs a %shared_ptr that shares ownership with @a __r
  330. * and stores a copy of the pointer stored in @a __r.
  331. * @param __r A weak_ptr.
  332. * @post use_count() == __r.use_count()
  333. * @throw bad_weak_ptr when __r.expired(),
  334. * in which case the constructor has no effect.
  335. */
  336. template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
  337. explicit shared_ptr(const weak_ptr<_Yp>& __r)
  338. : __shared_ptr<_Tp>(__r) { }
  339. #if _GLIBCXX_USE_DEPRECATED
  340. #pragma GCC diagnostic push
  341. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  342. template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
  343. shared_ptr(auto_ptr<_Yp>&& __r);
  344. #pragma GCC diagnostic pop
  345. #endif
  346. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  347. // 2399. shared_ptr's constructor from unique_ptr should be constrained
  348. template<typename _Yp, typename _Del,
  349. typename = _Constructible<unique_ptr<_Yp, _Del>>>
  350. shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  351. : __shared_ptr<_Tp>(std::move(__r)) { }
  352. #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
  353. // This non-standard constructor exists to support conversions that
  354. // were possible in C++11 and C++14 but are ill-formed in C++17.
  355. // If an exception is thrown this constructor has no effect.
  356. template<typename _Yp, typename _Del,
  357. _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
  358. shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  359. : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
  360. #endif
  361. /**
  362. * @brief Construct an empty %shared_ptr.
  363. * @post use_count() == 0 && get() == nullptr
  364. */
  365. constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
  366. shared_ptr& operator=(const shared_ptr&) noexcept = default;
  367. template<typename _Yp>
  368. _Assignable<const shared_ptr<_Yp>&>
  369. operator=(const shared_ptr<_Yp>& __r) noexcept
  370. {
  371. this->__shared_ptr<_Tp>::operator=(__r);
  372. return *this;
  373. }
  374. #if _GLIBCXX_USE_DEPRECATED
  375. #pragma GCC diagnostic push
  376. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  377. template<typename _Yp>
  378. _Assignable<auto_ptr<_Yp>>
  379. operator=(auto_ptr<_Yp>&& __r)
  380. {
  381. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  382. return *this;
  383. }
  384. #pragma GCC diagnostic pop
  385. #endif
  386. shared_ptr&
  387. operator=(shared_ptr&& __r) noexcept
  388. {
  389. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  390. return *this;
  391. }
  392. template<class _Yp>
  393. _Assignable<shared_ptr<_Yp>>
  394. operator=(shared_ptr<_Yp>&& __r) noexcept
  395. {
  396. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  397. return *this;
  398. }
  399. template<typename _Yp, typename _Del>
  400. _Assignable<unique_ptr<_Yp, _Del>>
  401. operator=(unique_ptr<_Yp, _Del>&& __r)
  402. {
  403. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  404. return *this;
  405. }
  406. private:
  407. // This constructor is non-standard, it is used by allocate_shared.
  408. template<typename _Alloc, typename... _Args>
  409. shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
  410. : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
  411. { }
  412. template<typename _Yp, typename _Alloc, typename... _Args>
  413. friend shared_ptr<_NonArray<_Yp>>
  414. allocate_shared(const _Alloc&, _Args&&...);
  415. template<typename _Yp, typename... _Args>
  416. friend shared_ptr<_NonArray<_Yp>>
  417. make_shared(_Args&&...);
  418. #if __cpp_lib_shared_ptr_arrays >= 201707L
  419. // This constructor is non-standard, it is used by allocate_shared<T[]>.
  420. template<typename _Alloc, typename _Init = const remove_extent_t<_Tp>*>
  421. shared_ptr(const _Sp_counted_array_base<_Alloc>& __a,
  422. _Init __init = nullptr)
  423. : __shared_ptr<_Tp>(__a, __init)
  424. { }
  425. template<typename _Yp, typename _Alloc>
  426. friend shared_ptr<_UnboundedArray<_Yp>>
  427. allocate_shared(const _Alloc&, size_t);
  428. template<typename _Yp>
  429. friend shared_ptr<_UnboundedArray<_Yp>>
  430. make_shared(size_t);
  431. template<typename _Yp, typename _Alloc>
  432. friend shared_ptr<_UnboundedArray<_Yp>>
  433. allocate_shared(const _Alloc&, size_t, const remove_extent_t<_Yp>&);
  434. template<typename _Yp>
  435. friend shared_ptr<_UnboundedArray<_Yp>>
  436. make_shared(size_t, const remove_extent_t<_Yp>&);
  437. template<typename _Yp, typename _Alloc>
  438. friend shared_ptr<_BoundedArray<_Yp>>
  439. allocate_shared(const _Alloc&);
  440. template<typename _Yp>
  441. friend shared_ptr<_BoundedArray<_Yp>>
  442. make_shared();
  443. template<typename _Yp, typename _Alloc>
  444. friend shared_ptr<_BoundedArray<_Yp>>
  445. allocate_shared(const _Alloc&, const remove_extent_t<_Yp>&);
  446. template<typename _Yp>
  447. friend shared_ptr<_BoundedArray<_Yp>>
  448. make_shared(const remove_extent_t<_Yp>&);
  449. #if __cpp_lib_smart_ptr_for_overwrite
  450. template<typename _Yp, typename _Alloc>
  451. friend shared_ptr<_NotUnboundedArray<_Yp>>
  452. allocate_shared_for_overwrite(const _Alloc&);
  453. template<typename _Yp>
  454. friend shared_ptr<_NotUnboundedArray<_Yp>>
  455. make_shared_for_overwrite();
  456. template<typename _Yp, typename _Alloc>
  457. friend shared_ptr<_UnboundedArray<_Yp>>
  458. allocate_shared_for_overwrite(const _Alloc&, size_t);
  459. template<typename _Yp>
  460. friend shared_ptr<_UnboundedArray<_Yp>>
  461. make_shared_for_overwrite(size_t);
  462. #endif
  463. #endif
  464. // This constructor is non-standard, it is used by weak_ptr::lock().
  465. shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) noexcept
  466. : __shared_ptr<_Tp>(__r, std::nothrow) { }
  467. friend class weak_ptr<_Tp>;
  468. };
  469. #if __cpp_deduction_guides >= 201606
  470. template<typename _Tp>
  471. shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
  472. template<typename _Tp, typename _Del>
  473. shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
  474. #endif
  475. // 20.7.2.2.7 shared_ptr comparisons
  476. /// @relates shared_ptr @{
  477. /// Equality operator for shared_ptr objects, compares the stored pointers
  478. template<typename _Tp, typename _Up>
  479. _GLIBCXX_NODISCARD inline bool
  480. operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  481. { return __a.get() == __b.get(); }
  482. /// shared_ptr comparison with nullptr
  483. template<typename _Tp>
  484. _GLIBCXX_NODISCARD inline bool
  485. operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  486. { return !__a; }
  487. #ifdef __cpp_lib_three_way_comparison
  488. template<typename _Tp, typename _Up>
  489. inline strong_ordering
  490. operator<=>(const shared_ptr<_Tp>& __a,
  491. const shared_ptr<_Up>& __b) noexcept
  492. { return compare_three_way()(__a.get(), __b.get()); }
  493. template<typename _Tp>
  494. inline strong_ordering
  495. operator<=>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  496. {
  497. using pointer = typename shared_ptr<_Tp>::element_type*;
  498. return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
  499. }
  500. #else
  501. /// shared_ptr comparison with nullptr
  502. template<typename _Tp>
  503. _GLIBCXX_NODISCARD inline bool
  504. operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  505. { return !__a; }
  506. /// Inequality operator for shared_ptr objects, compares the stored pointers
  507. template<typename _Tp, typename _Up>
  508. _GLIBCXX_NODISCARD inline bool
  509. operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  510. { return __a.get() != __b.get(); }
  511. /// shared_ptr comparison with nullptr
  512. template<typename _Tp>
  513. _GLIBCXX_NODISCARD inline bool
  514. operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  515. { return (bool)__a; }
  516. /// shared_ptr comparison with nullptr
  517. template<typename _Tp>
  518. _GLIBCXX_NODISCARD inline bool
  519. operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  520. { return (bool)__a; }
  521. /// Relational operator for shared_ptr objects, compares the stored pointers
  522. template<typename _Tp, typename _Up>
  523. _GLIBCXX_NODISCARD inline bool
  524. operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  525. {
  526. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  527. using _Up_elt = typename shared_ptr<_Up>::element_type;
  528. using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
  529. return less<_Vp>()(__a.get(), __b.get());
  530. }
  531. /// shared_ptr comparison with nullptr
  532. template<typename _Tp>
  533. _GLIBCXX_NODISCARD inline bool
  534. operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  535. {
  536. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  537. return less<_Tp_elt*>()(__a.get(), nullptr);
  538. }
  539. /// shared_ptr comparison with nullptr
  540. template<typename _Tp>
  541. _GLIBCXX_NODISCARD inline bool
  542. operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  543. {
  544. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  545. return less<_Tp_elt*>()(nullptr, __a.get());
  546. }
  547. /// Relational operator for shared_ptr objects, compares the stored pointers
  548. template<typename _Tp, typename _Up>
  549. _GLIBCXX_NODISCARD inline bool
  550. operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  551. { return !(__b < __a); }
  552. /// shared_ptr comparison with nullptr
  553. template<typename _Tp>
  554. _GLIBCXX_NODISCARD inline bool
  555. operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  556. { return !(nullptr < __a); }
  557. /// shared_ptr comparison with nullptr
  558. template<typename _Tp>
  559. _GLIBCXX_NODISCARD inline bool
  560. operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  561. { return !(__a < nullptr); }
  562. /// Relational operator for shared_ptr objects, compares the stored pointers
  563. template<typename _Tp, typename _Up>
  564. _GLIBCXX_NODISCARD inline bool
  565. operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  566. { return (__b < __a); }
  567. /// shared_ptr comparison with nullptr
  568. template<typename _Tp>
  569. _GLIBCXX_NODISCARD inline bool
  570. operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  571. { return nullptr < __a; }
  572. /// shared_ptr comparison with nullptr
  573. template<typename _Tp>
  574. _GLIBCXX_NODISCARD inline bool
  575. operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  576. { return __a < nullptr; }
  577. /// Relational operator for shared_ptr objects, compares the stored pointers
  578. template<typename _Tp, typename _Up>
  579. _GLIBCXX_NODISCARD inline bool
  580. operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  581. { return !(__a < __b); }
  582. /// shared_ptr comparison with nullptr
  583. template<typename _Tp>
  584. _GLIBCXX_NODISCARD inline bool
  585. operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  586. { return !(__a < nullptr); }
  587. /// shared_ptr comparison with nullptr
  588. template<typename _Tp>
  589. _GLIBCXX_NODISCARD inline bool
  590. operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  591. { return !(nullptr < __a); }
  592. #endif
  593. // 20.7.2.2.8 shared_ptr specialized algorithms.
  594. /// Swap overload for shared_ptr
  595. template<typename _Tp>
  596. inline void
  597. swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
  598. { __a.swap(__b); }
  599. // 20.7.2.2.9 shared_ptr casts.
  600. /// Convert type of `shared_ptr`, via `static_cast`
  601. template<typename _Tp, typename _Up>
  602. inline shared_ptr<_Tp>
  603. static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  604. {
  605. using _Sp = shared_ptr<_Tp>;
  606. return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
  607. }
  608. /// Convert type of `shared_ptr`, via `const_cast`
  609. template<typename _Tp, typename _Up>
  610. inline shared_ptr<_Tp>
  611. const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  612. {
  613. using _Sp = shared_ptr<_Tp>;
  614. return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
  615. }
  616. /// Convert type of `shared_ptr`, via `dynamic_cast`
  617. template<typename _Tp, typename _Up>
  618. inline shared_ptr<_Tp>
  619. dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  620. {
  621. using _Sp = shared_ptr<_Tp>;
  622. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  623. return _Sp(__r, __p);
  624. return _Sp();
  625. }
  626. #if __cplusplus >= 201703L
  627. /// Convert type of `shared_ptr`, via `reinterpret_cast`
  628. /// @since C++17
  629. template<typename _Tp, typename _Up>
  630. inline shared_ptr<_Tp>
  631. reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  632. {
  633. using _Sp = shared_ptr<_Tp>;
  634. return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  635. }
  636. #if __cplusplus > 201703L
  637. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  638. // 2996. Missing rvalue overloads for shared_ptr operations
  639. /// Convert type of `shared_ptr` rvalue, via `static_cast`
  640. /// @since C++20
  641. template<typename _Tp, typename _Up>
  642. inline shared_ptr<_Tp>
  643. static_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  644. {
  645. using _Sp = shared_ptr<_Tp>;
  646. return _Sp(std::move(__r),
  647. static_cast<typename _Sp::element_type*>(__r.get()));
  648. }
  649. /// Convert type of `shared_ptr` rvalue, via `const_cast`
  650. /// @since C++20
  651. template<typename _Tp, typename _Up>
  652. inline shared_ptr<_Tp>
  653. const_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  654. {
  655. using _Sp = shared_ptr<_Tp>;
  656. return _Sp(std::move(__r),
  657. const_cast<typename _Sp::element_type*>(__r.get()));
  658. }
  659. /// Convert type of `shared_ptr` rvalue, via `dynamic_cast`
  660. /// @since C++20
  661. template<typename _Tp, typename _Up>
  662. inline shared_ptr<_Tp>
  663. dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  664. {
  665. using _Sp = shared_ptr<_Tp>;
  666. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  667. return _Sp(std::move(__r), __p);
  668. return _Sp();
  669. }
  670. /// Convert type of `shared_ptr` rvalue, via `reinterpret_cast`
  671. /// @since C++20
  672. template<typename _Tp, typename _Up>
  673. inline shared_ptr<_Tp>
  674. reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  675. {
  676. using _Sp = shared_ptr<_Tp>;
  677. return _Sp(std::move(__r),
  678. reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  679. }
  680. #endif // C++20
  681. #endif // C++17
  682. /// @}
  683. /**
  684. * @brief A non-owning observer for a pointer owned by a shared_ptr
  685. * @headerfile memory
  686. * @since C++11
  687. *
  688. * A weak_ptr provides a safe alternative to a raw pointer when you want
  689. * a non-owning reference to an object that is managed by a shared_ptr.
  690. *
  691. * Unlike a raw pointer, a weak_ptr can be converted to a new shared_ptr
  692. * that shares ownership with every other shared_ptr that already owns
  693. * the pointer. In other words you can upgrade from a non-owning "weak"
  694. * reference to an owning shared_ptr, without having access to any of
  695. * the existing shared_ptr objects.
  696. *
  697. * Also unlike a raw pointer, a weak_ptr does not become "dangling" after
  698. * the object it points to has been destroyed. Instead, a weak_ptr
  699. * becomes _expired_ and can no longer be converted to a shared_ptr that
  700. * owns the freed pointer, so you cannot accidentally access the pointed-to
  701. * object after it has been destroyed.
  702. */
  703. template<typename _Tp>
  704. class weak_ptr : public __weak_ptr<_Tp>
  705. {
  706. template<typename _Arg>
  707. using _Constructible = typename enable_if<
  708. is_constructible<__weak_ptr<_Tp>, _Arg>::value
  709. >::type;
  710. template<typename _Arg>
  711. using _Assignable = typename enable_if<
  712. is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
  713. >::type;
  714. public:
  715. constexpr weak_ptr() noexcept = default;
  716. template<typename _Yp,
  717. typename = _Constructible<const shared_ptr<_Yp>&>>
  718. weak_ptr(const shared_ptr<_Yp>& __r) noexcept
  719. : __weak_ptr<_Tp>(__r) { }
  720. weak_ptr(const weak_ptr&) noexcept = default;
  721. template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
  722. weak_ptr(const weak_ptr<_Yp>& __r) noexcept
  723. : __weak_ptr<_Tp>(__r) { }
  724. weak_ptr(weak_ptr&&) noexcept = default;
  725. template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
  726. weak_ptr(weak_ptr<_Yp>&& __r) noexcept
  727. : __weak_ptr<_Tp>(std::move(__r)) { }
  728. weak_ptr&
  729. operator=(const weak_ptr& __r) noexcept = default;
  730. template<typename _Yp>
  731. _Assignable<const weak_ptr<_Yp>&>
  732. operator=(const weak_ptr<_Yp>& __r) noexcept
  733. {
  734. this->__weak_ptr<_Tp>::operator=(__r);
  735. return *this;
  736. }
  737. template<typename _Yp>
  738. _Assignable<const shared_ptr<_Yp>&>
  739. operator=(const shared_ptr<_Yp>& __r) noexcept
  740. {
  741. this->__weak_ptr<_Tp>::operator=(__r);
  742. return *this;
  743. }
  744. weak_ptr&
  745. operator=(weak_ptr&& __r) noexcept = default;
  746. template<typename _Yp>
  747. _Assignable<weak_ptr<_Yp>>
  748. operator=(weak_ptr<_Yp>&& __r) noexcept
  749. {
  750. this->__weak_ptr<_Tp>::operator=(std::move(__r));
  751. return *this;
  752. }
  753. shared_ptr<_Tp>
  754. lock() const noexcept
  755. { return shared_ptr<_Tp>(*this, std::nothrow); }
  756. };
  757. #if __cpp_deduction_guides >= 201606
  758. template<typename _Tp>
  759. weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
  760. #endif
  761. // 20.7.2.3.6 weak_ptr specialized algorithms.
  762. /// Swap overload for weak_ptr
  763. /// @relates weak_ptr
  764. template<typename _Tp>
  765. inline void
  766. swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
  767. { __a.swap(__b); }
  768. /// Primary template owner_less
  769. template<typename _Tp = void>
  770. struct owner_less;
  771. /// Void specialization of owner_less compares either shared_ptr or weak_ptr
  772. template<>
  773. struct owner_less<void> : _Sp_owner_less<void, void>
  774. { };
  775. /// Partial specialization of owner_less for shared_ptr.
  776. template<typename _Tp>
  777. struct owner_less<shared_ptr<_Tp>>
  778. : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
  779. { };
  780. /// Partial specialization of owner_less for weak_ptr.
  781. template<typename _Tp>
  782. struct owner_less<weak_ptr<_Tp>>
  783. : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
  784. { };
  785. /**
  786. * @brief Base class allowing use of the member function `shared_from_this`.
  787. * @headerfile memory
  788. * @since C++11
  789. */
  790. template<typename _Tp>
  791. class enable_shared_from_this
  792. {
  793. protected:
  794. constexpr enable_shared_from_this() noexcept { }
  795. enable_shared_from_this(const enable_shared_from_this&) noexcept { }
  796. enable_shared_from_this&
  797. operator=(const enable_shared_from_this&) noexcept
  798. { return *this; }
  799. ~enable_shared_from_this() { }
  800. public:
  801. shared_ptr<_Tp>
  802. shared_from_this()
  803. { return shared_ptr<_Tp>(this->_M_weak_this); }
  804. shared_ptr<const _Tp>
  805. shared_from_this() const
  806. { return shared_ptr<const _Tp>(this->_M_weak_this); }
  807. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  808. #define __cpp_lib_enable_shared_from_this 201603L
  809. /** @{
  810. * Get a `weak_ptr` referring to the object that has `*this` as its base.
  811. * @since C++17
  812. */
  813. weak_ptr<_Tp>
  814. weak_from_this() noexcept
  815. { return this->_M_weak_this; }
  816. weak_ptr<const _Tp>
  817. weak_from_this() const noexcept
  818. { return this->_M_weak_this; }
  819. /// @}
  820. #endif
  821. private:
  822. template<typename _Tp1>
  823. void
  824. _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
  825. { _M_weak_this._M_assign(__p, __n); }
  826. // Found by ADL when this is an associated class.
  827. friend const enable_shared_from_this*
  828. __enable_shared_from_this_base(const __shared_count<>&,
  829. const enable_shared_from_this* __p)
  830. { return __p; }
  831. template<typename, _Lock_policy>
  832. friend class __shared_ptr;
  833. mutable weak_ptr<_Tp> _M_weak_this;
  834. };
  835. /// @relates shared_ptr @{
  836. /**
  837. * @brief Create an object that is owned by a shared_ptr.
  838. * @param __a An allocator.
  839. * @param __args Arguments for the @a _Tp object's constructor.
  840. * @return A shared_ptr that owns the newly created object.
  841. * @throw An exception thrown from @a _Alloc::allocate or from the
  842. * constructor of @a _Tp.
  843. *
  844. * A copy of @a __a will be used to allocate memory for the shared_ptr
  845. * and the new object.
  846. */
  847. template<typename _Tp, typename _Alloc, typename... _Args>
  848. inline shared_ptr<_NonArray<_Tp>>
  849. allocate_shared(const _Alloc& __a, _Args&&... __args)
  850. {
  851. return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
  852. std::forward<_Args>(__args)...);
  853. }
  854. /**
  855. * @brief Create an object that is owned by a shared_ptr.
  856. * @param __args Arguments for the @a _Tp object's constructor.
  857. * @return A shared_ptr that owns the newly created object.
  858. * @throw std::bad_alloc, or an exception thrown from the
  859. * constructor of @a _Tp.
  860. */
  861. template<typename _Tp, typename... _Args>
  862. inline shared_ptr<_NonArray<_Tp>>
  863. make_shared(_Args&&... __args)
  864. {
  865. using _Alloc = allocator<void>;
  866. _Alloc __a;
  867. return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
  868. std::forward<_Args>(__args)...);
  869. }
  870. #if __cpp_lib_shared_ptr_arrays >= 201707L
  871. /// @cond undocumented
  872. template<typename _Tp, typename _Alloc = allocator<void>>
  873. auto
  874. __make_shared_arr_tag(size_t __n, const _Alloc& __a = _Alloc()) noexcept
  875. {
  876. using _Up = remove_all_extents_t<_Tp>;
  877. using _UpAlloc = __alloc_rebind<_Alloc, _Up>;
  878. size_t __s = sizeof(remove_extent_t<_Tp>) / sizeof(_Up);
  879. if (__builtin_mul_overflow(__s, __n, &__n))
  880. std::__throw_bad_array_new_length();
  881. return _Sp_counted_array_base<_UpAlloc>{_UpAlloc(__a), __n};
  882. }
  883. /// @endcond
  884. template<typename _Tp, typename _Alloc>
  885. inline shared_ptr<_UnboundedArray<_Tp>>
  886. allocate_shared(const _Alloc& __a, size_t __n)
  887. {
  888. return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n, __a));
  889. }
  890. template<typename _Tp>
  891. inline shared_ptr<_UnboundedArray<_Tp>>
  892. make_shared(size_t __n)
  893. {
  894. return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n));
  895. }
  896. template<typename _Tp, typename _Alloc>
  897. inline shared_ptr<_UnboundedArray<_Tp>>
  898. allocate_shared(const _Alloc& __a, size_t __n,
  899. const remove_extent_t<_Tp>& __u)
  900. {
  901. return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n, __a),
  902. std::__addressof(__u));
  903. }
  904. template<typename _Tp>
  905. inline shared_ptr<_UnboundedArray<_Tp>>
  906. make_shared(size_t __n, const remove_extent_t<_Tp>& __u)
  907. {
  908. return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n),
  909. std::__addressof(__u));
  910. }
  911. /// @cond undocumented
  912. template<typename _Tp, typename _Alloc = allocator<void>>
  913. auto
  914. __make_shared_arrN_tag(const _Alloc& __a = _Alloc()) noexcept
  915. {
  916. using _Up = remove_all_extents_t<_Tp>;
  917. using _UpAlloc = __alloc_rebind<_Alloc, _Up>;
  918. size_t __n = sizeof(_Tp) / sizeof(_Up);
  919. return _Sp_counted_array_base<_UpAlloc>{_UpAlloc(__a), __n};
  920. }
  921. /// @endcond
  922. template<typename _Tp, typename _Alloc>
  923. inline shared_ptr<_BoundedArray<_Tp>>
  924. allocate_shared(const _Alloc& __a)
  925. {
  926. return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(__a));
  927. }
  928. template<typename _Tp>
  929. inline shared_ptr<_BoundedArray<_Tp>>
  930. make_shared()
  931. {
  932. return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>());
  933. }
  934. template<typename _Tp, typename _Alloc>
  935. inline shared_ptr<_BoundedArray<_Tp>>
  936. allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u)
  937. {
  938. return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(__a),
  939. std::__addressof(__u));
  940. }
  941. template<typename _Tp>
  942. inline shared_ptr<_BoundedArray<_Tp>>
  943. make_shared(const remove_extent_t<_Tp>& __u)
  944. {
  945. return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(),
  946. std::__addressof(__u));
  947. }
  948. #if __cpp_lib_smart_ptr_for_overwrite
  949. template<typename _Tp, typename _Alloc>
  950. inline shared_ptr<_NotUnboundedArray<_Tp>>
  951. allocate_shared_for_overwrite(const _Alloc& __a)
  952. {
  953. if constexpr (is_array_v<_Tp>)
  954. return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(__a),
  955. _Sp_overwrite_tag{});
  956. else
  957. {
  958. // Rebind the allocator to _Sp_overwrite_tag, so that the
  959. // relevant _Sp_counted_ptr_inplace specialization is used.
  960. using _Alloc2 = __alloc_rebind<_Alloc, _Sp_overwrite_tag>;
  961. _Alloc2 __a2 = __a;
  962. return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc2>{__a2});
  963. }
  964. }
  965. template<typename _Tp>
  966. inline shared_ptr<_NotUnboundedArray<_Tp>>
  967. make_shared_for_overwrite()
  968. {
  969. if constexpr (is_array_v<_Tp>)
  970. return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(),
  971. _Sp_overwrite_tag{});
  972. else
  973. {
  974. using _Alloc = allocator<_Sp_overwrite_tag>;
  975. return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{{}});
  976. }
  977. }
  978. template<typename _Tp, typename _Alloc>
  979. inline shared_ptr<_UnboundedArray<_Tp>>
  980. allocate_shared_for_overwrite(const _Alloc& __a, size_t __n)
  981. {
  982. return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n, __a),
  983. _Sp_overwrite_tag{});
  984. }
  985. template<typename _Tp>
  986. inline shared_ptr<_UnboundedArray<_Tp>>
  987. make_shared_for_overwrite(size_t __n)
  988. {
  989. return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n),
  990. _Sp_overwrite_tag{});
  991. }
  992. #endif // smart_ptr_for_overwrite
  993. #endif // shared_ptr_arrays
  994. /// std::hash specialization for shared_ptr.
  995. template<typename _Tp>
  996. struct hash<shared_ptr<_Tp>>
  997. : public __hash_base<size_t, shared_ptr<_Tp>>
  998. {
  999. size_t
  1000. operator()(const shared_ptr<_Tp>& __s) const noexcept
  1001. {
  1002. return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get());
  1003. }
  1004. };
  1005. /// @} relates shared_ptr
  1006. /// @} group pointer_abstractions
  1007. #if __cplusplus >= 201703L
  1008. namespace __detail::__variant
  1009. {
  1010. template<typename> struct _Never_valueless_alt; // see <variant>
  1011. // Provide the strong exception-safety guarantee when emplacing a
  1012. // shared_ptr into a variant.
  1013. template<typename _Tp>
  1014. struct _Never_valueless_alt<std::shared_ptr<_Tp>>
  1015. : std::true_type
  1016. { };
  1017. // Provide the strong exception-safety guarantee when emplacing a
  1018. // weak_ptr into a variant.
  1019. template<typename _Tp>
  1020. struct _Never_valueless_alt<std::weak_ptr<_Tp>>
  1021. : std::true_type
  1022. { };
  1023. } // namespace __detail::__variant
  1024. #endif // C++17
  1025. _GLIBCXX_END_NAMESPACE_VERSION
  1026. } // namespace
  1027. #endif // _SHARED_PTR_H