shared_ptr.h 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_SHARED_PTR_H
  10. #define _LIBCPP___MEMORY_SHARED_PTR_H
  11. #include <__compare/compare_three_way.h>
  12. #include <__compare/ordering.h>
  13. #include <__config>
  14. #include <__exception/exception.h>
  15. #include <__functional/binary_function.h>
  16. #include <__functional/operations.h>
  17. #include <__functional/reference_wrapper.h>
  18. #include <__fwd/ostream.h>
  19. #include <__iterator/access.h>
  20. #include <__memory/addressof.h>
  21. #include <__memory/allocation_guard.h>
  22. #include <__memory/allocator.h>
  23. #include <__memory/allocator_destructor.h>
  24. #include <__memory/allocator_traits.h>
  25. #include <__memory/auto_ptr.h>
  26. #include <__memory/compressed_pair.h>
  27. #include <__memory/construct_at.h>
  28. #include <__memory/pointer_traits.h>
  29. #include <__memory/uninitialized_algorithms.h>
  30. #include <__memory/unique_ptr.h>
  31. #include <__type_traits/add_lvalue_reference.h>
  32. #include <__type_traits/conditional.h>
  33. #include <__type_traits/conjunction.h>
  34. #include <__type_traits/disjunction.h>
  35. #include <__type_traits/is_array.h>
  36. #include <__type_traits/is_bounded_array.h>
  37. #include <__type_traits/is_constructible.h>
  38. #include <__type_traits/is_convertible.h>
  39. #include <__type_traits/is_reference.h>
  40. #include <__type_traits/is_unbounded_array.h>
  41. #include <__type_traits/nat.h>
  42. #include <__type_traits/negation.h>
  43. #include <__type_traits/remove_extent.h>
  44. #include <__type_traits/remove_reference.h>
  45. #include <__utility/declval.h>
  46. #include <__utility/forward.h>
  47. #include <__utility/move.h>
  48. #include <__utility/swap.h>
  49. #include <__verbose_abort>
  50. #include <cstddef>
  51. #include <new>
  52. #include <typeinfo>
  53. #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
  54. # include <atomic>
  55. #endif
  56. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  57. # pragma GCC system_header
  58. #endif
  59. _LIBCPP_PUSH_MACROS
  60. #include <__undef_macros>
  61. _LIBCPP_BEGIN_NAMESPACE_STD
  62. // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
  63. // should be sufficient for thread safety.
  64. // See https://llvm.org/PR22803
  65. #if defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && defined(__ATOMIC_ACQ_REL)
  66. # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
  67. #elif defined(_LIBCPP_COMPILER_GCC)
  68. # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
  69. #endif
  70. template <class _ValueType>
  71. inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
  72. #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__ATOMIC_RELAXED) && \
  73. (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
  74. return __atomic_load_n(__value, __ATOMIC_RELAXED);
  75. #else
  76. return *__value;
  77. #endif
  78. }
  79. template <class _ValueType>
  80. inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
  81. #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__ATOMIC_ACQUIRE) && \
  82. (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
  83. return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
  84. #else
  85. return *__value;
  86. #endif
  87. }
  88. template <class _Tp>
  89. inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {
  90. #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
  91. return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
  92. #else
  93. return __t += 1;
  94. #endif
  95. }
  96. template <class _Tp>
  97. inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {
  98. #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
  99. return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
  100. #else
  101. return __t -= 1;
  102. #endif
  103. }
  104. class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception {
  105. public:
  106. _LIBCPP_HIDE_FROM_ABI bad_weak_ptr() _NOEXCEPT = default;
  107. _LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
  108. _LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default;
  109. ~bad_weak_ptr() _NOEXCEPT override;
  110. const char* what() const _NOEXCEPT override;
  111. };
  112. _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_weak_ptr() {
  113. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  114. throw bad_weak_ptr();
  115. #else
  116. _LIBCPP_VERBOSE_ABORT("bad_weak_ptr was thrown in -fno-exceptions mode");
  117. #endif
  118. }
  119. template <class _Tp>
  120. class _LIBCPP_TEMPLATE_VIS weak_ptr;
  121. class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
  122. __shared_count(const __shared_count&);
  123. __shared_count& operator=(const __shared_count&);
  124. protected:
  125. #ifdef _LIBCPP_HAS_NO_THREADS
  126. typedef long __atomic_count;
  127. #else
  128. typedef atomic<long> __atomic_count;
  129. #endif
  130. __atomic_count __shared_owners_;
  131. virtual ~__shared_count();
  132. private:
  133. virtual void __on_zero_shared() _NOEXCEPT = 0;
  134. public:
  135. _LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {}
  136. #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
  137. void __add_shared() noexcept;
  138. bool __release_shared() noexcept;
  139. #else
  140. _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT {
  141. # ifdef _LIBCPP_HAS_NO_THREADS
  142. __libcpp_atomic_refcount_increment(__shared_owners_);
  143. # else
  144. __shared_owners_++;
  145. # endif
  146. }
  147. _LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT {
  148. # ifdef _LIBCPP_HAS_NO_THREADS
  149. if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
  150. # else
  151. if (--__shared_owners_ == -1) {
  152. # endif
  153. __on_zero_shared();
  154. return true;
  155. }
  156. return false;
  157. }
  158. #endif
  159. _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT {
  160. #ifdef _LIBCPP_HAS_NO_THREADS
  161. return __libcpp_relaxed_load(&__shared_owners_) + 1;
  162. #else
  163. return __shared_owners_.load(memory_order_relaxed) + 1;
  164. #endif
  165. }
  166. };
  167. class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {
  168. #ifdef _LIBCPP_HAS_NO_THREADS
  169. typedef long __atomic_count;
  170. #else
  171. typedef atomic<long> __atomic_count;
  172. #endif
  173. __atomic_count __shared_weak_owners_;
  174. public:
  175. _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
  176. : __shared_count(__refs),
  177. __shared_weak_owners_(__refs) {}
  178. protected:
  179. ~__shared_weak_count() override;
  180. public:
  181. #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
  182. void __add_shared() noexcept;
  183. void __add_weak() noexcept;
  184. void __release_shared() noexcept;
  185. #else
  186. _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); }
  187. _LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT {
  188. # ifdef _LIBCPP_HAS_NO_THREADS
  189. __libcpp_atomic_refcount_increment(__shared_weak_owners_);
  190. # else
  191. __shared_weak_owners_++;
  192. # endif
  193. }
  194. _LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT {
  195. if (__shared_count::__release_shared())
  196. __release_weak();
  197. }
  198. #endif
  199. void __release_weak() _NOEXCEPT;
  200. _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); }
  201. __shared_weak_count* lock() _NOEXCEPT;
  202. virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
  203. private:
  204. virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
  205. };
  206. template <class _Tp, class _Dp, class _Alloc>
  207. class __shared_ptr_pointer : public __shared_weak_count {
  208. __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
  209. public:
  210. _LIBCPP_HIDE_FROM_ABI __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
  211. : __data_(__compressed_pair<_Tp, _Dp>(__p, std::move(__d)), std::move(__a)) {}
  212. #ifndef _LIBCPP_HAS_NO_RTTI
  213. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* __get_deleter(const type_info&) const _NOEXCEPT override;
  214. #endif
  215. private:
  216. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
  217. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override;
  218. };
  219. #ifndef _LIBCPP_HAS_NO_RTTI
  220. template <class _Tp, class _Dp, class _Alloc>
  221. const void* __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT {
  222. return __t == typeid(_Dp) ? std::addressof(__data_.first().second()) : nullptr;
  223. }
  224. #endif // _LIBCPP_HAS_NO_RTTI
  225. template <class _Tp, class _Dp, class _Alloc>
  226. void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT {
  227. __data_.first().second()(__data_.first().first());
  228. __data_.first().second().~_Dp();
  229. }
  230. template <class _Tp, class _Dp, class _Alloc>
  231. void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT {
  232. typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
  233. typedef allocator_traits<_Al> _ATraits;
  234. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  235. _Al __a(__data_.second());
  236. __data_.second().~_Alloc();
  237. __a.deallocate(_PTraits::pointer_to(*this), 1);
  238. }
  239. // This tag is used to instantiate an allocator type. The various shared_ptr control blocks
  240. // detect that the allocator has been instantiated for this type and perform alternative
  241. // initialization/destruction based on that.
  242. struct __for_overwrite_tag {};
  243. template <class _Tp, class _Alloc>
  244. struct __shared_ptr_emplace : __shared_weak_count {
  245. template <class... _Args,
  246. class _Allocator = _Alloc,
  247. __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
  248. _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&...) : __storage_(std::move(__a)) {
  249. static_assert(
  250. sizeof...(_Args) == 0, "No argument should be provided to the control block when using _for_overwrite");
  251. ::new ((void*)__get_elem()) _Tp;
  252. }
  253. template <class... _Args,
  254. class _Allocator = _Alloc,
  255. __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
  256. _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&... __args) : __storage_(std::move(__a)) {
  257. using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
  258. _TpAlloc __tmp(*__get_alloc());
  259. allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), std::forward<_Args>(__args)...);
  260. }
  261. _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
  262. _LIBCPP_HIDE_FROM_ABI _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
  263. private:
  264. template <class _Allocator = _Alloc,
  265. __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
  266. _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {
  267. __get_elem()->~_Tp();
  268. }
  269. template <class _Allocator = _Alloc,
  270. __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
  271. _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {
  272. using _TpAlloc = typename __allocator_traits_rebind<_Allocator, _Tp>::type;
  273. _TpAlloc __tmp(*__get_alloc());
  274. allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
  275. }
  276. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { __on_zero_shared_impl(); }
  277. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
  278. using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
  279. using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
  280. _ControlBlockAlloc __tmp(*__get_alloc());
  281. __storage_.~_Storage();
  282. allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
  283. }
  284. // This class implements the control block for non-array shared pointers created
  285. // through `std::allocate_shared` and `std::make_shared`.
  286. //
  287. // In previous versions of the library, we used a compressed pair to store
  288. // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
  289. // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
  290. // we now use a properly aligned char buffer while making sure that we maintain
  291. // the same layout that we had when we used a compressed pair.
  292. using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
  293. struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
  294. char __blob_[sizeof(_CompressedPair)];
  295. _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { ::new ((void*)__get_alloc()) _Alloc(std::move(__a)); }
  296. _LIBCPP_HIDE_FROM_ABI ~_Storage() { __get_alloc()->~_Alloc(); }
  297. _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT {
  298. _CompressedPair* __as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
  299. typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
  300. _Alloc* __alloc = reinterpret_cast<_Alloc*>(__first);
  301. return __alloc;
  302. }
  303. _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
  304. _CompressedPair* __as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
  305. typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
  306. _Tp* __elem = reinterpret_cast<_Tp*>(__second);
  307. return __elem;
  308. }
  309. };
  310. static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
  311. static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
  312. _Storage __storage_;
  313. };
  314. struct __shared_ptr_dummy_rebind_allocator_type;
  315. template <>
  316. class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> {
  317. public:
  318. template <class _Other>
  319. struct rebind {
  320. typedef allocator<_Other> other;
  321. };
  322. };
  323. template <class _Tp>
  324. class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
  325. // http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.general-6
  326. // A pointer type Y* is said to be compatible with a pointer type T*
  327. // when either Y* is convertible to T* or Y is U[N] and T is cv U[].
  328. #if _LIBCPP_STD_VER >= 17
  329. template <class _Yp, class _Tp>
  330. struct __bounded_convertible_to_unbounded : false_type {};
  331. template <class _Up, std::size_t _Np, class _Tp>
  332. struct __bounded_convertible_to_unbounded<_Up[_Np], _Tp> : is_same<__remove_cv_t<_Tp>, _Up[]> {};
  333. template <class _Yp, class _Tp>
  334. struct __compatible_with : _Or< is_convertible<_Yp*, _Tp*>, __bounded_convertible_to_unbounded<_Yp, _Tp> > {};
  335. #else
  336. template <class _Yp, class _Tp>
  337. struct __compatible_with : is_convertible<_Yp*, _Tp*> {};
  338. #endif // _LIBCPP_STD_VER >= 17
  339. // Constructors that take raw pointers have a different set of "compatible" constraints
  340. // http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.const-9.1
  341. // - If T is an array type, then either T is U[N] and Y(*)[N] is convertible to T*,
  342. // or T is U[] and Y(*)[] is convertible to T*.
  343. // - If T is not an array type, then Y* is convertible to T*.
  344. #if _LIBCPP_STD_VER >= 17
  345. template <class _Yp, class _Tp, class = void>
  346. struct __raw_pointer_compatible_with : _And< _Not<is_array<_Tp>>, is_convertible<_Yp*, _Tp*> > {};
  347. template <class _Yp, class _Up, std::size_t _Np>
  348. struct __raw_pointer_compatible_with<_Yp, _Up[_Np], __enable_if_t< is_convertible<_Yp (*)[_Np], _Up (*)[_Np]>::value> >
  349. : true_type {};
  350. template <class _Yp, class _Up>
  351. struct __raw_pointer_compatible_with<_Yp, _Up[], __enable_if_t< is_convertible<_Yp (*)[], _Up (*)[]>::value> >
  352. : true_type {};
  353. #else
  354. template <class _Yp, class _Tp>
  355. struct __raw_pointer_compatible_with : is_convertible<_Yp*, _Tp*> {};
  356. #endif // _LIBCPP_STD_VER >= 17
  357. template <class _Ptr, class = void>
  358. struct __is_deletable : false_type {};
  359. template <class _Ptr>
  360. struct __is_deletable<_Ptr, decltype(delete std::declval<_Ptr>())> : true_type {};
  361. template <class _Ptr, class = void>
  362. struct __is_array_deletable : false_type {};
  363. template <class _Ptr>
  364. struct __is_array_deletable<_Ptr, decltype(delete[] std::declval<_Ptr>())> : true_type {};
  365. template <class _Dp, class _Pt, class = decltype(std::declval<_Dp>()(std::declval<_Pt>()))>
  366. true_type __well_formed_deleter_test(int);
  367. template <class, class>
  368. false_type __well_formed_deleter_test(...);
  369. template <class _Dp, class _Pt>
  370. struct __well_formed_deleter : decltype(std::__well_formed_deleter_test<_Dp, _Pt>(0)) {};
  371. template <class _Dp, class _Yp, class _Tp>
  372. struct __shared_ptr_deleter_ctor_reqs {
  373. static const bool value = __raw_pointer_compatible_with<_Yp, _Tp>::value && is_move_constructible<_Dp>::value &&
  374. __well_formed_deleter<_Dp, _Yp*>::value;
  375. };
  376. #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
  377. # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))
  378. #else
  379. # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
  380. #endif
  381. template <class _Tp>
  382. class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
  383. public:
  384. #if _LIBCPP_STD_VER >= 17
  385. typedef weak_ptr<_Tp> weak_type;
  386. typedef remove_extent_t<_Tp> element_type;
  387. #else
  388. typedef _Tp element_type;
  389. #endif
  390. private:
  391. element_type* __ptr_;
  392. __shared_weak_count* __cntrl_;
  393. public:
  394. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
  395. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
  396. template <class _Yp,
  397. __enable_if_t< _And< __raw_pointer_compatible_with<_Yp, _Tp>
  398. // In C++03 we get errors when trying to do SFINAE with the
  399. // delete operator, so we always pretend that it's deletable.
  400. // The same happens on GCC.
  401. #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)
  402. ,
  403. _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >
  404. #endif
  405. >::value,
  406. int> = 0>
  407. _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
  408. unique_ptr<_Yp> __hold(__p);
  409. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  410. typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk;
  411. __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
  412. __hold.release();
  413. __enable_weak_this(__p, __p);
  414. }
  415. template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
  416. _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) {
  417. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  418. try {
  419. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  420. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  421. typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
  422. #ifndef _LIBCPP_CXX03_LANG
  423. __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
  424. #else
  425. __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
  426. #endif // not _LIBCPP_CXX03_LANG
  427. __enable_weak_this(__p, __p);
  428. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  429. } catch (...) {
  430. __d(__p);
  431. throw;
  432. }
  433. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  434. }
  435. template <class _Yp,
  436. class _Dp,
  437. class _Alloc,
  438. __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
  439. _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {
  440. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  441. try {
  442. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  443. typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
  444. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  445. typedef __allocator_destructor<_A2> _D2;
  446. _A2 __a2(__a);
  447. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  448. ::new ((void*)std::addressof(*__hold2.get()))
  449. #ifndef _LIBCPP_CXX03_LANG
  450. _CntrlBlk(__p, std::move(__d), __a);
  451. #else
  452. _CntrlBlk(__p, __d, __a);
  453. #endif // not _LIBCPP_CXX03_LANG
  454. __cntrl_ = std::addressof(*__hold2.release());
  455. __enable_weak_this(__p, __p);
  456. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  457. } catch (...) {
  458. __d(__p);
  459. throw;
  460. }
  461. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  462. }
  463. template <class _Dp>
  464. _LIBCPP_HIDE_FROM_ABI shared_ptr(nullptr_t __p, _Dp __d) : __ptr_(nullptr) {
  465. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  466. try {
  467. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  468. typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
  469. typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
  470. #ifndef _LIBCPP_CXX03_LANG
  471. __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
  472. #else
  473. __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
  474. #endif // not _LIBCPP_CXX03_LANG
  475. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  476. } catch (...) {
  477. __d(__p);
  478. throw;
  479. }
  480. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  481. }
  482. template <class _Dp, class _Alloc>
  483. _LIBCPP_HIDE_FROM_ABI shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a) : __ptr_(nullptr) {
  484. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  485. try {
  486. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  487. typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
  488. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  489. typedef __allocator_destructor<_A2> _D2;
  490. _A2 __a2(__a);
  491. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  492. ::new ((void*)std::addressof(*__hold2.get()))
  493. #ifndef _LIBCPP_CXX03_LANG
  494. _CntrlBlk(__p, std::move(__d), __a);
  495. #else
  496. _CntrlBlk(__p, __d, __a);
  497. #endif // not _LIBCPP_CXX03_LANG
  498. __cntrl_ = std::addressof(*__hold2.release());
  499. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  500. } catch (...) {
  501. __d(__p);
  502. throw;
  503. }
  504. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  505. }
  506. template <class _Yp>
  507. _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT
  508. : __ptr_(__p),
  509. __cntrl_(__r.__cntrl_) {
  510. if (__cntrl_)
  511. __cntrl_->__add_shared();
  512. }
  513. // LWG-2996
  514. // We don't backport because it is an evolutionary change.
  515. #if _LIBCPP_STD_VER >= 20
  516. template <class _Yp>
  517. _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept
  518. : __ptr_(__p), __cntrl_(__r.__cntrl_) {
  519. __r.__ptr_ = nullptr;
  520. __r.__cntrl_ = nullptr;
  521. }
  522. #endif
  523. _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  524. if (__cntrl_)
  525. __cntrl_->__add_shared();
  526. }
  527. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  528. _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  529. if (__cntrl_)
  530. __cntrl_->__add_shared();
  531. }
  532. _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  533. __r.__ptr_ = nullptr;
  534. __r.__cntrl_ = nullptr;
  535. }
  536. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  537. _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  538. __r.__ptr_ = nullptr;
  539. __r.__cntrl_ = nullptr;
  540. }
  541. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  542. _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(const weak_ptr<_Yp>& __r)
  543. : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) {
  544. if (__cntrl_ == nullptr)
  545. __throw_bad_weak_ptr();
  546. }
  547. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  548. template <class _Yp, __enable_if_t<is_convertible<_Yp*, element_type*>::value, int> = 0>
  549. _LIBCPP_HIDE_FROM_ABI shared_ptr(auto_ptr<_Yp>&& __r) : __ptr_(__r.get()) {
  550. typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
  551. __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
  552. __enable_weak_this(__r.get(), __r.get());
  553. __r.release();
  554. }
  555. #endif
  556. template <class _Yp,
  557. class _Dp,
  558. __enable_if_t<!is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&
  559. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  560. int> = 0>
  561. _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {
  562. #if _LIBCPP_STD_VER >= 14
  563. if (__ptr_ == nullptr)
  564. __cntrl_ = nullptr;
  565. else
  566. #endif
  567. {
  568. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  569. typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk;
  570. __cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());
  571. __enable_weak_this(__r.get(), __r.get());
  572. }
  573. __r.release();
  574. }
  575. template <class _Yp,
  576. class _Dp,
  577. class = void,
  578. __enable_if_t<is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&
  579. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  580. int> = 0>
  581. _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {
  582. #if _LIBCPP_STD_VER >= 14
  583. if (__ptr_ == nullptr)
  584. __cntrl_ = nullptr;
  585. else
  586. #endif
  587. {
  588. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  589. typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
  590. reference_wrapper<__libcpp_remove_reference_t<_Dp> >,
  591. _AllocT>
  592. _CntrlBlk;
  593. __cntrl_ = new _CntrlBlk(__r.get(), std::ref(__r.get_deleter()), _AllocT());
  594. __enable_weak_this(__r.get(), __r.get());
  595. }
  596. __r.release();
  597. }
  598. _LIBCPP_HIDE_FROM_ABI ~shared_ptr() {
  599. if (__cntrl_)
  600. __cntrl_->__release_shared();
  601. }
  602. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT {
  603. shared_ptr(__r).swap(*this);
  604. return *this;
  605. }
  606. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  607. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT {
  608. shared_ptr(__r).swap(*this);
  609. return *this;
  610. }
  611. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT {
  612. shared_ptr(std::move(__r)).swap(*this);
  613. return *this;
  614. }
  615. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  616. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r) {
  617. shared_ptr(std::move(__r)).swap(*this);
  618. return *this;
  619. }
  620. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  621. template <class _Yp,
  622. __enable_if_t<!is_array<_Yp>::value && is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  623. int> = 0>
  624. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r) {
  625. shared_ptr(std::move(__r)).swap(*this);
  626. return *this;
  627. }
  628. #endif
  629. template <class _Yp,
  630. class _Dp,
  631. __enable_if_t<_And< __compatible_with<_Yp, _Tp>,
  632. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*> >::value,
  633. int> = 0>
  634. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r) {
  635. shared_ptr(std::move(__r)).swap(*this);
  636. return *this;
  637. }
  638. _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr& __r) _NOEXCEPT {
  639. std::swap(__ptr_, __r.__ptr_);
  640. std::swap(__cntrl_, __r.__cntrl_);
  641. }
  642. _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { shared_ptr().swap(*this); }
  643. template <class _Yp, __enable_if_t<__raw_pointer_compatible_with<_Yp, _Tp>::value, int> = 0>
  644. _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p) {
  645. shared_ptr(__p).swap(*this);
  646. }
  647. template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
  648. _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d) {
  649. shared_ptr(__p, __d).swap(*this);
  650. }
  651. template <class _Yp,
  652. class _Dp,
  653. class _Alloc,
  654. __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
  655. _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d, _Alloc __a) {
  656. shared_ptr(__p, __d, __a).swap(*this);
  657. }
  658. _LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; }
  659. _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT { return *__ptr_; }
  660. _LIBCPP_HIDE_FROM_ABI element_type* operator->() const _NOEXCEPT {
  661. static_assert(!is_array<_Tp>::value, "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
  662. return __ptr_;
  663. }
  664. _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
  665. #if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE)
  666. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }
  667. #endif
  668. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; }
  669. template <class _Up>
  670. _LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {
  671. return __cntrl_ < __p.__cntrl_;
  672. }
  673. template <class _Up>
  674. _LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {
  675. return __cntrl_ < __p.__cntrl_;
  676. }
  677. _LIBCPP_HIDE_FROM_ABI bool __owner_equivalent(const shared_ptr& __p) const { return __cntrl_ == __p.__cntrl_; }
  678. #if _LIBCPP_STD_VER >= 17
  679. _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {
  680. static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
  681. return __ptr_[__i];
  682. }
  683. #endif
  684. #ifndef _LIBCPP_HAS_NO_RTTI
  685. template <class _Dp>
  686. _LIBCPP_HIDE_FROM_ABI _Dp* __get_deleter() const _NOEXCEPT {
  687. return static_cast<_Dp*>(__cntrl_ ? const_cast<void*>(__cntrl_->__get_deleter(typeid(_Dp))) : nullptr);
  688. }
  689. #endif // _LIBCPP_HAS_NO_RTTI
  690. template <class _Yp, class _CntrlBlk>
  691. _LIBCPP_HIDE_FROM_ABI static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT {
  692. shared_ptr<_Tp> __r;
  693. __r.__ptr_ = __p;
  694. __r.__cntrl_ = __cntrl;
  695. __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
  696. return __r;
  697. }
  698. private:
  699. template <class _Yp, bool = is_function<_Yp>::value>
  700. struct __shared_ptr_default_allocator {
  701. typedef allocator<_Yp> type;
  702. };
  703. template <class _Yp>
  704. struct __shared_ptr_default_allocator<_Yp, true> {
  705. typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
  706. };
  707. template <class _Yp,
  708. class _OrigPtr,
  709. __enable_if_t<is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value, int> = 0>
  710. _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT {
  711. typedef __remove_cv_t<_Yp> _RawYp;
  712. if (__e && __e->__weak_this_.expired()) {
  713. __e->__weak_this_ = shared_ptr<_RawYp>(*this, const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
  714. }
  715. }
  716. _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT {}
  717. template <class, class _Yp>
  718. struct __shared_ptr_default_delete : default_delete<_Yp> {};
  719. template <class _Yp, class _Un, size_t _Sz>
  720. struct __shared_ptr_default_delete<_Yp[_Sz], _Un> : default_delete<_Yp[]> {};
  721. template <class _Yp, class _Un>
  722. struct __shared_ptr_default_delete<_Yp[], _Un> : default_delete<_Yp[]> {};
  723. template <class _Up>
  724. friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
  725. template <class _Up>
  726. friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
  727. };
  728. #if _LIBCPP_STD_VER >= 17
  729. template <class _Tp>
  730. shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
  731. template <class _Tp, class _Dp>
  732. shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
  733. #endif
  734. //
  735. // std::allocate_shared and std::make_shared
  736. //
  737. template <class _Tp, class _Alloc, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>
  738. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) {
  739. using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
  740. using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
  741. __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
  742. ::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__a, std::forward<_Args>(__args)...);
  743. auto __control_block = __guard.__release_ptr();
  744. return shared_ptr<_Tp>::__create_with_control_block(
  745. (*__control_block).__get_elem(), std::addressof(*__control_block));
  746. }
  747. template <class _Tp, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>
  748. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) {
  749. return std::allocate_shared<_Tp>(allocator<_Tp>(), std::forward<_Args>(__args)...);
  750. }
  751. #if _LIBCPP_STD_VER >= 20
  752. template <class _Tp, class _Alloc, __enable_if_t<!is_array<_Tp>::value, int> = 0>
  753. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
  754. using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
  755. _ForOverwriteAllocator __alloc(__a);
  756. return std::allocate_shared<_Tp>(__alloc);
  757. }
  758. template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
  759. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
  760. return std::allocate_shared_for_overwrite<_Tp>(allocator<_Tp>());
  761. }
  762. #endif // _LIBCPP_STD_VER >= 20
  763. #if _LIBCPP_STD_VER >= 17
  764. template <size_t _Alignment>
  765. struct __sp_aligned_storage {
  766. alignas(_Alignment) char __storage[_Alignment];
  767. };
  768. template <class _Tp, class _Alloc>
  769. struct __unbounded_array_control_block;
  770. template <class _Tp, class _Alloc>
  771. struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count {
  772. _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; }
  773. _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(
  774. _Alloc const& __alloc, size_t __count, _Tp const& __arg)
  775. : __alloc_(__alloc), __count_(__count) {
  776. std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::begin(__data_), __count_, __arg);
  777. }
  778. _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count)
  779. : __alloc_(__alloc), __count_(__count) {
  780. # if _LIBCPP_STD_VER >= 20
  781. if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
  782. // We are purposefully not using an allocator-aware default construction because the spec says so.
  783. // There's currently no way of expressing default initialization in an allocator-aware manner anyway.
  784. std::uninitialized_default_construct_n(std::begin(__data_), __count_);
  785. } else {
  786. std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_);
  787. }
  788. # else
  789. std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_);
  790. # endif
  791. }
  792. // Returns the number of bytes required to store a control block followed by the given number
  793. // of elements of _Tp, with the whole storage being aligned to a multiple of _Tp's alignment.
  794. _LIBCPP_HIDE_FROM_ABI static constexpr size_t __bytes_for(size_t __elements) {
  795. // When there's 0 elements, the control block alone is enough since it holds one element.
  796. // Otherwise, we allocate one fewer element than requested because the control block already
  797. // holds one. Also, we use the bitwise formula below to ensure that we allocate enough bytes
  798. // for the whole allocation to be a multiple of _Tp's alignment. That formula is taken from [1].
  799. //
  800. // [1]: https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding
  801. size_t __bytes = __elements == 0 ? sizeof(__unbounded_array_control_block)
  802. : (__elements - 1) * sizeof(_Tp) + sizeof(__unbounded_array_control_block);
  803. constexpr size_t __align = alignof(_Tp);
  804. return (__bytes + __align - 1) & ~(__align - 1);
  805. }
  806. _LIBCPP_HIDE_FROM_ABI_VIRTUAL
  807. ~__unbounded_array_control_block() override {
  808. } // can't be `= default` because of the sometimes-non-trivial union member __data_
  809. private:
  810. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {
  811. # if _LIBCPP_STD_VER >= 20
  812. if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
  813. std::__reverse_destroy(__data_, __data_ + __count_);
  814. } else {
  815. __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
  816. std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);
  817. }
  818. # else
  819. __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
  820. std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);
  821. # endif
  822. }
  823. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
  824. using _AlignedStorage = __sp_aligned_storage<alignof(__unbounded_array_control_block)>;
  825. using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
  826. using _PointerTraits = pointer_traits<typename allocator_traits<_StorageAlloc>::pointer>;
  827. _StorageAlloc __tmp(__alloc_);
  828. __alloc_.~_Alloc();
  829. size_t __size = __unbounded_array_control_block::__bytes_for(__count_);
  830. _AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this);
  831. allocator_traits<_StorageAlloc>::deallocate(
  832. __tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage));
  833. }
  834. _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
  835. size_t __count_;
  836. union {
  837. _Tp __data_[1];
  838. };
  839. };
  840. template <class _Array, class _Alloc, class... _Arg>
  841. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array>
  842. __allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&&... __arg) {
  843. static_assert(__libcpp_is_unbounded_array<_Array>::value);
  844. // We compute the number of bytes necessary to hold the control block and the
  845. // array elements. Then, we allocate an array of properly-aligned dummy structs
  846. // large enough to hold the control block and array. This allows shifting the
  847. // burden of aligning memory properly from us to the allocator.
  848. using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>;
  849. using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>;
  850. using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
  851. __allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage));
  852. _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
  853. std::__construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...);
  854. __guard.__release_ptr();
  855. return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
  856. }
  857. template <class _Tp, class _Alloc>
  858. struct __bounded_array_control_block;
  859. template <class _Tp, size_t _Count, class _Alloc>
  860. struct __bounded_array_control_block<_Tp[_Count], _Alloc> : __shared_weak_count {
  861. _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; }
  862. _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg)
  863. : __alloc_(__alloc) {
  864. std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count, __arg);
  865. }
  866. _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) {
  867. # if _LIBCPP_STD_VER >= 20
  868. if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
  869. // We are purposefully not using an allocator-aware default construction because the spec says so.
  870. // There's currently no way of expressing default initialization in an allocator-aware manner anyway.
  871. std::uninitialized_default_construct_n(std::addressof(__data_[0]), _Count);
  872. } else {
  873. std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);
  874. }
  875. # else
  876. std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);
  877. # endif
  878. }
  879. _LIBCPP_HIDE_FROM_ABI_VIRTUAL
  880. ~__bounded_array_control_block() override {
  881. } // can't be `= default` because of the sometimes-non-trivial union member __data_
  882. private:
  883. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {
  884. # if _LIBCPP_STD_VER >= 20
  885. if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
  886. std::__reverse_destroy(__data_, __data_ + _Count);
  887. } else {
  888. __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
  889. std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
  890. }
  891. # else
  892. __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
  893. std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
  894. # endif
  895. }
  896. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
  897. using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>;
  898. using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>;
  899. _ControlBlockAlloc __tmp(__alloc_);
  900. __alloc_.~_Alloc();
  901. allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1);
  902. }
  903. _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
  904. union {
  905. _Tp __data_[_Count];
  906. };
  907. };
  908. template <class _Array, class _Alloc, class... _Arg>
  909. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&&... __arg) {
  910. static_assert(__libcpp_is_bounded_array<_Array>::value);
  911. using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>;
  912. using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>;
  913. __allocation_guard<_ControlBlockAlloc> __guard(__a, 1);
  914. _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
  915. std::__construct_at(__control_block, __a, std::forward<_Arg>(__arg)...);
  916. __guard.__release_ptr();
  917. return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
  918. }
  919. #endif // _LIBCPP_STD_VER >= 17
  920. #if _LIBCPP_STD_VER >= 20
  921. // bounded array variants
  922. template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
  923. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) {
  924. return std::__allocate_shared_bounded_array<_Tp>(__a);
  925. }
  926. template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
  927. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) {
  928. return std::__allocate_shared_bounded_array<_Tp>(__a, __u);
  929. }
  930. template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
  931. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
  932. using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
  933. _ForOverwriteAllocator __alloc(__a);
  934. return std::__allocate_shared_bounded_array<_Tp>(__alloc);
  935. }
  936. template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
  937. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {
  938. return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());
  939. }
  940. template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
  941. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) {
  942. return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);
  943. }
  944. template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
  945. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
  946. return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>());
  947. }
  948. // unbounded array variants
  949. template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
  950. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) {
  951. return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);
  952. }
  953. template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
  954. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) {
  955. return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);
  956. }
  957. template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
  958. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) {
  959. using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
  960. _ForOverwriteAllocator __alloc(__a);
  961. return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n);
  962. }
  963. template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
  964. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) {
  965. return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);
  966. }
  967. template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
  968. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) {
  969. return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);
  970. }
  971. template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
  972. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) {
  973. return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n);
  974. }
  975. #endif // _LIBCPP_STD_VER >= 20
  976. template <class _Tp, class _Up>
  977. inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
  978. return __x.get() == __y.get();
  979. }
  980. #if _LIBCPP_STD_VER <= 17
  981. template <class _Tp, class _Up>
  982. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
  983. return !(__x == __y);
  984. }
  985. template <class _Tp, class _Up>
  986. inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
  987. # if _LIBCPP_STD_VER <= 11
  988. typedef typename common_type<_Tp*, _Up*>::type _Vp;
  989. return less<_Vp>()(__x.get(), __y.get());
  990. # else
  991. return less<>()(__x.get(), __y.get());
  992. # endif
  993. }
  994. template <class _Tp, class _Up>
  995. inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
  996. return __y < __x;
  997. }
  998. template <class _Tp, class _Up>
  999. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
  1000. return !(__y < __x);
  1001. }
  1002. template <class _Tp, class _Up>
  1003. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
  1004. return !(__x < __y);
  1005. }
  1006. #endif // _LIBCPP_STD_VER <= 17
  1007. #if _LIBCPP_STD_VER >= 20
  1008. template <class _Tp, class _Up>
  1009. _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) noexcept {
  1010. return compare_three_way()(__x.get(), __y.get());
  1011. }
  1012. #endif
  1013. template <class _Tp>
  1014. inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
  1015. return !__x;
  1016. }
  1017. #if _LIBCPP_STD_VER <= 17
  1018. template <class _Tp>
  1019. inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
  1020. return !__x;
  1021. }
  1022. template <class _Tp>
  1023. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
  1024. return static_cast<bool>(__x);
  1025. }
  1026. template <class _Tp>
  1027. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
  1028. return static_cast<bool>(__x);
  1029. }
  1030. template <class _Tp>
  1031. inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
  1032. return less<typename shared_ptr<_Tp>::element_type*>()(__x.get(), nullptr);
  1033. }
  1034. template <class _Tp>
  1035. inline _LIBCPP_HIDE_FROM_ABI bool operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
  1036. return less<typename shared_ptr<_Tp>::element_type*>()(nullptr, __x.get());
  1037. }
  1038. template <class _Tp>
  1039. inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
  1040. return nullptr < __x;
  1041. }
  1042. template <class _Tp>
  1043. inline _LIBCPP_HIDE_FROM_ABI bool operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
  1044. return __x < nullptr;
  1045. }
  1046. template <class _Tp>
  1047. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
  1048. return !(nullptr < __x);
  1049. }
  1050. template <class _Tp>
  1051. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
  1052. return !(__x < nullptr);
  1053. }
  1054. template <class _Tp>
  1055. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
  1056. return !(__x < nullptr);
  1057. }
  1058. template <class _Tp>
  1059. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
  1060. return !(nullptr < __x);
  1061. }
  1062. #endif // _LIBCPP_STD_VER <= 17
  1063. #if _LIBCPP_STD_VER >= 20
  1064. template <class _Tp>
  1065. _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, nullptr_t) noexcept {
  1066. return compare_three_way()(__x.get(), static_cast<typename shared_ptr<_Tp>::element_type*>(nullptr));
  1067. }
  1068. #endif
  1069. template <class _Tp>
  1070. inline _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT {
  1071. __x.swap(__y);
  1072. }
  1073. template <class _Tp, class _Up>
  1074. inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
  1075. return shared_ptr<_Tp>(__r, static_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1076. }
  1077. // LWG-2996
  1078. // We don't backport because it is an evolutionary change.
  1079. #if _LIBCPP_STD_VER >= 20
  1080. template <class _Tp, class _Up>
  1081. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
  1082. return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1083. }
  1084. #endif
  1085. template <class _Tp, class _Up>
  1086. inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
  1087. typedef typename shared_ptr<_Tp>::element_type _ET;
  1088. _ET* __p = dynamic_cast<_ET*>(__r.get());
  1089. return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
  1090. }
  1091. // LWG-2996
  1092. // We don't backport because it is an evolutionary change.
  1093. #if _LIBCPP_STD_VER >= 20
  1094. template <class _Tp, class _Up>
  1095. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
  1096. auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get());
  1097. return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>();
  1098. }
  1099. #endif
  1100. template <class _Tp, class _Up>
  1101. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
  1102. typedef typename shared_ptr<_Tp>::element_type _RTp;
  1103. return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
  1104. }
  1105. // LWG-2996
  1106. // We don't backport because it is an evolutionary change.
  1107. #if _LIBCPP_STD_VER >= 20
  1108. template <class _Tp, class _Up>
  1109. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
  1110. return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1111. }
  1112. #endif
  1113. template <class _Tp, class _Up>
  1114. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
  1115. return shared_ptr<_Tp>(__r, reinterpret_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1116. }
  1117. // LWG-2996
  1118. // We don't backport because it is an evolutionary change.
  1119. #if _LIBCPP_STD_VER >= 20
  1120. template <class _Tp, class _Up>
  1121. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
  1122. return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1123. }
  1124. #endif
  1125. #ifndef _LIBCPP_HAS_NO_RTTI
  1126. template <class _Dp, class _Tp>
  1127. inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT {
  1128. return __p.template __get_deleter<_Dp>();
  1129. }
  1130. #endif // _LIBCPP_HAS_NO_RTTI
  1131. template <class _Tp>
  1132. class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr {
  1133. public:
  1134. #if _LIBCPP_STD_VER >= 17
  1135. typedef remove_extent_t<_Tp> element_type;
  1136. #else
  1137. typedef _Tp element_type;
  1138. #endif
  1139. private:
  1140. element_type* __ptr_;
  1141. __shared_weak_count* __cntrl_;
  1142. public:
  1143. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
  1144. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  1145. _LIBCPP_HIDE_FROM_ABI weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT;
  1146. _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr const& __r) _NOEXCEPT;
  1147. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  1148. _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT;
  1149. _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr&& __r) _NOEXCEPT;
  1150. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  1151. _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT;
  1152. _LIBCPP_HIDE_FROM_ABI ~weak_ptr();
  1153. _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
  1154. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  1155. _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
  1156. _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
  1157. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  1158. _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
  1159. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
  1160. _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
  1161. _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr& __r) _NOEXCEPT;
  1162. _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT;
  1163. _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
  1164. _LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT { return __cntrl_ == nullptr || __cntrl_->use_count() == 0; }
  1165. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT;
  1166. template <class _Up>
  1167. _LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT {
  1168. return __cntrl_ < __r.__cntrl_;
  1169. }
  1170. template <class _Up>
  1171. _LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT {
  1172. return __cntrl_ < __r.__cntrl_;
  1173. }
  1174. template <class _Up>
  1175. friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
  1176. template <class _Up>
  1177. friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
  1178. };
  1179. #if _LIBCPP_STD_VER >= 17
  1180. template <class _Tp>
  1181. weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
  1182. #endif
  1183. template <class _Tp>
  1184. inline _LIBCPP_CONSTEXPR weak_ptr<_Tp>::weak_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
  1185. template <class _Tp>
  1186. inline weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  1187. if (__cntrl_)
  1188. __cntrl_->__add_weak();
  1189. }
  1190. template <class _Tp>
  1191. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
  1192. inline weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  1193. if (__cntrl_)
  1194. __cntrl_->__add_weak();
  1195. }
  1196. template <class _Tp>
  1197. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
  1198. inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {
  1199. shared_ptr<_Yp> __s = __r.lock();
  1200. *this = weak_ptr<_Tp>(__s);
  1201. }
  1202. template <class _Tp>
  1203. inline weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
  1204. __r.__ptr_ = nullptr;
  1205. __r.__cntrl_ = nullptr;
  1206. }
  1207. template <class _Tp>
  1208. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
  1209. inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {
  1210. shared_ptr<_Yp> __s = __r.lock();
  1211. *this = weak_ptr<_Tp>(__s);
  1212. __r.reset();
  1213. }
  1214. template <class _Tp>
  1215. weak_ptr<_Tp>::~weak_ptr() {
  1216. if (__cntrl_)
  1217. __cntrl_->__release_weak();
  1218. }
  1219. template <class _Tp>
  1220. inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT {
  1221. weak_ptr(__r).swap(*this);
  1222. return *this;
  1223. }
  1224. template <class _Tp>
  1225. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
  1226. inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT {
  1227. weak_ptr(__r).swap(*this);
  1228. return *this;
  1229. }
  1230. template <class _Tp>
  1231. inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT {
  1232. weak_ptr(std::move(__r)).swap(*this);
  1233. return *this;
  1234. }
  1235. template <class _Tp>
  1236. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
  1237. inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT {
  1238. weak_ptr(std::move(__r)).swap(*this);
  1239. return *this;
  1240. }
  1241. template <class _Tp>
  1242. template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
  1243. inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT {
  1244. weak_ptr(__r).swap(*this);
  1245. return *this;
  1246. }
  1247. template <class _Tp>
  1248. inline void weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT {
  1249. std::swap(__ptr_, __r.__ptr_);
  1250. std::swap(__cntrl_, __r.__cntrl_);
  1251. }
  1252. template <class _Tp>
  1253. inline _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT {
  1254. __x.swap(__y);
  1255. }
  1256. template <class _Tp>
  1257. inline void weak_ptr<_Tp>::reset() _NOEXCEPT {
  1258. weak_ptr().swap(*this);
  1259. }
  1260. template <class _Tp>
  1261. shared_ptr<_Tp> weak_ptr<_Tp>::lock() const _NOEXCEPT {
  1262. shared_ptr<_Tp> __r;
  1263. __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
  1264. if (__r.__cntrl_)
  1265. __r.__ptr_ = __ptr_;
  1266. return __r;
  1267. }
  1268. #if _LIBCPP_STD_VER >= 17
  1269. template <class _Tp = void>
  1270. struct owner_less;
  1271. #else
  1272. template <class _Tp>
  1273. struct owner_less;
  1274. #endif
  1275. template <class _Tp>
  1276. struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > : __binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> {
  1277. _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {
  1278. return __x.owner_before(__y);
  1279. }
  1280. _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {
  1281. return __x.owner_before(__y);
  1282. }
  1283. _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {
  1284. return __x.owner_before(__y);
  1285. }
  1286. };
  1287. template <class _Tp>
  1288. struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > : __binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> {
  1289. _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {
  1290. return __x.owner_before(__y);
  1291. }
  1292. _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {
  1293. return __x.owner_before(__y);
  1294. }
  1295. _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {
  1296. return __x.owner_before(__y);
  1297. }
  1298. };
  1299. #if _LIBCPP_STD_VER >= 17
  1300. template <>
  1301. struct _LIBCPP_TEMPLATE_VIS owner_less<void> {
  1302. template <class _Tp, class _Up>
  1303. _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT {
  1304. return __x.owner_before(__y);
  1305. }
  1306. template <class _Tp, class _Up>
  1307. _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT {
  1308. return __x.owner_before(__y);
  1309. }
  1310. template <class _Tp, class _Up>
  1311. _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT {
  1312. return __x.owner_before(__y);
  1313. }
  1314. template <class _Tp, class _Up>
  1315. _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT {
  1316. return __x.owner_before(__y);
  1317. }
  1318. typedef void is_transparent;
  1319. };
  1320. #endif
  1321. template <class _Tp>
  1322. class _LIBCPP_TEMPLATE_VIS enable_shared_from_this {
  1323. mutable weak_ptr<_Tp> __weak_this_;
  1324. protected:
  1325. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR enable_shared_from_this() _NOEXCEPT {}
  1326. _LIBCPP_HIDE_FROM_ABI enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
  1327. _LIBCPP_HIDE_FROM_ABI enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT { return *this; }
  1328. _LIBCPP_HIDE_FROM_ABI ~enable_shared_from_this() {}
  1329. public:
  1330. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); }
  1331. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const { return shared_ptr<const _Tp>(__weak_this_); }
  1332. #if _LIBCPP_STD_VER >= 17
  1333. _LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; }
  1334. _LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; }
  1335. #endif // _LIBCPP_STD_VER >= 17
  1336. template <class _Up>
  1337. friend class shared_ptr;
  1338. };
  1339. template <class _Tp>
  1340. struct _LIBCPP_TEMPLATE_VIS hash;
  1341. template <class _Tp>
  1342. struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > {
  1343. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  1344. _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;
  1345. _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
  1346. #endif
  1347. _LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT {
  1348. return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
  1349. }
  1350. };
  1351. template <class _CharT, class _Traits, class _Yp>
  1352. inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  1353. operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
  1354. #if !defined(_LIBCPP_HAS_NO_THREADS)
  1355. class _LIBCPP_EXPORTED_FROM_ABI __sp_mut {
  1356. void* __lx_;
  1357. public:
  1358. void lock() _NOEXCEPT;
  1359. void unlock() _NOEXCEPT;
  1360. private:
  1361. _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
  1362. __sp_mut(const __sp_mut&);
  1363. __sp_mut& operator=(const __sp_mut&);
  1364. friend _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);
  1365. };
  1366. _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);
  1367. template <class _Tp>
  1368. inline _LIBCPP_HIDE_FROM_ABI bool atomic_is_lock_free(const shared_ptr<_Tp>*) {
  1369. return false;
  1370. }
  1371. template <class _Tp>
  1372. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p) {
  1373. __sp_mut& __m = std::__get_sp_mut(__p);
  1374. __m.lock();
  1375. shared_ptr<_Tp> __q = *__p;
  1376. __m.unlock();
  1377. return __q;
  1378. }
  1379. template <class _Tp>
  1380. inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) {
  1381. return std::atomic_load(__p);
  1382. }
  1383. template <class _Tp>
  1384. _LIBCPP_HIDE_FROM_ABI void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) {
  1385. __sp_mut& __m = std::__get_sp_mut(__p);
  1386. __m.lock();
  1387. __p->swap(__r);
  1388. __m.unlock();
  1389. }
  1390. template <class _Tp>
  1391. inline _LIBCPP_HIDE_FROM_ABI void atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) {
  1392. std::atomic_store(__p, __r);
  1393. }
  1394. template <class _Tp>
  1395. _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) {
  1396. __sp_mut& __m = std::__get_sp_mut(__p);
  1397. __m.lock();
  1398. __p->swap(__r);
  1399. __m.unlock();
  1400. return __r;
  1401. }
  1402. template <class _Tp>
  1403. inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
  1404. atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) {
  1405. return std::atomic_exchange(__p, __r);
  1406. }
  1407. template <class _Tp>
  1408. _LIBCPP_HIDE_FROM_ABI bool
  1409. atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) {
  1410. shared_ptr<_Tp> __temp;
  1411. __sp_mut& __m = std::__get_sp_mut(__p);
  1412. __m.lock();
  1413. if (__p->__owner_equivalent(*__v)) {
  1414. std::swap(__temp, *__p);
  1415. *__p = __w;
  1416. __m.unlock();
  1417. return true;
  1418. }
  1419. std::swap(__temp, *__v);
  1420. *__v = *__p;
  1421. __m.unlock();
  1422. return false;
  1423. }
  1424. template <class _Tp>
  1425. inline _LIBCPP_HIDE_FROM_ABI bool
  1426. atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) {
  1427. return std::atomic_compare_exchange_strong(__p, __v, __w);
  1428. }
  1429. template <class _Tp>
  1430. inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_strong_explicit(
  1431. shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) {
  1432. return std::atomic_compare_exchange_strong(__p, __v, __w);
  1433. }
  1434. template <class _Tp>
  1435. inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_weak_explicit(
  1436. shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) {
  1437. return std::atomic_compare_exchange_weak(__p, __v, __w);
  1438. }
  1439. #endif // !defined(_LIBCPP_HAS_NO_THREADS)
  1440. _LIBCPP_END_NAMESPACE_STD
  1441. _LIBCPP_POP_MACROS
  1442. #endif // _LIBCPP___MEMORY_SHARED_PTR_H