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