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