xref: /freebsd/contrib/llvm-project/libcxx/include/memory (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
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;          // deprecated in C++17
106    typedef const void*                           const_pointer;    // deprecated in C++17
107    typedef void                                  value_type;       // deprecated in C++17
108
109    template <class _Up> struct rebind {typedef allocator<_Up> other;}; // deprecated in C++17
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
157    : public iterator<output_iterator_tag,
158                      T,                               // purposefully not C++03
159                      ptrdiff_t,                       // purposefully not C++03
160                      T*,                              // purposefully not C++03
161                      raw_storage_iterator&>           // purposefully not C++03
162{
163public:
164    explicit raw_storage_iterator(OutputIterator x);
165    raw_storage_iterator& operator*();
166    raw_storage_iterator& operator=(const T& element);
167    raw_storage_iterator& operator++();
168    raw_storage_iterator  operator++(int);
169};
170
171template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
172template <class T> void               return_temporary_buffer(T* p) noexcept;
173
174template <class T> T* addressof(T& r) noexcept;
175template <class T> T* addressof(const T&& r) noexcept = delete;
176
177template <class InputIterator, class ForwardIterator>
178ForwardIterator
179uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
180
181template <class InputIterator, class Size, class ForwardIterator>
182ForwardIterator
183uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
184
185template <class ForwardIterator, class T>
186void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
187
188template <class ForwardIterator, class Size, class T>
189ForwardIterator
190uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
191
192template <class T, class ...Args>
193constexpr T* construct_at(T* location, Args&& ...args); // since C++20
194
195template <class T>
196void destroy_at(T* location); // constexpr in C++20
197
198template <class ForwardIterator>
199void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
200
201template <class ForwardIterator, class Size>
202ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
203
204template <class InputIterator, class ForwardIterator>
205 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
206
207template <class InputIterator, class Size, class ForwardIterator>
208 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
209
210template <class ForwardIterator>
211 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
212
213template <class ForwardIterator, class Size>
214 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
215
216template <class ForwardIterator>
217 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
218
219template <class ForwardIterator, class Size>
220 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
221
222template <class Y> struct auto_ptr_ref {};      // deprecated in C++11, removed in C++17
223
224template<class X>
225class auto_ptr                                  // deprecated in C++11, removed in C++17
226{
227public:
228    typedef X element_type;
229
230    explicit auto_ptr(X* p =0) throw();
231    auto_ptr(auto_ptr&) throw();
232    template<class Y> auto_ptr(auto_ptr<Y>&) throw();
233    auto_ptr& operator=(auto_ptr&) throw();
234    template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
235    auto_ptr& operator=(auto_ptr_ref<X> r) throw();
236    ~auto_ptr() throw();
237
238    typename add_lvalue_reference<X>::type operator*() const throw();
239    X* operator->() const throw();
240    X* get() const throw();
241    X* release() throw();
242    void reset(X* p =0) throw();
243
244    auto_ptr(auto_ptr_ref<X>) throw();
245    template<class Y> operator auto_ptr_ref<Y>() throw();
246    template<class Y> operator auto_ptr<Y>() throw();
247};
248
249template <class T>
250struct default_delete
251{
252    constexpr default_delete() noexcept = default;
253    template <class U> default_delete(const default_delete<U>&) noexcept;
254
255    void operator()(T*) const noexcept;
256};
257
258template <class T>
259struct default_delete<T[]>
260{
261    constexpr default_delete() noexcept = default;
262    void operator()(T*) const noexcept;
263    template <class U> void operator()(U*) const = delete;
264};
265
266template <class T, class D = default_delete<T>>
267class unique_ptr
268{
269public:
270    typedef see below pointer;
271    typedef T element_type;
272    typedef D deleter_type;
273
274    // constructors
275    constexpr unique_ptr() noexcept;
276    explicit unique_ptr(pointer p) noexcept;
277    unique_ptr(pointer p, see below d1) noexcept;
278    unique_ptr(pointer p, see below d2) noexcept;
279    unique_ptr(unique_ptr&& u) noexcept;
280    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
281    template <class U, class E>
282        unique_ptr(unique_ptr<U, E>&& u) noexcept;
283    template <class U>
284        unique_ptr(auto_ptr<U>&& u) noexcept;       // removed in C++17
285
286    // destructor
287    ~unique_ptr();
288
289    // assignment
290    unique_ptr& operator=(unique_ptr&& u) noexcept;
291    template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
292    unique_ptr& operator=(nullptr_t) noexcept;
293
294    // observers
295    typename add_lvalue_reference<T>::type operator*() const;
296    pointer operator->() const noexcept;
297    pointer get() const noexcept;
298    deleter_type& get_deleter() noexcept;
299    const deleter_type& get_deleter() const noexcept;
300    explicit operator bool() const noexcept;
301
302    // modifiers
303    pointer release() noexcept;
304    void reset(pointer p = pointer()) noexcept;
305    void swap(unique_ptr& u) noexcept;
306};
307
308template <class T, class D>
309class unique_ptr<T[], D>
310{
311public:
312    typedef implementation-defined pointer;
313    typedef T element_type;
314    typedef D deleter_type;
315
316    // constructors
317    constexpr unique_ptr() noexcept;
318    explicit unique_ptr(pointer p) noexcept;
319    unique_ptr(pointer p, see below d) noexcept;
320    unique_ptr(pointer p, see below d) noexcept;
321    unique_ptr(unique_ptr&& u) noexcept;
322    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
323
324    // destructor
325    ~unique_ptr();
326
327    // assignment
328    unique_ptr& operator=(unique_ptr&& u) noexcept;
329    unique_ptr& operator=(nullptr_t) noexcept;
330
331    // observers
332    T& operator[](size_t i) const;
333    pointer get() const noexcept;
334    deleter_type& get_deleter() noexcept;
335    const deleter_type& get_deleter() const noexcept;
336    explicit operator bool() const noexcept;
337
338    // modifiers
339    pointer release() noexcept;
340    void reset(pointer p = pointer()) noexcept;
341    void reset(nullptr_t) noexcept;
342  template <class U> void reset(U) = delete;
343    void swap(unique_ptr& u) noexcept;
344};
345
346template <class T, class D>
347    void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
348
349template <class T1, class D1, class T2, class D2>
350    bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352    bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353template <class T1, class D1, class T2, class D2>
354    bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
355template <class T1, class D1, class T2, class D2>
356    bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
357template <class T1, class D1, class T2, class D2>
358    bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
359template <class T1, class D1, class T2, class D2>
360    bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
361
362template <class T, class D>
363    bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
364template <class T, class D>
365    bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
366template <class T, class D>
367    bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
368template <class T, class D>
369    bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
370
371template <class T, class D>
372    bool operator<(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374    bool operator<(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376    bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378    bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
379template <class T, class D>
380    bool operator>(const unique_ptr<T, D>& x, nullptr_t);
381template <class T, class D>
382    bool operator>(nullptr_t, const unique_ptr<T, D>& y);
383template <class T, class D>
384    bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
385template <class T, class D>
386    bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
387
388class bad_weak_ptr
389    : public std::exception
390{
391    bad_weak_ptr() noexcept;
392};
393
394template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);     // C++14
395template<class T>                unique_ptr<T> make_unique(size_t n);           // C++14
396template<class T, class... Args> unspecified   make_unique(Args&&...) = delete; // C++14, T == U[N]
397
398template<class E, class T, class Y, class D>
399    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
400
401template<class T>
402class shared_ptr
403{
404public:
405    typedef T element_type;
406    typedef weak_ptr<T> weak_type; // C++17
407
408    // constructors:
409    constexpr shared_ptr() noexcept;
410    template<class Y> explicit shared_ptr(Y* p);
411    template<class Y, class D> shared_ptr(Y* p, D d);
412    template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
413    template <class D> shared_ptr(nullptr_t p, D d);
414    template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
415    template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
416    shared_ptr(const shared_ptr& r) noexcept;
417    template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
418    shared_ptr(shared_ptr&& r) noexcept;
419    template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
420    template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
421    template<class Y> shared_ptr(auto_ptr<Y>&& r);          // removed in C++17
422    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
423    shared_ptr(nullptr_t) : shared_ptr() { }
424
425    // destructor:
426    ~shared_ptr();
427
428    // assignment:
429    shared_ptr& operator=(const shared_ptr& r) noexcept;
430    template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
431    shared_ptr& operator=(shared_ptr&& r) noexcept;
432    template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
433    template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
434    template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
435
436    // modifiers:
437    void swap(shared_ptr& r) noexcept;
438    void reset() noexcept;
439    template<class Y> void reset(Y* p);
440    template<class Y, class D> void reset(Y* p, D d);
441    template<class Y, class D, class A> void reset(Y* p, D d, A a);
442
443    // observers:
444    T* get() const noexcept;
445    T& operator*() const noexcept;
446    T* operator->() const noexcept;
447    long use_count() const noexcept;
448    bool unique() const noexcept;
449    explicit operator bool() const noexcept;
450    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
451    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
452};
453
454template<class T>
455shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
456template<class T, class D>
457shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
458
459// shared_ptr comparisons:
460template<class T, class U>
461    bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
462template<class T, class U>
463    bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
464template<class T, class U>
465    bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
466template<class T, class U>
467    bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
468template<class T, class U>
469    bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
470template<class T, class U>
471    bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
472
473template <class T>
474    bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476    bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478    bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480    bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482    bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
485template <class T>
486    bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
487template <class T>
488    bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
489template <class T>
490    bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
491template <class T>
492    bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
493template <class T>
494    bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
495template <class T>
496    bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
497
498// shared_ptr specialized algorithms:
499template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
500
501// shared_ptr casts:
502template<class T, class U>
503    shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
504template<class T, class U>
505    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
506template<class T, class U>
507    shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
508
509// shared_ptr I/O:
510template<class E, class T, class Y>
511    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
512
513// shared_ptr get_deleter:
514template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
515
516template<class T, class... Args>
517    shared_ptr<T> make_shared(Args&&... args);
518template<class T, class A, class... Args>
519    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
520
521template<class T>
522class weak_ptr
523{
524public:
525    typedef T element_type;
526
527    // constructors
528    constexpr weak_ptr() noexcept;
529    template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
530    weak_ptr(weak_ptr const& r) noexcept;
531    template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
532    weak_ptr(weak_ptr&& r) noexcept;                      // C++14
533    template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
534
535    // destructor
536    ~weak_ptr();
537
538    // assignment
539    weak_ptr& operator=(weak_ptr const& r) noexcept;
540    template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
541    template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
542    weak_ptr& operator=(weak_ptr&& r) noexcept;                      // C++14
543    template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
544
545    // modifiers
546    void swap(weak_ptr& r) noexcept;
547    void reset() noexcept;
548
549    // observers
550    long use_count() const noexcept;
551    bool expired() const noexcept;
552    shared_ptr<T> lock() const noexcept;
553    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
554    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
555};
556
557template<class T>
558weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
559
560// weak_ptr specialized algorithms:
561template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
562
563// class owner_less:
564template<class T> struct owner_less;
565
566template<class T>
567struct owner_less<shared_ptr<T> >
568    : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
569{
570    typedef bool result_type;
571    bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
572    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
574};
575
576template<class T>
577struct owner_less<weak_ptr<T> >
578    : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
579{
580    typedef bool result_type;
581    bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
583    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
584};
585
586template <>  // Added in C++14
587struct owner_less<void>
588{
589    template <class _Tp, class _Up>
590    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
591    template <class _Tp, class _Up>
592    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
593    template <class _Tp, class _Up>
594    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
595    template <class _Tp, class _Up>
596    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
597
598    typedef void is_transparent;
599};
600
601template<class T>
602class enable_shared_from_this
603{
604protected:
605    constexpr enable_shared_from_this() noexcept;
606    enable_shared_from_this(enable_shared_from_this const&) noexcept;
607    enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
608    ~enable_shared_from_this();
609public:
610    shared_ptr<T> shared_from_this();
611    shared_ptr<T const> shared_from_this() const;
612};
613
614template<class T>
615    bool atomic_is_lock_free(const shared_ptr<T>* p);
616template<class T>
617    shared_ptr<T> atomic_load(const shared_ptr<T>* p);
618template<class T>
619    shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
620template<class T>
621    void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
622template<class T>
623    void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
624template<class T>
625    shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
626template<class T>
627    shared_ptr<T>
628    atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630    bool
631    atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
632template<class T>
633    bool
634    atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
635template<class T>
636    bool
637    atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
638                                          shared_ptr<T> w, memory_order success,
639                                          memory_order failure);
640template<class T>
641    bool
642    atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643                                            shared_ptr<T> w, memory_order success,
644                                            memory_order failure);
645// Hash support
646template <class T> struct hash;
647template <class T, class D> struct hash<unique_ptr<T, D> >;
648template <class T> struct hash<shared_ptr<T> >;
649
650template <class T, class Alloc>
651  inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
652
653// Pointer safety
654enum class pointer_safety { relaxed, preferred, strict };
655void declare_reachable(void *p);
656template <class T> T *undeclare_reachable(T *p);
657void declare_no_pointers(char *p, size_t n);
658void undeclare_no_pointers(char *p, size_t n);
659pointer_safety get_pointer_safety() noexcept;
660
661void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
662
663}  // std
664
665*/
666
667#include <__config>
668#include <__availability>
669#include <type_traits>
670#include <typeinfo>
671#include <cstddef>
672#include <cstdint>
673#include <new>
674#include <utility>
675#include <limits>
676#include <iterator>
677#include <__functional_base>
678#include <iosfwd>
679#include <tuple>
680#include <stdexcept>
681#include <cstring>
682#include <__memory/allocator_traits.h>
683#include <__memory/base.h>
684#include <__memory/pointer_traits.h>
685#include <__memory/utilities.h>
686#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
687#  include <atomic>
688#endif
689#include <version>
690
691#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
692#pragma GCC system_header
693#endif
694
695_LIBCPP_PUSH_MACROS
696#include <__undef_macros>
697
698
699_LIBCPP_BEGIN_NAMESPACE_STD
700
701template <class _ValueType>
702inline _LIBCPP_INLINE_VISIBILITY
703_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
704#if !defined(_LIBCPP_HAS_NO_THREADS) && \
705    defined(__ATOMIC_RELAXED) &&        \
706    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
707    return __atomic_load_n(__value, __ATOMIC_RELAXED);
708#else
709    return *__value;
710#endif
711}
712
713template <class _ValueType>
714inline _LIBCPP_INLINE_VISIBILITY
715_ValueType __libcpp_acquire_load(_ValueType const* __value) {
716#if !defined(_LIBCPP_HAS_NO_THREADS) && \
717    defined(__ATOMIC_ACQUIRE) &&        \
718    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
719    return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
720#else
721    return *__value;
722#endif
723}
724
725template <bool _UsePointerTraits> struct __to_address_helper;
726
727template <> struct __to_address_helper<true> {
728    template <class _Pointer>
729    using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
730
731    template <class _Pointer>
732    _LIBCPP_CONSTEXPR
733    static __return_type<_Pointer>
734    __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
735};
736
737template <class _Pointer, bool _Dummy = true>
738using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
739
740
741template <class _Tp>
742inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
743_Tp*
744__to_address(_Tp* __p) _NOEXCEPT
745{
746    static_assert(!is_function<_Tp>::value, "_Tp is a function type");
747    return __p;
748}
749
750template <class _Pointer>
751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
752typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
753__to_address(const _Pointer& __p) _NOEXCEPT {
754  return __choose_to_address<_Pointer>::__do_it(__p);
755}
756
757template <> struct __to_address_helper<false> {
758    template <class _Pointer>
759    using __return_type = typename pointer_traits<_Pointer>::element_type*;
760
761    template <class _Pointer>
762    _LIBCPP_CONSTEXPR
763    static __return_type<_Pointer>
764    __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
765};
766
767
768#if _LIBCPP_STD_VER > 17
769template <class _Tp>
770inline _LIBCPP_INLINE_VISIBILITY constexpr
771_Tp*
772to_address(_Tp* __p) _NOEXCEPT
773{
774    static_assert(!is_function_v<_Tp>, "_Tp is a function type");
775    return __p;
776}
777
778template <class _Pointer>
779inline _LIBCPP_INLINE_VISIBILITY constexpr
780auto
781to_address(const _Pointer& __p) _NOEXCEPT
782{
783    return _VSTD::__to_address(__p);
784}
785#endif
786
787template <class _Tp> class allocator;
788
789#if _LIBCPP_STD_VER <= 17
790template <>
791class _LIBCPP_TEMPLATE_VIS allocator<void>
792{
793public:
794    _LIBCPP_DEPRECATED_IN_CXX17 typedef void*             pointer;
795    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       const_pointer;
796    _LIBCPP_DEPRECATED_IN_CXX17 typedef void              value_type;
797
798    template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
799};
800
801template <>
802class _LIBCPP_TEMPLATE_VIS allocator<const void>
803{
804public:
805    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       pointer;
806    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       const_pointer;
807    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void        value_type;
808
809    template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
810};
811#endif
812
813// This class provides a non-trivial default constructor to the class that derives from it
814// if the condition is satisfied.
815//
816// The second template parameter exists to allow giving a unique type to __non_trivial_if,
817// which makes it possible to avoid breaking the ABI when making this a base class of an
818// existing class. Without that, imagine we have classes D1 and D2, both of which used to
819// have no base classes, but which now derive from __non_trivial_if. The layout of a class
820// that inherits from both D1 and D2 will change because the two __non_trivial_if base
821// classes are not allowed to share the same address.
822//
823// By making those __non_trivial_if base classes unique, we work around this problem and
824// it is safe to start deriving from __non_trivial_if in existing classes.
825template <bool _Cond, class _Unique>
826struct __non_trivial_if { };
827
828template <class _Unique>
829struct __non_trivial_if<true, _Unique> {
830    _LIBCPP_INLINE_VISIBILITY
831    _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT { }
832};
833
834// allocator
835//
836// Note: For ABI compatibility between C++20 and previous standards, we make
837//       allocator<void> trivial in C++20.
838
839template <class _Tp>
840class _LIBCPP_TEMPLATE_VIS allocator
841    : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> >
842{
843public:
844    typedef size_t      size_type;
845    typedef ptrdiff_t   difference_type;
846    typedef _Tp         value_type;
847    typedef true_type   propagate_on_container_move_assignment;
848    typedef true_type   is_always_equal;
849
850    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
851    allocator() _NOEXCEPT _LIBCPP_DEFAULT
852
853    template <class _Up>
854    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
855    allocator(const allocator<_Up>&) _NOEXCEPT { }
856
857    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
858    _Tp* allocate(size_t __n) {
859        if (__n > allocator_traits<allocator>::max_size(*this))
860            __throw_length_error("allocator<T>::allocate(size_t n)"
861                                 " 'n' exceeds maximum supported size");
862        if (__libcpp_is_constant_evaluated()) {
863            return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
864        } else {
865            return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
866        }
867    }
868
869    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
870    void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
871        if (__libcpp_is_constant_evaluated()) {
872            ::operator delete(__p);
873        } else {
874            _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
875        }
876    }
877
878    // C++20 Removed members
879#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
880    _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp*       pointer;
881    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
882    _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp&       reference;
883    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
884
885    template <class _Up>
886    struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
887        typedef allocator<_Up> other;
888    };
889
890    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
891    pointer address(reference __x) const _NOEXCEPT {
892        return _VSTD::addressof(__x);
893    }
894    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
895    const_pointer address(const_reference __x) const _NOEXCEPT {
896        return _VSTD::addressof(__x);
897    }
898
899    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
900    _Tp* allocate(size_t __n, const void*) {
901        return allocate(__n);
902    }
903
904    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
905        return size_type(~0) / sizeof(_Tp);
906    }
907
908    template <class _Up, class... _Args>
909    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
910    void construct(_Up* __p, _Args&&... __args) {
911        ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
912    }
913
914    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
915    void destroy(pointer __p) {
916        __p->~_Tp();
917    }
918#endif
919};
920
921template <class _Tp>
922class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
923    : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
924{
925public:
926    typedef size_t      size_type;
927    typedef ptrdiff_t   difference_type;
928    typedef const _Tp   value_type;
929    typedef true_type   propagate_on_container_move_assignment;
930    typedef true_type   is_always_equal;
931
932    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
933    allocator() _NOEXCEPT _LIBCPP_DEFAULT
934
935    template <class _Up>
936    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
937    allocator(const allocator<_Up>&) _NOEXCEPT { }
938
939    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
940    const _Tp* allocate(size_t __n) {
941        if (__n > allocator_traits<allocator>::max_size(*this))
942            __throw_length_error("allocator<const T>::allocate(size_t n)"
943                                 " 'n' exceeds maximum supported size");
944        if (__libcpp_is_constant_evaluated()) {
945            return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
946        } else {
947            return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
948        }
949    }
950
951    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
952    void deallocate(const _Tp* __p, size_t __n) {
953        if (__libcpp_is_constant_evaluated()) {
954            ::operator delete(const_cast<_Tp*>(__p));
955        } else {
956            _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
957        }
958    }
959
960    // C++20 Removed members
961#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
962    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
963    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
964    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
965    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
966
967    template <class _Up>
968    struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
969        typedef allocator<_Up> other;
970    };
971
972    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
973    const_pointer address(const_reference __x) const _NOEXCEPT {
974        return _VSTD::addressof(__x);
975    }
976
977    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
978    const _Tp* allocate(size_t __n, const void*) {
979        return allocate(__n);
980    }
981
982    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
983        return size_type(~0) / sizeof(_Tp);
984    }
985
986    template <class _Up, class... _Args>
987    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
988    void construct(_Up* __p, _Args&&... __args) {
989        ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
990    }
991
992    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
993    void destroy(pointer __p) {
994        __p->~_Tp();
995    }
996#endif
997};
998
999template <class _Tp, class _Up>
1000inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1001bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
1002
1003template <class _Tp, class _Up>
1004inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1005bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
1006
1007template <class _Alloc, class _Ptr>
1008_LIBCPP_INLINE_VISIBILITY
1009void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
1010    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1011        "The specified type does not meet the requirements of Cpp17MoveInsertable");
1012    typedef allocator_traits<_Alloc> _Traits;
1013    for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
1014        _Traits::construct(__a, _VSTD::__to_address(__begin2),
1015#ifdef _LIBCPP_NO_EXCEPTIONS
1016            _VSTD::move(*__begin1)
1017#else
1018            _VSTD::move_if_noexcept(*__begin1)
1019#endif
1020        );
1021    }
1022}
1023
1024template <class _Alloc, class _Tp, typename enable_if<
1025    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1026    is_trivially_move_constructible<_Tp>::value
1027>::type>
1028_LIBCPP_INLINE_VISIBILITY
1029void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1030    ptrdiff_t _Np = __end1 - __begin1;
1031    if (_Np > 0) {
1032        _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1033        __begin2 += _Np;
1034    }
1035}
1036
1037template <class _Alloc, class _Iter, class _Ptr>
1038_LIBCPP_INLINE_VISIBILITY
1039void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1040    typedef allocator_traits<_Alloc> _Traits;
1041    for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1042        _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1043    }
1044}
1045
1046template <class _Alloc, class _Source, class _Dest,
1047          class _RawSource = typename remove_const<_Source>::type,
1048          class _RawDest = typename remove_const<_Dest>::type,
1049          class =
1050    typename enable_if<
1051        is_trivially_copy_constructible<_Dest>::value &&
1052        is_same<_RawSource, _RawDest>::value &&
1053        (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1054    >::type>
1055_LIBCPP_INLINE_VISIBILITY
1056void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1057    ptrdiff_t _Np = __end1 - __begin1;
1058    if (_Np > 0) {
1059        _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1060        __begin2 += _Np;
1061    }
1062}
1063
1064template <class _Alloc, class _Ptr>
1065_LIBCPP_INLINE_VISIBILITY
1066void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1067    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1068        "The specified type does not meet the requirements of Cpp17MoveInsertable");
1069    typedef allocator_traits<_Alloc> _Traits;
1070    while (__end1 != __begin1) {
1071        _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1072#ifdef _LIBCPP_NO_EXCEPTIONS
1073            _VSTD::move(*--__end1)
1074#else
1075            _VSTD::move_if_noexcept(*--__end1)
1076#endif
1077        );
1078        --__end2;
1079    }
1080}
1081
1082template <class _Alloc, class _Tp, class = typename enable_if<
1083    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1084    is_trivially_move_constructible<_Tp>::value
1085>::type>
1086_LIBCPP_INLINE_VISIBILITY
1087void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1088    ptrdiff_t _Np = __end1 - __begin1;
1089    __end2 -= _Np;
1090    if (_Np > 0)
1091        _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1092}
1093
1094template <class _OutputIterator, class _Tp>
1095class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
1096    : public iterator<output_iterator_tag,
1097                      _Tp,                                         // purposefully not C++03
1098                      ptrdiff_t,                                   // purposefully not C++03
1099                      _Tp*,                                        // purposefully not C++03
1100                      raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1101{
1102private:
1103    _OutputIterator __x_;
1104public:
1105    _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1106    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1107    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1108        {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
1109#if _LIBCPP_STD_VER >= 14
1110    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1111        {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
1112#endif
1113    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1114    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator  operator++(int)
1115        {raw_storage_iterator __t(*this); ++__x_; return __t;}
1116#if _LIBCPP_STD_VER >= 14
1117    _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1118#endif
1119};
1120
1121template <class _Tp>
1122_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
1123pair<_Tp*, ptrdiff_t>
1124get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
1125{
1126    pair<_Tp*, ptrdiff_t> __r(0, 0);
1127    const ptrdiff_t __m = (~ptrdiff_t(0) ^
1128                           ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1129                           / sizeof(_Tp);
1130    if (__n > __m)
1131        __n = __m;
1132    while (__n > 0)
1133    {
1134#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
1135    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
1136        {
1137            align_val_t __al =
1138                align_val_t(alignment_of<_Tp>::value);
1139            __r.first = static_cast<_Tp*>(::operator new(
1140                __n * sizeof(_Tp), __al, nothrow));
1141        } else {
1142            __r.first = static_cast<_Tp*>(::operator new(
1143                __n * sizeof(_Tp), nothrow));
1144        }
1145#else
1146    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
1147        {
1148            // Since aligned operator new is unavailable, return an empty
1149            // buffer rather than one with invalid alignment.
1150            return __r;
1151        }
1152
1153        __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1154#endif
1155
1156        if (__r.first)
1157        {
1158            __r.second = __n;
1159            break;
1160        }
1161        __n /= 2;
1162    }
1163    return __r;
1164}
1165
1166template <class _Tp>
1167inline _LIBCPP_INLINE_VISIBILITY
1168void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1169{
1170  _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
1171}
1172
1173#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1174template <class _Tp>
1175struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
1176{
1177    _Tp* __ptr_;
1178};
1179
1180template<class _Tp>
1181class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
1182{
1183private:
1184    _Tp* __ptr_;
1185public:
1186    typedef _Tp element_type;
1187
1188    _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1189    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1190    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
1191        : __ptr_(__p.release()) {}
1192    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
1193        {reset(__p.release()); return *this;}
1194    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
1195        {reset(__p.release()); return *this;}
1196    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
1197        {reset(__p.__ptr_); return *this;}
1198    _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
1199
1200    _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
1201        {return *__ptr_;}
1202    _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1203    _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1204    _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
1205    {
1206        _Tp* __t = __ptr_;
1207        __ptr_ = nullptr;
1208        return __t;
1209    }
1210    _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
1211    {
1212        if (__ptr_ != __p)
1213            delete __ptr_;
1214        __ptr_ = __p;
1215    }
1216
1217    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1218    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
1219        {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1220    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
1221        {return auto_ptr<_Up>(release());}
1222};
1223
1224template <>
1225class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
1226{
1227public:
1228    typedef void element_type;
1229};
1230#endif
1231
1232// Tag used to default initialize one or both of the pair's elements.
1233struct __default_init_tag {};
1234struct __value_init_tag {};
1235
1236template <class _Tp, int _Idx,
1237          bool _CanBeEmptyBase =
1238              is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1239struct __compressed_pair_elem {
1240  typedef _Tp _ParamT;
1241  typedef _Tp& reference;
1242  typedef const _Tp& const_reference;
1243
1244  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1245  __compressed_pair_elem(__default_init_tag) {}
1246  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1247  __compressed_pair_elem(__value_init_tag) : __value_() {}
1248
1249  template <class _Up, class = typename enable_if<
1250      !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1251  >::type>
1252  _LIBCPP_INLINE_VISIBILITY
1253  _LIBCPP_CONSTEXPR explicit
1254  __compressed_pair_elem(_Up&& __u)
1255      : __value_(_VSTD::forward<_Up>(__u))
1256    {
1257    }
1258
1259
1260#ifndef _LIBCPP_CXX03_LANG
1261  template <class... _Args, size_t... _Indexes>
1262  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1263  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1264                         __tuple_indices<_Indexes...>)
1265      : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
1266#endif
1267
1268
1269  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1270  _LIBCPP_INLINE_VISIBILITY
1271  const_reference __get() const _NOEXCEPT { return __value_; }
1272
1273private:
1274  _Tp __value_;
1275};
1276
1277template <class _Tp, int _Idx>
1278struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1279  typedef _Tp _ParamT;
1280  typedef _Tp& reference;
1281  typedef const _Tp& const_reference;
1282  typedef _Tp __value_type;
1283
1284  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1285  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1286  __compressed_pair_elem(__default_init_tag) {}
1287  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1288  __compressed_pair_elem(__value_init_tag) : __value_type() {}
1289
1290  template <class _Up, class = typename enable_if<
1291        !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1292  >::type>
1293  _LIBCPP_INLINE_VISIBILITY
1294  _LIBCPP_CONSTEXPR explicit
1295  __compressed_pair_elem(_Up&& __u)
1296      : __value_type(_VSTD::forward<_Up>(__u))
1297  {}
1298
1299#ifndef _LIBCPP_CXX03_LANG
1300  template <class... _Args, size_t... _Indexes>
1301  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1302  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1303                         __tuple_indices<_Indexes...>)
1304      : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
1305#endif
1306
1307  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1308  _LIBCPP_INLINE_VISIBILITY
1309  const_reference __get() const _NOEXCEPT { return *this; }
1310};
1311
1312template <class _T1, class _T2>
1313class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1314                          private __compressed_pair_elem<_T2, 1> {
1315public:
1316  // NOTE: This static assert should never fire because __compressed_pair
1317  // is *almost never* used in a scenario where it's possible for T1 == T2.
1318  // (The exception is std::function where it is possible that the function
1319  //  object and the allocator have the same type).
1320  static_assert((!is_same<_T1, _T2>::value),
1321    "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
1322    "The current implementation is NOT ABI-compatible with the previous "
1323    "implementation for this configuration");
1324
1325    typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1326    typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1327
1328    template <bool _Dummy = true,
1329      class = typename enable_if<
1330          __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1331          __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1332      >::type
1333  >
1334  _LIBCPP_INLINE_VISIBILITY
1335  _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
1336
1337  template <class _U1, class _U2>
1338  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1339  __compressed_pair(_U1&& __t1, _U2&& __t2)
1340      : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
1341
1342#ifndef _LIBCPP_CXX03_LANG
1343  template <class... _Args1, class... _Args2>
1344  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1345  __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1346                    tuple<_Args2...> __second_args)
1347      : _Base1(__pc, _VSTD::move(__first_args),
1348               typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1349        _Base2(__pc, _VSTD::move(__second_args),
1350               typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
1351#endif
1352
1353  _LIBCPP_INLINE_VISIBILITY
1354  typename _Base1::reference first() _NOEXCEPT {
1355    return static_cast<_Base1&>(*this).__get();
1356  }
1357
1358  _LIBCPP_INLINE_VISIBILITY
1359  typename _Base1::const_reference first() const _NOEXCEPT {
1360    return static_cast<_Base1 const&>(*this).__get();
1361  }
1362
1363  _LIBCPP_INLINE_VISIBILITY
1364  typename _Base2::reference second() _NOEXCEPT {
1365    return static_cast<_Base2&>(*this).__get();
1366  }
1367
1368  _LIBCPP_INLINE_VISIBILITY
1369  typename _Base2::const_reference second() const _NOEXCEPT {
1370    return static_cast<_Base2 const&>(*this).__get();
1371  }
1372
1373  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1374  static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1375    return static_cast<_Base1*>(__pair);
1376  }
1377  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1378  static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1379    return static_cast<_Base2*>(__pair);
1380  }
1381
1382  _LIBCPP_INLINE_VISIBILITY
1383  void swap(__compressed_pair& __x)
1384    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1385               __is_nothrow_swappable<_T2>::value)
1386  {
1387    using _VSTD::swap;
1388    swap(first(), __x.first());
1389    swap(second(), __x.second());
1390  }
1391};
1392
1393template <class _T1, class _T2>
1394inline _LIBCPP_INLINE_VISIBILITY
1395void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1396    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1397               __is_nothrow_swappable<_T2>::value) {
1398  __x.swap(__y);
1399}
1400
1401// default_delete
1402
1403template <class _Tp>
1404struct _LIBCPP_TEMPLATE_VIS default_delete {
1405    static_assert(!is_function<_Tp>::value,
1406                  "default_delete cannot be instantiated for function types");
1407#ifndef _LIBCPP_CXX03_LANG
1408  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
1409#else
1410  _LIBCPP_INLINE_VISIBILITY default_delete() {}
1411#endif
1412  template <class _Up>
1413  _LIBCPP_INLINE_VISIBILITY
1414  default_delete(const default_delete<_Up>&,
1415                 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1416                     0) _NOEXCEPT {}
1417
1418  _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1419    static_assert(sizeof(_Tp) > 0,
1420                  "default_delete can not delete incomplete type");
1421    static_assert(!is_void<_Tp>::value,
1422                  "default_delete can not delete incomplete type");
1423    delete __ptr;
1424  }
1425};
1426
1427template <class _Tp>
1428struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1429private:
1430  template <class _Up>
1431  struct _EnableIfConvertible
1432      : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1433
1434public:
1435#ifndef _LIBCPP_CXX03_LANG
1436  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
1437#else
1438  _LIBCPP_INLINE_VISIBILITY default_delete() {}
1439#endif
1440
1441  template <class _Up>
1442  _LIBCPP_INLINE_VISIBILITY
1443  default_delete(const default_delete<_Up[]>&,
1444                 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1445
1446  template <class _Up>
1447  _LIBCPP_INLINE_VISIBILITY
1448  typename _EnableIfConvertible<_Up>::type
1449  operator()(_Up* __ptr) const _NOEXCEPT {
1450    static_assert(sizeof(_Tp) > 0,
1451                  "default_delete can not delete incomplete type");
1452    static_assert(!is_void<_Tp>::value,
1453                  "default_delete can not delete void type");
1454    delete[] __ptr;
1455  }
1456};
1457
1458template <class _Deleter>
1459struct __unique_ptr_deleter_sfinae {
1460  static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1461  typedef const _Deleter& __lval_ref_type;
1462  typedef _Deleter&& __good_rval_ref_type;
1463  typedef true_type __enable_rval_overload;
1464};
1465
1466template <class _Deleter>
1467struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1468  typedef const _Deleter& __lval_ref_type;
1469  typedef const _Deleter&& __bad_rval_ref_type;
1470  typedef false_type __enable_rval_overload;
1471};
1472
1473template <class _Deleter>
1474struct __unique_ptr_deleter_sfinae<_Deleter&> {
1475  typedef _Deleter& __lval_ref_type;
1476  typedef _Deleter&& __bad_rval_ref_type;
1477  typedef false_type __enable_rval_overload;
1478};
1479
1480#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1481#  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1482#else
1483#  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1484#endif
1485
1486template <class _Tp, class _Dp = default_delete<_Tp> >
1487class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
1488public:
1489  typedef _Tp element_type;
1490  typedef _Dp deleter_type;
1491  typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
1492
1493  static_assert(!is_rvalue_reference<deleter_type>::value,
1494                "the specified deleter type cannot be an rvalue reference");
1495
1496private:
1497  __compressed_pair<pointer, deleter_type> __ptr_;
1498
1499  struct __nat { int __for_bool_; };
1500
1501  typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
1502
1503  template <bool _Dummy>
1504  using _LValRefType _LIBCPP_NODEBUG_TYPE =
1505      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
1506
1507  template <bool _Dummy>
1508  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
1509      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
1510
1511  template <bool _Dummy>
1512  using _BadRValRefType _LIBCPP_NODEBUG_TYPE  =
1513      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
1514
1515  template <bool _Dummy, class _Deleter = typename __dependent_type<
1516                             __identity<deleter_type>, _Dummy>::type>
1517  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
1518      typename enable_if<is_default_constructible<_Deleter>::value &&
1519                         !is_pointer<_Deleter>::value>::type;
1520
1521  template <class _ArgType>
1522  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
1523      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1524
1525  template <class _UPtr, class _Up>
1526  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1527      is_convertible<typename _UPtr::pointer, pointer>::value &&
1528      !is_array<_Up>::value
1529  >::type;
1530
1531  template <class _UDel>
1532  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1533      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1534      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1535    >::type;
1536
1537  template <class _UDel>
1538  using _EnableIfDeleterAssignable = typename enable_if<
1539      is_assignable<_Dp&, _UDel&&>::value
1540    >::type;
1541
1542public:
1543  template <bool _Dummy = true,
1544            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1545  _LIBCPP_INLINE_VISIBILITY
1546  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1547
1548  template <bool _Dummy = true,
1549            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1550  _LIBCPP_INLINE_VISIBILITY
1551  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1552
1553  template <bool _Dummy = true,
1554            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1555  _LIBCPP_INLINE_VISIBILITY
1556  explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
1557
1558  template <bool _Dummy = true,
1559            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
1560  _LIBCPP_INLINE_VISIBILITY
1561  unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
1562      : __ptr_(__p, __d) {}
1563
1564  template <bool _Dummy = true,
1565            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
1566  _LIBCPP_INLINE_VISIBILITY
1567  unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
1568      : __ptr_(__p, _VSTD::move(__d)) {
1569    static_assert(!is_reference<deleter_type>::value,
1570                  "rvalue deleter bound to reference");
1571  }
1572
1573  template <bool _Dummy = true,
1574            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
1575  _LIBCPP_INLINE_VISIBILITY
1576  unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1577
1578  _LIBCPP_INLINE_VISIBILITY
1579  unique_ptr(unique_ptr&& __u) _NOEXCEPT
1580      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1581  }
1582
1583  template <class _Up, class _Ep,
1584      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1585      class = _EnableIfDeleterConvertible<_Ep>
1586  >
1587  _LIBCPP_INLINE_VISIBILITY
1588  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1589      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1590
1591#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1592  template <class _Up>
1593  _LIBCPP_INLINE_VISIBILITY
1594  unique_ptr(auto_ptr<_Up>&& __p,
1595             typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1596                                    is_same<_Dp, default_delete<_Tp> >::value,
1597                                __nat>::type = __nat()) _NOEXCEPT
1598      : __ptr_(__p.release(), __default_init_tag()) {}
1599#endif
1600
1601  _LIBCPP_INLINE_VISIBILITY
1602  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1603    reset(__u.release());
1604    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1605    return *this;
1606  }
1607
1608  template <class _Up, class _Ep,
1609      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1610      class = _EnableIfDeleterAssignable<_Ep>
1611  >
1612  _LIBCPP_INLINE_VISIBILITY
1613  unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1614    reset(__u.release());
1615    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1616    return *this;
1617  }
1618
1619#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1620  template <class _Up>
1621  _LIBCPP_INLINE_VISIBILITY
1622      typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1623                             is_same<_Dp, default_delete<_Tp> >::value,
1624                         unique_ptr&>::type
1625      operator=(auto_ptr<_Up> __p) {
1626    reset(__p.release());
1627    return *this;
1628  }
1629#endif
1630
1631#ifdef _LIBCPP_CXX03_LANG
1632  unique_ptr(unique_ptr const&) = delete;
1633  unique_ptr& operator=(unique_ptr const&) = delete;
1634#endif
1635
1636
1637  _LIBCPP_INLINE_VISIBILITY
1638  ~unique_ptr() { reset(); }
1639
1640  _LIBCPP_INLINE_VISIBILITY
1641  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1642    reset();
1643    return *this;
1644  }
1645
1646  _LIBCPP_INLINE_VISIBILITY
1647  typename add_lvalue_reference<_Tp>::type
1648  operator*() const {
1649    return *__ptr_.first();
1650  }
1651  _LIBCPP_INLINE_VISIBILITY
1652  pointer operator->() const _NOEXCEPT {
1653    return __ptr_.first();
1654  }
1655  _LIBCPP_INLINE_VISIBILITY
1656  pointer get() const _NOEXCEPT {
1657    return __ptr_.first();
1658  }
1659  _LIBCPP_INLINE_VISIBILITY
1660  deleter_type& get_deleter() _NOEXCEPT {
1661    return __ptr_.second();
1662  }
1663  _LIBCPP_INLINE_VISIBILITY
1664  const deleter_type& get_deleter() const _NOEXCEPT {
1665    return __ptr_.second();
1666  }
1667  _LIBCPP_INLINE_VISIBILITY
1668  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1669    return __ptr_.first() != nullptr;
1670  }
1671
1672  _LIBCPP_INLINE_VISIBILITY
1673  pointer release() _NOEXCEPT {
1674    pointer __t = __ptr_.first();
1675    __ptr_.first() = pointer();
1676    return __t;
1677  }
1678
1679  _LIBCPP_INLINE_VISIBILITY
1680  void reset(pointer __p = pointer()) _NOEXCEPT {
1681    pointer __tmp = __ptr_.first();
1682    __ptr_.first() = __p;
1683    if (__tmp)
1684      __ptr_.second()(__tmp);
1685  }
1686
1687  _LIBCPP_INLINE_VISIBILITY
1688  void swap(unique_ptr& __u) _NOEXCEPT {
1689    __ptr_.swap(__u.__ptr_);
1690  }
1691};
1692
1693
1694template <class _Tp, class _Dp>
1695class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
1696public:
1697  typedef _Tp element_type;
1698  typedef _Dp deleter_type;
1699  typedef typename __pointer<_Tp, deleter_type>::type pointer;
1700
1701private:
1702  __compressed_pair<pointer, deleter_type> __ptr_;
1703
1704  template <class _From>
1705  struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
1706
1707  template <class _FromElem>
1708  struct _CheckArrayPointerConversion<_FromElem*>
1709      : integral_constant<bool,
1710          is_same<_FromElem*, pointer>::value ||
1711            (is_same<pointer, element_type*>::value &&
1712             is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1713      >
1714  {};
1715
1716  typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
1717
1718  template <bool _Dummy>
1719  using _LValRefType _LIBCPP_NODEBUG_TYPE =
1720      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
1721
1722  template <bool _Dummy>
1723  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
1724      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
1725
1726  template <bool _Dummy>
1727  using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
1728      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
1729
1730  template <bool _Dummy, class _Deleter = typename __dependent_type<
1731                             __identity<deleter_type>, _Dummy>::type>
1732  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE  =
1733      typename enable_if<is_default_constructible<_Deleter>::value &&
1734                         !is_pointer<_Deleter>::value>::type;
1735
1736  template <class _ArgType>
1737  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
1738      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1739
1740  template <class _Pp>
1741  using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1742      _CheckArrayPointerConversion<_Pp>::value
1743  >::type;
1744
1745  template <class _UPtr, class _Up,
1746        class _ElemT = typename _UPtr::element_type>
1747  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1748      is_array<_Up>::value &&
1749      is_same<pointer, element_type*>::value &&
1750      is_same<typename _UPtr::pointer, _ElemT*>::value &&
1751      is_convertible<_ElemT(*)[], element_type(*)[]>::value
1752    >::type;
1753
1754  template <class _UDel>
1755  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1756      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1757      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1758    >::type;
1759
1760  template <class _UDel>
1761  using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1762      is_assignable<_Dp&, _UDel&&>::value
1763    >::type;
1764
1765public:
1766  template <bool _Dummy = true,
1767            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1768  _LIBCPP_INLINE_VISIBILITY
1769  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1770
1771  template <bool _Dummy = true,
1772            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1773  _LIBCPP_INLINE_VISIBILITY
1774  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1775
1776  template <class _Pp, bool _Dummy = true,
1777            class = _EnableIfDeleterDefaultConstructible<_Dummy>,
1778            class = _EnableIfPointerConvertible<_Pp> >
1779  _LIBCPP_INLINE_VISIBILITY
1780  explicit unique_ptr(_Pp __p) _NOEXCEPT
1781      : __ptr_(__p, __default_init_tag()) {}
1782
1783  template <class _Pp, bool _Dummy = true,
1784            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1785            class = _EnableIfPointerConvertible<_Pp> >
1786  _LIBCPP_INLINE_VISIBILITY
1787  unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
1788      : __ptr_(__p, __d) {}
1789
1790  template <bool _Dummy = true,
1791            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
1792  _LIBCPP_INLINE_VISIBILITY
1793  unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
1794      : __ptr_(nullptr, __d) {}
1795
1796  template <class _Pp, bool _Dummy = true,
1797            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1798            class = _EnableIfPointerConvertible<_Pp> >
1799  _LIBCPP_INLINE_VISIBILITY
1800  unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
1801      : __ptr_(__p, _VSTD::move(__d)) {
1802    static_assert(!is_reference<deleter_type>::value,
1803                  "rvalue deleter bound to reference");
1804  }
1805
1806  template <bool _Dummy = true,
1807            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
1808  _LIBCPP_INLINE_VISIBILITY
1809  unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
1810      : __ptr_(nullptr, _VSTD::move(__d)) {
1811    static_assert(!is_reference<deleter_type>::value,
1812                  "rvalue deleter bound to reference");
1813  }
1814
1815  template <class _Pp, bool _Dummy = true,
1816            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1817            class = _EnableIfPointerConvertible<_Pp> >
1818  _LIBCPP_INLINE_VISIBILITY
1819  unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
1820
1821  _LIBCPP_INLINE_VISIBILITY
1822  unique_ptr(unique_ptr&& __u) _NOEXCEPT
1823      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1824  }
1825
1826  _LIBCPP_INLINE_VISIBILITY
1827  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1828    reset(__u.release());
1829    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1830    return *this;
1831  }
1832
1833  template <class _Up, class _Ep,
1834      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1835      class = _EnableIfDeleterConvertible<_Ep>
1836  >
1837  _LIBCPP_INLINE_VISIBILITY
1838  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1839      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
1840  }
1841
1842  template <class _Up, class _Ep,
1843      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1844      class = _EnableIfDeleterAssignable<_Ep>
1845  >
1846  _LIBCPP_INLINE_VISIBILITY
1847  unique_ptr&
1848  operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1849    reset(__u.release());
1850    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1851    return *this;
1852  }
1853
1854#ifdef _LIBCPP_CXX03_LANG
1855  unique_ptr(unique_ptr const&) = delete;
1856  unique_ptr& operator=(unique_ptr const&) = delete;
1857#endif
1858
1859public:
1860  _LIBCPP_INLINE_VISIBILITY
1861  ~unique_ptr() { reset(); }
1862
1863  _LIBCPP_INLINE_VISIBILITY
1864  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1865    reset();
1866    return *this;
1867  }
1868
1869  _LIBCPP_INLINE_VISIBILITY
1870  typename add_lvalue_reference<_Tp>::type
1871  operator[](size_t __i) const {
1872    return __ptr_.first()[__i];
1873  }
1874  _LIBCPP_INLINE_VISIBILITY
1875  pointer get() const _NOEXCEPT {
1876    return __ptr_.first();
1877  }
1878
1879  _LIBCPP_INLINE_VISIBILITY
1880  deleter_type& get_deleter() _NOEXCEPT {
1881    return __ptr_.second();
1882  }
1883
1884  _LIBCPP_INLINE_VISIBILITY
1885  const deleter_type& get_deleter() const _NOEXCEPT {
1886    return __ptr_.second();
1887  }
1888  _LIBCPP_INLINE_VISIBILITY
1889  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1890    return __ptr_.first() != nullptr;
1891  }
1892
1893  _LIBCPP_INLINE_VISIBILITY
1894  pointer release() _NOEXCEPT {
1895    pointer __t = __ptr_.first();
1896    __ptr_.first() = pointer();
1897    return __t;
1898  }
1899
1900  template <class _Pp>
1901  _LIBCPP_INLINE_VISIBILITY
1902  typename enable_if<
1903      _CheckArrayPointerConversion<_Pp>::value
1904  >::type
1905  reset(_Pp __p) _NOEXCEPT {
1906    pointer __tmp = __ptr_.first();
1907    __ptr_.first() = __p;
1908    if (__tmp)
1909      __ptr_.second()(__tmp);
1910  }
1911
1912  _LIBCPP_INLINE_VISIBILITY
1913  void reset(nullptr_t = nullptr) _NOEXCEPT {
1914    pointer __tmp = __ptr_.first();
1915    __ptr_.first() = nullptr;
1916    if (__tmp)
1917      __ptr_.second()(__tmp);
1918  }
1919
1920  _LIBCPP_INLINE_VISIBILITY
1921  void swap(unique_ptr& __u) _NOEXCEPT {
1922    __ptr_.swap(__u.__ptr_);
1923  }
1924
1925};
1926
1927template <class _Tp, class _Dp>
1928inline _LIBCPP_INLINE_VISIBILITY
1929typename enable_if<
1930    __is_swappable<_Dp>::value,
1931    void
1932>::type
1933swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
1934
1935template <class _T1, class _D1, class _T2, class _D2>
1936inline _LIBCPP_INLINE_VISIBILITY
1937bool
1938operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1939
1940template <class _T1, class _D1, class _T2, class _D2>
1941inline _LIBCPP_INLINE_VISIBILITY
1942bool
1943operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1944
1945template <class _T1, class _D1, class _T2, class _D2>
1946inline _LIBCPP_INLINE_VISIBILITY
1947bool
1948operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1949{
1950    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1951    typedef typename unique_ptr<_T2, _D2>::pointer _P2;
1952    typedef typename common_type<_P1, _P2>::type _Vp;
1953    return less<_Vp>()(__x.get(), __y.get());
1954}
1955
1956template <class _T1, class _D1, class _T2, class _D2>
1957inline _LIBCPP_INLINE_VISIBILITY
1958bool
1959operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1960
1961template <class _T1, class _D1, class _T2, class _D2>
1962inline _LIBCPP_INLINE_VISIBILITY
1963bool
1964operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1965
1966template <class _T1, class _D1, class _T2, class _D2>
1967inline _LIBCPP_INLINE_VISIBILITY
1968bool
1969operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1970
1971template <class _T1, class _D1>
1972inline _LIBCPP_INLINE_VISIBILITY
1973bool
1974operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
1975{
1976    return !__x;
1977}
1978
1979template <class _T1, class _D1>
1980inline _LIBCPP_INLINE_VISIBILITY
1981bool
1982operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
1983{
1984    return !__x;
1985}
1986
1987template <class _T1, class _D1>
1988inline _LIBCPP_INLINE_VISIBILITY
1989bool
1990operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
1991{
1992    return static_cast<bool>(__x);
1993}
1994
1995template <class _T1, class _D1>
1996inline _LIBCPP_INLINE_VISIBILITY
1997bool
1998operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
1999{
2000    return static_cast<bool>(__x);
2001}
2002
2003template <class _T1, class _D1>
2004inline _LIBCPP_INLINE_VISIBILITY
2005bool
2006operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2007{
2008    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2009    return less<_P1>()(__x.get(), nullptr);
2010}
2011
2012template <class _T1, class _D1>
2013inline _LIBCPP_INLINE_VISIBILITY
2014bool
2015operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2016{
2017    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2018    return less<_P1>()(nullptr, __x.get());
2019}
2020
2021template <class _T1, class _D1>
2022inline _LIBCPP_INLINE_VISIBILITY
2023bool
2024operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2025{
2026    return nullptr < __x;
2027}
2028
2029template <class _T1, class _D1>
2030inline _LIBCPP_INLINE_VISIBILITY
2031bool
2032operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2033{
2034    return __x < nullptr;
2035}
2036
2037template <class _T1, class _D1>
2038inline _LIBCPP_INLINE_VISIBILITY
2039bool
2040operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2041{
2042    return !(nullptr < __x);
2043}
2044
2045template <class _T1, class _D1>
2046inline _LIBCPP_INLINE_VISIBILITY
2047bool
2048operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2049{
2050    return !(__x < nullptr);
2051}
2052
2053template <class _T1, class _D1>
2054inline _LIBCPP_INLINE_VISIBILITY
2055bool
2056operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2057{
2058    return !(__x < nullptr);
2059}
2060
2061template <class _T1, class _D1>
2062inline _LIBCPP_INLINE_VISIBILITY
2063bool
2064operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2065{
2066    return !(nullptr < __x);
2067}
2068
2069#if _LIBCPP_STD_VER > 11
2070
2071template<class _Tp>
2072struct __unique_if
2073{
2074    typedef unique_ptr<_Tp> __unique_single;
2075};
2076
2077template<class _Tp>
2078struct __unique_if<_Tp[]>
2079{
2080    typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2081};
2082
2083template<class _Tp, size_t _Np>
2084struct __unique_if<_Tp[_Np]>
2085{
2086    typedef void __unique_array_known_bound;
2087};
2088
2089template<class _Tp, class... _Args>
2090inline _LIBCPP_INLINE_VISIBILITY
2091typename __unique_if<_Tp>::__unique_single
2092make_unique(_Args&&... __args)
2093{
2094    return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2095}
2096
2097template<class _Tp>
2098inline _LIBCPP_INLINE_VISIBILITY
2099typename __unique_if<_Tp>::__unique_array_unknown_bound
2100make_unique(size_t __n)
2101{
2102    typedef typename remove_extent<_Tp>::type _Up;
2103    return unique_ptr<_Tp>(new _Up[__n]());
2104}
2105
2106template<class _Tp, class... _Args>
2107    typename __unique_if<_Tp>::__unique_array_known_bound
2108    make_unique(_Args&&...) = delete;
2109
2110#endif  // _LIBCPP_STD_VER > 11
2111
2112template <class _Tp, class _Dp>
2113#ifdef _LIBCPP_CXX03_LANG
2114struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
2115#else
2116struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
2117    unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
2118#endif
2119{
2120    typedef unique_ptr<_Tp, _Dp> argument_type;
2121    typedef size_t               result_type;
2122    _LIBCPP_INLINE_VISIBILITY
2123    result_type operator()(const argument_type& __ptr) const
2124    {
2125        typedef typename argument_type::pointer pointer;
2126        return hash<pointer>()(__ptr.get());
2127    }
2128};
2129
2130struct __destruct_n
2131{
2132private:
2133    size_t __size_;
2134
2135    template <class _Tp>
2136    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
2137        {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
2138
2139    template <class _Tp>
2140    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
2141        {}
2142
2143    _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
2144        {++__size_;}
2145    _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
2146        {}
2147
2148    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
2149        {__size_ = __s;}
2150    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
2151        {}
2152public:
2153    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2154        : __size_(__s) {}
2155
2156    template <class _Tp>
2157    _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
2158        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2159
2160    template <class _Tp>
2161    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
2162        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2163
2164    template <class _Tp>
2165    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
2166        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2167};
2168
2169template <class _Alloc>
2170class __allocator_destructor
2171{
2172    typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
2173public:
2174    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2175    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
2176private:
2177    _Alloc& __alloc_;
2178    size_type __s_;
2179public:
2180    _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2181             _NOEXCEPT
2182        : __alloc_(__a), __s_(__s) {}
2183    _LIBCPP_INLINE_VISIBILITY
2184    void operator()(pointer __p) _NOEXCEPT
2185        {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2186};
2187
2188template <class _InputIterator, class _ForwardIterator>
2189_ForwardIterator
2190uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2191{
2192    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2193#ifndef _LIBCPP_NO_EXCEPTIONS
2194    _ForwardIterator __s = __r;
2195    try
2196    {
2197#endif
2198        for (; __f != __l; ++__f, (void) ++__r)
2199            ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
2200#ifndef _LIBCPP_NO_EXCEPTIONS
2201    }
2202    catch (...)
2203    {
2204        for (; __s != __r; ++__s)
2205            __s->~value_type();
2206        throw;
2207    }
2208#endif
2209    return __r;
2210}
2211
2212template <class _InputIterator, class _Size, class _ForwardIterator>
2213_ForwardIterator
2214uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2215{
2216    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2217#ifndef _LIBCPP_NO_EXCEPTIONS
2218    _ForwardIterator __s = __r;
2219    try
2220    {
2221#endif
2222        for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2223            ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
2224#ifndef _LIBCPP_NO_EXCEPTIONS
2225    }
2226    catch (...)
2227    {
2228        for (; __s != __r; ++__s)
2229            __s->~value_type();
2230        throw;
2231    }
2232#endif
2233    return __r;
2234}
2235
2236template <class _ForwardIterator, class _Tp>
2237void
2238uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2239{
2240    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2241#ifndef _LIBCPP_NO_EXCEPTIONS
2242    _ForwardIterator __s = __f;
2243    try
2244    {
2245#endif
2246        for (; __f != __l; ++__f)
2247            ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
2248#ifndef _LIBCPP_NO_EXCEPTIONS
2249    }
2250    catch (...)
2251    {
2252        for (; __s != __f; ++__s)
2253            __s->~value_type();
2254        throw;
2255    }
2256#endif
2257}
2258
2259template <class _ForwardIterator, class _Size, class _Tp>
2260_ForwardIterator
2261uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2262{
2263    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2264#ifndef _LIBCPP_NO_EXCEPTIONS
2265    _ForwardIterator __s = __f;
2266    try
2267    {
2268#endif
2269        for (; __n > 0; ++__f, (void) --__n)
2270            ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
2271#ifndef _LIBCPP_NO_EXCEPTIONS
2272    }
2273    catch (...)
2274    {
2275        for (; __s != __f; ++__s)
2276            __s->~value_type();
2277        throw;
2278    }
2279#endif
2280    return __f;
2281}
2282
2283#if _LIBCPP_STD_VER > 14
2284
2285template <class _ForwardIterator>
2286inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2287void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2288    for (; __first != __last; ++__first)
2289        _VSTD::destroy_at(_VSTD::addressof(*__first));
2290}
2291
2292template <class _ForwardIterator, class _Size>
2293inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2294_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2295    for (; __n > 0; (void)++__first, --__n)
2296        _VSTD::destroy_at(_VSTD::addressof(*__first));
2297    return __first;
2298}
2299
2300template <class _ForwardIterator>
2301inline _LIBCPP_INLINE_VISIBILITY
2302void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2303    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2304    auto __idx = __first;
2305#ifndef _LIBCPP_NO_EXCEPTIONS
2306    try {
2307#endif
2308    for (; __idx != __last; ++__idx)
2309        ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
2310#ifndef _LIBCPP_NO_EXCEPTIONS
2311    } catch (...) {
2312        _VSTD::destroy(__first, __idx);
2313        throw;
2314    }
2315#endif
2316}
2317
2318template <class _ForwardIterator, class _Size>
2319inline _LIBCPP_INLINE_VISIBILITY
2320_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2321    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2322    auto __idx = __first;
2323#ifndef _LIBCPP_NO_EXCEPTIONS
2324    try {
2325#endif
2326    for (; __n > 0; (void)++__idx, --__n)
2327        ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
2328    return __idx;
2329#ifndef _LIBCPP_NO_EXCEPTIONS
2330    } catch (...) {
2331        _VSTD::destroy(__first, __idx);
2332        throw;
2333    }
2334#endif
2335}
2336
2337
2338template <class _ForwardIterator>
2339inline _LIBCPP_INLINE_VISIBILITY
2340void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2341    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2342    auto __idx = __first;
2343#ifndef _LIBCPP_NO_EXCEPTIONS
2344    try {
2345#endif
2346    for (; __idx != __last; ++__idx)
2347        ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
2348#ifndef _LIBCPP_NO_EXCEPTIONS
2349    } catch (...) {
2350        _VSTD::destroy(__first, __idx);
2351        throw;
2352    }
2353#endif
2354}
2355
2356template <class _ForwardIterator, class _Size>
2357inline _LIBCPP_INLINE_VISIBILITY
2358_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2359    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2360    auto __idx = __first;
2361#ifndef _LIBCPP_NO_EXCEPTIONS
2362    try {
2363#endif
2364    for (; __n > 0; (void)++__idx, --__n)
2365        ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
2366    return __idx;
2367#ifndef _LIBCPP_NO_EXCEPTIONS
2368    } catch (...) {
2369        _VSTD::destroy(__first, __idx);
2370        throw;
2371    }
2372#endif
2373}
2374
2375
2376template <class _InputIt, class _ForwardIt>
2377inline _LIBCPP_INLINE_VISIBILITY
2378_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2379    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2380    auto __idx = __first_res;
2381#ifndef _LIBCPP_NO_EXCEPTIONS
2382    try {
2383#endif
2384    for (; __first != __last; (void)++__idx, ++__first)
2385        ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
2386    return __idx;
2387#ifndef _LIBCPP_NO_EXCEPTIONS
2388    } catch (...) {
2389        _VSTD::destroy(__first_res, __idx);
2390        throw;
2391    }
2392#endif
2393}
2394
2395template <class _InputIt, class _Size, class _ForwardIt>
2396inline _LIBCPP_INLINE_VISIBILITY
2397pair<_InputIt, _ForwardIt>
2398uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2399    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2400    auto __idx = __first_res;
2401#ifndef _LIBCPP_NO_EXCEPTIONS
2402    try {
2403#endif
2404    for (; __n > 0; ++__idx, (void)++__first, --__n)
2405        ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
2406    return {__first, __idx};
2407#ifndef _LIBCPP_NO_EXCEPTIONS
2408    } catch (...) {
2409        _VSTD::destroy(__first_res, __idx);
2410        throw;
2411    }
2412#endif
2413}
2414
2415
2416#endif // _LIBCPP_STD_VER > 14
2417
2418// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2419// should be sufficient for thread safety.
2420// See https://bugs.llvm.org/show_bug.cgi?id=22803
2421#if defined(__clang__) && __has_builtin(__atomic_add_fetch)          \
2422                       && defined(__ATOMIC_RELAXED)                  \
2423                       && defined(__ATOMIC_ACQ_REL)
2424#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2425#elif defined(_LIBCPP_COMPILER_GCC)
2426#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2427#endif
2428
2429template <class _Tp>
2430inline _LIBCPP_INLINE_VISIBILITY _Tp
2431__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2432{
2433#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2434    return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2435#else
2436    return __t += 1;
2437#endif
2438}
2439
2440template <class _Tp>
2441inline _LIBCPP_INLINE_VISIBILITY _Tp
2442__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2443{
2444#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2445    return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2446#else
2447    return __t -= 1;
2448#endif
2449}
2450
2451class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
2452    : public std::exception
2453{
2454public:
2455    bad_weak_ptr() _NOEXCEPT = default;
2456    bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
2457    virtual ~bad_weak_ptr() _NOEXCEPT;
2458    virtual const char* what() const  _NOEXCEPT;
2459};
2460
2461_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2462void __throw_bad_weak_ptr()
2463{
2464#ifndef _LIBCPP_NO_EXCEPTIONS
2465    throw bad_weak_ptr();
2466#else
2467    _VSTD::abort();
2468#endif
2469}
2470
2471template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
2472
2473class _LIBCPP_TYPE_VIS __shared_count
2474{
2475    __shared_count(const __shared_count&);
2476    __shared_count& operator=(const __shared_count&);
2477
2478protected:
2479    long __shared_owners_;
2480    virtual ~__shared_count();
2481private:
2482    virtual void __on_zero_shared() _NOEXCEPT = 0;
2483
2484public:
2485    _LIBCPP_INLINE_VISIBILITY
2486    explicit __shared_count(long __refs = 0) _NOEXCEPT
2487        : __shared_owners_(__refs) {}
2488
2489#if defined(_LIBCPP_BUILDING_LIBRARY) && \
2490    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
2491    void __add_shared() _NOEXCEPT;
2492    bool __release_shared() _NOEXCEPT;
2493#else
2494    _LIBCPP_INLINE_VISIBILITY
2495    void __add_shared() _NOEXCEPT {
2496      __libcpp_atomic_refcount_increment(__shared_owners_);
2497    }
2498    _LIBCPP_INLINE_VISIBILITY
2499    bool __release_shared() _NOEXCEPT {
2500      if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2501        __on_zero_shared();
2502        return true;
2503      }
2504      return false;
2505    }
2506#endif
2507    _LIBCPP_INLINE_VISIBILITY
2508    long use_count() const _NOEXCEPT {
2509        return __libcpp_relaxed_load(&__shared_owners_) + 1;
2510    }
2511};
2512
2513class _LIBCPP_TYPE_VIS __shared_weak_count
2514    : private __shared_count
2515{
2516    long __shared_weak_owners_;
2517
2518public:
2519    _LIBCPP_INLINE_VISIBILITY
2520    explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
2521        : __shared_count(__refs),
2522          __shared_weak_owners_(__refs) {}
2523protected:
2524    virtual ~__shared_weak_count();
2525
2526public:
2527#if defined(_LIBCPP_BUILDING_LIBRARY) && \
2528    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
2529    void __add_shared() _NOEXCEPT;
2530    void __add_weak() _NOEXCEPT;
2531    void __release_shared() _NOEXCEPT;
2532#else
2533    _LIBCPP_INLINE_VISIBILITY
2534    void __add_shared() _NOEXCEPT {
2535      __shared_count::__add_shared();
2536    }
2537    _LIBCPP_INLINE_VISIBILITY
2538    void __add_weak() _NOEXCEPT {
2539      __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2540    }
2541    _LIBCPP_INLINE_VISIBILITY
2542    void __release_shared() _NOEXCEPT {
2543      if (__shared_count::__release_shared())
2544        __release_weak();
2545    }
2546#endif
2547    void __release_weak() _NOEXCEPT;
2548    _LIBCPP_INLINE_VISIBILITY
2549    long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2550    __shared_weak_count* lock() _NOEXCEPT;
2551
2552    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
2553private:
2554    virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
2555};
2556
2557template <class _Tp, class _Dp, class _Alloc>
2558class __shared_ptr_pointer
2559    : public __shared_weak_count
2560{
2561    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2562public:
2563    _LIBCPP_INLINE_VISIBILITY
2564    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2565        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
2566
2567#ifndef _LIBCPP_NO_RTTI
2568    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
2569#endif
2570
2571private:
2572    virtual void __on_zero_shared() _NOEXCEPT;
2573    virtual void __on_zero_shared_weak() _NOEXCEPT;
2574};
2575
2576#ifndef _LIBCPP_NO_RTTI
2577
2578template <class _Tp, class _Dp, class _Alloc>
2579const void*
2580__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
2581{
2582    return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
2583}
2584
2585#endif  // _LIBCPP_NO_RTTI
2586
2587template <class _Tp, class _Dp, class _Alloc>
2588void
2589__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
2590{
2591    __data_.first().second()(__data_.first().first());
2592    __data_.first().second().~_Dp();
2593}
2594
2595template <class _Tp, class _Dp, class _Alloc>
2596void
2597__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
2598{
2599    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2600    typedef allocator_traits<_Al> _ATraits;
2601    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2602
2603    _Al __a(__data_.second());
2604    __data_.second().~_Alloc();
2605    __a.deallocate(_PTraits::pointer_to(*this), 1);
2606}
2607
2608template <class _Tp, class _Alloc>
2609struct __shared_ptr_emplace
2610    : __shared_weak_count
2611{
2612    template<class ..._Args>
2613    _LIBCPP_HIDE_FROM_ABI
2614    explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2615        : __storage_(_VSTD::move(__a))
2616    {
2617#if _LIBCPP_STD_VER > 17
2618        using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2619        _TpAlloc __tmp(*__get_alloc());
2620        allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
2621#else
2622        ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
2623#endif
2624    }
2625
2626    _LIBCPP_HIDE_FROM_ABI
2627    _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
2628
2629    _LIBCPP_HIDE_FROM_ABI
2630    _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
2631
2632private:
2633    virtual void __on_zero_shared() _NOEXCEPT {
2634#if _LIBCPP_STD_VER > 17
2635        using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2636        _TpAlloc __tmp(*__get_alloc());
2637        allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2638#else
2639        __get_elem()->~_Tp();
2640#endif
2641    }
2642
2643    virtual void __on_zero_shared_weak() _NOEXCEPT {
2644        using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2645        using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
2646        _ControlBlockAlloc __tmp(*__get_alloc());
2647        __storage_.~_Storage();
2648        allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2649            pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2650    }
2651
2652    // This class implements the control block for non-array shared pointers created
2653    // through `std::allocate_shared` and `std::make_shared`.
2654    //
2655    // In previous versions of the library, we used a compressed pair to store
2656    // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2657    // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2658    // we now use a properly aligned char buffer while making sure that we maintain
2659    // the same layout that we had when we used a compressed pair.
2660    using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2661    struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2662        char __blob_[sizeof(_CompressedPair)];
2663
2664        _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2665            ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2666        }
2667        _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2668            __get_alloc()->~_Alloc();
2669        }
2670        _Alloc* __get_alloc() _NOEXCEPT {
2671            _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2672            typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2673            _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2674            return __alloc;
2675        }
2676        _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
2677            _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2678            typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2679            _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2680            return __elem;
2681        }
2682    };
2683
2684    static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2685    static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2686    _Storage __storage_;
2687};
2688
2689struct __shared_ptr_dummy_rebind_allocator_type;
2690template <>
2691class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2692{
2693public:
2694    template <class _Other>
2695    struct rebind
2696    {
2697        typedef allocator<_Other> other;
2698    };
2699};
2700
2701template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
2702
2703template<class _Tp, class _Up>
2704struct __compatible_with
2705#if _LIBCPP_STD_VER > 14
2706    : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2707#else
2708    : is_convertible<_Tp*, _Up*> {};
2709#endif // _LIBCPP_STD_VER > 14
2710
2711#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2712#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2713#else
2714#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2715#endif
2716
2717template<class _Tp>
2718class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
2719{
2720public:
2721#if _LIBCPP_STD_VER > 14
2722    typedef weak_ptr<_Tp> weak_type;
2723    typedef remove_extent_t<_Tp> element_type;
2724#else
2725    typedef _Tp element_type;
2726#endif
2727
2728private:
2729    element_type*      __ptr_;
2730    __shared_weak_count* __cntrl_;
2731
2732    struct __nat {int __for_bool_;};
2733public:
2734    _LIBCPP_INLINE_VISIBILITY
2735    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
2736    _LIBCPP_INLINE_VISIBILITY
2737    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
2738    template<class _Yp>
2739        explicit shared_ptr(_Yp* __p,
2740                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
2741    template<class _Yp, class _Dp>
2742        shared_ptr(_Yp* __p, _Dp __d,
2743                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
2744    template<class _Yp, class _Dp, class _Alloc>
2745        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
2746                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
2747    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2748    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
2749    template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2750    _LIBCPP_INLINE_VISIBILITY
2751    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
2752    template<class _Yp>
2753        _LIBCPP_INLINE_VISIBILITY
2754        shared_ptr(const shared_ptr<_Yp>& __r,
2755                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
2756                       _NOEXCEPT;
2757    _LIBCPP_INLINE_VISIBILITY
2758    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
2759    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
2760                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
2761                       _NOEXCEPT;
2762    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
2763                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
2764#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2765    template<class _Yp>
2766        shared_ptr(auto_ptr<_Yp>&& __r,
2767                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
2768#endif
2769    template <class _Yp, class _Dp>
2770        shared_ptr(unique_ptr<_Yp, _Dp>&&,
2771                   typename enable_if
2772                   <
2773                       !is_lvalue_reference<_Dp>::value &&
2774                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2775                       __nat
2776                   >::type = __nat());
2777    template <class _Yp, class _Dp>
2778        shared_ptr(unique_ptr<_Yp, _Dp>&&,
2779                   typename enable_if
2780                   <
2781                       is_lvalue_reference<_Dp>::value &&
2782                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2783                       __nat
2784                   >::type = __nat());
2785
2786    ~shared_ptr();
2787
2788    _LIBCPP_INLINE_VISIBILITY
2789    shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
2790    template<class _Yp>
2791        typename enable_if
2792        <
2793            __compatible_with<_Yp, element_type>::value,
2794            shared_ptr&
2795        >::type
2796        _LIBCPP_INLINE_VISIBILITY
2797        operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
2798    _LIBCPP_INLINE_VISIBILITY
2799    shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
2800    template<class _Yp>
2801        typename enable_if
2802        <
2803            __compatible_with<_Yp, element_type>::value,
2804            shared_ptr&
2805        >::type
2806        _LIBCPP_INLINE_VISIBILITY
2807        operator=(shared_ptr<_Yp>&& __r);
2808#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2809    template<class _Yp>
2810        _LIBCPP_INLINE_VISIBILITY
2811        typename enable_if
2812        <
2813            !is_array<_Yp>::value &&
2814            is_convertible<_Yp*, element_type*>::value,
2815            shared_ptr
2816        >::type&
2817        operator=(auto_ptr<_Yp>&& __r);
2818#endif
2819    template <class _Yp, class _Dp>
2820        typename enable_if
2821        <
2822            is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2823            shared_ptr&
2824        >::type
2825        _LIBCPP_INLINE_VISIBILITY
2826        operator=(unique_ptr<_Yp, _Dp>&& __r);
2827
2828    _LIBCPP_INLINE_VISIBILITY
2829    void swap(shared_ptr& __r) _NOEXCEPT;
2830    _LIBCPP_INLINE_VISIBILITY
2831    void reset() _NOEXCEPT;
2832    template<class _Yp>
2833        typename enable_if
2834        <
2835            __compatible_with<_Yp, element_type>::value,
2836            void
2837        >::type
2838        _LIBCPP_INLINE_VISIBILITY
2839        reset(_Yp* __p);
2840    template<class _Yp, class _Dp>
2841        typename enable_if
2842        <
2843            __compatible_with<_Yp, element_type>::value,
2844            void
2845        >::type
2846        _LIBCPP_INLINE_VISIBILITY
2847        reset(_Yp* __p, _Dp __d);
2848    template<class _Yp, class _Dp, class _Alloc>
2849        typename enable_if
2850        <
2851            __compatible_with<_Yp, element_type>::value,
2852            void
2853        >::type
2854        _LIBCPP_INLINE_VISIBILITY
2855        reset(_Yp* __p, _Dp __d, _Alloc __a);
2856
2857    _LIBCPP_INLINE_VISIBILITY
2858    element_type* get() const _NOEXCEPT {return __ptr_;}
2859    _LIBCPP_INLINE_VISIBILITY
2860    typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2861        {return *__ptr_;}
2862    _LIBCPP_INLINE_VISIBILITY
2863    element_type* operator->() const _NOEXCEPT
2864    {
2865        static_assert(!_VSTD::is_array<_Tp>::value,
2866                      "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2867        return __ptr_;
2868    }
2869    _LIBCPP_INLINE_VISIBILITY
2870    long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
2871    _LIBCPP_INLINE_VISIBILITY
2872    bool unique() const _NOEXCEPT {return use_count() == 1;}
2873    _LIBCPP_INLINE_VISIBILITY
2874    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
2875    template <class _Up>
2876        _LIBCPP_INLINE_VISIBILITY
2877        bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
2878        {return __cntrl_ < __p.__cntrl_;}
2879    template <class _Up>
2880        _LIBCPP_INLINE_VISIBILITY
2881        bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
2882        {return __cntrl_ < __p.__cntrl_;}
2883    _LIBCPP_INLINE_VISIBILITY
2884    bool
2885    __owner_equivalent(const shared_ptr& __p) const
2886        {return __cntrl_ == __p.__cntrl_;}
2887
2888#if _LIBCPP_STD_VER > 14
2889    typename add_lvalue_reference<element_type>::type
2890    _LIBCPP_INLINE_VISIBILITY
2891    operator[](ptrdiff_t __i) const
2892    {
2893            static_assert(_VSTD::is_array<_Tp>::value,
2894                          "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2895            return __ptr_[__i];
2896    }
2897#endif
2898
2899#ifndef _LIBCPP_NO_RTTI
2900    template <class _Dp>
2901        _LIBCPP_INLINE_VISIBILITY
2902        _Dp* __get_deleter() const _NOEXCEPT
2903            {return static_cast<_Dp*>(__cntrl_
2904                    ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
2905                      : nullptr);}
2906#endif  // _LIBCPP_NO_RTTI
2907
2908    template<class _Yp, class _CntrlBlk>
2909    static shared_ptr<_Tp>
2910    __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
2911    {
2912        shared_ptr<_Tp> __r;
2913        __r.__ptr_ = __p;
2914        __r.__cntrl_ = __cntrl;
2915        __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2916        return __r;
2917    }
2918
2919private:
2920    template <class _Yp, bool = is_function<_Yp>::value>
2921        struct __shared_ptr_default_allocator
2922        {
2923            typedef allocator<_Yp> type;
2924        };
2925
2926    template <class _Yp>
2927        struct __shared_ptr_default_allocator<_Yp, true>
2928        {
2929            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2930        };
2931
2932    template <class _Yp, class _OrigPtr>
2933        _LIBCPP_INLINE_VISIBILITY
2934        typename enable_if<is_convertible<_OrigPtr*,
2935                                          const enable_shared_from_this<_Yp>*
2936        >::value,
2937            void>::type
2938        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2939                           _OrigPtr* __ptr) _NOEXCEPT
2940        {
2941            typedef typename remove_cv<_Yp>::type _RawYp;
2942            if (__e && __e->__weak_this_.expired())
2943            {
2944                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2945                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
2946            }
2947        }
2948
2949    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
2950
2951    template <class, class _Yp>
2952        struct __shared_ptr_default_delete
2953            : default_delete<_Yp> {};
2954
2955    template <class _Yp, class _Un, size_t _Sz>
2956        struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2957            : default_delete<_Yp[]> {};
2958
2959    template <class _Yp, class _Un>
2960        struct __shared_ptr_default_delete<_Yp[], _Un>
2961            : default_delete<_Yp[]> {};
2962
2963    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2964    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
2965};
2966
2967#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2968template<class _Tp>
2969shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2970template<class _Tp, class _Dp>
2971shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2972#endif
2973
2974template<class _Tp>
2975inline
2976_LIBCPP_CONSTEXPR
2977shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
2978    : __ptr_(nullptr),
2979      __cntrl_(nullptr)
2980{
2981}
2982
2983template<class _Tp>
2984inline
2985_LIBCPP_CONSTEXPR
2986shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
2987    : __ptr_(nullptr),
2988      __cntrl_(nullptr)
2989{
2990}
2991
2992template<class _Tp>
2993template<class _Yp>
2994shared_ptr<_Tp>::shared_ptr(_Yp* __p,
2995                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
2996    : __ptr_(__p)
2997{
2998    unique_ptr<_Yp> __hold(__p);
2999    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3000    typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3001    __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
3002    __hold.release();
3003    __enable_weak_this(__p, __p);
3004}
3005
3006template<class _Tp>
3007template<class _Yp, class _Dp>
3008shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3009                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3010    : __ptr_(__p)
3011{
3012#ifndef _LIBCPP_NO_EXCEPTIONS
3013    try
3014    {
3015#endif  // _LIBCPP_NO_EXCEPTIONS
3016        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3017        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3018        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3019        __enable_weak_this(__p, __p);
3020#ifndef _LIBCPP_NO_EXCEPTIONS
3021    }
3022    catch (...)
3023    {
3024        __d(__p);
3025        throw;
3026    }
3027#endif  // _LIBCPP_NO_EXCEPTIONS
3028}
3029
3030template<class _Tp>
3031template<class _Dp>
3032shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3033    : __ptr_(nullptr)
3034{
3035#ifndef _LIBCPP_NO_EXCEPTIONS
3036    try
3037    {
3038#endif  // _LIBCPP_NO_EXCEPTIONS
3039        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3040        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3041        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3042#ifndef _LIBCPP_NO_EXCEPTIONS
3043    }
3044    catch (...)
3045    {
3046        __d(__p);
3047        throw;
3048    }
3049#endif  // _LIBCPP_NO_EXCEPTIONS
3050}
3051
3052template<class _Tp>
3053template<class _Yp, class _Dp, class _Alloc>
3054shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3055                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3056    : __ptr_(__p)
3057{
3058#ifndef _LIBCPP_NO_EXCEPTIONS
3059    try
3060    {
3061#endif  // _LIBCPP_NO_EXCEPTIONS
3062        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3063        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3064        typedef __allocator_destructor<_A2> _D2;
3065        _A2 __a2(__a);
3066        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3067        ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
3068        __cntrl_ = _VSTD::addressof(*__hold2.release());
3069        __enable_weak_this(__p, __p);
3070#ifndef _LIBCPP_NO_EXCEPTIONS
3071    }
3072    catch (...)
3073    {
3074        __d(__p);
3075        throw;
3076    }
3077#endif  // _LIBCPP_NO_EXCEPTIONS
3078}
3079
3080template<class _Tp>
3081template<class _Dp, class _Alloc>
3082shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3083    : __ptr_(nullptr)
3084{
3085#ifndef _LIBCPP_NO_EXCEPTIONS
3086    try
3087    {
3088#endif  // _LIBCPP_NO_EXCEPTIONS
3089        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3090        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3091        typedef __allocator_destructor<_A2> _D2;
3092        _A2 __a2(__a);
3093        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3094        ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
3095        __cntrl_ = _VSTD::addressof(*__hold2.release());
3096#ifndef _LIBCPP_NO_EXCEPTIONS
3097    }
3098    catch (...)
3099    {
3100        __d(__p);
3101        throw;
3102    }
3103#endif  // _LIBCPP_NO_EXCEPTIONS
3104}
3105
3106template<class _Tp>
3107template<class _Yp>
3108inline
3109shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
3110    : __ptr_(__p),
3111      __cntrl_(__r.__cntrl_)
3112{
3113    if (__cntrl_)
3114        __cntrl_->__add_shared();
3115}
3116
3117template<class _Tp>
3118inline
3119shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
3120    : __ptr_(__r.__ptr_),
3121      __cntrl_(__r.__cntrl_)
3122{
3123    if (__cntrl_)
3124        __cntrl_->__add_shared();
3125}
3126
3127template<class _Tp>
3128template<class _Yp>
3129inline
3130shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3131                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3132         _NOEXCEPT
3133    : __ptr_(__r.__ptr_),
3134      __cntrl_(__r.__cntrl_)
3135{
3136    if (__cntrl_)
3137        __cntrl_->__add_shared();
3138}
3139
3140template<class _Tp>
3141inline
3142shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
3143    : __ptr_(__r.__ptr_),
3144      __cntrl_(__r.__cntrl_)
3145{
3146    __r.__ptr_ = nullptr;
3147    __r.__cntrl_ = nullptr;
3148}
3149
3150template<class _Tp>
3151template<class _Yp>
3152inline
3153shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3154                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3155         _NOEXCEPT
3156    : __ptr_(__r.__ptr_),
3157      __cntrl_(__r.__cntrl_)
3158{
3159    __r.__ptr_ = nullptr;
3160    __r.__cntrl_ = nullptr;
3161}
3162
3163#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3164template<class _Tp>
3165template<class _Yp>
3166shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
3167                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3168    : __ptr_(__r.get())
3169{
3170    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3171    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3172    __enable_weak_this(__r.get(), __r.get());
3173    __r.release();
3174}
3175#endif
3176
3177template<class _Tp>
3178template <class _Yp, class _Dp>
3179shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3180                            typename enable_if
3181                            <
3182                                !is_lvalue_reference<_Dp>::value &&
3183                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3184                                __nat
3185                            >::type)
3186    : __ptr_(__r.get())
3187{
3188#if _LIBCPP_STD_VER > 11
3189    if (__ptr_ == nullptr)
3190        __cntrl_ = nullptr;
3191    else
3192#endif
3193    {
3194        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3195        typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
3196        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
3197        __enable_weak_this(__r.get(), __r.get());
3198    }
3199    __r.release();
3200}
3201
3202template<class _Tp>
3203template <class _Yp, class _Dp>
3204shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3205                            typename enable_if
3206                            <
3207                                is_lvalue_reference<_Dp>::value &&
3208                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3209                                __nat
3210                            >::type)
3211    : __ptr_(__r.get())
3212{
3213#if _LIBCPP_STD_VER > 11
3214    if (__ptr_ == nullptr)
3215        __cntrl_ = nullptr;
3216    else
3217#endif
3218    {
3219        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3220        typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
3221                                     reference_wrapper<typename remove_reference<_Dp>::type>,
3222                                     _AllocT > _CntrlBlk;
3223        __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
3224        __enable_weak_this(__r.get(), __r.get());
3225    }
3226    __r.release();
3227}
3228
3229template<class _Tp>
3230shared_ptr<_Tp>::~shared_ptr()
3231{
3232    if (__cntrl_)
3233        __cntrl_->__release_shared();
3234}
3235
3236template<class _Tp>
3237inline
3238shared_ptr<_Tp>&
3239shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
3240{
3241    shared_ptr(__r).swap(*this);
3242    return *this;
3243}
3244
3245template<class _Tp>
3246template<class _Yp>
3247inline
3248typename enable_if
3249<
3250    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3251    shared_ptr<_Tp>&
3252>::type
3253shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
3254{
3255    shared_ptr(__r).swap(*this);
3256    return *this;
3257}
3258
3259template<class _Tp>
3260inline
3261shared_ptr<_Tp>&
3262shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
3263{
3264    shared_ptr(_VSTD::move(__r)).swap(*this);
3265    return *this;
3266}
3267
3268template<class _Tp>
3269template<class _Yp>
3270inline
3271typename enable_if
3272<
3273    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3274    shared_ptr<_Tp>&
3275>::type
3276shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3277{
3278    shared_ptr(_VSTD::move(__r)).swap(*this);
3279    return *this;
3280}
3281
3282#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3283template<class _Tp>
3284template<class _Yp>
3285inline
3286typename enable_if
3287<
3288    !is_array<_Yp>::value &&
3289    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
3290    shared_ptr<_Tp>
3291>::type&
3292shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3293{
3294    shared_ptr(_VSTD::move(__r)).swap(*this);
3295    return *this;
3296}
3297#endif
3298
3299template<class _Tp>
3300template <class _Yp, class _Dp>
3301inline
3302typename enable_if
3303<
3304    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
3305                   typename shared_ptr<_Tp>::element_type*>::value,
3306    shared_ptr<_Tp>&
3307>::type
3308shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3309{
3310    shared_ptr(_VSTD::move(__r)).swap(*this);
3311    return *this;
3312}
3313
3314template<class _Tp>
3315inline
3316void
3317shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
3318{
3319    _VSTD::swap(__ptr_, __r.__ptr_);
3320    _VSTD::swap(__cntrl_, __r.__cntrl_);
3321}
3322
3323template<class _Tp>
3324inline
3325void
3326shared_ptr<_Tp>::reset() _NOEXCEPT
3327{
3328    shared_ptr().swap(*this);
3329}
3330
3331template<class _Tp>
3332template<class _Yp>
3333inline
3334typename enable_if
3335<
3336    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3337    void
3338>::type
3339shared_ptr<_Tp>::reset(_Yp* __p)
3340{
3341    shared_ptr(__p).swap(*this);
3342}
3343
3344template<class _Tp>
3345template<class _Yp, class _Dp>
3346inline
3347typename enable_if
3348<
3349    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3350    void
3351>::type
3352shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3353{
3354    shared_ptr(__p, __d).swap(*this);
3355}
3356
3357template<class _Tp>
3358template<class _Yp, class _Dp, class _Alloc>
3359inline
3360typename enable_if
3361<
3362    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3363    void
3364>::type
3365shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3366{
3367    shared_ptr(__p, __d, __a).swap(*this);
3368}
3369
3370//
3371// std::allocate_shared and std::make_shared
3372//
3373template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3374_LIBCPP_HIDE_FROM_ABI
3375shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
3376{
3377    using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3378    using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3379    __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3380    ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3381    auto __control_block = __guard.__release_ptr();
3382    return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
3383}
3384
3385template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3386_LIBCPP_HIDE_FROM_ABI
3387shared_ptr<_Tp> make_shared(_Args&& ...__args)
3388{
3389    return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3390}
3391
3392template<class _Tp, class _Up>
3393inline _LIBCPP_INLINE_VISIBILITY
3394bool
3395operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3396{
3397    return __x.get() == __y.get();
3398}
3399
3400template<class _Tp, class _Up>
3401inline _LIBCPP_INLINE_VISIBILITY
3402bool
3403operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3404{
3405    return !(__x == __y);
3406}
3407
3408template<class _Tp, class _Up>
3409inline _LIBCPP_INLINE_VISIBILITY
3410bool
3411operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3412{
3413#if _LIBCPP_STD_VER <= 11
3414    typedef typename common_type<_Tp*, _Up*>::type _Vp;
3415    return less<_Vp>()(__x.get(), __y.get());
3416#else
3417    return less<>()(__x.get(), __y.get());
3418#endif
3419
3420}
3421
3422template<class _Tp, class _Up>
3423inline _LIBCPP_INLINE_VISIBILITY
3424bool
3425operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3426{
3427    return __y < __x;
3428}
3429
3430template<class _Tp, class _Up>
3431inline _LIBCPP_INLINE_VISIBILITY
3432bool
3433operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3434{
3435    return !(__y < __x);
3436}
3437
3438template<class _Tp, class _Up>
3439inline _LIBCPP_INLINE_VISIBILITY
3440bool
3441operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3442{
3443    return !(__x < __y);
3444}
3445
3446template<class _Tp>
3447inline _LIBCPP_INLINE_VISIBILITY
3448bool
3449operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3450{
3451    return !__x;
3452}
3453
3454template<class _Tp>
3455inline _LIBCPP_INLINE_VISIBILITY
3456bool
3457operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3458{
3459    return !__x;
3460}
3461
3462template<class _Tp>
3463inline _LIBCPP_INLINE_VISIBILITY
3464bool
3465operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3466{
3467    return static_cast<bool>(__x);
3468}
3469
3470template<class _Tp>
3471inline _LIBCPP_INLINE_VISIBILITY
3472bool
3473operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3474{
3475    return static_cast<bool>(__x);
3476}
3477
3478template<class _Tp>
3479inline _LIBCPP_INLINE_VISIBILITY
3480bool
3481operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3482{
3483    return less<_Tp*>()(__x.get(), nullptr);
3484}
3485
3486template<class _Tp>
3487inline _LIBCPP_INLINE_VISIBILITY
3488bool
3489operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3490{
3491    return less<_Tp*>()(nullptr, __x.get());
3492}
3493
3494template<class _Tp>
3495inline _LIBCPP_INLINE_VISIBILITY
3496bool
3497operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3498{
3499    return nullptr < __x;
3500}
3501
3502template<class _Tp>
3503inline _LIBCPP_INLINE_VISIBILITY
3504bool
3505operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3506{
3507    return __x < nullptr;
3508}
3509
3510template<class _Tp>
3511inline _LIBCPP_INLINE_VISIBILITY
3512bool
3513operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3514{
3515    return !(nullptr < __x);
3516}
3517
3518template<class _Tp>
3519inline _LIBCPP_INLINE_VISIBILITY
3520bool
3521operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3522{
3523    return !(__x < nullptr);
3524}
3525
3526template<class _Tp>
3527inline _LIBCPP_INLINE_VISIBILITY
3528bool
3529operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3530{
3531    return !(__x < nullptr);
3532}
3533
3534template<class _Tp>
3535inline _LIBCPP_INLINE_VISIBILITY
3536bool
3537operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3538{
3539    return !(nullptr < __x);
3540}
3541
3542template<class _Tp>
3543inline _LIBCPP_INLINE_VISIBILITY
3544void
3545swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
3546{
3547    __x.swap(__y);
3548}
3549
3550template<class _Tp, class _Up>
3551inline _LIBCPP_INLINE_VISIBILITY
3552shared_ptr<_Tp>
3553static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3554{
3555    return shared_ptr<_Tp>(__r,
3556                           static_cast<
3557                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
3558}
3559
3560template<class _Tp, class _Up>
3561inline _LIBCPP_INLINE_VISIBILITY
3562shared_ptr<_Tp>
3563dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3564{
3565    typedef typename shared_ptr<_Tp>::element_type _ET;
3566    _ET* __p = dynamic_cast<_ET*>(__r.get());
3567    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3568}
3569
3570template<class _Tp, class _Up>
3571shared_ptr<_Tp>
3572const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3573{
3574    typedef typename shared_ptr<_Tp>::element_type _RTp;
3575    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
3576}
3577
3578template<class _Tp, class _Up>
3579shared_ptr<_Tp>
3580reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3581{
3582    return shared_ptr<_Tp>(__r,
3583                           reinterpret_cast<
3584                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
3585}
3586
3587#ifndef _LIBCPP_NO_RTTI
3588
3589template<class _Dp, class _Tp>
3590inline _LIBCPP_INLINE_VISIBILITY
3591_Dp*
3592get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
3593{
3594    return __p.template __get_deleter<_Dp>();
3595}
3596
3597#endif  // _LIBCPP_NO_RTTI
3598
3599template<class _Tp>
3600class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
3601{
3602public:
3603    typedef _Tp element_type;
3604private:
3605    element_type*        __ptr_;
3606    __shared_weak_count* __cntrl_;
3607
3608public:
3609    _LIBCPP_INLINE_VISIBILITY
3610    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
3611    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
3612                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3613                        _NOEXCEPT;
3614    _LIBCPP_INLINE_VISIBILITY
3615    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
3616    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
3617                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3618                         _NOEXCEPT;
3619
3620    _LIBCPP_INLINE_VISIBILITY
3621    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
3622    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
3623                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3624                         _NOEXCEPT;
3625    ~weak_ptr();
3626
3627    _LIBCPP_INLINE_VISIBILITY
3628    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3629    template<class _Yp>
3630        typename enable_if
3631        <
3632            is_convertible<_Yp*, element_type*>::value,
3633            weak_ptr&
3634        >::type
3635        _LIBCPP_INLINE_VISIBILITY
3636        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3637
3638    _LIBCPP_INLINE_VISIBILITY
3639    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3640    template<class _Yp>
3641        typename enable_if
3642        <
3643            is_convertible<_Yp*, element_type*>::value,
3644            weak_ptr&
3645        >::type
3646        _LIBCPP_INLINE_VISIBILITY
3647        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3648
3649    template<class _Yp>
3650        typename enable_if
3651        <
3652            is_convertible<_Yp*, element_type*>::value,
3653            weak_ptr&
3654        >::type
3655        _LIBCPP_INLINE_VISIBILITY
3656        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
3657
3658    _LIBCPP_INLINE_VISIBILITY
3659    void swap(weak_ptr& __r) _NOEXCEPT;
3660    _LIBCPP_INLINE_VISIBILITY
3661    void reset() _NOEXCEPT;
3662
3663    _LIBCPP_INLINE_VISIBILITY
3664    long use_count() const _NOEXCEPT
3665        {return __cntrl_ ? __cntrl_->use_count() : 0;}
3666    _LIBCPP_INLINE_VISIBILITY
3667    bool expired() const _NOEXCEPT
3668        {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
3669    shared_ptr<_Tp> lock() const _NOEXCEPT;
3670    template<class _Up>
3671        _LIBCPP_INLINE_VISIBILITY
3672        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
3673        {return __cntrl_ < __r.__cntrl_;}
3674    template<class _Up>
3675        _LIBCPP_INLINE_VISIBILITY
3676        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
3677        {return __cntrl_ < __r.__cntrl_;}
3678
3679    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3680    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3681};
3682
3683#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3684template<class _Tp>
3685weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3686#endif
3687
3688template<class _Tp>
3689inline
3690_LIBCPP_CONSTEXPR
3691weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
3692    : __ptr_(nullptr),
3693      __cntrl_(nullptr)
3694{
3695}
3696
3697template<class _Tp>
3698inline
3699weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
3700    : __ptr_(__r.__ptr_),
3701      __cntrl_(__r.__cntrl_)
3702{
3703    if (__cntrl_)
3704        __cntrl_->__add_weak();
3705}
3706
3707template<class _Tp>
3708template<class _Yp>
3709inline
3710weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3711                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3712                         _NOEXCEPT
3713    : __ptr_(__r.__ptr_),
3714      __cntrl_(__r.__cntrl_)
3715{
3716    if (__cntrl_)
3717        __cntrl_->__add_weak();
3718}
3719
3720template<class _Tp>
3721template<class _Yp>
3722inline
3723weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3724                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3725         _NOEXCEPT
3726    : __ptr_(__r.__ptr_),
3727      __cntrl_(__r.__cntrl_)
3728{
3729    if (__cntrl_)
3730        __cntrl_->__add_weak();
3731}
3732
3733template<class _Tp>
3734inline
3735weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3736    : __ptr_(__r.__ptr_),
3737      __cntrl_(__r.__cntrl_)
3738{
3739    __r.__ptr_ = nullptr;
3740    __r.__cntrl_ = nullptr;
3741}
3742
3743template<class _Tp>
3744template<class _Yp>
3745inline
3746weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3747                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3748         _NOEXCEPT
3749    : __ptr_(__r.__ptr_),
3750      __cntrl_(__r.__cntrl_)
3751{
3752    __r.__ptr_ = nullptr;
3753    __r.__cntrl_ = nullptr;
3754}
3755
3756template<class _Tp>
3757weak_ptr<_Tp>::~weak_ptr()
3758{
3759    if (__cntrl_)
3760        __cntrl_->__release_weak();
3761}
3762
3763template<class _Tp>
3764inline
3765weak_ptr<_Tp>&
3766weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
3767{
3768    weak_ptr(__r).swap(*this);
3769    return *this;
3770}
3771
3772template<class _Tp>
3773template<class _Yp>
3774inline
3775typename enable_if
3776<
3777    is_convertible<_Yp*, _Tp*>::value,
3778    weak_ptr<_Tp>&
3779>::type
3780weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
3781{
3782    weak_ptr(__r).swap(*this);
3783    return *this;
3784}
3785
3786template<class _Tp>
3787inline
3788weak_ptr<_Tp>&
3789weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3790{
3791    weak_ptr(_VSTD::move(__r)).swap(*this);
3792    return *this;
3793}
3794
3795template<class _Tp>
3796template<class _Yp>
3797inline
3798typename enable_if
3799<
3800    is_convertible<_Yp*, _Tp*>::value,
3801    weak_ptr<_Tp>&
3802>::type
3803weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3804{
3805    weak_ptr(_VSTD::move(__r)).swap(*this);
3806    return *this;
3807}
3808
3809template<class _Tp>
3810template<class _Yp>
3811inline
3812typename enable_if
3813<
3814    is_convertible<_Yp*, _Tp*>::value,
3815    weak_ptr<_Tp>&
3816>::type
3817weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
3818{
3819    weak_ptr(__r).swap(*this);
3820    return *this;
3821}
3822
3823template<class _Tp>
3824inline
3825void
3826weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
3827{
3828    _VSTD::swap(__ptr_, __r.__ptr_);
3829    _VSTD::swap(__cntrl_, __r.__cntrl_);
3830}
3831
3832template<class _Tp>
3833inline _LIBCPP_INLINE_VISIBILITY
3834void
3835swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
3836{
3837    __x.swap(__y);
3838}
3839
3840template<class _Tp>
3841inline
3842void
3843weak_ptr<_Tp>::reset() _NOEXCEPT
3844{
3845    weak_ptr().swap(*this);
3846}
3847
3848template<class _Tp>
3849template<class _Yp>
3850shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3851                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3852    : __ptr_(__r.__ptr_),
3853      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3854{
3855    if (__cntrl_ == nullptr)
3856        __throw_bad_weak_ptr();
3857}
3858
3859template<class _Tp>
3860shared_ptr<_Tp>
3861weak_ptr<_Tp>::lock() const _NOEXCEPT
3862{
3863    shared_ptr<_Tp> __r;
3864    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3865    if (__r.__cntrl_)
3866        __r.__ptr_ = __ptr_;
3867    return __r;
3868}
3869
3870#if _LIBCPP_STD_VER > 14
3871template <class _Tp = void> struct owner_less;
3872#else
3873template <class _Tp> struct owner_less;
3874#endif
3875
3876template <class _Tp>
3877struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
3878    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
3879{
3880    typedef bool result_type;
3881    _LIBCPP_INLINE_VISIBILITY
3882    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
3883        {return __x.owner_before(__y);}
3884    _LIBCPP_INLINE_VISIBILITY
3885    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
3886        {return __x.owner_before(__y);}
3887    _LIBCPP_INLINE_VISIBILITY
3888    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
3889        {return __x.owner_before(__y);}
3890};
3891
3892template <class _Tp>
3893struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
3894    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3895{
3896    typedef bool result_type;
3897    _LIBCPP_INLINE_VISIBILITY
3898    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
3899        {return __x.owner_before(__y);}
3900    _LIBCPP_INLINE_VISIBILITY
3901    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
3902        {return __x.owner_before(__y);}
3903    _LIBCPP_INLINE_VISIBILITY
3904    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
3905        {return __x.owner_before(__y);}
3906};
3907
3908#if _LIBCPP_STD_VER > 14
3909template <>
3910struct _LIBCPP_TEMPLATE_VIS owner_less<void>
3911{
3912    template <class _Tp, class _Up>
3913    _LIBCPP_INLINE_VISIBILITY
3914    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
3915        {return __x.owner_before(__y);}
3916    template <class _Tp, class _Up>
3917    _LIBCPP_INLINE_VISIBILITY
3918    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
3919        {return __x.owner_before(__y);}
3920    template <class _Tp, class _Up>
3921    _LIBCPP_INLINE_VISIBILITY
3922    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
3923        {return __x.owner_before(__y);}
3924    template <class _Tp, class _Up>
3925    _LIBCPP_INLINE_VISIBILITY
3926    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
3927        {return __x.owner_before(__y);}
3928    typedef void is_transparent;
3929};
3930#endif
3931
3932template<class _Tp>
3933class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
3934{
3935    mutable weak_ptr<_Tp> __weak_this_;
3936protected:
3937    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
3938    enable_shared_from_this() _NOEXCEPT {}
3939    _LIBCPP_INLINE_VISIBILITY
3940    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
3941    _LIBCPP_INLINE_VISIBILITY
3942    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3943        {return *this;}
3944    _LIBCPP_INLINE_VISIBILITY
3945    ~enable_shared_from_this() {}
3946public:
3947    _LIBCPP_INLINE_VISIBILITY
3948    shared_ptr<_Tp> shared_from_this()
3949        {return shared_ptr<_Tp>(__weak_this_);}
3950    _LIBCPP_INLINE_VISIBILITY
3951    shared_ptr<_Tp const> shared_from_this() const
3952        {return shared_ptr<const _Tp>(__weak_this_);}
3953
3954#if _LIBCPP_STD_VER > 14
3955    _LIBCPP_INLINE_VISIBILITY
3956    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3957       { return __weak_this_; }
3958
3959    _LIBCPP_INLINE_VISIBILITY
3960    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3961        { return __weak_this_; }
3962#endif // _LIBCPP_STD_VER > 14
3963
3964    template <class _Up> friend class shared_ptr;
3965};
3966
3967template <class _Tp>
3968struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
3969{
3970    typedef shared_ptr<_Tp>      argument_type;
3971    typedef size_t               result_type;
3972
3973    _LIBCPP_INLINE_VISIBILITY
3974    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
3975    {
3976        return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
3977    }
3978};
3979
3980template<class _CharT, class _Traits, class _Yp>
3981inline _LIBCPP_INLINE_VISIBILITY
3982basic_ostream<_CharT, _Traits>&
3983operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
3984
3985
3986#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
3987
3988class _LIBCPP_TYPE_VIS __sp_mut
3989{
3990    void* __lx;
3991public:
3992    void lock() _NOEXCEPT;
3993    void unlock() _NOEXCEPT;
3994
3995private:
3996    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3997    __sp_mut(const __sp_mut&);
3998    __sp_mut& operator=(const __sp_mut&);
3999
4000    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
4001};
4002
4003_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4004__sp_mut& __get_sp_mut(const void*);
4005
4006template <class _Tp>
4007inline _LIBCPP_INLINE_VISIBILITY
4008bool
4009atomic_is_lock_free(const shared_ptr<_Tp>*)
4010{
4011    return false;
4012}
4013
4014template <class _Tp>
4015_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4016shared_ptr<_Tp>
4017atomic_load(const shared_ptr<_Tp>* __p)
4018{
4019    __sp_mut& __m = __get_sp_mut(__p);
4020    __m.lock();
4021    shared_ptr<_Tp> __q = *__p;
4022    __m.unlock();
4023    return __q;
4024}
4025
4026template <class _Tp>
4027inline _LIBCPP_INLINE_VISIBILITY
4028_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4029shared_ptr<_Tp>
4030atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4031{
4032    return atomic_load(__p);
4033}
4034
4035template <class _Tp>
4036_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4037void
4038atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4039{
4040    __sp_mut& __m = __get_sp_mut(__p);
4041    __m.lock();
4042    __p->swap(__r);
4043    __m.unlock();
4044}
4045
4046template <class _Tp>
4047inline _LIBCPP_INLINE_VISIBILITY
4048_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4049void
4050atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4051{
4052    atomic_store(__p, __r);
4053}
4054
4055template <class _Tp>
4056_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4057shared_ptr<_Tp>
4058atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4059{
4060    __sp_mut& __m = __get_sp_mut(__p);
4061    __m.lock();
4062    __p->swap(__r);
4063    __m.unlock();
4064    return __r;
4065}
4066
4067template <class _Tp>
4068inline _LIBCPP_INLINE_VISIBILITY
4069_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4070shared_ptr<_Tp>
4071atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4072{
4073    return atomic_exchange(__p, __r);
4074}
4075
4076template <class _Tp>
4077_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4078bool
4079atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4080{
4081    shared_ptr<_Tp> __temp;
4082    __sp_mut& __m = __get_sp_mut(__p);
4083    __m.lock();
4084    if (__p->__owner_equivalent(*__v))
4085    {
4086        _VSTD::swap(__temp, *__p);
4087        *__p = __w;
4088        __m.unlock();
4089        return true;
4090    }
4091    _VSTD::swap(__temp, *__v);
4092    *__v = *__p;
4093    __m.unlock();
4094    return false;
4095}
4096
4097template <class _Tp>
4098inline _LIBCPP_INLINE_VISIBILITY
4099_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4100bool
4101atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4102{
4103    return atomic_compare_exchange_strong(__p, __v, __w);
4104}
4105
4106template <class _Tp>
4107inline _LIBCPP_INLINE_VISIBILITY
4108_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4109bool
4110atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4111                                        shared_ptr<_Tp> __w, memory_order, memory_order)
4112{
4113    return atomic_compare_exchange_strong(__p, __v, __w);
4114}
4115
4116template <class _Tp>
4117inline _LIBCPP_INLINE_VISIBILITY
4118_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4119bool
4120atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4121                                      shared_ptr<_Tp> __w, memory_order, memory_order)
4122{
4123    return atomic_compare_exchange_weak(__p, __v, __w);
4124}
4125
4126#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
4127
4128//enum class
4129#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4130# ifndef _LIBCPP_CXX03_LANG
4131enum class pointer_safety : unsigned char {
4132  relaxed,
4133  preferred,
4134  strict
4135};
4136# endif
4137#else
4138struct _LIBCPP_TYPE_VIS pointer_safety
4139{
4140    enum __lx
4141    {
4142        relaxed,
4143        preferred,
4144        strict
4145    };
4146
4147    __lx __v_;
4148
4149    _LIBCPP_INLINE_VISIBILITY
4150    pointer_safety() : __v_() {}
4151
4152    _LIBCPP_INLINE_VISIBILITY
4153    pointer_safety(__lx __v) : __v_(__v) {}
4154    _LIBCPP_INLINE_VISIBILITY
4155    operator int() const {return __v_;}
4156};
4157#endif
4158
4159#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
4160    defined(_LIBCPP_BUILDING_LIBRARY)
4161_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4162#else
4163// This function is only offered in C++03 under ABI v1.
4164# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4165inline _LIBCPP_INLINE_VISIBILITY
4166pointer_safety get_pointer_safety() _NOEXCEPT {
4167  return pointer_safety::relaxed;
4168}
4169# endif
4170#endif
4171
4172
4173_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4174_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4175_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
4176_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
4177
4178template <class _Tp>
4179inline _LIBCPP_INLINE_VISIBILITY
4180_Tp*
4181undeclare_reachable(_Tp* __p)
4182{
4183    return static_cast<_Tp*>(__undeclare_reachable(__p));
4184}
4185
4186_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
4187
4188// --- Helper for container swap --
4189template <typename _Alloc>
4190_LIBCPP_INLINE_VISIBILITY
4191void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4192#if _LIBCPP_STD_VER >= 14
4193    _NOEXCEPT
4194#else
4195    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4196#endif
4197{
4198    using _VSTD::swap;
4199    swap(__a1, __a2);
4200}
4201
4202template <typename _Alloc>
4203inline _LIBCPP_INLINE_VISIBILITY
4204void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4205
4206template <typename _Alloc>
4207inline _LIBCPP_INLINE_VISIBILITY
4208void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4209#if _LIBCPP_STD_VER >= 14
4210    _NOEXCEPT
4211#else
4212    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4213#endif
4214{
4215    _VSTD::__swap_allocator(__a1, __a2,
4216      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4217}
4218
4219template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
4220struct __noexcept_move_assign_container : public integral_constant<bool,
4221    _Traits::propagate_on_container_move_assignment::value
4222#if _LIBCPP_STD_VER > 14
4223        || _Traits::is_always_equal::value
4224#else
4225        && is_nothrow_move_assignable<_Alloc>::value
4226#endif
4227    > {};
4228
4229
4230template <class _Tp, class _Alloc>
4231struct __temp_value {
4232    typedef allocator_traits<_Alloc> _Traits;
4233
4234    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
4235    _Alloc &__a;
4236
4237    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4238    _Tp &   get() { return *__addr(); }
4239
4240    template<class... _Args>
4241    _LIBCPP_NO_CFI
4242    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4243      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4244                         _VSTD::forward<_Args>(__args)...);
4245    }
4246
4247    ~__temp_value() { _Traits::destroy(__a, __addr()); }
4248    };
4249
4250template<typename _Alloc, typename = void, typename = void>
4251struct __is_allocator : false_type {};
4252
4253template<typename _Alloc>
4254struct __is_allocator<_Alloc,
4255       typename __void_t<typename _Alloc::value_type>::type,
4256       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4257     >
4258   : true_type {};
4259
4260// __builtin_new_allocator -- A non-templated helper for allocating and
4261// deallocating memory using __builtin_operator_new and
4262// __builtin_operator_delete. It should be used in preference to
4263// `std::allocator<T>` to avoid additional instantiations.
4264struct __builtin_new_allocator {
4265  struct __builtin_new_deleter {
4266    typedef void* pointer_type;
4267
4268    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4269        : __size_(__size), __align_(__align) {}
4270
4271    void operator()(void* p) const _NOEXCEPT {
4272        _VSTD::__libcpp_deallocate(p, __size_, __align_);
4273    }
4274
4275   private:
4276    size_t __size_;
4277    size_t __align_;
4278  };
4279
4280  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4281
4282  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
4283      return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
4284                     __builtin_new_deleter(__s, __align));
4285  }
4286
4287  static void __deallocate_bytes(void* __p, size_t __s,
4288                                 size_t __align) _NOEXCEPT {
4289      _VSTD::__libcpp_deallocate(__p, __s, __align);
4290  }
4291
4292  template <class _Tp>
4293  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4294  static __holder_t __allocate_type(size_t __n) {
4295      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4296  }
4297
4298  template <class _Tp>
4299  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4300  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4301      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4302  }
4303};
4304
4305
4306_LIBCPP_END_NAMESPACE_STD
4307
4308_LIBCPP_POP_MACROS
4309
4310#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
4311#   include <__pstl_memory>
4312#endif
4313
4314#endif  // _LIBCPP_MEMORY
4315