xref: /llvm-project-15.0.7/libcxx/include/memory (revision 8d23b742)
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/*
14    memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
20inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
21
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27    typedef Ptr pointer;
28    typedef <details> element_type;
29    typedef <details> difference_type;
30
31    template <class U> using rebind = <details>;
32
33    static pointer pointer_to(<details>);
34};
35
36template <class T>
37struct pointer_traits<T*>
38{
39    typedef T* pointer;
40    typedef T element_type;
41    typedef ptrdiff_t difference_type;
42
43    template <class U> using rebind = U*;
44
45    static pointer pointer_to(<details>) noexcept; // constexpr in C++20
46};
47
48template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
50
51template <class Alloc>
52struct allocator_traits
53{
54    typedef Alloc                        allocator_type;
55    typedef typename allocator_type::value_type
56                                         value_type;
57
58    typedef Alloc::pointer | value_type* pointer;
59    typedef Alloc::const_pointer
60          | pointer_traits<pointer>::rebind<const value_type>
61                                         const_pointer;
62    typedef Alloc::void_pointer
63          | pointer_traits<pointer>::rebind<void>
64                                         void_pointer;
65    typedef Alloc::const_void_pointer
66          | pointer_traits<pointer>::rebind<const void>
67                                         const_void_pointer;
68    typedef Alloc::difference_type
69          | pointer_traits<pointer>::difference_type
70                                         difference_type;
71    typedef Alloc::size_type
72          | make_unsigned<difference_type>::type
73                                         size_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;
82
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>>;
85
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
88
89    static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
90
91    template <class T, class... Args>
92    static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
93
94    template <class T>
95    static void destroy(allocator_type& a, T* p); // constexpr in C++20
96
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
99};
100
101template <>
102class allocator<void> // removed in C++20
103{
104public:
105    typedef void*                                 pointer;
106    typedef const void*                           const_pointer;
107    typedef void                                  value_type;
108
109    template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
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
124
125    typedef T         value_type;
126
127    template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
128
129    typedef true_type propagate_on_container_move_assignment;
130    typedef true_type is_always_equal;
131
132    constexpr allocator() noexcept;                      // constexpr in C++20
133    constexpr allocator(const allocator&) noexcept;      // constexpr in C++20
134    template <class U>
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
145    template <class U>
146        void destroy(U* p);                           // deprecated in C++17, removed in C++20
147};
148
149template <class T, class U>
150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
151
152template <class T, class U>
153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
154
155template <class OutputIterator, class T>
156class raw_storage_iterator // deprecated in C++17, removed in C++20
157    : public iterator<output_iterator_tag, void, void, void, void> // until C++17
158{
159public:
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;
166
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);
172};
173
174template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
175template <class T> void               return_temporary_buffer(T* p) noexcept;
176
177template <class T> T* addressof(T& r) noexcept;
178template <class T> T* addressof(const T&& r) noexcept = delete;
179
180template <class InputIterator, class ForwardIterator>
181ForwardIterator
182uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
183
184namespace ranges {
185
186template<class InputIterator, class OutputIterator>
187using uninitialized_copy_result = in_out_result<InputIterator, OutputIterator>; // since C++20
188
189template<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>>
191uninitialized_copy_result<InputIterator, OutputIterator>
192uninitialized_copy(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
193
194template<input_range InputRange, nothrow-forward-range OutputRange>
195  requires constructible_from<range_value_t<OutputRange>, range_reference_t<InputRange>>
196uninitialized_copy_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
197uninitialized_copy(InputRange&& in_range, OutputRange&& out_range); // since C++20
198
199}
200
201template <class InputIterator, class Size, class ForwardIterator>
202ForwardIterator
203uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
204
205namespace ranges {
206
207template<class InputIterator, class OutputIterator>
208using uninitialized_copy_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
209
210template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
211  requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
212uninitialized_copy_n_result<InputIterator, OutputIterator>
213uninitialized_copy_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
214
215}
216
217template <class ForwardIterator, class T>
218void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
219
220namespace ranges {
221
222template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel, class T>
223  requires constructible_from<iter_value_t<ForwardIterator>, const T&>
224ForwardIterator uninitialized_fill(ForwardIterator first, Sentinel last, const T& x); // since C++20
225
226template <nothrow-forward-range ForwardRange, class T>
227  requires constructible_from<range_value_t<ForwardRange>, const T&>
228borrowed_iterator_t<ForwardRange> uninitialized_fill(ForwardRange&& range, const T& x); // since C++20
229
230}
231
232template <class ForwardIterator, class Size, class T>
233ForwardIterator
234uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
235
236namespace ranges {
237
238template <nothrow-forward-iterator ForwardIterator, class T>
239  requires constructible_from<iter_value_t<ForwardIterator>, const T&>
240ForwardIterator uninitialized_fill_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
241
242}
243
244template <class T, class ...Args>
245constexpr T* construct_at(T* location, Args&& ...args); // since C++20
246
247template <class T>
248void destroy_at(T* location); // constexpr in C++20
249
250template <class ForwardIterator>
251void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
252
253template <class ForwardIterator, class Size>
254ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
255
256template <class InputIterator, class ForwardIterator>
257 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
258
259namespace ranges {
260
261template<class InputIterator, class OutputIterator>
262using uninitialized_move_result = in_out_result<InputIterator, OutputIterator>; // since C++20
263
264template <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<O> Sentinel2>
265  requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
266uninitialized_move_result<InputIterator, OutputIterator>
267uninitialized_move(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
268
269template<input_range InputRange, nothrow-forward-range OutputRange>
270  requires constructible_from<range_value_t<OutputRange>, range_rvalue_reference_t<InputRange>>
271uninitialized_move_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
272uninitialized_move(InputRange&& in_range, OutputRange&& out_range); // since C++20
273
274}
275
276template <class InputIterator, class Size, class ForwardIterator>
277 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
278
279namespace ranges {
280
281template<class InputIterator, class OutputIterator>
282using uninitialized_move_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
283
284template<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
285  requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
286uninitialized_move_n_result<InputIterator, OutputIterator>
287uninitialized_move_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
288
289}
290
291template <class ForwardIterator>
292 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
293
294namespace ranges {
295
296template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
297  requires default_initializable<iter_value_t<ForwardIterator>>
298 ForwardIterator uninitialized_value_construct(ForwardIterator first, Sentinel last); // since C++20
299
300template <nothrow-forward-range ForwardRange>
301  requires default_initializable<range_value_t<ForwardRange>>
302 borrowed_iterator_t<ForwardRange> uninitialized_value_construct(ForwardRange&& r); // since C++20
303
304}
305
306template <class ForwardIterator, class Size>
307 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
308
309namespace ranges {
310
311template <nothrow-forward-iterator ForwardIterator>
312  requires default_initializable<iter_value_t<ForwardIterator>>
313 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
314
315}
316
317template <class ForwardIterator>
318 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
319
320namespace ranges {
321
322template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
323  requires default_initializable<iter_value_t<ForwardIterator>>
324 ForwardIterator uninitialized_default_construct(ForwardIterator first, Sentinel last); // since C++20
325
326template <nothrow-forward-range ForwardRange>
327  requires default_initializable<range_value_t<ForwardRange>>
328 borrowed_iterator_t<ForwardRange> uninitialized_default_construct(ForwardRange&& r); // since C++20
329
330}
331
332template <class ForwardIterator, class Size>
333 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
334
335namespace ranges {
336
337template <nothrow-forward-iterator ForwardIterator>
338  requires default_initializable<iter_value_t<ForwardIterator>>
339 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
340
341}
342
343template <class Y> struct auto_ptr_ref {};      // deprecated in C++11, removed in C++17
344
345template<class X>
346class auto_ptr                                  // deprecated in C++11, removed in C++17
347{
348public:
349    typedef X element_type;
350
351    explicit auto_ptr(X* p =0) throw();
352    auto_ptr(auto_ptr&) throw();
353    template<class Y> auto_ptr(auto_ptr<Y>&) throw();
354    auto_ptr& operator=(auto_ptr&) throw();
355    template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
356    auto_ptr& operator=(auto_ptr_ref<X> r) throw();
357    ~auto_ptr() throw();
358
359    typename add_lvalue_reference<X>::type operator*() const throw();
360    X* operator->() const throw();
361    X* get() const throw();
362    X* release() throw();
363    void reset(X* p =0) throw();
364
365    auto_ptr(auto_ptr_ref<X>) throw();
366    template<class Y> operator auto_ptr_ref<Y>() throw();
367    template<class Y> operator auto_ptr<Y>() throw();
368};
369
370template <class T>
371struct default_delete
372{
373    constexpr default_delete() noexcept = default;
374    template <class U> default_delete(const default_delete<U>&) noexcept;
375
376    void operator()(T*) const noexcept;
377};
378
379template <class T>
380struct default_delete<T[]>
381{
382    constexpr default_delete() noexcept = default;
383    void operator()(T*) const noexcept;
384    template <class U> void operator()(U*) const = delete;
385};
386
387template <class T, class D = default_delete<T>>
388class unique_ptr
389{
390public:
391    typedef see below pointer;
392    typedef T element_type;
393    typedef D deleter_type;
394
395    // constructors
396    constexpr unique_ptr() noexcept;
397    explicit unique_ptr(pointer p) noexcept;
398    unique_ptr(pointer p, see below d1) noexcept;
399    unique_ptr(pointer p, see below d2) noexcept;
400    unique_ptr(unique_ptr&& u) noexcept;
401    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
402    template <class U, class E>
403        unique_ptr(unique_ptr<U, E>&& u) noexcept;
404    template <class U>
405        unique_ptr(auto_ptr<U>&& u) noexcept;       // removed in C++17
406
407    // destructor
408    ~unique_ptr();
409
410    // assignment
411    unique_ptr& operator=(unique_ptr&& u) noexcept;
412    template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
413    unique_ptr& operator=(nullptr_t) noexcept;
414
415    // observers
416    typename add_lvalue_reference<T>::type operator*() const;
417    pointer operator->() const noexcept;
418    pointer get() const noexcept;
419    deleter_type& get_deleter() noexcept;
420    const deleter_type& get_deleter() const noexcept;
421    explicit operator bool() const noexcept;
422
423    // modifiers
424    pointer release() noexcept;
425    void reset(pointer p = pointer()) noexcept;
426    void swap(unique_ptr& u) noexcept;
427};
428
429template <class T, class D>
430class unique_ptr<T[], D>
431{
432public:
433    typedef implementation-defined pointer;
434    typedef T element_type;
435    typedef D deleter_type;
436
437    // constructors
438    constexpr unique_ptr() noexcept;
439    explicit unique_ptr(pointer p) noexcept;
440    unique_ptr(pointer p, see below d) noexcept;
441    unique_ptr(pointer p, see below d) noexcept;
442    unique_ptr(unique_ptr&& u) noexcept;
443    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
444
445    // destructor
446    ~unique_ptr();
447
448    // assignment
449    unique_ptr& operator=(unique_ptr&& u) noexcept;
450    unique_ptr& operator=(nullptr_t) noexcept;
451
452    // observers
453    T& operator[](size_t i) const;
454    pointer get() const noexcept;
455    deleter_type& get_deleter() noexcept;
456    const deleter_type& get_deleter() const noexcept;
457    explicit operator bool() const noexcept;
458
459    // modifiers
460    pointer release() noexcept;
461    void reset(pointer p = pointer()) noexcept;
462    void reset(nullptr_t) noexcept;
463  template <class U> void reset(U) = delete;
464    void swap(unique_ptr& u) noexcept;
465};
466
467template <class T, class D>
468    void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
469
470template <class T1, class D1, class T2, class D2>
471    bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
472template <class T1, class D1, class T2, class D2>
473    bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
474template <class T1, class D1, class T2, class D2>
475    bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
476template <class T1, class D1, class T2, class D2>
477    bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
478template <class T1, class D1, class T2, class D2>
479    bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
480template <class T1, class D1, class T2, class D2>
481    bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
482
483template <class T, class D>
484    bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
485template <class T, class D>
486    bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
487template <class T, class D>
488    bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
489template <class T, class D>
490    bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
491
492template <class T, class D>
493    bool operator<(const unique_ptr<T, D>& x, nullptr_t);
494template <class T, class D>
495    bool operator<(nullptr_t, const unique_ptr<T, D>& y);
496template <class T, class D>
497    bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
498template <class T, class D>
499    bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
500template <class T, class D>
501    bool operator>(const unique_ptr<T, D>& x, nullptr_t);
502template <class T, class D>
503    bool operator>(nullptr_t, const unique_ptr<T, D>& y);
504template <class T, class D>
505    bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
506template <class T, class D>
507    bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
508
509class bad_weak_ptr
510    : public std::exception
511{
512    bad_weak_ptr() noexcept;
513};
514
515template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);     // C++14
516template<class T>                unique_ptr<T> make_unique(size_t n);           // C++14
517template<class T, class... Args> unspecified   make_unique(Args&&...) = delete; // C++14, T == U[N]
518
519template<class E, class T, class Y, class D>
520    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
521
522template<class T>
523class shared_ptr
524{
525public:
526    typedef T element_type; // until C++17
527    typedef remove_extent_t<T> element_type; // since C++17
528    typedef weak_ptr<T> weak_type; // C++17
529
530    // constructors:
531    constexpr shared_ptr() noexcept;
532    template<class Y> explicit shared_ptr(Y* p);
533    template<class Y, class D> shared_ptr(Y* p, D d);
534    template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
535    template <class D> shared_ptr(nullptr_t p, D d);
536    template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
537    template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
538    shared_ptr(const shared_ptr& r) noexcept;
539    template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
540    shared_ptr(shared_ptr&& r) noexcept;
541    template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
542    template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
543    template<class Y> shared_ptr(auto_ptr<Y>&& r);          // removed in C++17
544    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
545    shared_ptr(nullptr_t) : shared_ptr() { }
546
547    // destructor:
548    ~shared_ptr();
549
550    // assignment:
551    shared_ptr& operator=(const shared_ptr& r) noexcept;
552    template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
553    shared_ptr& operator=(shared_ptr&& r) noexcept;
554    template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
555    template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
556    template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
557
558    // modifiers:
559    void swap(shared_ptr& r) noexcept;
560    void reset() noexcept;
561    template<class Y> void reset(Y* p);
562    template<class Y, class D> void reset(Y* p, D d);
563    template<class Y, class D, class A> void reset(Y* p, D d, A a);
564
565    // observers:
566    T* get() const noexcept;
567    T& operator*() const noexcept;
568    T* operator->() const noexcept;
569    long use_count() const noexcept;
570    bool unique() const noexcept;
571    explicit operator bool() const noexcept;
572    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
573    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
574};
575
576template<class T>
577shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
578template<class T, class D>
579shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
580
581// shared_ptr comparisons:
582template<class T, class U>
583    bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
584template<class T, class U>
585    bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
586template<class T, class U>
587    bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
588template<class T, class U>
589    bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
590template<class T, class U>
591    bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
592template<class T, class U>
593    bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
594
595template <class T>
596    bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
597template <class T>
598    bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
599template <class T>
600    bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
601template <class T>
602    bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
603template <class T>
604    bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
605template <class T>
606bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
607template <class T>
608    bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
609template <class T>
610    bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
611template <class T>
612    bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
613template <class T>
614    bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
615template <class T>
616    bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
617template <class T>
618    bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
619
620// shared_ptr specialized algorithms:
621template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
622
623// shared_ptr casts:
624template<class T, class U>
625    shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
626template<class T, class U>
627    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
628template<class T, class U>
629    shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
630
631// shared_ptr I/O:
632template<class E, class T, class Y>
633    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
634
635// shared_ptr get_deleter:
636template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
637
638template<class T, class... Args>
639    shared_ptr<T> make_shared(Args&&... args);
640template<class T, class A, class... Args>
641    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
642
643template<class T>
644class weak_ptr
645{
646public:
647    typedef T element_type; // until C++17
648    typedef remove_extent_t<T> element_type; // since C++17
649
650    // constructors
651    constexpr weak_ptr() noexcept;
652    template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
653    weak_ptr(weak_ptr const& r) noexcept;
654    template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
655    weak_ptr(weak_ptr&& r) noexcept;                      // C++14
656    template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
657
658    // destructor
659    ~weak_ptr();
660
661    // assignment
662    weak_ptr& operator=(weak_ptr const& r) noexcept;
663    template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
664    template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
665    weak_ptr& operator=(weak_ptr&& r) noexcept;                      // C++14
666    template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
667
668    // modifiers
669    void swap(weak_ptr& r) noexcept;
670    void reset() noexcept;
671
672    // observers
673    long use_count() const noexcept;
674    bool expired() const noexcept;
675    shared_ptr<T> lock() const noexcept;
676    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
677    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
678};
679
680template<class T>
681weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
682
683// weak_ptr specialized algorithms:
684template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
685
686// class owner_less:
687template<class T> struct owner_less;
688
689template<class T>
690struct owner_less<shared_ptr<T> >
691    : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
692{
693    typedef bool result_type;
694    bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
695    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
696    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
697};
698
699template<class T>
700struct owner_less<weak_ptr<T> >
701    : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
702{
703    typedef bool result_type;
704    bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
705    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
706    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
707};
708
709template <>  // Added in C++14
710struct owner_less<void>
711{
712    template <class _Tp, class _Up>
713    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
714    template <class _Tp, class _Up>
715    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
716    template <class _Tp, class _Up>
717    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
718    template <class _Tp, class _Up>
719    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
720
721    typedef void is_transparent;
722};
723
724template<class T>
725class enable_shared_from_this
726{
727protected:
728    constexpr enable_shared_from_this() noexcept;
729    enable_shared_from_this(enable_shared_from_this const&) noexcept;
730    enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
731    ~enable_shared_from_this();
732public:
733    shared_ptr<T> shared_from_this();
734    shared_ptr<T const> shared_from_this() const;
735};
736
737template<class T>
738    bool atomic_is_lock_free(const shared_ptr<T>* p);
739template<class T>
740    shared_ptr<T> atomic_load(const shared_ptr<T>* p);
741template<class T>
742    shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
743template<class T>
744    void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
745template<class T>
746    void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
747template<class T>
748    shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
749template<class T>
750    shared_ptr<T>
751    atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
752template<class T>
753    bool
754    atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
755template<class T>
756    bool
757    atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
758template<class T>
759    bool
760    atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
761                                          shared_ptr<T> w, memory_order success,
762                                          memory_order failure);
763template<class T>
764    bool
765    atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
766                                            shared_ptr<T> w, memory_order success,
767                                            memory_order failure);
768// Hash support
769template <class T> struct hash;
770template <class T, class D> struct hash<unique_ptr<T, D> >;
771template <class T> struct hash<shared_ptr<T> >;
772
773template <class T, class Alloc>
774  inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
775
776void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
777
778}  // std
779
780*/
781
782#include <__config>
783#include <__functional_base>
784#include <__memory/addressof.h>
785#include <__memory/allocation_guard.h>
786#include <__memory/allocator.h>
787#include <__memory/allocator_arg_t.h>
788#include <__memory/allocator_traits.h>
789#include <__memory/compressed_pair.h>
790#include <__memory/concepts.h>
791#include <__memory/construct_at.h>
792#include <__memory/pointer_traits.h>
793#include <__memory/ranges_uninitialized_algorithms.h>
794#include <__memory/raw_storage_iterator.h>
795#include <__memory/shared_ptr.h>
796#include <__memory/temporary_buffer.h>
797#include <__memory/uninitialized_algorithms.h>
798#include <__memory/unique_ptr.h>
799#include <__memory/uses_allocator.h>
800#include <compare>
801#include <cstddef>
802#include <cstdint>
803#include <cstring>
804#include <iosfwd>
805#include <iterator>
806#include <new>
807#include <stdexcept>
808#include <tuple>
809#include <type_traits>
810#include <typeinfo>
811#include <utility>
812#include <version>
813
814#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
815#   include <__memory/auto_ptr.h>
816#endif
817
818#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
819#pragma GCC system_header
820#endif
821
822_LIBCPP_BEGIN_NAMESPACE_STD
823
824template <class _Alloc, class _Ptr>
825_LIBCPP_INLINE_VISIBILITY
826void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
827    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
828        "The specified type does not meet the requirements of Cpp17MoveInsertable");
829    typedef allocator_traits<_Alloc> _Traits;
830    for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
831        _Traits::construct(__a, _VSTD::__to_address(__begin2),
832#ifdef _LIBCPP_NO_EXCEPTIONS
833            _VSTD::move(*__begin1)
834#else
835            _VSTD::move_if_noexcept(*__begin1)
836#endif
837        );
838    }
839}
840
841template <class _Alloc, class _Tp, typename enable_if<
842    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
843    is_trivially_move_constructible<_Tp>::value
844>::type>
845_LIBCPP_INLINE_VISIBILITY
846void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
847    ptrdiff_t _Np = __end1 - __begin1;
848    if (_Np > 0) {
849        _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
850        __begin2 += _Np;
851    }
852}
853
854template <class _Alloc, class _Iter, class _Ptr>
855_LIBCPP_INLINE_VISIBILITY
856void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
857    typedef allocator_traits<_Alloc> _Traits;
858    for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
859        _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
860    }
861}
862
863template <class _Alloc, class _Source, class _Dest,
864          class _RawSource = typename remove_const<_Source>::type,
865          class _RawDest = typename remove_const<_Dest>::type,
866          class =
867    typename enable_if<
868        is_trivially_copy_constructible<_Dest>::value &&
869        is_same<_RawSource, _RawDest>::value &&
870        (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
871    >::type>
872_LIBCPP_INLINE_VISIBILITY
873void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
874    ptrdiff_t _Np = __end1 - __begin1;
875    if (_Np > 0) {
876        _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
877        __begin2 += _Np;
878    }
879}
880
881template <class _Alloc, class _Ptr>
882_LIBCPP_INLINE_VISIBILITY
883void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
884    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
885        "The specified type does not meet the requirements of Cpp17MoveInsertable");
886    typedef allocator_traits<_Alloc> _Traits;
887    while (__end1 != __begin1) {
888        _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
889#ifdef _LIBCPP_NO_EXCEPTIONS
890            _VSTD::move(*--__end1)
891#else
892            _VSTD::move_if_noexcept(*--__end1)
893#endif
894        );
895        --__end2;
896    }
897}
898
899template <class _Alloc, class _Tp, class = typename enable_if<
900    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
901    is_trivially_move_constructible<_Tp>::value
902>::type>
903_LIBCPP_INLINE_VISIBILITY
904void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
905    ptrdiff_t _Np = __end1 - __begin1;
906    __end2 -= _Np;
907    if (_Np > 0)
908        _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp));
909}
910
911struct __destruct_n
912{
913private:
914    size_t __size_;
915
916    template <class _Tp>
917    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
918        {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
919
920    template <class _Tp>
921    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
922        {}
923
924    _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
925        {++__size_;}
926    _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
927        {}
928
929    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
930        {__size_ = __s;}
931    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
932        {}
933public:
934    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
935        : __size_(__s) {}
936
937    template <class _Tp>
938    _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
939        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
940
941    template <class _Tp>
942    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
943        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
944
945    template <class _Tp>
946    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
947        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
948};
949
950_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
951
952// --- Helper for container swap --
953template <typename _Alloc>
954_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
955void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
956#if _LIBCPP_STD_VER > 11
957    _NOEXCEPT
958#else
959    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
960#endif
961{
962    using _VSTD::swap;
963    swap(__a1, __a2);
964}
965
966template <typename _Alloc>
967inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
968void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
969
970template <typename _Alloc>
971inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
972void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
973#if _LIBCPP_STD_VER > 11
974    _NOEXCEPT
975#else
976    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
977#endif
978{
979    _VSTD::__swap_allocator(__a1, __a2,
980      integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>());
981}
982
983template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
984struct __noexcept_move_assign_container : public integral_constant<bool,
985    _Traits::propagate_on_container_move_assignment::value
986#if _LIBCPP_STD_VER > 14
987        || _Traits::is_always_equal::value
988#else
989        && is_nothrow_move_assignable<_Alloc>::value
990#endif
991    > {};
992
993
994template <class _Tp, class _Alloc>
995struct __temp_value {
996    typedef allocator_traits<_Alloc> _Traits;
997
998    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
999    _Alloc &__a;
1000
1001    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
1002    _Tp &   get() { return *__addr(); }
1003
1004    template<class... _Args>
1005    _LIBCPP_NO_CFI
1006    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
1007      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
1008                         _VSTD::forward<_Args>(__args)...);
1009    }
1010
1011    ~__temp_value() { _Traits::destroy(__a, __addr()); }
1012    };
1013
1014template<typename _Alloc, typename = void, typename = void>
1015struct __is_allocator : false_type {};
1016
1017template<typename _Alloc>
1018struct __is_allocator<_Alloc,
1019       typename __void_t<typename _Alloc::value_type>::type,
1020       typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type
1021     >
1022   : true_type {};
1023
1024// __builtin_new_allocator -- A non-templated helper for allocating and
1025// deallocating memory using __builtin_operator_new and
1026// __builtin_operator_delete. It should be used in preference to
1027// `std::allocator<T>` to avoid additional instantiations.
1028struct __builtin_new_allocator {
1029  struct __builtin_new_deleter {
1030    typedef void* pointer_type;
1031
1032    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
1033        : __size_(__size), __align_(__align) {}
1034
1035    void operator()(void* p) const _NOEXCEPT {
1036        _VSTD::__libcpp_deallocate(p, __size_, __align_);
1037    }
1038
1039   private:
1040    size_t __size_;
1041    size_t __align_;
1042  };
1043
1044  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
1045
1046  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
1047      return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
1048                     __builtin_new_deleter(__s, __align));
1049  }
1050
1051  static void __deallocate_bytes(void* __p, size_t __s,
1052                                 size_t __align) _NOEXCEPT {
1053      _VSTD::__libcpp_deallocate(__p, __s, __align);
1054  }
1055
1056  template <class _Tp>
1057  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
1058  static __holder_t __allocate_type(size_t __n) {
1059      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1060  }
1061
1062  template <class _Tp>
1063  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
1064  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
1065      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1066  }
1067};
1068
1069
1070_LIBCPP_END_NAMESPACE_STD
1071
1072#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
1073#   include <__pstl_memory>
1074#endif
1075
1076#endif // _LIBCPP_MEMORY
1077