memory 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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
  10. #define _LIBCPP_MEMORY
  11. /*
  12. memory synopsis
  13. namespace std
  14. {
  15. struct allocator_arg_t { };
  16. inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
  17. template <class T, class Alloc> struct uses_allocator;
  18. template <class Ptr>
  19. struct pointer_traits
  20. {
  21. typedef Ptr pointer;
  22. typedef <details> element_type;
  23. typedef <details> difference_type;
  24. template <class U> using rebind = <details>;
  25. static pointer pointer_to(<details>);
  26. };
  27. template <class T>
  28. struct pointer_traits<T*>
  29. {
  30. typedef T* pointer;
  31. typedef T element_type;
  32. typedef ptrdiff_t difference_type;
  33. template <class U> using rebind = U*;
  34. static pointer pointer_to(<details>) noexcept; // constexpr in C++20
  35. };
  36. template <class T> constexpr T* to_address(T* p) noexcept; // C++20
  37. template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
  38. template <class Alloc>
  39. struct allocator_traits
  40. {
  41. typedef Alloc allocator_type;
  42. typedef typename allocator_type::value_type
  43. value_type;
  44. typedef Alloc::pointer | value_type* pointer;
  45. typedef Alloc::const_pointer
  46. | pointer_traits<pointer>::rebind<const value_type>
  47. const_pointer;
  48. typedef Alloc::void_pointer
  49. | pointer_traits<pointer>::rebind<void>
  50. void_pointer;
  51. typedef Alloc::const_void_pointer
  52. | pointer_traits<pointer>::rebind<const void>
  53. const_void_pointer;
  54. typedef Alloc::difference_type
  55. | pointer_traits<pointer>::difference_type
  56. difference_type;
  57. typedef Alloc::size_type
  58. | make_unsigned<difference_type>::type
  59. size_type;
  60. typedef Alloc::propagate_on_container_copy_assignment
  61. | false_type propagate_on_container_copy_assignment;
  62. typedef Alloc::propagate_on_container_move_assignment
  63. | false_type propagate_on_container_move_assignment;
  64. typedef Alloc::propagate_on_container_swap
  65. | false_type propagate_on_container_swap;
  66. typedef Alloc::is_always_equal
  67. | is_empty is_always_equal;
  68. template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
  69. template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
  70. static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
  71. static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
  72. static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
  73. template <class T, class... Args>
  74. static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
  75. template <class T>
  76. static void destroy(allocator_type& a, T* p); // constexpr in C++20
  77. static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
  78. static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
  79. };
  80. template<class Pointer>
  81. struct allocation_result {
  82. Pointer ptr;
  83. size_t count;
  84. }; // since C++23
  85. template<class Allocator>
  86. [[nodiscard]] constexpr allocation_result<typename allocator_traits<Allocator>::pointer>
  87. allocate_at_least(Allocator& a, size_t n); // since C++23
  88. template <>
  89. class allocator<void> // removed in C++20
  90. {
  91. public:
  92. typedef void* pointer;
  93. typedef const void* const_pointer;
  94. typedef void value_type;
  95. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  96. };
  97. template <class T>
  98. class allocator
  99. {
  100. public:
  101. typedef size_t size_type;
  102. typedef ptrdiff_t difference_type;
  103. typedef T* pointer; // deprecated in C++17, removed in C++20
  104. typedef const T* const_pointer; // deprecated in C++17, removed in C++20
  105. typedef typename add_lvalue_reference<T>::type
  106. reference; // deprecated in C++17, removed in C++20
  107. typedef typename add_lvalue_reference<const T>::type
  108. const_reference; // deprecated in C++17, removed in C++20
  109. typedef T value_type;
  110. template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
  111. typedef true_type propagate_on_container_move_assignment;
  112. typedef true_type is_always_equal;
  113. constexpr allocator() noexcept; // constexpr in C++20
  114. constexpr allocator(const allocator&) noexcept; // constexpr in C++20
  115. template <class U>
  116. constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
  117. ~allocator(); // constexpr in C++20
  118. pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
  119. const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
  120. T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
  121. T* allocate(size_t n); // constexpr in C++20
  122. void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
  123. size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
  124. template<class U, class... Args>
  125. void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
  126. template <class U>
  127. void destroy(U* p); // deprecated in C++17, removed in C++20
  128. };
  129. template <class T, class U>
  130. bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
  131. template <class T, class U>
  132. bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // removed in C++20
  133. template <class OutputIterator, class T>
  134. class raw_storage_iterator // deprecated in C++17, removed in C++20
  135. : public iterator<output_iterator_tag, void, void, void, void> // until C++17
  136. {
  137. public:
  138. typedef output_iterator_tag iterator_category;
  139. typedef void value_type;
  140. typedef void difference_type; // until C++20
  141. typedef ptrdiff_t difference_type; // since C++20
  142. typedef void pointer;
  143. typedef void reference;
  144. explicit raw_storage_iterator(OutputIterator x);
  145. raw_storage_iterator& operator*();
  146. raw_storage_iterator& operator=(const T& element);
  147. raw_storage_iterator& operator++();
  148. raw_storage_iterator operator++(int);
  149. };
  150. template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
  151. template <class T> void return_temporary_buffer(T* p) noexcept;
  152. template <class T> T* addressof(T& r) noexcept;
  153. template <class T> T* addressof(const T&& r) noexcept = delete;
  154. template <class InputIterator, class ForwardIterator>
  155. ForwardIterator
  156. uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
  157. namespace ranges {
  158. template<class InputIterator, class OutputIterator>
  159. using uninitialized_copy_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  160. template<input_iterator InputIterator, sentinel-for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel2>
  161. requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
  162. uninitialized_copy_result<InputIterator, OutputIterator>
  163. uninitialized_copy(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
  164. template<input_range InputRange, nothrow-forward-range OutputRange>
  165. requires constructible_from<range_value_t<OutputRange>, range_reference_t<InputRange>>
  166. uninitialized_copy_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
  167. uninitialized_copy(InputRange&& in_range, OutputRange&& out_range); // since C++20
  168. }
  169. template <class InputIterator, class Size, class ForwardIterator>
  170. ForwardIterator
  171. uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
  172. namespace ranges {
  173. template<class InputIterator, class OutputIterator>
  174. using uninitialized_copy_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  175. template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
  176. requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
  177. uninitialized_copy_n_result<InputIterator, OutputIterator>
  178. uninitialized_copy_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
  179. }
  180. template <class ForwardIterator, class T>
  181. void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
  182. namespace ranges {
  183. template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel, class T>
  184. requires constructible_from<iter_value_t<ForwardIterator>, const T&>
  185. ForwardIterator uninitialized_fill(ForwardIterator first, Sentinel last, const T& x); // since C++20
  186. template <nothrow-forward-range ForwardRange, class T>
  187. requires constructible_from<range_value_t<ForwardRange>, const T&>
  188. borrowed_iterator_t<ForwardRange> uninitialized_fill(ForwardRange&& range, const T& x); // since C++20
  189. }
  190. template <class ForwardIterator, class Size, class T>
  191. ForwardIterator
  192. uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
  193. namespace ranges {
  194. template <nothrow-forward-iterator ForwardIterator, class T>
  195. requires constructible_from<iter_value_t<ForwardIterator>, const T&>
  196. ForwardIterator uninitialized_fill_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
  197. }
  198. template <class T, class ...Args>
  199. constexpr T* construct_at(T* location, Args&& ...args); // since C++20
  200. namespace ranges {
  201. template<class T, class... Args>
  202. constexpr T* construct_at(T* location, Args&&... args); // since C++20
  203. }
  204. template <class T>
  205. void destroy_at(T* location); // constexpr in C++20
  206. namespace ranges {
  207. template<destructible T>
  208. constexpr void destroy_at(T* location) noexcept; // since C++20
  209. }
  210. template <class ForwardIterator>
  211. void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
  212. namespace ranges {
  213. template<nothrow-input-iterator InputIterator, nothrow-sentinel-for<InputIterator> Sentinel>
  214. requires destructible<iter_value_t<InputIterator>>
  215. constexpr InputIterator destroy(InputIterator first, Sentinel last) noexcept; // since C++20
  216. template<nothrow-input-range InputRange>
  217. requires destructible<range_value_t<InputRange>>
  218. constexpr borrowed_iterator_t<InputRange> destroy(InputRange&& range) noexcept; // since C++20
  219. }
  220. template <class ForwardIterator, class Size>
  221. ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
  222. namespace ranges {
  223. template<nothrow-input-iterator InputIterator>
  224. requires destructible<iter_value_t<InputIterator>>
  225. constexpr InputIterator destroy_n(InputIterator first, iter_difference_t<InputIterator> n) noexcept; // since C++20
  226. }
  227. template <class InputIterator, class ForwardIterator>
  228. ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
  229. namespace ranges {
  230. template<class InputIterator, class OutputIterator>
  231. using uninitialized_move_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  232. template <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<O> Sentinel2>
  233. requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
  234. uninitialized_move_result<InputIterator, OutputIterator>
  235. uninitialized_move(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
  236. template<input_range InputRange, nothrow-forward-range OutputRange>
  237. requires constructible_from<range_value_t<OutputRange>, range_rvalue_reference_t<InputRange>>
  238. uninitialized_move_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
  239. uninitialized_move(InputRange&& in_range, OutputRange&& out_range); // since C++20
  240. }
  241. template <class InputIterator, class Size, class ForwardIterator>
  242. pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
  243. namespace ranges {
  244. template<class InputIterator, class OutputIterator>
  245. using uninitialized_move_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
  246. template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
  247. requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
  248. uninitialized_move_n_result<InputIterator, OutputIterator>
  249. uninitialized_move_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
  250. }
  251. template <class ForwardIterator>
  252. void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
  253. namespace ranges {
  254. template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
  255. requires default_initializable<iter_value_t<ForwardIterator>>
  256. ForwardIterator uninitialized_value_construct(ForwardIterator first, Sentinel last); // since C++20
  257. template <nothrow-forward-range ForwardRange>
  258. requires default_initializable<range_value_t<ForwardRange>>
  259. borrowed_iterator_t<ForwardRange> uninitialized_value_construct(ForwardRange&& r); // since C++20
  260. }
  261. template <class ForwardIterator, class Size>
  262. ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
  263. namespace ranges {
  264. template <nothrow-forward-iterator ForwardIterator>
  265. requires default_initializable<iter_value_t<ForwardIterator>>
  266. ForwardIterator uninitialized_value_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
  267. }
  268. template <class ForwardIterator>
  269. void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
  270. namespace ranges {
  271. template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
  272. requires default_initializable<iter_value_t<ForwardIterator>>
  273. ForwardIterator uninitialized_default_construct(ForwardIterator first, Sentinel last); // since C++20
  274. template <nothrow-forward-range ForwardRange>
  275. requires default_initializable<range_value_t<ForwardRange>>
  276. borrowed_iterator_t<ForwardRange> uninitialized_default_construct(ForwardRange&& r); // since C++20
  277. }
  278. template <class ForwardIterator, class Size>
  279. ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
  280. namespace ranges {
  281. template <nothrow-forward-iterator ForwardIterator>
  282. requires default_initializable<iter_value_t<ForwardIterator>>
  283. ForwardIterator uninitialized_default_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
  284. }
  285. template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
  286. template<class X>
  287. class auto_ptr // deprecated in C++11, removed in C++17
  288. {
  289. public:
  290. typedef X element_type;
  291. explicit auto_ptr(X* p =0) throw();
  292. auto_ptr(auto_ptr&) throw();
  293. template<class Y> auto_ptr(auto_ptr<Y>&) throw();
  294. auto_ptr& operator=(auto_ptr&) throw();
  295. template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
  296. auto_ptr& operator=(auto_ptr_ref<X> r) throw();
  297. ~auto_ptr() throw();
  298. typename add_lvalue_reference<X>::type operator*() const throw();
  299. X* operator->() const throw();
  300. X* get() const throw();
  301. X* release() throw();
  302. void reset(X* p =0) throw();
  303. auto_ptr(auto_ptr_ref<X>) throw();
  304. template<class Y> operator auto_ptr_ref<Y>() throw();
  305. template<class Y> operator auto_ptr<Y>() throw();
  306. };
  307. template <class T>
  308. struct default_delete
  309. {
  310. constexpr default_delete() noexcept = default;
  311. template <class U> constexpr default_delete(const default_delete<U>&) noexcept; // constexpr since C++23
  312. constexpr void operator()(T*) const noexcept; // constexpr since C++23
  313. };
  314. template <class T>
  315. struct default_delete<T[]>
  316. {
  317. constexpr default_delete() noexcept = default;
  318. template <class U> constexpr default_delete(const default_delete <U[]>&) noexcept; // constexpr since C++23
  319. constexpr void operator()(T*) const noexcept; // constexpr since C++23
  320. template <class U> void operator()(U*) const = delete;
  321. };
  322. template <class T, class D = default_delete<T>>
  323. class unique_ptr
  324. {
  325. public:
  326. typedef see below pointer;
  327. typedef T element_type;
  328. typedef D deleter_type;
  329. // constructors
  330. constexpr unique_ptr() noexcept;
  331. constexpr explicit unique_ptr(pointer p) noexcept; // constexpr since C++23
  332. constexpr unique_ptr(pointer p, see below d1) noexcept; // constexpr since C++23
  333. constexpr unique_ptr(pointer p, see below d2) noexcept; // constexpr since C++23
  334. constexpr unique_ptr(unique_ptr&& u) noexcept; // constexpr since C++23
  335. constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
  336. template <class U, class E>
  337. constexpr unique_ptr(unique_ptr<U, E>&& u) noexcept; // constexpr since C++23
  338. template <class U>
  339. unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
  340. // destructor
  341. constexpr ~unique_ptr(); // constexpr since C++23
  342. // assignment
  343. constexpr unique_ptr& operator=(unique_ptr&& u) noexcept; // constexpr since C++23
  344. template <class U, class E>
  345. constexpr unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; // constexpr since C++23
  346. constexpr unique_ptr& operator=(nullptr_t) noexcept; // constexpr since C++23
  347. // observers
  348. typename constexpr add_lvalue_reference<T>::type operator*() const; // constexpr since C++23
  349. constexpr pointer operator->() const noexcept; // constexpr since C++23
  350. constexpr pointer get() const noexcept; // constexpr since C++23
  351. constexpr deleter_type& get_deleter() noexcept; // constexpr since C++23
  352. constexpr const deleter_type& get_deleter() const noexcept; // constexpr since C++23
  353. constexpr explicit operator bool() const noexcept; // constexpr since C++23
  354. // modifiers
  355. constexpr pointer release() noexcept; // constexpr since C++23
  356. constexpr void reset(pointer p = pointer()) noexcept; // constexpr since C++23
  357. constexpr void swap(unique_ptr& u) noexcept; // constexpr since C++23
  358. };
  359. template <class T, class D>
  360. class unique_ptr<T[], D>
  361. {
  362. public:
  363. typedef implementation-defined pointer;
  364. typedef T element_type;
  365. typedef D deleter_type;
  366. // constructors
  367. constexpr unique_ptr() noexcept;
  368. constexpr explicit unique_ptr(pointer p) noexcept; // constexpr since C++23
  369. constexpr unique_ptr(pointer p, see below d) noexcept; // constexpr since C++23
  370. constexpr unique_ptr(pointer p, see below d) noexcept; // constexpr since C++23
  371. constexpr unique_ptr(unique_ptr&& u) noexcept; // constexpr since C++23
  372. template <class U, class E>
  373. constexpr unique_ptr(unique_ptr <U, E>&& u) noexcept; // constexpr since C++23
  374. constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
  375. // destructor
  376. constexpr ~unique_ptr(); // constexpr since C++23
  377. // assignment
  378. constexpr unique_ptr& operator=(unique_ptr&& u) noexcept; // constexpr since C++23
  379. template <class U, class E>
  380. constexpr unique_ptr& operator=(unique_ptr <U, E>&& u) noexcept; // constexpr since C++23
  381. constexpr unique_ptr& operator=(nullptr_t) noexcept; // constexpr since C++23
  382. // observers
  383. constexpr T& operator[](size_t i) const; // constexpr since C++23
  384. constexpr pointer get() const noexcept; // constexpr since C++23
  385. constexpr deleter_type& get_deleter() noexcept; // constexpr since C++23
  386. constexpr const deleter_type& get_deleter() const noexcept; // constexpr since C++23
  387. constexpr explicit operator bool() const noexcept; // constexpr since C++23
  388. // modifiers
  389. constexpr pointer release() noexcept; // constexpr since C++23
  390. constexpr void reset(pointer p = pointer()) noexcept; // constexpr since C++23
  391. constexpr void reset(nullptr_t) noexcept; // constexpr since C++23
  392. template <class U> void reset(U) = delete;
  393. constexpr void swap(unique_ptr& u) noexcept; // constexpr since C++23
  394. };
  395. template <class T, class D>
  396. constexpr void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; // constexpr since C++23
  397. template <class T1, class D1, class T2, class D2>
  398. constexpr bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // constexpr since C++23
  399. template <class T1, class D1, class T2, class D2>
  400. bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // removed in C++20
  401. template <class T1, class D1, class T2, class D2>
  402. bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  403. template <class T1, class D1, class T2, class D2>
  404. bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  405. template <class T1, class D1, class T2, class D2>
  406. bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  407. template <class T1, class D1, class T2, class D2>
  408. bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  409. template<class T1, class D1, class T2, class D2>
  410. requires three_way_comparable_with<typename unique_ptr<T1, D1>::pointer,
  411. typename unique_ptr<T2, D2>::pointer>
  412. compare_three_way_result_t<typename unique_ptr<T1, D1>::pointer,
  413. typename unique_ptr<T2, D2>::pointer>
  414. operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // C++20
  415. template <class T, class D>
  416. constexpr bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; // constexpr since C++23
  417. template <class T, class D>
  418. bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; // removed in C++20
  419. template <class T, class D>
  420. bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; // removed in C++20
  421. template <class T, class D>
  422. bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; // removed in C++20
  423. template <class T, class D>
  424. constexpr bool operator<(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23
  425. template <class T, class D>
  426. constexpr bool operator<(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23
  427. template <class T, class D>
  428. constexpr bool operator<=(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23
  429. template <class T, class D>
  430. constexpr bool operator<=(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23
  431. template <class T, class D>
  432. constexpr bool operator>(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23
  433. template <class T, class D>
  434. constexpr bool operator>(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23
  435. template <class T, class D>
  436. constexpr bool operator>=(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23
  437. template <class T, class D>
  438. constexpr bool operator>=(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23
  439. template<class T, class D>
  440. requires three_way_comparable<typename unique_ptr<T, D>::pointer>
  441. compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
  442. constexpr operator<=>(const unique_ptr<T, D>& x, nullptr_t); // C++20, constexpr since C++23
  443. class bad_weak_ptr
  444. : public std::exception
  445. {
  446. bad_weak_ptr() noexcept;
  447. };
  448. template<class T, class... Args>
  449. constexpr unique_ptr<T> make_unique(Args&&... args); // C++14, constexpr since C++23
  450. template<class T>
  451. constexpr unique_ptr<T> make_unique(size_t n); // C++14, constexpr since C++23
  452. template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
  453. template<class T>
  454. constexpr unique_ptr<T> make_unique_for_overwrite(); // T is not array, C++20, constexpr since C++23
  455. template<class T>
  456. constexpr unique_ptr<T> make_unique_for_overwrite(size_t n); // T is U[], C++20, constexpr since C++23
  457. template<class T, class... Args>
  458. unspecified make_unique_for_overwrite(Args&&...) = delete; // T is U[N], C++20
  459. template<class E, class T, class Y, class D>
  460. basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
  461. template<class T>
  462. class shared_ptr
  463. {
  464. public:
  465. typedef T element_type; // until C++17
  466. typedef remove_extent_t<T> element_type; // since C++17
  467. typedef weak_ptr<T> weak_type; // C++17
  468. // constructors:
  469. constexpr shared_ptr() noexcept;
  470. template<class Y> explicit shared_ptr(Y* p);
  471. template<class Y, class D> shared_ptr(Y* p, D d);
  472. template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
  473. template <class D> shared_ptr(nullptr_t p, D d);
  474. template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
  475. template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
  476. shared_ptr(const shared_ptr& r) noexcept;
  477. template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
  478. shared_ptr(shared_ptr&& r) noexcept;
  479. template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
  480. template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
  481. template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
  482. template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
  483. shared_ptr(nullptr_t) : shared_ptr() { }
  484. // destructor:
  485. ~shared_ptr();
  486. // assignment:
  487. shared_ptr& operator=(const shared_ptr& r) noexcept;
  488. template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
  489. shared_ptr& operator=(shared_ptr&& r) noexcept;
  490. template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
  491. template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
  492. template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
  493. // modifiers:
  494. void swap(shared_ptr& r) noexcept;
  495. void reset() noexcept;
  496. template<class Y> void reset(Y* p);
  497. template<class Y, class D> void reset(Y* p, D d);
  498. template<class Y, class D, class A> void reset(Y* p, D d, A a);
  499. // observers:
  500. T* get() const noexcept;
  501. T& operator*() const noexcept;
  502. T* operator->() const noexcept;
  503. long use_count() const noexcept;
  504. bool unique() const noexcept;
  505. explicit operator bool() const noexcept;
  506. template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
  507. template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
  508. };
  509. template<class T>
  510. shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
  511. template<class T, class D>
  512. shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
  513. // shared_ptr comparisons:
  514. template<class T, class U>
  515. bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  516. template<class T, class U>
  517. bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; // removed in C++20
  518. template<class T, class U>
  519. bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; // removed in C++20
  520. template<class T, class U>
  521. bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; // removed in C++20
  522. template<class T, class U>
  523. bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; // removed in C++20
  524. template<class T, class U>
  525. bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; // removed in C++20
  526. template<class T, class U>
  527. strong_ordering operator<=>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; // C++20
  528. template <class T>
  529. bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
  530. template <class T>
  531. bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; // removed in C++20
  532. template <class T>
  533. bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; // removed in C++20
  534. template <class T>
  535. bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; // removed in C++20
  536. template <class T>
  537. bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; // removed in C++20
  538. template <class T>
  539. bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; // removed in C++20
  540. template <class T>
  541. bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; // removed in C++20
  542. template <class T>
  543. bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; // removed in C++20
  544. template <class T>
  545. bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; // removed in C++20
  546. template <class T>
  547. bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; // removed in C++20
  548. template <class T>
  549. bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; // removed in C++20
  550. template <class T>
  551. bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; // removed in C++20
  552. template<class T>
  553. strong_ordering operator<=>(shared_ptr<T> const& x, nullptr_t) noexcept; // C++20
  554. // shared_ptr specialized algorithms:
  555. template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
  556. // shared_ptr casts:
  557. template<class T, class U>
  558. shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
  559. template<class T, class U>
  560. shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
  561. template<class T, class U>
  562. shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
  563. // shared_ptr I/O:
  564. template<class E, class T, class Y>
  565. basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
  566. // shared_ptr get_deleter:
  567. template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
  568. template<class T, class... Args>
  569. shared_ptr<T> make_shared(Args&&... args); // T is not an array
  570. template<class T, class A, class... Args>
  571. shared_ptr<T> allocate_shared(const A& a, Args&&... args); // T is not an array
  572. template<class T>
  573. shared_ptr<T> make_shared(size_t N); // T is U[] (since C++20)
  574. template<class T, class A>
  575. shared_ptr<T> allocate_shared(const A& a, size_t N); // T is U[] (since C++20)
  576. template<class T>
  577. shared_ptr<T> make_shared(); // T is U[N] (since C++20)
  578. template<class T, class A>
  579. shared_ptr<T> allocate_shared(const A& a); // T is U[N] (since C++20)
  580. template<class T>
  581. shared_ptr<T> make_shared(size_t N, const remove_extent_t<T>& u); // T is U[] (since C++20)
  582. template<class T, class A>
  583. shared_ptr<T> allocate_shared(const A& a, size_t N, const remove_extent_t<T>& u); // T is U[] (since C++20)
  584. template<class T> shared_ptr<T>
  585. make_shared(const remove_extent_t<T>& u); // T is U[N] (since C++20)
  586. template<class T, class A>
  587. shared_ptr<T> allocate_shared(const A& a, const remove_extent_t<T>& u); // T is U[N] (since C++20)
  588. template<class T>
  589. shared_ptr<T> make_shared_for_overwrite(); // T is not U[], C++20
  590. template<class T, class A>
  591. shared_ptr<T> allocate_shared_for_overwrite(const A& a); // T is not U[], C++20
  592. template<class T>
  593. shared_ptr<T> make_shared_for_overwrite(size_t N); // T is U[], C++20
  594. template<class T, class A>
  595. shared_ptr<T> allocate_shared_for_overwrite(const A& a, size_t N); // T is U[], C++20
  596. template<class T>
  597. class weak_ptr
  598. {
  599. public:
  600. typedef T element_type; // until C++17
  601. typedef remove_extent_t<T> element_type; // since C++17
  602. // constructors
  603. constexpr weak_ptr() noexcept;
  604. template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
  605. weak_ptr(weak_ptr const& r) noexcept;
  606. template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
  607. weak_ptr(weak_ptr&& r) noexcept; // C++14
  608. template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
  609. // destructor
  610. ~weak_ptr();
  611. // assignment
  612. weak_ptr& operator=(weak_ptr const& r) noexcept;
  613. template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
  614. template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
  615. weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
  616. template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
  617. // modifiers
  618. void swap(weak_ptr& r) noexcept;
  619. void reset() noexcept;
  620. // observers
  621. long use_count() const noexcept;
  622. bool expired() const noexcept;
  623. shared_ptr<T> lock() const noexcept;
  624. template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
  625. template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
  626. };
  627. template<class T>
  628. weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
  629. // weak_ptr specialized algorithms:
  630. template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
  631. // class owner_less:
  632. template<class T> struct owner_less;
  633. template<class T>
  634. struct owner_less<shared_ptr<T> >
  635. : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
  636. {
  637. typedef bool result_type;
  638. bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  639. bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  640. bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  641. };
  642. template<class T>
  643. struct owner_less<weak_ptr<T> >
  644. : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
  645. {
  646. typedef bool result_type;
  647. bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  648. bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  649. bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  650. };
  651. template <> // Added in C++14
  652. struct owner_less<void>
  653. {
  654. template <class _Tp, class _Up>
  655. bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
  656. template <class _Tp, class _Up>
  657. bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
  658. template <class _Tp, class _Up>
  659. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
  660. template <class _Tp, class _Up>
  661. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
  662. typedef void is_transparent;
  663. };
  664. template<class T>
  665. class enable_shared_from_this
  666. {
  667. protected:
  668. constexpr enable_shared_from_this() noexcept;
  669. enable_shared_from_this(enable_shared_from_this const&) noexcept;
  670. enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
  671. ~enable_shared_from_this();
  672. public:
  673. shared_ptr<T> shared_from_this();
  674. shared_ptr<T const> shared_from_this() const;
  675. };
  676. template<class T>
  677. bool atomic_is_lock_free(const shared_ptr<T>* p);
  678. template<class T>
  679. shared_ptr<T> atomic_load(const shared_ptr<T>* p);
  680. template<class T>
  681. shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
  682. template<class T>
  683. void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
  684. template<class T>
  685. void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
  686. template<class T>
  687. shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
  688. template<class T>
  689. shared_ptr<T>
  690. atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
  691. template<class T>
  692. bool
  693. atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
  694. template<class T>
  695. bool
  696. atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
  697. template<class T>
  698. bool
  699. atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
  700. shared_ptr<T> w, memory_order success,
  701. memory_order failure);
  702. template<class T>
  703. bool
  704. atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
  705. shared_ptr<T> w, memory_order success,
  706. memory_order failure);
  707. // Hash support
  708. template <class T> struct hash;
  709. template <class T, class D> struct hash<unique_ptr<T, D> >;
  710. template <class T> struct hash<shared_ptr<T> >;
  711. template <class T, class Alloc>
  712. inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
  713. // [ptr.align]
  714. void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
  715. template<size_t N, class T>
  716. [[nodiscard]] constexpr T* assume_aligned(T* ptr); // since C++20
  717. } // std
  718. */
  719. #include <__assert> // all public C++ headers provide the assertion handler
  720. #include <__config>
  721. #include <__memory/addressof.h>
  722. #include <__memory/align.h>
  723. #include <__memory/allocate_at_least.h>
  724. #include <__memory/allocation_guard.h>
  725. #include <__memory/allocator.h>
  726. #include <__memory/allocator_arg_t.h>
  727. #include <__memory/allocator_traits.h>
  728. #include <__memory/assume_aligned.h>
  729. #include <__memory/auto_ptr.h>
  730. #include <__memory/compressed_pair.h>
  731. #include <__memory/concepts.h>
  732. #include <__memory/construct_at.h>
  733. #include <__memory/pointer_traits.h>
  734. #include <__memory/ranges_construct_at.h>
  735. #include <__memory/ranges_uninitialized_algorithms.h>
  736. #include <__memory/raw_storage_iterator.h>
  737. #include <__memory/shared_ptr.h>
  738. #include <__memory/temporary_buffer.h>
  739. #include <__memory/uninitialized_algorithms.h>
  740. #include <__memory/unique_ptr.h>
  741. #include <__memory/uses_allocator.h>
  742. #include <__memory/uses_allocator_construction.h>
  743. #include <version>
  744. // standard-mandated includes
  745. // [memory.syn]
  746. #include <compare>
  747. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  748. # pragma GCC system_header
  749. #endif
  750. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  751. # include <atomic>
  752. # include <concepts>
  753. # include <cstddef>
  754. # include <cstdint>
  755. # include <cstdlib>
  756. # include <cstring>
  757. # include <iosfwd>
  758. # include <iterator>
  759. # include <new>
  760. # include <stdexcept>
  761. # include <tuple>
  762. # include <type_traits>
  763. # include <typeinfo>
  764. # include <utility>
  765. #endif
  766. #endif // _LIBCPP_MEMORY