2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_MEMORY
11 #define _LIBCPP_MEMORY
19 struct allocator_arg_t { };
20 inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22 template <class T, class Alloc> struct uses_allocator;
28 typedef <details> element_type;
29 typedef <details> difference_type;
31 template <class U> using rebind = <details>;
33 static pointer pointer_to(<details>);
37 struct pointer_traits<T*>
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
43 template <class U> using rebind = U*;
45 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
48 template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49 template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
51 template <class Alloc>
52 struct allocator_traits
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
68 typedef Alloc::difference_type
69 | pointer_traits<pointer>::difference_type
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
74 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
80 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
83 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
84 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
86 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
89 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
95 static void destroy(allocator_type& a, T* p); // constexpr in C++20
97 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
102 class allocator<void> // removed in C++20
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer; // deprecated in C++17, removed in C++20
119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
120 typedef typename add_lvalue_reference<T>::type
121 reference; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<const T>::type
123 const_reference; // deprecated in C++17, removed in C++20
125 typedef T value_type;
127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
129 typedef true_type propagate_on_container_move_assignment;
130 typedef true_type is_always_equal;
132 constexpr allocator() noexcept; // constexpr in C++20
133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
136 ~allocator(); // constexpr in C++20
137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
140 T* allocate(size_t n); // constexpr in C++20
141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
143 template<class U, class... Args>
144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
146 void destroy(U* p); // deprecated in C++17, removed in C++20
149 template <class T, class U>
150 bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
152 template <class T, class U>
153 bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
155 template <class OutputIterator, class T>
156 class raw_storage_iterator // deprecated in C++17, removed in C++20
157 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
160 typedef output_iterator_tag iterator_category;
161 typedef void value_type;
162 typedef void difference_type; // until C++20
163 typedef ptrdiff_t difference_type; // since C++20
164 typedef void pointer;
165 typedef void reference;
167 explicit raw_storage_iterator(OutputIterator x);
168 raw_storage_iterator& operator*();
169 raw_storage_iterator& operator=(const T& element);
170 raw_storage_iterator& operator++();
171 raw_storage_iterator operator++(int);
174 template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
175 template <class T> void return_temporary_buffer(T* p) noexcept;
177 template <class T> T* addressof(T& r) noexcept;
178 template <class T> T* addressof(const T&& r) noexcept = delete;
180 template <class InputIterator, class ForwardIterator>
182 uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
186 template<class InputIterator, class OutputIterator>
187 using uninitialized_copy_result = in_out_result<InputIterator, OutputIterator>; // since C++20
189 template<input_iterator InputIterator, sentinel-for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel2>
190 requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
191 uninitialized_copy_result<InputIterator, OutputIterator>
192 uninitialized_copy(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
194 template<input_range InputRange, nothrow-forward-range OutputRange>
195 requires constructible_from<range_value_t<OutputRange>, range_reference_t<InputRange>>
196 uninitialized_copy_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
197 uninitialized_copy(InputRange&& in_range, OutputRange&& out_range); // since C++20
201 template <class InputIterator, class Size, class ForwardIterator>
203 uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
207 template<class InputIterator, class OutputIterator>
208 using uninitialized_copy_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
210 template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
211 requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
212 uninitialized_copy_n_result<InputIterator, OutputIterator>
213 uninitialized_copy_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
217 template <class ForwardIterator, class T>
218 void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
222 template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel, class T>
223 requires constructible_from<iter_value_t<ForwardIterator>, const T&>
224 ForwardIterator uninitialized_fill(ForwardIterator first, Sentinel last, const T& x); // since C++20
226 template <nothrow-forward-range ForwardRange, class T>
227 requires constructible_from<range_value_t<ForwardRange>, const T&>
228 borrowed_iterator_t<ForwardRange> uninitialized_fill(ForwardRange&& range, const T& x); // since C++20
232 template <class ForwardIterator, class Size, class T>
234 uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
238 template <nothrow-forward-iterator ForwardIterator, class T>
239 requires constructible_from<iter_value_t<ForwardIterator>, const T&>
240 ForwardIterator uninitialized_fill_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
244 template <class T, class ...Args>
245 constexpr T* construct_at(T* location, Args&& ...args); // since C++20
248 template<class T, class... Args>
249 constexpr T* construct_at(T* location, Args&&... args); // since C++20
253 void destroy_at(T* location); // constexpr in C++20
256 template<destructible T>
257 constexpr void destroy_at(T* location) noexcept; // since C++20
260 template <class ForwardIterator>
261 void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
264 template<nothrow-input-iterator InputIterator, nothrow-sentinel-for<InputIterator> Sentinel>
265 requires destructible<iter_value_t<InputIterator>>
266 constexpr InputIterator destroy(InputIterator first, Sentinel last) noexcept; // since C++20
267 template<nothrow-input-range InputRange>
268 requires destructible<range_value_t<InputRange>>
269 constexpr borrowed_iterator_t<InputRange> destroy(InputRange&& range) noexcept; // since C++20
272 template <class ForwardIterator, class Size>
273 ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
276 template<nothrow-input-iterator InputIterator>
277 requires destructible<iter_value_t<InputIterator>>
278 constexpr InputIterator destroy_n(InputIterator first, iter_difference_t<InputIterator> n) noexcept; // since C++20
281 template <class InputIterator, class ForwardIterator>
282 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
286 template<class InputIterator, class OutputIterator>
287 using uninitialized_move_result = in_out_result<InputIterator, OutputIterator>; // since C++20
289 template <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<O> Sentinel2>
290 requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
291 uninitialized_move_result<InputIterator, OutputIterator>
292 uninitialized_move(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
294 template<input_range InputRange, nothrow-forward-range OutputRange>
295 requires constructible_from<range_value_t<OutputRange>, range_rvalue_reference_t<InputRange>>
296 uninitialized_move_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
297 uninitialized_move(InputRange&& in_range, OutputRange&& out_range); // since C++20
301 template <class InputIterator, class Size, class ForwardIterator>
302 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
306 template<class InputIterator, class OutputIterator>
307 using uninitialized_move_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
309 template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
310 requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
311 uninitialized_move_n_result<InputIterator, OutputIterator>
312 uninitialized_move_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
316 template <class ForwardIterator>
317 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
321 template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
322 requires default_initializable<iter_value_t<ForwardIterator>>
323 ForwardIterator uninitialized_value_construct(ForwardIterator first, Sentinel last); // since C++20
325 template <nothrow-forward-range ForwardRange>
326 requires default_initializable<range_value_t<ForwardRange>>
327 borrowed_iterator_t<ForwardRange> uninitialized_value_construct(ForwardRange&& r); // since C++20
331 template <class ForwardIterator, class Size>
332 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
336 template <nothrow-forward-iterator ForwardIterator>
337 requires default_initializable<iter_value_t<ForwardIterator>>
338 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
342 template <class ForwardIterator>
343 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
347 template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
348 requires default_initializable<iter_value_t<ForwardIterator>>
349 ForwardIterator uninitialized_default_construct(ForwardIterator first, Sentinel last); // since C++20
351 template <nothrow-forward-range ForwardRange>
352 requires default_initializable<range_value_t<ForwardRange>>
353 borrowed_iterator_t<ForwardRange> uninitialized_default_construct(ForwardRange&& r); // since C++20
357 template <class ForwardIterator, class Size>
358 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
362 template <nothrow-forward-iterator ForwardIterator>
363 requires default_initializable<iter_value_t<ForwardIterator>>
364 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
368 template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
371 class auto_ptr // deprecated in C++11, removed in C++17
374 typedef X element_type;
376 explicit auto_ptr(X* p =0) throw();
377 auto_ptr(auto_ptr&) throw();
378 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
379 auto_ptr& operator=(auto_ptr&) throw();
380 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
381 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
384 typename add_lvalue_reference<X>::type operator*() const throw();
385 X* operator->() const throw();
386 X* get() const throw();
387 X* release() throw();
388 void reset(X* p =0) throw();
390 auto_ptr(auto_ptr_ref<X>) throw();
391 template<class Y> operator auto_ptr_ref<Y>() throw();
392 template<class Y> operator auto_ptr<Y>() throw();
396 struct default_delete
398 constexpr default_delete() noexcept = default;
399 template <class U> default_delete(const default_delete<U>&) noexcept;
401 void operator()(T*) const noexcept;
405 struct default_delete<T[]>
407 constexpr default_delete() noexcept = default;
408 void operator()(T*) const noexcept;
409 template <class U> void operator()(U*) const = delete;
412 template <class T, class D = default_delete<T>>
416 typedef see below pointer;
417 typedef T element_type;
418 typedef D deleter_type;
421 constexpr unique_ptr() noexcept;
422 explicit unique_ptr(pointer p) noexcept;
423 unique_ptr(pointer p, see below d1) noexcept;
424 unique_ptr(pointer p, see below d2) noexcept;
425 unique_ptr(unique_ptr&& u) noexcept;
426 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
427 template <class U, class E>
428 unique_ptr(unique_ptr<U, E>&& u) noexcept;
430 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
436 unique_ptr& operator=(unique_ptr&& u) noexcept;
437 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
438 unique_ptr& operator=(nullptr_t) noexcept;
441 typename add_lvalue_reference<T>::type operator*() const;
442 pointer operator->() const noexcept;
443 pointer get() const noexcept;
444 deleter_type& get_deleter() noexcept;
445 const deleter_type& get_deleter() const noexcept;
446 explicit operator bool() const noexcept;
449 pointer release() noexcept;
450 void reset(pointer p = pointer()) noexcept;
451 void swap(unique_ptr& u) noexcept;
454 template <class T, class D>
455 class unique_ptr<T[], D>
458 typedef implementation-defined pointer;
459 typedef T element_type;
460 typedef D deleter_type;
463 constexpr unique_ptr() noexcept;
464 explicit unique_ptr(pointer p) noexcept;
465 unique_ptr(pointer p, see below d) noexcept;
466 unique_ptr(pointer p, see below d) noexcept;
467 unique_ptr(unique_ptr&& u) noexcept;
468 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
474 unique_ptr& operator=(unique_ptr&& u) noexcept;
475 unique_ptr& operator=(nullptr_t) noexcept;
478 T& operator[](size_t i) const;
479 pointer get() const noexcept;
480 deleter_type& get_deleter() noexcept;
481 const deleter_type& get_deleter() const noexcept;
482 explicit operator bool() const noexcept;
485 pointer release() noexcept;
486 void reset(pointer p = pointer()) noexcept;
487 void reset(nullptr_t) noexcept;
488 template <class U> void reset(U) = delete;
489 void swap(unique_ptr& u) noexcept;
492 template <class T, class D>
493 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
495 template <class T1, class D1, class T2, class D2>
496 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
497 template <class T1, class D1, class T2, class D2>
498 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
499 template <class T1, class D1, class T2, class D2>
500 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
501 template <class T1, class D1, class T2, class D2>
502 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
503 template <class T1, class D1, class T2, class D2>
504 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
505 template <class T1, class D1, class T2, class D2>
506 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
508 template <class T, class D>
509 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
510 template <class T, class D>
511 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
512 template <class T, class D>
513 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
514 template <class T, class D>
515 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
517 template <class T, class D>
518 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
519 template <class T, class D>
520 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
521 template <class T, class D>
522 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
523 template <class T, class D>
524 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
525 template <class T, class D>
526 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
527 template <class T, class D>
528 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
529 template <class T, class D>
530 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
531 template <class T, class D>
532 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
535 : public std::exception
537 bad_weak_ptr() noexcept;
540 template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
541 template<class T> unique_ptr<T> make_unique(size_t n); // C++14
542 template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
544 template<class E, class T, class Y, class D>
545 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
551 typedef T element_type; // until C++17
552 typedef remove_extent_t<T> element_type; // since C++17
553 typedef weak_ptr<T> weak_type; // C++17
556 constexpr shared_ptr() noexcept;
557 template<class Y> explicit shared_ptr(Y* p);
558 template<class Y, class D> shared_ptr(Y* p, D d);
559 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
560 template <class D> shared_ptr(nullptr_t p, D d);
561 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
562 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
563 shared_ptr(const shared_ptr& r) noexcept;
564 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
565 shared_ptr(shared_ptr&& r) noexcept;
566 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
567 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
568 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
569 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
570 shared_ptr(nullptr_t) : shared_ptr() { }
576 shared_ptr& operator=(const shared_ptr& r) noexcept;
577 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
578 shared_ptr& operator=(shared_ptr&& r) noexcept;
579 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
580 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
581 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
584 void swap(shared_ptr& r) noexcept;
585 void reset() noexcept;
586 template<class Y> void reset(Y* p);
587 template<class Y, class D> void reset(Y* p, D d);
588 template<class Y, class D, class A> void reset(Y* p, D d, A a);
591 T* get() const noexcept;
592 T& operator*() const noexcept;
593 T* operator->() const noexcept;
594 long use_count() const noexcept;
595 bool unique() const noexcept;
596 explicit operator bool() const noexcept;
597 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
598 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
602 shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
603 template<class T, class D>
604 shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
606 // shared_ptr comparisons:
607 template<class T, class U>
608 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
609 template<class T, class U>
610 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
611 template<class T, class U>
612 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
613 template<class T, class U>
614 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
615 template<class T, class U>
616 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
617 template<class T, class U>
618 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
621 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
623 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
625 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
627 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
629 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
631 bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
633 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
635 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
637 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
639 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
641 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
643 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
645 // shared_ptr specialized algorithms:
646 template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
649 template<class T, class U>
650 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
651 template<class T, class U>
652 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
653 template<class T, class U>
654 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
657 template<class E, class T, class Y>
658 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
660 // shared_ptr get_deleter:
661 template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
663 template<class T, class... Args>
664 shared_ptr<T> make_shared(Args&&... args);
665 template<class T, class A, class... Args>
666 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
672 typedef T element_type; // until C++17
673 typedef remove_extent_t<T> element_type; // since C++17
676 constexpr weak_ptr() noexcept;
677 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
678 weak_ptr(weak_ptr const& r) noexcept;
679 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
680 weak_ptr(weak_ptr&& r) noexcept; // C++14
681 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
687 weak_ptr& operator=(weak_ptr const& r) noexcept;
688 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
689 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
690 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
691 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
694 void swap(weak_ptr& r) noexcept;
695 void reset() noexcept;
698 long use_count() const noexcept;
699 bool expired() const noexcept;
700 shared_ptr<T> lock() const noexcept;
701 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
702 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
706 weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
708 // weak_ptr specialized algorithms:
709 template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
712 template<class T> struct owner_less;
715 struct owner_less<shared_ptr<T> >
716 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
718 typedef bool result_type;
719 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
720 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
721 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
725 struct owner_less<weak_ptr<T> >
726 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
728 typedef bool result_type;
729 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
730 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
731 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
734 template <> // Added in C++14
735 struct owner_less<void>
737 template <class _Tp, class _Up>
738 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
739 template <class _Tp, class _Up>
740 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
741 template <class _Tp, class _Up>
742 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
743 template <class _Tp, class _Up>
744 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
746 typedef void is_transparent;
750 class enable_shared_from_this
753 constexpr enable_shared_from_this() noexcept;
754 enable_shared_from_this(enable_shared_from_this const&) noexcept;
755 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
756 ~enable_shared_from_this();
758 shared_ptr<T> shared_from_this();
759 shared_ptr<T const> shared_from_this() const;
763 bool atomic_is_lock_free(const shared_ptr<T>* p);
765 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
767 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
769 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
771 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
773 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
776 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
779 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
782 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
785 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
786 shared_ptr<T> w, memory_order success,
787 memory_order failure);
790 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
791 shared_ptr<T> w, memory_order success,
792 memory_order failure);
794 template <class T> struct hash;
795 template <class T, class D> struct hash<unique_ptr<T, D> >;
796 template <class T> struct hash<shared_ptr<T> >;
798 template <class T, class Alloc>
799 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
801 void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
808 #include <__functional_base>
809 #include <__memory/addressof.h>
810 #include <__memory/allocation_guard.h>
811 #include <__memory/allocator.h>
812 #include <__memory/allocator_arg_t.h>
813 #include <__memory/allocator_traits.h>
814 #include <__memory/compressed_pair.h>
815 #include <__memory/concepts.h>
816 #include <__memory/construct_at.h>
817 #include <__memory/pointer_traits.h>
818 #include <__memory/ranges_construct_at.h>
819 #include <__memory/ranges_uninitialized_algorithms.h>
820 #include <__memory/raw_storage_iterator.h>
821 #include <__memory/shared_ptr.h>
822 #include <__memory/temporary_buffer.h>
823 #include <__memory/uninitialized_algorithms.h>
824 #include <__memory/unique_ptr.h>
825 #include <__memory/uses_allocator.h>
835 #include <type_traits>
840 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
841 # include <__memory/auto_ptr.h>
844 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
845 #pragma GCC system_header
848 _LIBCPP_BEGIN_NAMESPACE_STD
850 template <class _Alloc, class _Ptr>
851 _LIBCPP_INLINE_VISIBILITY
852 void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
853 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
854 "The specified type does not meet the requirements of Cpp17MoveInsertable");
855 typedef allocator_traits<_Alloc> _Traits;
856 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
857 _Traits::construct(__a, _VSTD::__to_address(__begin2),
858 #ifdef _LIBCPP_NO_EXCEPTIONS
859 _VSTD::move(*__begin1)
861 _VSTD::move_if_noexcept(*__begin1)
867 template <class _Alloc, class _Tp, typename enable_if<
868 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
869 is_trivially_move_constructible<_Tp>::value
871 _LIBCPP_INLINE_VISIBILITY
872 void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
873 ptrdiff_t _Np = __end1 - __begin1;
875 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
880 template <class _Alloc, class _Iter, class _Ptr>
881 _LIBCPP_INLINE_VISIBILITY
882 void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
883 typedef allocator_traits<_Alloc> _Traits;
884 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
885 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
889 template <class _Alloc, class _Source, class _Dest,
890 class _RawSource = typename remove_const<_Source>::type,
891 class _RawDest = typename remove_const<_Dest>::type,
894 is_trivially_copy_constructible<_Dest>::value &&
895 is_same<_RawSource, _RawDest>::value &&
896 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
898 _LIBCPP_INLINE_VISIBILITY
899 void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
900 ptrdiff_t _Np = __end1 - __begin1;
902 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
907 template <class _Alloc, class _Ptr>
908 _LIBCPP_INLINE_VISIBILITY
909 void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
910 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
911 "The specified type does not meet the requirements of Cpp17MoveInsertable");
912 typedef allocator_traits<_Alloc> _Traits;
913 while (__end1 != __begin1) {
914 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
915 #ifdef _LIBCPP_NO_EXCEPTIONS
916 _VSTD::move(*--__end1)
918 _VSTD::move_if_noexcept(*--__end1)
925 template <class _Alloc, class _Tp, class = typename enable_if<
926 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
927 is_trivially_move_constructible<_Tp>::value
929 _LIBCPP_INLINE_VISIBILITY
930 void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
931 ptrdiff_t _Np = __end1 - __begin1;
934 _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp));
943 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
944 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
947 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
950 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
952 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
955 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
957 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
960 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
964 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
965 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
968 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
969 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
972 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
973 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
976 _LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
978 // --- Helper for container swap --
979 template <typename _Alloc>
980 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
981 void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
982 #if _LIBCPP_STD_VER > 11
985 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
992 template <typename _Alloc>
993 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
994 void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
996 template <typename _Alloc>
997 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
998 void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
999 #if _LIBCPP_STD_VER > 11
1002 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
1005 _VSTD::__swap_allocator(__a1, __a2,
1006 integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>());
1009 template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
1010 struct __noexcept_move_assign_container : public integral_constant<bool,
1011 _Traits::propagate_on_container_move_assignment::value
1012 #if _LIBCPP_STD_VER > 14
1013 || _Traits::is_always_equal::value
1015 && is_nothrow_move_assignable<_Alloc>::value
1020 template <class _Tp, class _Alloc>
1021 struct __temp_value {
1022 typedef allocator_traits<_Alloc> _Traits;
1024 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
1027 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
1028 _Tp & get() { return *__addr(); }
1030 template<class... _Args>
1032 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
1033 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
1034 _VSTD::forward<_Args>(__args)...);
1037 ~__temp_value() { _Traits::destroy(__a, __addr()); }
1040 template<typename _Alloc, typename = void, typename = void>
1041 struct __is_allocator : false_type {};
1043 template<typename _Alloc>
1044 struct __is_allocator<_Alloc,
1045 typename __void_t<typename _Alloc::value_type>::type,
1046 typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type
1050 // __builtin_new_allocator -- A non-templated helper for allocating and
1051 // deallocating memory using __builtin_operator_new and
1052 // __builtin_operator_delete. It should be used in preference to
1053 // `std::allocator<T>` to avoid additional instantiations.
1054 struct __builtin_new_allocator {
1055 struct __builtin_new_deleter {
1056 typedef void* pointer_type;
1058 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
1059 : __size_(__size), __align_(__align) {}
1061 void operator()(void* p) const _NOEXCEPT {
1062 _VSTD::__libcpp_deallocate(p, __size_, __align_);
1070 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
1072 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
1073 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
1074 __builtin_new_deleter(__s, __align));
1077 static void __deallocate_bytes(void* __p, size_t __s,
1078 size_t __align) _NOEXCEPT {
1079 _VSTD::__libcpp_deallocate(__p, __s, __align);
1082 template <class _Tp>
1083 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
1084 static __holder_t __allocate_type(size_t __n) {
1085 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1088 template <class _Tp>
1089 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
1090 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
1091 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1096 _LIBCPP_END_NAMESPACE_STD
1098 #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
1099 # include <__pstl_memory>
1102 #endif // _LIBCPP_MEMORY