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#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
683#  include <atomic>
684#endif
685#include <version>
686
687#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
688#pragma GCC system_header
689#endif
690
691_LIBCPP_PUSH_MACROS
692#include <__undef_macros>
693
694
695_LIBCPP_BEGIN_NAMESPACE_STD
696
697template <class _ValueType>
698inline _LIBCPP_INLINE_VISIBILITY
699_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
700#if !defined(_LIBCPP_HAS_NO_THREADS) && \
701    defined(__ATOMIC_RELAXED) &&        \
702    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
703    return __atomic_load_n(__value, __ATOMIC_RELAXED);
704#else
705    return *__value;
706#endif
707}
708
709template <class _ValueType>
710inline _LIBCPP_INLINE_VISIBILITY
711_ValueType __libcpp_acquire_load(_ValueType const* __value) {
712#if !defined(_LIBCPP_HAS_NO_THREADS) && \
713    defined(__ATOMIC_ACQUIRE) &&        \
714    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
715    return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
716#else
717    return *__value;
718#endif
719}
720
721// addressof moved to <type_traits>
722
723template <class _Tp> class allocator;
724
725#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
726template <>
727class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
728{
729public:
730    typedef void*             pointer;
731    typedef const void*       const_pointer;
732    typedef void              value_type;
733
734    template <class _Up> struct rebind {typedef allocator<_Up> other;};
735};
736
737template <>
738class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
739{
740public:
741    typedef const void*       pointer;
742    typedef const void*       const_pointer;
743    typedef const void        value_type;
744
745    template <class _Up> struct rebind {typedef allocator<_Up> other;};
746};
747#endif
748
749// pointer_traits
750
751template <class _Tp, class = void>
752struct __has_element_type : false_type {};
753
754template <class _Tp>
755struct __has_element_type<_Tp,
756              typename __void_t<typename _Tp::element_type>::type> : true_type {};
757
758template <class _Ptr, bool = __has_element_type<_Ptr>::value>
759struct __pointer_traits_element_type;
760
761template <class _Ptr>
762struct __pointer_traits_element_type<_Ptr, true>
763{
764    typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
765};
766
767template <template <class, class...> class _Sp, class _Tp, class ..._Args>
768struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
769{
770    typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
771};
772
773template <template <class, class...> class _Sp, class _Tp, class ..._Args>
774struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
775{
776    typedef _LIBCPP_NODEBUG_TYPE _Tp type;
777};
778
779template <class _Tp, class = void>
780struct __has_difference_type : false_type {};
781
782template <class _Tp>
783struct __has_difference_type<_Tp,
784            typename __void_t<typename _Tp::difference_type>::type> : true_type {};
785
786template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
787struct __pointer_traits_difference_type
788{
789    typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
790};
791
792template <class _Ptr>
793struct __pointer_traits_difference_type<_Ptr, true>
794{
795    typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
796};
797
798template <class _Tp, class _Up>
799struct __has_rebind
800{
801private:
802    struct __two {char __lx; char __lxx;};
803    template <class _Xp> static __two __test(...);
804    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
805    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
806    _LIBCPP_SUPPRESS_DEPRECATED_POP
807public:
808    static const bool value = sizeof(__test<_Tp>(0)) == 1;
809};
810
811template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
812struct __pointer_traits_rebind
813{
814#ifndef _LIBCPP_CXX03_LANG
815    typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
816#else
817    typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
818#endif
819};
820
821template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
823{
824#ifndef _LIBCPP_CXX03_LANG
825    typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
826#else
827    typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
828#endif
829};
830
831template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
832struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
833{
834    typedef _Sp<_Up, _Args...> type;
835};
836
837template <class _Ptr>
838struct _LIBCPP_TEMPLATE_VIS pointer_traits
839{
840    typedef _Ptr                                                     pointer;
841    typedef typename __pointer_traits_element_type<pointer>::type    element_type;
842    typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
843
844#ifndef _LIBCPP_CXX03_LANG
845    template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
846#else
847    template <class _Up> struct rebind
848        {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
849#endif  // _LIBCPP_CXX03_LANG
850
851private:
852    struct __nat {};
853public:
854    _LIBCPP_INLINE_VISIBILITY
855    static pointer pointer_to(typename conditional<is_void<element_type>::value,
856                                           __nat, element_type>::type& __r)
857        {return pointer::pointer_to(__r);}
858};
859
860template <class _Tp>
861struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
862{
863    typedef _Tp*      pointer;
864    typedef _Tp       element_type;
865    typedef ptrdiff_t difference_type;
866
867#ifndef _LIBCPP_CXX03_LANG
868    template <class _Up> using rebind = _Up*;
869#else
870    template <class _Up> struct rebind {typedef _Up* other;};
871#endif
872
873private:
874    struct __nat {};
875public:
876    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
877    static pointer pointer_to(typename conditional<is_void<element_type>::value,
878                                      __nat, element_type>::type& __r) _NOEXCEPT
879        {return _VSTD::addressof(__r);}
880};
881
882template <class _From, class _To>
883struct __rebind_pointer {
884#ifndef _LIBCPP_CXX03_LANG
885    typedef typename pointer_traits<_From>::template rebind<_To>        type;
886#else
887    typedef typename pointer_traits<_From>::template rebind<_To>::other type;
888#endif
889};
890
891// construct_at
892
893#if _LIBCPP_STD_VER > 17
894
895template<class _Tp>
896_LIBCPP_CONSTEXPR_AFTER_CXX17 void* __voidify(_Tp& __ptr) noexcept {
897    return const_cast<void*>(static_cast<const volatile void*>(_VSTD::addressof(__ptr)));
898}
899
900template<class _Tp, class ..._Args, class = decltype(
901    ::new (_VSTD::declval<void*>()) _Tp(_VSTD::declval<_Args>()...)
902)>
903_LIBCPP_INLINE_VISIBILITY
904constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
905    _LIBCPP_ASSERT(__location, "null pointer given to construct_at");
906    return ::new (_VSTD::__voidify(*__location)) _Tp(_VSTD::forward<_Args>(__args)...);
907}
908
909#endif
910
911// destroy_at
912
913#if _LIBCPP_STD_VER > 14
914
915template <class _Tp>
916inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
917void destroy_at(_Tp* __loc) {
918    _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
919    __loc->~_Tp();
920}
921
922#endif
923
924// allocator_traits
925
926template <class _Tp, class = void>
927struct __has_pointer_type : false_type {};
928
929template <class _Tp>
930struct __has_pointer_type<_Tp,
931          typename __void_t<typename _Tp::pointer>::type> : true_type {};
932
933namespace __pointer_type_imp
934{
935
936template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
937struct __pointer_type
938{
939    typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
940};
941
942template <class _Tp, class _Dp>
943struct __pointer_type<_Tp, _Dp, false>
944{
945    typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
946};
947
948}  // __pointer_type_imp
949
950template <class _Tp, class _Dp>
951struct __pointer_type
952{
953    typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
954};
955
956template <class _Tp, class = void>
957struct __has_const_pointer : false_type {};
958
959template <class _Tp>
960struct __has_const_pointer<_Tp,
961            typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
962
963template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
964struct __const_pointer
965{
966    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
967};
968
969template <class _Tp, class _Ptr, class _Alloc>
970struct __const_pointer<_Tp, _Ptr, _Alloc, false>
971{
972#ifndef _LIBCPP_CXX03_LANG
973    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
974#else
975    typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
976#endif
977};
978
979template <class _Tp, class = void>
980struct __has_void_pointer : false_type {};
981
982template <class _Tp>
983struct __has_void_pointer<_Tp,
984               typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
985
986template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
987struct __void_pointer
988{
989    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
990};
991
992template <class _Ptr, class _Alloc>
993struct __void_pointer<_Ptr, _Alloc, false>
994{
995#ifndef _LIBCPP_CXX03_LANG
996    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
997#else
998    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
999#endif
1000};
1001
1002template <class _Tp, class = void>
1003struct __has_const_void_pointer : false_type {};
1004
1005template <class _Tp>
1006struct __has_const_void_pointer<_Tp,
1007            typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
1008
1009template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1010struct __const_void_pointer
1011{
1012    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
1013};
1014
1015template <class _Ptr, class _Alloc>
1016struct __const_void_pointer<_Ptr, _Alloc, false>
1017{
1018#ifndef _LIBCPP_CXX03_LANG
1019    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
1020#else
1021    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1022#endif
1023};
1024
1025
1026template <bool _UsePointerTraits> struct __to_address_helper;
1027
1028template <> struct __to_address_helper<true> {
1029    template <class _Pointer>
1030    using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
1031
1032    template <class _Pointer>
1033    _LIBCPP_CONSTEXPR
1034    static __return_type<_Pointer>
1035    __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1036};
1037
1038template <class _Pointer, bool _Dummy = true>
1039using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1040
1041
1042template <class _Tp>
1043inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1044_Tp*
1045__to_address(_Tp* __p) _NOEXCEPT
1046{
1047    static_assert(!is_function<_Tp>::value, "_Tp is a function type");
1048    return __p;
1049}
1050
1051template <class _Pointer>
1052inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1053typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1054__to_address(const _Pointer& __p) _NOEXCEPT {
1055  return __choose_to_address<_Pointer>::__do_it(__p);
1056}
1057
1058template <> struct __to_address_helper<false> {
1059    template <class _Pointer>
1060    using __return_type = typename pointer_traits<_Pointer>::element_type*;
1061
1062    template <class _Pointer>
1063    _LIBCPP_CONSTEXPR
1064    static __return_type<_Pointer>
1065    __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
1066};
1067
1068
1069#if _LIBCPP_STD_VER > 17
1070template <class _Tp>
1071inline _LIBCPP_INLINE_VISIBILITY constexpr
1072_Tp*
1073to_address(_Tp* __p) _NOEXCEPT
1074{
1075    static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1076    return __p;
1077}
1078
1079template <class _Pointer>
1080inline _LIBCPP_INLINE_VISIBILITY constexpr
1081auto
1082to_address(const _Pointer& __p) _NOEXCEPT
1083{
1084    return _VSTD::__to_address(__p);
1085}
1086#endif
1087
1088template <class _Tp, class = void>
1089struct __has_size_type : false_type {};
1090
1091template <class _Tp>
1092struct __has_size_type<_Tp,
1093               typename __void_t<typename _Tp::size_type>::type> : true_type {};
1094
1095template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
1096struct __size_type
1097{
1098    typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
1099};
1100
1101template <class _Alloc, class _DiffType>
1102struct __size_type<_Alloc, _DiffType, true>
1103{
1104    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
1105};
1106
1107template <class _Tp, class = void>
1108struct __has_propagate_on_container_copy_assignment : false_type {};
1109
1110template <class _Tp>
1111struct __has_propagate_on_container_copy_assignment<_Tp,
1112    typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1113        : true_type {};
1114
1115template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1116struct __propagate_on_container_copy_assignment
1117{
1118    typedef _LIBCPP_NODEBUG_TYPE false_type type;
1119};
1120
1121template <class _Alloc>
1122struct __propagate_on_container_copy_assignment<_Alloc, true>
1123{
1124    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
1125};
1126
1127template <class _Tp, class = void>
1128struct __has_propagate_on_container_move_assignment : false_type {};
1129
1130template <class _Tp>
1131struct __has_propagate_on_container_move_assignment<_Tp,
1132           typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1133               : true_type {};
1134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138    typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
1144    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
1145};
1146
1147template <class _Tp, class = void>
1148struct __has_propagate_on_container_swap : false_type {};
1149
1150template <class _Tp>
1151struct __has_propagate_on_container_swap<_Tp,
1152           typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1153               : true_type {};
1154
1155template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1156struct __propagate_on_container_swap
1157{
1158    typedef false_type type;
1159};
1160
1161template <class _Alloc>
1162struct __propagate_on_container_swap<_Alloc, true>
1163{
1164    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
1165};
1166
1167template <class _Tp, class = void>
1168struct __has_is_always_equal : false_type {};
1169
1170template <class _Tp>
1171struct __has_is_always_equal<_Tp,
1172           typename __void_t<typename _Tp::is_always_equal>::type>
1173               : true_type {};
1174
1175template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1176struct __is_always_equal
1177{
1178    typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
1179};
1180
1181template <class _Alloc>
1182struct __is_always_equal<_Alloc, true>
1183{
1184    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
1185};
1186
1187template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1188struct __has_rebind_other
1189{
1190private:
1191    struct __two {char __lx; char __lxx;};
1192    template <class _Xp> static __two __test(...);
1193    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1194    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1195    _LIBCPP_SUPPRESS_DEPRECATED_POP
1196public:
1197    static const bool value = sizeof(__test<_Tp>(0)) == 1;
1198};
1199
1200template <class _Tp, class _Up>
1201struct __has_rebind_other<_Tp, _Up, false>
1202{
1203    static const bool value = false;
1204};
1205
1206template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1207struct __allocator_traits_rebind
1208{
1209    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1210    typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
1211    _LIBCPP_SUPPRESS_DEPRECATED_POP
1212};
1213
1214template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1215struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1216{
1217    _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1218    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1219    _LIBCPP_SUPPRESS_DEPRECATED_POP
1220};
1221
1222template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1223struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1224{
1225    typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
1226};
1227
1228#ifndef _LIBCPP_CXX03_LANG
1229
1230_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1231template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1232auto
1233__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1234    -> decltype((void)__a.allocate(__sz, __p), true_type());
1235_LIBCPP_SUPPRESS_DEPRECATED_POP
1236
1237template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1238auto
1239__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1240    -> false_type;
1241
1242template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1243struct __has_allocate_hint
1244    : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1245                                               declval<_SizeType>(),
1246                                               declval<_ConstVoidPtr>()))
1247{
1248};
1249
1250#else  // _LIBCPP_CXX03_LANG
1251
1252template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1253struct __has_allocate_hint
1254    : true_type
1255{
1256};
1257
1258#endif  // _LIBCPP_CXX03_LANG
1259
1260_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1261template <class _Alloc, class ..._Args,
1262    class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1263static true_type __test_has_construct(int);
1264_LIBCPP_SUPPRESS_DEPRECATED_POP
1265
1266template <class _Alloc, class...>
1267static false_type __test_has_construct(...);
1268
1269template <class _Alloc, class ..._Args>
1270struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1271
1272#if !defined(_LIBCPP_CXX03_LANG)
1273
1274_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1275template <class _Alloc, class _Pointer>
1276auto
1277__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1278    -> decltype(__a.destroy(__p), true_type());
1279_LIBCPP_SUPPRESS_DEPRECATED_POP
1280
1281template <class _Alloc, class _Pointer>
1282auto
1283__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1284    -> false_type;
1285
1286template <class _Alloc, class _Pointer>
1287struct __has_destroy
1288    : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1289                                         declval<_Pointer>()))
1290{
1291};
1292
1293_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1294template <class _Alloc>
1295auto
1296__has_max_size_test(_Alloc&& __a)
1297    -> decltype(__a.max_size(), true_type());
1298_LIBCPP_SUPPRESS_DEPRECATED_POP
1299
1300template <class _Alloc>
1301auto
1302__has_max_size_test(const volatile _Alloc& __a)
1303    -> false_type;
1304
1305template <class _Alloc>
1306struct __has_max_size
1307    : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
1308{
1309};
1310
1311template <class _Alloc>
1312auto
1313__has_select_on_container_copy_construction_test(_Alloc&& __a)
1314    -> decltype(__a.select_on_container_copy_construction(), true_type());
1315
1316template <class _Alloc>
1317auto
1318__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1319    -> false_type;
1320
1321template <class _Alloc>
1322struct __has_select_on_container_copy_construction
1323    : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
1324{
1325};
1326
1327#else  // _LIBCPP_CXX03_LANG
1328
1329template <class _Alloc, class _Pointer, class = void>
1330struct __has_destroy : false_type {};
1331
1332template <class _Alloc, class _Pointer>
1333struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1334    decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1335>::type> : true_type {};
1336
1337template <class _Alloc>
1338struct __has_max_size
1339    : true_type
1340{
1341};
1342
1343template <class _Alloc>
1344struct __has_select_on_container_copy_construction
1345    : false_type
1346{
1347};
1348
1349#endif  // _LIBCPP_CXX03_LANG
1350
1351template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1352struct __alloc_traits_difference_type
1353{
1354    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
1355};
1356
1357template <class _Alloc, class _Ptr>
1358struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1359{
1360    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
1361};
1362
1363template <class _Tp>
1364struct __is_default_allocator : false_type {};
1365
1366template <class _Tp>
1367struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1368
1369
1370
1371template <class _Alloc,
1372    bool = __has_construct<_Alloc, typename _Alloc::value_type*,  typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1373    >
1374struct __is_cpp17_move_insertable;
1375template <class _Alloc>
1376struct __is_cpp17_move_insertable<_Alloc, true> : true_type {};
1377template <class _Alloc>
1378struct __is_cpp17_move_insertable<_Alloc, false> : is_move_constructible<typename _Alloc::value_type> {};
1379
1380template <class _Alloc,
1381    bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1382    >
1383struct __is_cpp17_copy_insertable;
1384template <class _Alloc>
1385struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1386template <class _Alloc>
1387struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1388    is_copy_constructible<typename _Alloc::value_type>::value &&
1389    __is_cpp17_move_insertable<_Alloc>::value>
1390  {};
1391
1392
1393
1394template <class _Alloc>
1395struct _LIBCPP_TEMPLATE_VIS allocator_traits
1396{
1397    typedef _Alloc                              allocator_type;
1398    typedef typename allocator_type::value_type value_type;
1399
1400    typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1401    typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1402    typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1403    typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1404
1405    typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1406    typedef typename __size_type<allocator_type, difference_type>::type size_type;
1407
1408    typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1409                     propagate_on_container_copy_assignment;
1410    typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1411                     propagate_on_container_move_assignment;
1412    typedef typename __propagate_on_container_swap<allocator_type>::type
1413                     propagate_on_container_swap;
1414    typedef typename __is_always_equal<allocator_type>::type
1415                     is_always_equal;
1416
1417#ifndef _LIBCPP_CXX03_LANG
1418    template <class _Tp> using rebind_alloc =
1419                  typename __allocator_traits_rebind<allocator_type, _Tp>::type;
1420    template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
1421#else  // _LIBCPP_CXX03_LANG
1422    template <class _Tp> struct rebind_alloc
1423        {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1424    template <class _Tp> struct rebind_traits
1425        {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
1426#endif  // _LIBCPP_CXX03_LANG
1427
1428    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1429    static pointer allocate(allocator_type& __a, size_type __n)
1430        {return __a.allocate(__n);}
1431    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1432    static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1433        {return __allocate(__a, __n, __hint,
1434            __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1435
1436    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1437    static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
1438        {__a.deallocate(__p, __n);}
1439
1440    template <class _Tp, class... _Args>
1441        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1442        static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1443            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
1444                         __a, __p, _VSTD::forward<_Args>(__args)...);}
1445
1446    template <class _Tp>
1447        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1448        static void destroy(allocator_type& __a, _Tp* __p)
1449            {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1450
1451    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1452    static size_type max_size(const allocator_type& __a) _NOEXCEPT
1453        {return __max_size(__has_max_size<const allocator_type>(), __a);}
1454
1455    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1456    static allocator_type
1457        select_on_container_copy_construction(const allocator_type& __a)
1458            {return __select_on_container_copy_construction(
1459                __has_select_on_container_copy_construction<const allocator_type>(),
1460                __a);}
1461
1462private:
1463    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1464    static pointer __allocate(allocator_type& __a, size_type __n,
1465        const_void_pointer __hint, true_type)
1466        {
1467            _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1468            return __a.allocate(__n, __hint);
1469            _LIBCPP_SUPPRESS_DEPRECATED_POP
1470        }
1471    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1472    static pointer __allocate(allocator_type& __a, size_type __n,
1473        const_void_pointer, false_type)
1474        {return __a.allocate(__n);}
1475
1476    template <class _Tp, class... _Args>
1477        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1478        static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1479            {
1480                _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1481                __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1482                _LIBCPP_SUPPRESS_DEPRECATED_POP
1483            }
1484
1485    template <class _Tp, class... _Args>
1486        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1487        static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1488            {
1489#if _LIBCPP_STD_VER > 17
1490                _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
1491#else
1492                ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
1493#endif
1494            }
1495
1496    template <class _Tp>
1497        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1498        static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1499            {
1500                _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1501                __a.destroy(__p);
1502                _LIBCPP_SUPPRESS_DEPRECATED_POP
1503            }
1504    template <class _Tp>
1505        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1506        static void __destroy(false_type, allocator_type&, _Tp* __p)
1507            {
1508#if _LIBCPP_STD_VER > 17
1509                _VSTD::destroy_at(__p);
1510#else
1511                __p->~_Tp();
1512#endif
1513            }
1514
1515    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1516    static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
1517            {
1518                _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1519                return __a.max_size();
1520                _LIBCPP_SUPPRESS_DEPRECATED_POP
1521            }
1522
1523    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1524    static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
1525            {return numeric_limits<size_type>::max() / sizeof(value_type);}
1526
1527    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1528    static allocator_type
1529        __select_on_container_copy_construction(true_type, const allocator_type& __a)
1530            {return __a.select_on_container_copy_construction();}
1531    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1532    static allocator_type
1533        __select_on_container_copy_construction(false_type, const allocator_type& __a)
1534            {return __a;}
1535};
1536
1537template <class _Traits, class _Tp>
1538struct __rebind_alloc_helper
1539{
1540#ifndef _LIBCPP_CXX03_LANG
1541    typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp>        type;
1542#else
1543    typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1544#endif
1545};
1546
1547// allocator
1548
1549template <class _Tp>
1550class _LIBCPP_TEMPLATE_VIS allocator
1551{
1552public:
1553    typedef size_t      size_type;
1554    typedef ptrdiff_t   difference_type;
1555    typedef _Tp         value_type;
1556    typedef true_type   propagate_on_container_move_assignment;
1557    typedef true_type   is_always_equal;
1558
1559    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1560    allocator() _NOEXCEPT { }
1561
1562    template <class _Up>
1563    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1564    allocator(const allocator<_Up>&) _NOEXCEPT { }
1565
1566    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1567    _Tp* allocate(size_t __n) {
1568        if (__n > allocator_traits<allocator>::max_size(*this))
1569            __throw_length_error("allocator<T>::allocate(size_t n)"
1570                                 " 'n' exceeds maximum supported size");
1571        if (__libcpp_is_constant_evaluated()) {
1572            return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
1573        } else {
1574            return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1575        }
1576    }
1577
1578    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1579    void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
1580        if (__libcpp_is_constant_evaluated()) {
1581            ::operator delete(__p);
1582        } else {
1583            _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1584        }
1585    }
1586
1587    // C++20 Removed members
1588#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1589    _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp*       pointer;
1590    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1591    _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp&       reference;
1592    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1593
1594    template <class _Up>
1595    struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1596        typedef allocator<_Up> other;
1597    };
1598
1599    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1600    pointer address(reference __x) const _NOEXCEPT {
1601        return _VSTD::addressof(__x);
1602    }
1603    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1604    const_pointer address(const_reference __x) const _NOEXCEPT {
1605        return _VSTD::addressof(__x);
1606    }
1607
1608    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1609    _Tp* allocate(size_t __n, const void*) {
1610        return allocate(__n);
1611    }
1612
1613    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1614        return size_type(~0) / sizeof(_Tp);
1615    }
1616
1617    template <class _Up, class... _Args>
1618    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1619    void construct(_Up* __p, _Args&&... __args) {
1620        ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1621    }
1622
1623    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1624    void destroy(pointer __p) {
1625        __p->~_Tp();
1626    }
1627#endif
1628};
1629
1630template <class _Tp>
1631class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
1632{
1633public:
1634    typedef size_t      size_type;
1635    typedef ptrdiff_t   difference_type;
1636    typedef const _Tp   value_type;
1637    typedef true_type   propagate_on_container_move_assignment;
1638    typedef true_type   is_always_equal;
1639
1640    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1641    allocator() _NOEXCEPT { }
1642
1643    template <class _Up>
1644    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1645    allocator(const allocator<_Up>&) _NOEXCEPT { }
1646
1647    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1648    const _Tp* allocate(size_t __n) {
1649        if (__n > allocator_traits<allocator>::max_size(*this))
1650            __throw_length_error("allocator<const T>::allocate(size_t n)"
1651                                 " 'n' exceeds maximum supported size");
1652        if (__libcpp_is_constant_evaluated()) {
1653            return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
1654        } else {
1655            return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1656        }
1657    }
1658
1659    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1660    void deallocate(const _Tp* __p, size_t __n) {
1661        if (__libcpp_is_constant_evaluated()) {
1662            ::operator delete(const_cast<_Tp*>(__p));
1663        } else {
1664            _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1665        }
1666    }
1667
1668    // C++20 Removed members
1669#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1670    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1671    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1672    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1673    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1674
1675    template <class _Up>
1676    struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1677        typedef allocator<_Up> other;
1678    };
1679
1680    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1681    const_pointer address(const_reference __x) const _NOEXCEPT {
1682        return _VSTD::addressof(__x);
1683    }
1684
1685    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1686    const _Tp* allocate(size_t __n, const void*) {
1687        return allocate(__n);
1688    }
1689
1690    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1691        return size_type(~0) / sizeof(_Tp);
1692    }
1693
1694    template <class _Up, class... _Args>
1695    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1696    void construct(_Up* __p, _Args&&... __args) {
1697        ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1698    }
1699
1700    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1701    void destroy(pointer __p) {
1702        __p->~_Tp();
1703    }
1704#endif
1705};
1706
1707template <class _Tp, class _Up>
1708inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1709bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
1710
1711template <class _Tp, class _Up>
1712inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1713bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
1714
1715template <class _Alloc, class _Ptr>
1716_LIBCPP_INLINE_VISIBILITY
1717void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
1718    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1719        "The specified type does not meet the requirements of Cpp17MoveInsertible");
1720    typedef allocator_traits<_Alloc> _Traits;
1721    for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
1722        _Traits::construct(__a, _VSTD::__to_address(__begin2),
1723#ifdef _LIBCPP_NO_EXCEPTIONS
1724            _VSTD::move(*__begin1)
1725#else
1726            _VSTD::move_if_noexcept(*__begin1)
1727#endif
1728        );
1729    }
1730}
1731
1732template <class _Alloc, class _Tp, typename enable_if<
1733    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1734    is_trivially_move_constructible<_Tp>::value
1735>::type>
1736_LIBCPP_INLINE_VISIBILITY
1737void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1738    ptrdiff_t _Np = __end1 - __begin1;
1739    if (_Np > 0) {
1740        _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1741        __begin2 += _Np;
1742    }
1743}
1744
1745template <class _Alloc, class _Iter, class _Ptr>
1746_LIBCPP_INLINE_VISIBILITY
1747void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1748    typedef allocator_traits<_Alloc> _Traits;
1749    for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1750        _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1751    }
1752}
1753
1754template <class _Alloc, class _Source, class _Dest,
1755          class _RawSource = typename remove_const<_Source>::type,
1756          class _RawDest = typename remove_const<_Dest>::type,
1757          class =
1758    typename enable_if<
1759        is_trivially_copy_constructible<_Dest>::value &&
1760        is_same<_RawSource, _RawDest>::value &&
1761        (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1762    >::type>
1763_LIBCPP_INLINE_VISIBILITY
1764void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1765    ptrdiff_t _Np = __end1 - __begin1;
1766    if (_Np > 0) {
1767        _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1768        __begin2 += _Np;
1769    }
1770}
1771
1772template <class _Alloc, class _Ptr>
1773_LIBCPP_INLINE_VISIBILITY
1774void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1775    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1776        "The specified type does not meet the requirements of Cpp17MoveInsertable");
1777    typedef allocator_traits<_Alloc> _Traits;
1778    while (__end1 != __begin1) {
1779        _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1780#ifdef _LIBCPP_NO_EXCEPTIONS
1781            _VSTD::move(*--__end1)
1782#else
1783            _VSTD::move_if_noexcept(*--__end1)
1784#endif
1785        );
1786        --__end2;
1787    }
1788}
1789
1790template <class _Alloc, class _Tp, class = typename enable_if<
1791    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1792    is_trivially_move_constructible<_Tp>::value
1793>::type>
1794_LIBCPP_INLINE_VISIBILITY
1795void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1796    ptrdiff_t _Np = __end1 - __begin1;
1797    __end2 -= _Np;
1798    if (_Np > 0)
1799        _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1800}
1801
1802template <class _OutputIterator, class _Tp>
1803class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
1804    : public iterator<output_iterator_tag,
1805                      _Tp,                                         // purposefully not C++03
1806                      ptrdiff_t,                                   // purposefully not C++03
1807                      _Tp*,                                        // purposefully not C++03
1808                      raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1809{
1810private:
1811    _OutputIterator __x_;
1812public:
1813    _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1814    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1815    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1816        {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
1817#if _LIBCPP_STD_VER >= 14
1818    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1819        {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
1820#endif
1821    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1822    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator  operator++(int)
1823        {raw_storage_iterator __t(*this); ++__x_; return __t;}
1824#if _LIBCPP_STD_VER >= 14
1825    _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1826#endif
1827};
1828
1829template <class _Tp>
1830_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
1831pair<_Tp*, ptrdiff_t>
1832get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
1833{
1834    pair<_Tp*, ptrdiff_t> __r(0, 0);
1835    const ptrdiff_t __m = (~ptrdiff_t(0) ^
1836                           ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1837                           / sizeof(_Tp);
1838    if (__n > __m)
1839        __n = __m;
1840    while (__n > 0)
1841    {
1842#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
1843    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
1844        {
1845            align_val_t __al =
1846                align_val_t(alignment_of<_Tp>::value);
1847            __r.first = static_cast<_Tp*>(::operator new(
1848                __n * sizeof(_Tp), __al, nothrow));
1849        } else {
1850            __r.first = static_cast<_Tp*>(::operator new(
1851                __n * sizeof(_Tp), nothrow));
1852        }
1853#else
1854    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
1855        {
1856            // Since aligned operator new is unavailable, return an empty
1857            // buffer rather than one with invalid alignment.
1858            return __r;
1859        }
1860
1861        __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1862#endif
1863
1864        if (__r.first)
1865        {
1866            __r.second = __n;
1867            break;
1868        }
1869        __n /= 2;
1870    }
1871    return __r;
1872}
1873
1874template <class _Tp>
1875inline _LIBCPP_INLINE_VISIBILITY
1876void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1877{
1878  _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
1879}
1880
1881#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1882template <class _Tp>
1883struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
1884{
1885    _Tp* __ptr_;
1886};
1887
1888template<class _Tp>
1889class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
1890{
1891private:
1892    _Tp* __ptr_;
1893public:
1894    typedef _Tp element_type;
1895
1896    _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1897    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1898    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
1899        : __ptr_(__p.release()) {}
1900    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
1901        {reset(__p.release()); return *this;}
1902    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
1903        {reset(__p.release()); return *this;}
1904    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
1905        {reset(__p.__ptr_); return *this;}
1906    _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
1907
1908    _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
1909        {return *__ptr_;}
1910    _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1911    _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1912    _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
1913    {
1914        _Tp* __t = __ptr_;
1915        __ptr_ = nullptr;
1916        return __t;
1917    }
1918    _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
1919    {
1920        if (__ptr_ != __p)
1921            delete __ptr_;
1922        __ptr_ = __p;
1923    }
1924
1925    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1926    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
1927        {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1928    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
1929        {return auto_ptr<_Up>(release());}
1930};
1931
1932template <>
1933class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
1934{
1935public:
1936    typedef void element_type;
1937};
1938#endif
1939
1940// Tag used to default initialize one or both of the pair's elements.
1941struct __default_init_tag {};
1942struct __value_init_tag {};
1943
1944template <class _Tp, int _Idx,
1945          bool _CanBeEmptyBase =
1946              is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1947struct __compressed_pair_elem {
1948  typedef _Tp _ParamT;
1949  typedef _Tp& reference;
1950  typedef const _Tp& const_reference;
1951
1952  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1953  __compressed_pair_elem(__default_init_tag) {}
1954  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1955  __compressed_pair_elem(__value_init_tag) : __value_() {}
1956
1957  template <class _Up, class = typename enable_if<
1958      !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1959  >::type>
1960  _LIBCPP_INLINE_VISIBILITY
1961  _LIBCPP_CONSTEXPR explicit
1962  __compressed_pair_elem(_Up&& __u)
1963      : __value_(_VSTD::forward<_Up>(__u))
1964    {
1965    }
1966
1967
1968#ifndef _LIBCPP_CXX03_LANG
1969  template <class... _Args, size_t... _Indexes>
1970  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1971  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1972                         __tuple_indices<_Indexes...>)
1973      : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
1974#endif
1975
1976
1977  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1978  _LIBCPP_INLINE_VISIBILITY
1979  const_reference __get() const _NOEXCEPT { return __value_; }
1980
1981private:
1982  _Tp __value_;
1983};
1984
1985template <class _Tp, int _Idx>
1986struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1987  typedef _Tp _ParamT;
1988  typedef _Tp& reference;
1989  typedef const _Tp& const_reference;
1990  typedef _Tp __value_type;
1991
1992  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1993  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1994  __compressed_pair_elem(__default_init_tag) {}
1995  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1996  __compressed_pair_elem(__value_init_tag) : __value_type() {}
1997
1998  template <class _Up, class = typename enable_if<
1999        !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2000  >::type>
2001  _LIBCPP_INLINE_VISIBILITY
2002  _LIBCPP_CONSTEXPR explicit
2003  __compressed_pair_elem(_Up&& __u)
2004      : __value_type(_VSTD::forward<_Up>(__u))
2005  {}
2006
2007#ifndef _LIBCPP_CXX03_LANG
2008  template <class... _Args, size_t... _Indexes>
2009  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2010  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2011                         __tuple_indices<_Indexes...>)
2012      : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2013#endif
2014
2015  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2016  _LIBCPP_INLINE_VISIBILITY
2017  const_reference __get() const _NOEXCEPT { return *this; }
2018};
2019
2020template <class _T1, class _T2>
2021class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2022                          private __compressed_pair_elem<_T2, 1> {
2023  typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2024  typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
2025
2026  // NOTE: This static assert should never fire because __compressed_pair
2027  // is *almost never* used in a scenario where it's possible for T1 == T2.
2028  // (The exception is std::function where it is possible that the function
2029  //  object and the allocator have the same type).
2030  static_assert((!is_same<_T1, _T2>::value),
2031    "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
2032    "The current implementation is NOT ABI-compatible with the previous "
2033    "implementation for this configuration");
2034
2035public:
2036    template <bool _Dummy = true,
2037      class = typename enable_if<
2038          __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2039          __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2040      >::type
2041  >
2042  _LIBCPP_INLINE_VISIBILITY
2043  _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
2044
2045  template <class _U1, class _U2>
2046  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2047  __compressed_pair(_U1&& __t1, _U2&& __t2)
2048      : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
2049
2050#ifndef _LIBCPP_CXX03_LANG
2051  template <class... _Args1, class... _Args2>
2052  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2053  __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2054                    tuple<_Args2...> __second_args)
2055      : _Base1(__pc, _VSTD::move(__first_args),
2056               typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2057        _Base2(__pc, _VSTD::move(__second_args),
2058               typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
2059#endif
2060
2061  _LIBCPP_INLINE_VISIBILITY
2062  typename _Base1::reference first() _NOEXCEPT {
2063    return static_cast<_Base1&>(*this).__get();
2064  }
2065
2066  _LIBCPP_INLINE_VISIBILITY
2067  typename _Base1::const_reference first() const _NOEXCEPT {
2068    return static_cast<_Base1 const&>(*this).__get();
2069  }
2070
2071  _LIBCPP_INLINE_VISIBILITY
2072  typename _Base2::reference second() _NOEXCEPT {
2073    return static_cast<_Base2&>(*this).__get();
2074  }
2075
2076  _LIBCPP_INLINE_VISIBILITY
2077  typename _Base2::const_reference second() const _NOEXCEPT {
2078    return static_cast<_Base2 const&>(*this).__get();
2079  }
2080
2081  _LIBCPP_INLINE_VISIBILITY
2082  void swap(__compressed_pair& __x)
2083    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2084               __is_nothrow_swappable<_T2>::value)
2085  {
2086    using _VSTD::swap;
2087    swap(first(), __x.first());
2088    swap(second(), __x.second());
2089  }
2090};
2091
2092template <class _T1, class _T2>
2093inline _LIBCPP_INLINE_VISIBILITY
2094void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2095    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2096               __is_nothrow_swappable<_T2>::value) {
2097  __x.swap(__y);
2098}
2099
2100// default_delete
2101
2102template <class _Tp>
2103struct _LIBCPP_TEMPLATE_VIS default_delete {
2104    static_assert(!is_function<_Tp>::value,
2105                  "default_delete cannot be instantiated for function types");
2106#ifndef _LIBCPP_CXX03_LANG
2107  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
2108#else
2109  _LIBCPP_INLINE_VISIBILITY default_delete() {}
2110#endif
2111  template <class _Up>
2112  _LIBCPP_INLINE_VISIBILITY
2113  default_delete(const default_delete<_Up>&,
2114                 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2115                     0) _NOEXCEPT {}
2116
2117  _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2118    static_assert(sizeof(_Tp) > 0,
2119                  "default_delete can not delete incomplete type");
2120    static_assert(!is_void<_Tp>::value,
2121                  "default_delete can not delete incomplete type");
2122    delete __ptr;
2123  }
2124};
2125
2126template <class _Tp>
2127struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2128private:
2129  template <class _Up>
2130  struct _EnableIfConvertible
2131      : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2132
2133public:
2134#ifndef _LIBCPP_CXX03_LANG
2135  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
2136#else
2137  _LIBCPP_INLINE_VISIBILITY default_delete() {}
2138#endif
2139
2140  template <class _Up>
2141  _LIBCPP_INLINE_VISIBILITY
2142  default_delete(const default_delete<_Up[]>&,
2143                 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2144
2145  template <class _Up>
2146  _LIBCPP_INLINE_VISIBILITY
2147  typename _EnableIfConvertible<_Up>::type
2148  operator()(_Up* __ptr) const _NOEXCEPT {
2149    static_assert(sizeof(_Tp) > 0,
2150                  "default_delete can not delete incomplete type");
2151    static_assert(!is_void<_Tp>::value,
2152                  "default_delete can not delete void type");
2153    delete[] __ptr;
2154  }
2155};
2156
2157template <class _Deleter>
2158struct __unique_ptr_deleter_sfinae {
2159  static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2160  typedef const _Deleter& __lval_ref_type;
2161  typedef _Deleter&& __good_rval_ref_type;
2162  typedef true_type __enable_rval_overload;
2163};
2164
2165template <class _Deleter>
2166struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2167  typedef const _Deleter& __lval_ref_type;
2168  typedef const _Deleter&& __bad_rval_ref_type;
2169  typedef false_type __enable_rval_overload;
2170};
2171
2172template <class _Deleter>
2173struct __unique_ptr_deleter_sfinae<_Deleter&> {
2174  typedef _Deleter& __lval_ref_type;
2175  typedef _Deleter&& __bad_rval_ref_type;
2176  typedef false_type __enable_rval_overload;
2177};
2178
2179#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2180#  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2181#else
2182#  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2183#endif
2184
2185template <class _Tp, class _Dp = default_delete<_Tp> >
2186class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
2187public:
2188  typedef _Tp element_type;
2189  typedef _Dp deleter_type;
2190  typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
2191
2192  static_assert(!is_rvalue_reference<deleter_type>::value,
2193                "the specified deleter type cannot be an rvalue reference");
2194
2195private:
2196  __compressed_pair<pointer, deleter_type> __ptr_;
2197
2198  struct __nat { int __for_bool_; };
2199
2200  typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
2201
2202  template <bool _Dummy>
2203  using _LValRefType _LIBCPP_NODEBUG_TYPE =
2204      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
2205
2206  template <bool _Dummy>
2207  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
2208      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
2209
2210  template <bool _Dummy>
2211  using _BadRValRefType _LIBCPP_NODEBUG_TYPE  =
2212      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
2213
2214  template <bool _Dummy, class _Deleter = typename __dependent_type<
2215                             __identity<deleter_type>, _Dummy>::type>
2216  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
2217      typename enable_if<is_default_constructible<_Deleter>::value &&
2218                         !is_pointer<_Deleter>::value>::type;
2219
2220  template <class _ArgType>
2221  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
2222      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2223
2224  template <class _UPtr, class _Up>
2225  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
2226      is_convertible<typename _UPtr::pointer, pointer>::value &&
2227      !is_array<_Up>::value
2228  >::type;
2229
2230  template <class _UDel>
2231  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
2232      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2233      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2234    >::type;
2235
2236  template <class _UDel>
2237  using _EnableIfDeleterAssignable = typename enable_if<
2238      is_assignable<_Dp&, _UDel&&>::value
2239    >::type;
2240
2241public:
2242  template <bool _Dummy = true,
2243            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
2244  _LIBCPP_INLINE_VISIBILITY
2245  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
2246
2247  template <bool _Dummy = true,
2248            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
2249  _LIBCPP_INLINE_VISIBILITY
2250  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
2251
2252  template <bool _Dummy = true,
2253            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
2254  _LIBCPP_INLINE_VISIBILITY
2255  explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
2256
2257  template <bool _Dummy = true,
2258            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
2259  _LIBCPP_INLINE_VISIBILITY
2260  unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
2261      : __ptr_(__p, __d) {}
2262
2263  template <bool _Dummy = true,
2264            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
2265  _LIBCPP_INLINE_VISIBILITY
2266  unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
2267      : __ptr_(__p, _VSTD::move(__d)) {
2268    static_assert(!is_reference<deleter_type>::value,
2269                  "rvalue deleter bound to reference");
2270  }
2271
2272  template <bool _Dummy = true,
2273            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
2274  _LIBCPP_INLINE_VISIBILITY
2275  unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2276
2277  _LIBCPP_INLINE_VISIBILITY
2278  unique_ptr(unique_ptr&& __u) _NOEXCEPT
2279      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2280  }
2281
2282  template <class _Up, class _Ep,
2283      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2284      class = _EnableIfDeleterConvertible<_Ep>
2285  >
2286  _LIBCPP_INLINE_VISIBILITY
2287  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2288      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2289
2290#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2291  template <class _Up>
2292  _LIBCPP_INLINE_VISIBILITY
2293  unique_ptr(auto_ptr<_Up>&& __p,
2294             typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2295                                    is_same<_Dp, default_delete<_Tp> >::value,
2296                                __nat>::type = __nat()) _NOEXCEPT
2297      : __ptr_(__p.release(), __default_init_tag()) {}
2298#endif
2299
2300  _LIBCPP_INLINE_VISIBILITY
2301  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2302    reset(__u.release());
2303    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2304    return *this;
2305  }
2306
2307  template <class _Up, class _Ep,
2308      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2309      class = _EnableIfDeleterAssignable<_Ep>
2310  >
2311  _LIBCPP_INLINE_VISIBILITY
2312  unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2313    reset(__u.release());
2314    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2315    return *this;
2316  }
2317
2318#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2319  template <class _Up>
2320  _LIBCPP_INLINE_VISIBILITY
2321      typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2322                             is_same<_Dp, default_delete<_Tp> >::value,
2323                         unique_ptr&>::type
2324      operator=(auto_ptr<_Up> __p) {
2325    reset(__p.release());
2326    return *this;
2327  }
2328#endif
2329
2330#ifdef _LIBCPP_CXX03_LANG
2331  unique_ptr(unique_ptr const&) = delete;
2332  unique_ptr& operator=(unique_ptr const&) = delete;
2333#endif
2334
2335
2336  _LIBCPP_INLINE_VISIBILITY
2337  ~unique_ptr() { reset(); }
2338
2339  _LIBCPP_INLINE_VISIBILITY
2340  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2341    reset();
2342    return *this;
2343  }
2344
2345  _LIBCPP_INLINE_VISIBILITY
2346  typename add_lvalue_reference<_Tp>::type
2347  operator*() const {
2348    return *__ptr_.first();
2349  }
2350  _LIBCPP_INLINE_VISIBILITY
2351  pointer operator->() const _NOEXCEPT {
2352    return __ptr_.first();
2353  }
2354  _LIBCPP_INLINE_VISIBILITY
2355  pointer get() const _NOEXCEPT {
2356    return __ptr_.first();
2357  }
2358  _LIBCPP_INLINE_VISIBILITY
2359  deleter_type& get_deleter() _NOEXCEPT {
2360    return __ptr_.second();
2361  }
2362  _LIBCPP_INLINE_VISIBILITY
2363  const deleter_type& get_deleter() const _NOEXCEPT {
2364    return __ptr_.second();
2365  }
2366  _LIBCPP_INLINE_VISIBILITY
2367  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2368    return __ptr_.first() != nullptr;
2369  }
2370
2371  _LIBCPP_INLINE_VISIBILITY
2372  pointer release() _NOEXCEPT {
2373    pointer __t = __ptr_.first();
2374    __ptr_.first() = pointer();
2375    return __t;
2376  }
2377
2378  _LIBCPP_INLINE_VISIBILITY
2379  void reset(pointer __p = pointer()) _NOEXCEPT {
2380    pointer __tmp = __ptr_.first();
2381    __ptr_.first() = __p;
2382    if (__tmp)
2383      __ptr_.second()(__tmp);
2384  }
2385
2386  _LIBCPP_INLINE_VISIBILITY
2387  void swap(unique_ptr& __u) _NOEXCEPT {
2388    __ptr_.swap(__u.__ptr_);
2389  }
2390};
2391
2392
2393template <class _Tp, class _Dp>
2394class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
2395public:
2396  typedef _Tp element_type;
2397  typedef _Dp deleter_type;
2398  typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2399
2400private:
2401  __compressed_pair<pointer, deleter_type> __ptr_;
2402
2403  template <class _From>
2404  struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
2405
2406  template <class _FromElem>
2407  struct _CheckArrayPointerConversion<_FromElem*>
2408      : integral_constant<bool,
2409          is_same<_FromElem*, pointer>::value ||
2410            (is_same<pointer, element_type*>::value &&
2411             is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2412      >
2413  {};
2414
2415  typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
2416
2417  template <bool _Dummy>
2418  using _LValRefType _LIBCPP_NODEBUG_TYPE =
2419      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
2420
2421  template <bool _Dummy>
2422  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
2423      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
2424
2425  template <bool _Dummy>
2426  using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
2427      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
2428
2429  template <bool _Dummy, class _Deleter = typename __dependent_type<
2430                             __identity<deleter_type>, _Dummy>::type>
2431  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE  =
2432      typename enable_if<is_default_constructible<_Deleter>::value &&
2433                         !is_pointer<_Deleter>::value>::type;
2434
2435  template <class _ArgType>
2436  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
2437      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2438
2439  template <class _Pp>
2440  using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
2441      _CheckArrayPointerConversion<_Pp>::value
2442  >::type;
2443
2444  template <class _UPtr, class _Up,
2445        class _ElemT = typename _UPtr::element_type>
2446  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
2447      is_array<_Up>::value &&
2448      is_same<pointer, element_type*>::value &&
2449      is_same<typename _UPtr::pointer, _ElemT*>::value &&
2450      is_convertible<_ElemT(*)[], element_type(*)[]>::value
2451    >::type;
2452
2453  template <class _UDel>
2454  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
2455      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2456      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2457    >::type;
2458
2459  template <class _UDel>
2460  using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE  = typename enable_if<
2461      is_assignable<_Dp&, _UDel&&>::value
2462    >::type;
2463
2464public:
2465  template <bool _Dummy = true,
2466            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
2467  _LIBCPP_INLINE_VISIBILITY
2468  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
2469
2470  template <bool _Dummy = true,
2471            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
2472  _LIBCPP_INLINE_VISIBILITY
2473  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
2474
2475  template <class _Pp, bool _Dummy = true,
2476            class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2477            class = _EnableIfPointerConvertible<_Pp> >
2478  _LIBCPP_INLINE_VISIBILITY
2479  explicit unique_ptr(_Pp __p) _NOEXCEPT
2480      : __ptr_(__p, __default_init_tag()) {}
2481
2482  template <class _Pp, bool _Dummy = true,
2483            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2484            class = _EnableIfPointerConvertible<_Pp> >
2485  _LIBCPP_INLINE_VISIBILITY
2486  unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
2487      : __ptr_(__p, __d) {}
2488
2489  template <bool _Dummy = true,
2490            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
2491  _LIBCPP_INLINE_VISIBILITY
2492  unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
2493      : __ptr_(nullptr, __d) {}
2494
2495  template <class _Pp, bool _Dummy = true,
2496            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2497            class = _EnableIfPointerConvertible<_Pp> >
2498  _LIBCPP_INLINE_VISIBILITY
2499  unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
2500      : __ptr_(__p, _VSTD::move(__d)) {
2501    static_assert(!is_reference<deleter_type>::value,
2502                  "rvalue deleter bound to reference");
2503  }
2504
2505  template <bool _Dummy = true,
2506            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
2507  _LIBCPP_INLINE_VISIBILITY
2508  unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
2509      : __ptr_(nullptr, _VSTD::move(__d)) {
2510    static_assert(!is_reference<deleter_type>::value,
2511                  "rvalue deleter bound to reference");
2512  }
2513
2514  template <class _Pp, bool _Dummy = true,
2515            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2516            class = _EnableIfPointerConvertible<_Pp> >
2517  _LIBCPP_INLINE_VISIBILITY
2518  unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
2519
2520  _LIBCPP_INLINE_VISIBILITY
2521  unique_ptr(unique_ptr&& __u) _NOEXCEPT
2522      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2523  }
2524
2525  _LIBCPP_INLINE_VISIBILITY
2526  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2527    reset(__u.release());
2528    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2529    return *this;
2530  }
2531
2532  template <class _Up, class _Ep,
2533      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2534      class = _EnableIfDeleterConvertible<_Ep>
2535  >
2536  _LIBCPP_INLINE_VISIBILITY
2537  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2538      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
2539  }
2540
2541  template <class _Up, class _Ep,
2542      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2543      class = _EnableIfDeleterAssignable<_Ep>
2544  >
2545  _LIBCPP_INLINE_VISIBILITY
2546  unique_ptr&
2547  operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2548    reset(__u.release());
2549    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2550    return *this;
2551  }
2552
2553#ifdef _LIBCPP_CXX03_LANG
2554  unique_ptr(unique_ptr const&) = delete;
2555  unique_ptr& operator=(unique_ptr const&) = delete;
2556#endif
2557
2558public:
2559  _LIBCPP_INLINE_VISIBILITY
2560  ~unique_ptr() { reset(); }
2561
2562  _LIBCPP_INLINE_VISIBILITY
2563  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2564    reset();
2565    return *this;
2566  }
2567
2568  _LIBCPP_INLINE_VISIBILITY
2569  typename add_lvalue_reference<_Tp>::type
2570  operator[](size_t __i) const {
2571    return __ptr_.first()[__i];
2572  }
2573  _LIBCPP_INLINE_VISIBILITY
2574  pointer get() const _NOEXCEPT {
2575    return __ptr_.first();
2576  }
2577
2578  _LIBCPP_INLINE_VISIBILITY
2579  deleter_type& get_deleter() _NOEXCEPT {
2580    return __ptr_.second();
2581  }
2582
2583  _LIBCPP_INLINE_VISIBILITY
2584  const deleter_type& get_deleter() const _NOEXCEPT {
2585    return __ptr_.second();
2586  }
2587  _LIBCPP_INLINE_VISIBILITY
2588  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2589    return __ptr_.first() != nullptr;
2590  }
2591
2592  _LIBCPP_INLINE_VISIBILITY
2593  pointer release() _NOEXCEPT {
2594    pointer __t = __ptr_.first();
2595    __ptr_.first() = pointer();
2596    return __t;
2597  }
2598
2599  template <class _Pp>
2600  _LIBCPP_INLINE_VISIBILITY
2601  typename enable_if<
2602      _CheckArrayPointerConversion<_Pp>::value
2603  >::type
2604  reset(_Pp __p) _NOEXCEPT {
2605    pointer __tmp = __ptr_.first();
2606    __ptr_.first() = __p;
2607    if (__tmp)
2608      __ptr_.second()(__tmp);
2609  }
2610
2611  _LIBCPP_INLINE_VISIBILITY
2612  void reset(nullptr_t = nullptr) _NOEXCEPT {
2613    pointer __tmp = __ptr_.first();
2614    __ptr_.first() = nullptr;
2615    if (__tmp)
2616      __ptr_.second()(__tmp);
2617  }
2618
2619  _LIBCPP_INLINE_VISIBILITY
2620  void swap(unique_ptr& __u) _NOEXCEPT {
2621    __ptr_.swap(__u.__ptr_);
2622  }
2623
2624};
2625
2626template <class _Tp, class _Dp>
2627inline _LIBCPP_INLINE_VISIBILITY
2628typename enable_if<
2629    __is_swappable<_Dp>::value,
2630    void
2631>::type
2632swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
2633
2634template <class _T1, class _D1, class _T2, class _D2>
2635inline _LIBCPP_INLINE_VISIBILITY
2636bool
2637operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2638
2639template <class _T1, class _D1, class _T2, class _D2>
2640inline _LIBCPP_INLINE_VISIBILITY
2641bool
2642operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2643
2644template <class _T1, class _D1, class _T2, class _D2>
2645inline _LIBCPP_INLINE_VISIBILITY
2646bool
2647operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2648{
2649    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2650    typedef typename unique_ptr<_T2, _D2>::pointer _P2;
2651    typedef typename common_type<_P1, _P2>::type _Vp;
2652    return less<_Vp>()(__x.get(), __y.get());
2653}
2654
2655template <class _T1, class _D1, class _T2, class _D2>
2656inline _LIBCPP_INLINE_VISIBILITY
2657bool
2658operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2659
2660template <class _T1, class _D1, class _T2, class _D2>
2661inline _LIBCPP_INLINE_VISIBILITY
2662bool
2663operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2664
2665template <class _T1, class _D1, class _T2, class _D2>
2666inline _LIBCPP_INLINE_VISIBILITY
2667bool
2668operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2669
2670template <class _T1, class _D1>
2671inline _LIBCPP_INLINE_VISIBILITY
2672bool
2673operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
2674{
2675    return !__x;
2676}
2677
2678template <class _T1, class _D1>
2679inline _LIBCPP_INLINE_VISIBILITY
2680bool
2681operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
2682{
2683    return !__x;
2684}
2685
2686template <class _T1, class _D1>
2687inline _LIBCPP_INLINE_VISIBILITY
2688bool
2689operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
2690{
2691    return static_cast<bool>(__x);
2692}
2693
2694template <class _T1, class _D1>
2695inline _LIBCPP_INLINE_VISIBILITY
2696bool
2697operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
2698{
2699    return static_cast<bool>(__x);
2700}
2701
2702template <class _T1, class _D1>
2703inline _LIBCPP_INLINE_VISIBILITY
2704bool
2705operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2706{
2707    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2708    return less<_P1>()(__x.get(), nullptr);
2709}
2710
2711template <class _T1, class _D1>
2712inline _LIBCPP_INLINE_VISIBILITY
2713bool
2714operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2715{
2716    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2717    return less<_P1>()(nullptr, __x.get());
2718}
2719
2720template <class _T1, class _D1>
2721inline _LIBCPP_INLINE_VISIBILITY
2722bool
2723operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2724{
2725    return nullptr < __x;
2726}
2727
2728template <class _T1, class _D1>
2729inline _LIBCPP_INLINE_VISIBILITY
2730bool
2731operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2732{
2733    return __x < nullptr;
2734}
2735
2736template <class _T1, class _D1>
2737inline _LIBCPP_INLINE_VISIBILITY
2738bool
2739operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2740{
2741    return !(nullptr < __x);
2742}
2743
2744template <class _T1, class _D1>
2745inline _LIBCPP_INLINE_VISIBILITY
2746bool
2747operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2748{
2749    return !(__x < nullptr);
2750}
2751
2752template <class _T1, class _D1>
2753inline _LIBCPP_INLINE_VISIBILITY
2754bool
2755operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2756{
2757    return !(__x < nullptr);
2758}
2759
2760template <class _T1, class _D1>
2761inline _LIBCPP_INLINE_VISIBILITY
2762bool
2763operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2764{
2765    return !(nullptr < __x);
2766}
2767
2768#if _LIBCPP_STD_VER > 11
2769
2770template<class _Tp>
2771struct __unique_if
2772{
2773    typedef unique_ptr<_Tp> __unique_single;
2774};
2775
2776template<class _Tp>
2777struct __unique_if<_Tp[]>
2778{
2779    typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2780};
2781
2782template<class _Tp, size_t _Np>
2783struct __unique_if<_Tp[_Np]>
2784{
2785    typedef void __unique_array_known_bound;
2786};
2787
2788template<class _Tp, class... _Args>
2789inline _LIBCPP_INLINE_VISIBILITY
2790typename __unique_if<_Tp>::__unique_single
2791make_unique(_Args&&... __args)
2792{
2793    return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2794}
2795
2796template<class _Tp>
2797inline _LIBCPP_INLINE_VISIBILITY
2798typename __unique_if<_Tp>::__unique_array_unknown_bound
2799make_unique(size_t __n)
2800{
2801    typedef typename remove_extent<_Tp>::type _Up;
2802    return unique_ptr<_Tp>(new _Up[__n]());
2803}
2804
2805template<class _Tp, class... _Args>
2806    typename __unique_if<_Tp>::__unique_array_known_bound
2807    make_unique(_Args&&...) = delete;
2808
2809#endif  // _LIBCPP_STD_VER > 11
2810
2811template <class _Tp, class _Dp>
2812#ifdef _LIBCPP_CXX03_LANG
2813struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
2814#else
2815struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
2816    unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
2817#endif
2818{
2819    typedef unique_ptr<_Tp, _Dp> argument_type;
2820    typedef size_t               result_type;
2821    _LIBCPP_INLINE_VISIBILITY
2822    result_type operator()(const argument_type& __ptr) const
2823    {
2824        typedef typename argument_type::pointer pointer;
2825        return hash<pointer>()(__ptr.get());
2826    }
2827};
2828
2829struct __destruct_n
2830{
2831private:
2832    size_t __size_;
2833
2834    template <class _Tp>
2835    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
2836        {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
2837
2838    template <class _Tp>
2839    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
2840        {}
2841
2842    _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
2843        {++__size_;}
2844    _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
2845        {}
2846
2847    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
2848        {__size_ = __s;}
2849    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
2850        {}
2851public:
2852    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2853        : __size_(__s) {}
2854
2855    template <class _Tp>
2856    _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
2857        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2858
2859    template <class _Tp>
2860    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
2861        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2862
2863    template <class _Tp>
2864    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
2865        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2866};
2867
2868template <class _Alloc>
2869class __allocator_destructor
2870{
2871    typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
2872public:
2873    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2874    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
2875private:
2876    _Alloc& __alloc_;
2877    size_type __s_;
2878public:
2879    _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2880             _NOEXCEPT
2881        : __alloc_(__a), __s_(__s) {}
2882    _LIBCPP_INLINE_VISIBILITY
2883    void operator()(pointer __p) _NOEXCEPT
2884        {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2885};
2886
2887template <class _InputIterator, class _ForwardIterator>
2888_ForwardIterator
2889uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2890{
2891    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2892#ifndef _LIBCPP_NO_EXCEPTIONS
2893    _ForwardIterator __s = __r;
2894    try
2895    {
2896#endif
2897        for (; __f != __l; ++__f, (void) ++__r)
2898            ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
2899#ifndef _LIBCPP_NO_EXCEPTIONS
2900    }
2901    catch (...)
2902    {
2903        for (; __s != __r; ++__s)
2904            __s->~value_type();
2905        throw;
2906    }
2907#endif
2908    return __r;
2909}
2910
2911template <class _InputIterator, class _Size, class _ForwardIterator>
2912_ForwardIterator
2913uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2914{
2915    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2916#ifndef _LIBCPP_NO_EXCEPTIONS
2917    _ForwardIterator __s = __r;
2918    try
2919    {
2920#endif
2921        for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2922            ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
2923#ifndef _LIBCPP_NO_EXCEPTIONS
2924    }
2925    catch (...)
2926    {
2927        for (; __s != __r; ++__s)
2928            __s->~value_type();
2929        throw;
2930    }
2931#endif
2932    return __r;
2933}
2934
2935template <class _ForwardIterator, class _Tp>
2936void
2937uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2938{
2939    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2940#ifndef _LIBCPP_NO_EXCEPTIONS
2941    _ForwardIterator __s = __f;
2942    try
2943    {
2944#endif
2945        for (; __f != __l; ++__f)
2946            ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
2947#ifndef _LIBCPP_NO_EXCEPTIONS
2948    }
2949    catch (...)
2950    {
2951        for (; __s != __f; ++__s)
2952            __s->~value_type();
2953        throw;
2954    }
2955#endif
2956}
2957
2958template <class _ForwardIterator, class _Size, class _Tp>
2959_ForwardIterator
2960uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2961{
2962    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2963#ifndef _LIBCPP_NO_EXCEPTIONS
2964    _ForwardIterator __s = __f;
2965    try
2966    {
2967#endif
2968        for (; __n > 0; ++__f, (void) --__n)
2969            ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
2970#ifndef _LIBCPP_NO_EXCEPTIONS
2971    }
2972    catch (...)
2973    {
2974        for (; __s != __f; ++__s)
2975            __s->~value_type();
2976        throw;
2977    }
2978#endif
2979    return __f;
2980}
2981
2982#if _LIBCPP_STD_VER > 14
2983
2984template <class _ForwardIterator>
2985inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2986void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2987    for (; __first != __last; ++__first)
2988        _VSTD::destroy_at(_VSTD::addressof(*__first));
2989}
2990
2991template <class _ForwardIterator, class _Size>
2992inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2993_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2994    for (; __n > 0; (void)++__first, --__n)
2995        _VSTD::destroy_at(_VSTD::addressof(*__first));
2996    return __first;
2997}
2998
2999template <class _ForwardIterator>
3000inline _LIBCPP_INLINE_VISIBILITY
3001void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3002    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3003    auto __idx = __first;
3004#ifndef _LIBCPP_NO_EXCEPTIONS
3005    try {
3006#endif
3007    for (; __idx != __last; ++__idx)
3008        ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3009#ifndef _LIBCPP_NO_EXCEPTIONS
3010    } catch (...) {
3011        _VSTD::destroy(__first, __idx);
3012        throw;
3013    }
3014#endif
3015}
3016
3017template <class _ForwardIterator, class _Size>
3018inline _LIBCPP_INLINE_VISIBILITY
3019_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3020    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3021    auto __idx = __first;
3022#ifndef _LIBCPP_NO_EXCEPTIONS
3023    try {
3024#endif
3025    for (; __n > 0; (void)++__idx, --__n)
3026        ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3027    return __idx;
3028#ifndef _LIBCPP_NO_EXCEPTIONS
3029    } catch (...) {
3030        _VSTD::destroy(__first, __idx);
3031        throw;
3032    }
3033#endif
3034}
3035
3036
3037template <class _ForwardIterator>
3038inline _LIBCPP_INLINE_VISIBILITY
3039void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3040    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3041    auto __idx = __first;
3042#ifndef _LIBCPP_NO_EXCEPTIONS
3043    try {
3044#endif
3045    for (; __idx != __last; ++__idx)
3046        ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3047#ifndef _LIBCPP_NO_EXCEPTIONS
3048    } catch (...) {
3049        _VSTD::destroy(__first, __idx);
3050        throw;
3051    }
3052#endif
3053}
3054
3055template <class _ForwardIterator, class _Size>
3056inline _LIBCPP_INLINE_VISIBILITY
3057_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3058    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3059    auto __idx = __first;
3060#ifndef _LIBCPP_NO_EXCEPTIONS
3061    try {
3062#endif
3063    for (; __n > 0; (void)++__idx, --__n)
3064        ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3065    return __idx;
3066#ifndef _LIBCPP_NO_EXCEPTIONS
3067    } catch (...) {
3068        _VSTD::destroy(__first, __idx);
3069        throw;
3070    }
3071#endif
3072}
3073
3074
3075template <class _InputIt, class _ForwardIt>
3076inline _LIBCPP_INLINE_VISIBILITY
3077_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3078    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3079    auto __idx = __first_res;
3080#ifndef _LIBCPP_NO_EXCEPTIONS
3081    try {
3082#endif
3083    for (; __first != __last; (void)++__idx, ++__first)
3084        ::new((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
3085    return __idx;
3086#ifndef _LIBCPP_NO_EXCEPTIONS
3087    } catch (...) {
3088        _VSTD::destroy(__first_res, __idx);
3089        throw;
3090    }
3091#endif
3092}
3093
3094template <class _InputIt, class _Size, class _ForwardIt>
3095inline _LIBCPP_INLINE_VISIBILITY
3096pair<_InputIt, _ForwardIt>
3097uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3098    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3099    auto __idx = __first_res;
3100#ifndef _LIBCPP_NO_EXCEPTIONS
3101    try {
3102#endif
3103    for (; __n > 0; ++__idx, (void)++__first, --__n)
3104        ::new((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
3105    return {__first, __idx};
3106#ifndef _LIBCPP_NO_EXCEPTIONS
3107    } catch (...) {
3108        _VSTD::destroy(__first_res, __idx);
3109        throw;
3110    }
3111#endif
3112}
3113
3114
3115#endif // _LIBCPP_STD_VER > 14
3116
3117// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3118// should be sufficient for thread safety.
3119// See https://bugs.llvm.org/show_bug.cgi?id=22803
3120#if defined(__clang__) && __has_builtin(__atomic_add_fetch)          \
3121                       && defined(__ATOMIC_RELAXED)                  \
3122                       && defined(__ATOMIC_ACQ_REL)
3123#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3124#elif defined(_LIBCPP_COMPILER_GCC)
3125#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3126#endif
3127
3128template <class _Tp>
3129inline _LIBCPP_INLINE_VISIBILITY _Tp
3130__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3131{
3132#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3133    return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3134#else
3135    return __t += 1;
3136#endif
3137}
3138
3139template <class _Tp>
3140inline _LIBCPP_INLINE_VISIBILITY _Tp
3141__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3142{
3143#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3144    return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3145#else
3146    return __t -= 1;
3147#endif
3148}
3149
3150class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
3151    : public std::exception
3152{
3153public:
3154    bad_weak_ptr() _NOEXCEPT = default;
3155    bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
3156    virtual ~bad_weak_ptr() _NOEXCEPT;
3157    virtual const char* what() const  _NOEXCEPT;
3158};
3159
3160_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
3161void __throw_bad_weak_ptr()
3162{
3163#ifndef _LIBCPP_NO_EXCEPTIONS
3164    throw bad_weak_ptr();
3165#else
3166    _VSTD::abort();
3167#endif
3168}
3169
3170template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
3171
3172class _LIBCPP_TYPE_VIS __shared_count
3173{
3174    __shared_count(const __shared_count&);
3175    __shared_count& operator=(const __shared_count&);
3176
3177protected:
3178    long __shared_owners_;
3179    virtual ~__shared_count();
3180private:
3181    virtual void __on_zero_shared() _NOEXCEPT = 0;
3182
3183public:
3184    _LIBCPP_INLINE_VISIBILITY
3185    explicit __shared_count(long __refs = 0) _NOEXCEPT
3186        : __shared_owners_(__refs) {}
3187
3188#if defined(_LIBCPP_BUILDING_LIBRARY) && \
3189    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
3190    void __add_shared() _NOEXCEPT;
3191    bool __release_shared() _NOEXCEPT;
3192#else
3193    _LIBCPP_INLINE_VISIBILITY
3194    void __add_shared() _NOEXCEPT {
3195      __libcpp_atomic_refcount_increment(__shared_owners_);
3196    }
3197    _LIBCPP_INLINE_VISIBILITY
3198    bool __release_shared() _NOEXCEPT {
3199      if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3200        __on_zero_shared();
3201        return true;
3202      }
3203      return false;
3204    }
3205#endif
3206    _LIBCPP_INLINE_VISIBILITY
3207    long use_count() const _NOEXCEPT {
3208        return __libcpp_relaxed_load(&__shared_owners_) + 1;
3209    }
3210};
3211
3212class _LIBCPP_TYPE_VIS __shared_weak_count
3213    : private __shared_count
3214{
3215    long __shared_weak_owners_;
3216
3217public:
3218    _LIBCPP_INLINE_VISIBILITY
3219    explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
3220        : __shared_count(__refs),
3221          __shared_weak_owners_(__refs) {}
3222protected:
3223    virtual ~__shared_weak_count();
3224
3225public:
3226#if defined(_LIBCPP_BUILDING_LIBRARY) && \
3227    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
3228    void __add_shared() _NOEXCEPT;
3229    void __add_weak() _NOEXCEPT;
3230    void __release_shared() _NOEXCEPT;
3231#else
3232    _LIBCPP_INLINE_VISIBILITY
3233    void __add_shared() _NOEXCEPT {
3234      __shared_count::__add_shared();
3235    }
3236    _LIBCPP_INLINE_VISIBILITY
3237    void __add_weak() _NOEXCEPT {
3238      __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3239    }
3240    _LIBCPP_INLINE_VISIBILITY
3241    void __release_shared() _NOEXCEPT {
3242      if (__shared_count::__release_shared())
3243        __release_weak();
3244    }
3245#endif
3246    void __release_weak() _NOEXCEPT;
3247    _LIBCPP_INLINE_VISIBILITY
3248    long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3249    __shared_weak_count* lock() _NOEXCEPT;
3250
3251    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
3252private:
3253    virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
3254};
3255
3256template <class _Tp, class _Dp, class _Alloc>
3257class __shared_ptr_pointer
3258    : public __shared_weak_count
3259{
3260    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3261public:
3262    _LIBCPP_INLINE_VISIBILITY
3263    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
3264        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
3265
3266#ifndef _LIBCPP_NO_RTTI
3267    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
3268#endif
3269
3270private:
3271    virtual void __on_zero_shared() _NOEXCEPT;
3272    virtual void __on_zero_shared_weak() _NOEXCEPT;
3273};
3274
3275#ifndef _LIBCPP_NO_RTTI
3276
3277template <class _Tp, class _Dp, class _Alloc>
3278const void*
3279__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
3280{
3281    return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
3282}
3283
3284#endif  // _LIBCPP_NO_RTTI
3285
3286template <class _Tp, class _Dp, class _Alloc>
3287void
3288__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
3289{
3290    __data_.first().second()(__data_.first().first());
3291    __data_.first().second().~_Dp();
3292}
3293
3294template <class _Tp, class _Dp, class _Alloc>
3295void
3296__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
3297{
3298    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3299    typedef allocator_traits<_Al> _ATraits;
3300    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3301
3302    _Al __a(__data_.second());
3303    __data_.second().~_Alloc();
3304    __a.deallocate(_PTraits::pointer_to(*this), 1);
3305}
3306
3307template <class _Tp, class _Alloc>
3308class __shared_ptr_emplace
3309    : public __shared_weak_count
3310{
3311    __compressed_pair<_Alloc, _Tp> __data_;
3312public:
3313
3314    _LIBCPP_INLINE_VISIBILITY
3315    __shared_ptr_emplace(_Alloc __a)
3316        :  __data_(_VSTD::move(__a), __value_init_tag()) {}
3317
3318#ifndef _LIBCPP_CXX03_LANG
3319    template <class ..._Args>
3320        _LIBCPP_INLINE_VISIBILITY
3321        __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3322            :  __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3323                   _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
3324#else
3325    template <class ..._Args>
3326        _LIBCPP_INLINE_VISIBILITY
3327        __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3328            :  __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...)) {}
3329#endif
3330
3331private:
3332    virtual void __on_zero_shared() _NOEXCEPT;
3333    virtual void __on_zero_shared_weak() _NOEXCEPT;
3334public:
3335    _LIBCPP_INLINE_VISIBILITY
3336    _Alloc& __get_alloc() _NOEXCEPT {return __data_.first();}
3337    _LIBCPP_INLINE_VISIBILITY
3338    _Tp* __get_elem() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
3339};
3340
3341template <class _Tp, class _Alloc>
3342void
3343__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
3344{
3345    __get_elem()->~_Tp();
3346}
3347
3348template <class _Tp, class _Alloc>
3349void
3350__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
3351{
3352    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3353    typedef allocator_traits<_Al> _ATraits;
3354    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3355    _Al __a(__get_alloc());
3356    __get_alloc().~_Alloc();
3357    __a.deallocate(_PTraits::pointer_to(*this), 1);
3358}
3359
3360struct __shared_ptr_dummy_rebind_allocator_type;
3361template <>
3362class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3363{
3364public:
3365    template <class _Other>
3366    struct rebind
3367    {
3368        typedef allocator<_Other> other;
3369    };
3370};
3371
3372template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
3373
3374template<class _Tp, class _Up>
3375struct __compatible_with
3376#if _LIBCPP_STD_VER > 14
3377    : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3378#else
3379    : is_convertible<_Tp*, _Up*> {};
3380#endif // _LIBCPP_STD_VER > 14
3381
3382#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3383#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3384#else
3385#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3386#endif
3387
3388template<class _Tp>
3389class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
3390{
3391public:
3392#if _LIBCPP_STD_VER > 14
3393    typedef weak_ptr<_Tp> weak_type;
3394    typedef remove_extent_t<_Tp> element_type;
3395#else
3396    typedef _Tp element_type;
3397#endif
3398
3399private:
3400    element_type*      __ptr_;
3401    __shared_weak_count* __cntrl_;
3402
3403    struct __nat {int __for_bool_;};
3404public:
3405    _LIBCPP_INLINE_VISIBILITY
3406    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3407    _LIBCPP_INLINE_VISIBILITY
3408    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
3409    template<class _Yp>
3410        explicit shared_ptr(_Yp* __p,
3411                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
3412    template<class _Yp, class _Dp>
3413        shared_ptr(_Yp* __p, _Dp __d,
3414                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
3415    template<class _Yp, class _Dp, class _Alloc>
3416        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3417                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
3418    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3419    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
3420    template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3421    _LIBCPP_INLINE_VISIBILITY
3422    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
3423    template<class _Yp>
3424        _LIBCPP_INLINE_VISIBILITY
3425        shared_ptr(const shared_ptr<_Yp>& __r,
3426                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
3427                       _NOEXCEPT;
3428    _LIBCPP_INLINE_VISIBILITY
3429    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
3430    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
3431                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
3432                       _NOEXCEPT;
3433    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
3434                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
3435#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3436    template<class _Yp>
3437        shared_ptr(auto_ptr<_Yp>&& __r,
3438                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3439#endif
3440    template <class _Yp, class _Dp>
3441        shared_ptr(unique_ptr<_Yp, _Dp>&&,
3442                   typename enable_if
3443                   <
3444                       !is_lvalue_reference<_Dp>::value &&
3445                       !is_array<_Yp>::value &&
3446                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3447                       __nat
3448                   >::type = __nat());
3449    template <class _Yp, class _Dp>
3450        shared_ptr(unique_ptr<_Yp, _Dp>&&,
3451                   typename enable_if
3452                   <
3453                       is_lvalue_reference<_Dp>::value &&
3454                       !is_array<_Yp>::value &&
3455                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3456                       __nat
3457                   >::type = __nat());
3458
3459    ~shared_ptr();
3460
3461    _LIBCPP_INLINE_VISIBILITY
3462    shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3463    template<class _Yp>
3464        typename enable_if
3465        <
3466            __compatible_with<_Yp, element_type>::value,
3467            shared_ptr&
3468        >::type
3469        _LIBCPP_INLINE_VISIBILITY
3470        operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
3471    _LIBCPP_INLINE_VISIBILITY
3472    shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
3473    template<class _Yp>
3474        typename enable_if
3475        <
3476            __compatible_with<_Yp, element_type>::value,
3477            shared_ptr&
3478        >::type
3479        _LIBCPP_INLINE_VISIBILITY
3480        operator=(shared_ptr<_Yp>&& __r);
3481#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3482    template<class _Yp>
3483        _LIBCPP_INLINE_VISIBILITY
3484        typename enable_if
3485        <
3486            !is_array<_Yp>::value &&
3487            is_convertible<_Yp*, element_type*>::value,
3488            shared_ptr
3489        >::type&
3490        operator=(auto_ptr<_Yp>&& __r);
3491#endif
3492    template <class _Yp, class _Dp>
3493        typename enable_if
3494        <
3495            !is_array<_Yp>::value &&
3496            is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3497            shared_ptr&
3498        >::type
3499        _LIBCPP_INLINE_VISIBILITY
3500        operator=(unique_ptr<_Yp, _Dp>&& __r);
3501
3502    _LIBCPP_INLINE_VISIBILITY
3503    void swap(shared_ptr& __r) _NOEXCEPT;
3504    _LIBCPP_INLINE_VISIBILITY
3505    void reset() _NOEXCEPT;
3506    template<class _Yp>
3507        typename enable_if
3508        <
3509            __compatible_with<_Yp, element_type>::value,
3510            void
3511        >::type
3512        _LIBCPP_INLINE_VISIBILITY
3513        reset(_Yp* __p);
3514    template<class _Yp, class _Dp>
3515        typename enable_if
3516        <
3517            __compatible_with<_Yp, element_type>::value,
3518            void
3519        >::type
3520        _LIBCPP_INLINE_VISIBILITY
3521        reset(_Yp* __p, _Dp __d);
3522    template<class _Yp, class _Dp, class _Alloc>
3523        typename enable_if
3524        <
3525            __compatible_with<_Yp, element_type>::value,
3526            void
3527        >::type
3528        _LIBCPP_INLINE_VISIBILITY
3529        reset(_Yp* __p, _Dp __d, _Alloc __a);
3530
3531    _LIBCPP_INLINE_VISIBILITY
3532    element_type* get() const _NOEXCEPT {return __ptr_;}
3533    _LIBCPP_INLINE_VISIBILITY
3534    typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3535        {return *__ptr_;}
3536    _LIBCPP_INLINE_VISIBILITY
3537    element_type* operator->() const _NOEXCEPT
3538    {
3539        static_assert(!_VSTD::is_array<_Tp>::value,
3540                      "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3541        return __ptr_;
3542    }
3543    _LIBCPP_INLINE_VISIBILITY
3544    long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
3545    _LIBCPP_INLINE_VISIBILITY
3546    bool unique() const _NOEXCEPT {return use_count() == 1;}
3547    _LIBCPP_INLINE_VISIBILITY
3548    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
3549    template <class _Up>
3550        _LIBCPP_INLINE_VISIBILITY
3551        bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
3552        {return __cntrl_ < __p.__cntrl_;}
3553    template <class _Up>
3554        _LIBCPP_INLINE_VISIBILITY
3555        bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
3556        {return __cntrl_ < __p.__cntrl_;}
3557    _LIBCPP_INLINE_VISIBILITY
3558    bool
3559    __owner_equivalent(const shared_ptr& __p) const
3560        {return __cntrl_ == __p.__cntrl_;}
3561
3562#if _LIBCPP_STD_VER > 14
3563    typename add_lvalue_reference<element_type>::type
3564    _LIBCPP_INLINE_VISIBILITY
3565    operator[](ptrdiff_t __i) const
3566    {
3567            static_assert(_VSTD::is_array<_Tp>::value,
3568                          "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3569            return __ptr_[__i];
3570    }
3571#endif
3572
3573#ifndef _LIBCPP_NO_RTTI
3574    template <class _Dp>
3575        _LIBCPP_INLINE_VISIBILITY
3576        _Dp* __get_deleter() const _NOEXCEPT
3577            {return static_cast<_Dp*>(__cntrl_
3578                    ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
3579                      : nullptr);}
3580#endif  // _LIBCPP_NO_RTTI
3581
3582    template<class _Yp, class _CntrlBlk>
3583    static shared_ptr<_Tp>
3584    __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
3585    {
3586        shared_ptr<_Tp> __r;
3587        __r.__ptr_ = __p;
3588        __r.__cntrl_ = __cntrl;
3589        __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3590        return __r;
3591    }
3592
3593private:
3594    template <class _Yp, bool = is_function<_Yp>::value>
3595        struct __shared_ptr_default_allocator
3596        {
3597            typedef allocator<_Yp> type;
3598        };
3599
3600    template <class _Yp>
3601        struct __shared_ptr_default_allocator<_Yp, true>
3602        {
3603            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3604        };
3605
3606    template <class _Yp, class _OrigPtr>
3607        _LIBCPP_INLINE_VISIBILITY
3608        typename enable_if<is_convertible<_OrigPtr*,
3609                                          const enable_shared_from_this<_Yp>*
3610        >::value,
3611            void>::type
3612        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3613                           _OrigPtr* __ptr) _NOEXCEPT
3614        {
3615            typedef typename remove_cv<_Yp>::type _RawYp;
3616            if (__e && __e->__weak_this_.expired())
3617            {
3618                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3619                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
3620            }
3621        }
3622
3623    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
3624
3625    template <class, class _Yp>
3626        struct __shared_ptr_default_delete
3627            : default_delete<_Yp> {};
3628
3629    template <class _Yp, class _Un, size_t _Sz>
3630        struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3631            : default_delete<_Yp[]> {};
3632
3633    template <class _Yp, class _Un>
3634        struct __shared_ptr_default_delete<_Yp[], _Un>
3635            : default_delete<_Yp[]> {};
3636
3637    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3638    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3639};
3640
3641#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3642template<class _Tp>
3643shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3644template<class _Tp, class _Dp>
3645shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3646#endif
3647
3648template<class _Tp>
3649inline
3650_LIBCPP_CONSTEXPR
3651shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
3652    : __ptr_(nullptr),
3653      __cntrl_(nullptr)
3654{
3655}
3656
3657template<class _Tp>
3658inline
3659_LIBCPP_CONSTEXPR
3660shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
3661    : __ptr_(nullptr),
3662      __cntrl_(nullptr)
3663{
3664}
3665
3666template<class _Tp>
3667template<class _Yp>
3668shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3669                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3670    : __ptr_(__p)
3671{
3672    unique_ptr<_Yp> __hold(__p);
3673    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3674    typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3675    __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
3676    __hold.release();
3677    __enable_weak_this(__p, __p);
3678}
3679
3680template<class _Tp>
3681template<class _Yp, class _Dp>
3682shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3683                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3684    : __ptr_(__p)
3685{
3686#ifndef _LIBCPP_NO_EXCEPTIONS
3687    try
3688    {
3689#endif  // _LIBCPP_NO_EXCEPTIONS
3690        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3691        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3692        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3693        __enable_weak_this(__p, __p);
3694#ifndef _LIBCPP_NO_EXCEPTIONS
3695    }
3696    catch (...)
3697    {
3698        __d(__p);
3699        throw;
3700    }
3701#endif  // _LIBCPP_NO_EXCEPTIONS
3702}
3703
3704template<class _Tp>
3705template<class _Dp>
3706shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3707    : __ptr_(nullptr)
3708{
3709#ifndef _LIBCPP_NO_EXCEPTIONS
3710    try
3711    {
3712#endif  // _LIBCPP_NO_EXCEPTIONS
3713        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3714        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3715        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3716#ifndef _LIBCPP_NO_EXCEPTIONS
3717    }
3718    catch (...)
3719    {
3720        __d(__p);
3721        throw;
3722    }
3723#endif  // _LIBCPP_NO_EXCEPTIONS
3724}
3725
3726template<class _Tp>
3727template<class _Yp, class _Dp, class _Alloc>
3728shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3729                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3730    : __ptr_(__p)
3731{
3732#ifndef _LIBCPP_NO_EXCEPTIONS
3733    try
3734    {
3735#endif  // _LIBCPP_NO_EXCEPTIONS
3736        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3737        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3738        typedef __allocator_destructor<_A2> _D2;
3739        _A2 __a2(__a);
3740        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3741        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3742            _CntrlBlk(__p, __d, __a);
3743        __cntrl_ = _VSTD::addressof(*__hold2.release());
3744        __enable_weak_this(__p, __p);
3745#ifndef _LIBCPP_NO_EXCEPTIONS
3746    }
3747    catch (...)
3748    {
3749        __d(__p);
3750        throw;
3751    }
3752#endif  // _LIBCPP_NO_EXCEPTIONS
3753}
3754
3755template<class _Tp>
3756template<class _Dp, class _Alloc>
3757shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3758    : __ptr_(nullptr)
3759{
3760#ifndef _LIBCPP_NO_EXCEPTIONS
3761    try
3762    {
3763#endif  // _LIBCPP_NO_EXCEPTIONS
3764        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3765        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3766        typedef __allocator_destructor<_A2> _D2;
3767        _A2 __a2(__a);
3768        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3769        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3770            _CntrlBlk(__p, __d, __a);
3771        __cntrl_ = _VSTD::addressof(*__hold2.release());
3772#ifndef _LIBCPP_NO_EXCEPTIONS
3773    }
3774    catch (...)
3775    {
3776        __d(__p);
3777        throw;
3778    }
3779#endif  // _LIBCPP_NO_EXCEPTIONS
3780}
3781
3782template<class _Tp>
3783template<class _Yp>
3784inline
3785shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
3786    : __ptr_(__p),
3787      __cntrl_(__r.__cntrl_)
3788{
3789    if (__cntrl_)
3790        __cntrl_->__add_shared();
3791}
3792
3793template<class _Tp>
3794inline
3795shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
3796    : __ptr_(__r.__ptr_),
3797      __cntrl_(__r.__cntrl_)
3798{
3799    if (__cntrl_)
3800        __cntrl_->__add_shared();
3801}
3802
3803template<class _Tp>
3804template<class _Yp>
3805inline
3806shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3807                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3808         _NOEXCEPT
3809    : __ptr_(__r.__ptr_),
3810      __cntrl_(__r.__cntrl_)
3811{
3812    if (__cntrl_)
3813        __cntrl_->__add_shared();
3814}
3815
3816template<class _Tp>
3817inline
3818shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
3819    : __ptr_(__r.__ptr_),
3820      __cntrl_(__r.__cntrl_)
3821{
3822    __r.__ptr_ = nullptr;
3823    __r.__cntrl_ = nullptr;
3824}
3825
3826template<class _Tp>
3827template<class _Yp>
3828inline
3829shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3830                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3831         _NOEXCEPT
3832    : __ptr_(__r.__ptr_),
3833      __cntrl_(__r.__cntrl_)
3834{
3835    __r.__ptr_ = nullptr;
3836    __r.__cntrl_ = nullptr;
3837}
3838
3839#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3840template<class _Tp>
3841template<class _Yp>
3842shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
3843                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3844    : __ptr_(__r.get())
3845{
3846    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3847    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3848    __enable_weak_this(__r.get(), __r.get());
3849    __r.release();
3850}
3851#endif
3852
3853template<class _Tp>
3854template <class _Yp, class _Dp>
3855shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3856                            typename enable_if
3857                            <
3858                                !is_lvalue_reference<_Dp>::value &&
3859                                !is_array<_Yp>::value &&
3860                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3861                                __nat
3862                            >::type)
3863    : __ptr_(__r.get())
3864{
3865#if _LIBCPP_STD_VER > 11
3866    if (__ptr_ == nullptr)
3867        __cntrl_ = nullptr;
3868    else
3869#endif
3870    {
3871        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3872        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3873        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
3874        __enable_weak_this(__r.get(), __r.get());
3875    }
3876    __r.release();
3877}
3878
3879template<class _Tp>
3880template <class _Yp, class _Dp>
3881shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3882                            typename enable_if
3883                            <
3884                                is_lvalue_reference<_Dp>::value &&
3885                                !is_array<_Yp>::value &&
3886                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3887                                __nat
3888                            >::type)
3889    : __ptr_(__r.get())
3890{
3891#if _LIBCPP_STD_VER > 11
3892    if (__ptr_ == nullptr)
3893        __cntrl_ = nullptr;
3894    else
3895#endif
3896    {
3897        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3898        typedef __shared_ptr_pointer<_Yp*,
3899                                     reference_wrapper<typename remove_reference<_Dp>::type>,
3900                                     _AllocT > _CntrlBlk;
3901        __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
3902        __enable_weak_this(__r.get(), __r.get());
3903    }
3904    __r.release();
3905}
3906
3907template<class _Tp>
3908shared_ptr<_Tp>::~shared_ptr()
3909{
3910    if (__cntrl_)
3911        __cntrl_->__release_shared();
3912}
3913
3914template<class _Tp>
3915inline
3916shared_ptr<_Tp>&
3917shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
3918{
3919    shared_ptr(__r).swap(*this);
3920    return *this;
3921}
3922
3923template<class _Tp>
3924template<class _Yp>
3925inline
3926typename enable_if
3927<
3928    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3929    shared_ptr<_Tp>&
3930>::type
3931shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
3932{
3933    shared_ptr(__r).swap(*this);
3934    return *this;
3935}
3936
3937template<class _Tp>
3938inline
3939shared_ptr<_Tp>&
3940shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
3941{
3942    shared_ptr(_VSTD::move(__r)).swap(*this);
3943    return *this;
3944}
3945
3946template<class _Tp>
3947template<class _Yp>
3948inline
3949typename enable_if
3950<
3951    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3952    shared_ptr<_Tp>&
3953>::type
3954shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3955{
3956    shared_ptr(_VSTD::move(__r)).swap(*this);
3957    return *this;
3958}
3959
3960#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3961template<class _Tp>
3962template<class _Yp>
3963inline
3964typename enable_if
3965<
3966    !is_array<_Yp>::value &&
3967    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
3968    shared_ptr<_Tp>
3969>::type&
3970shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3971{
3972    shared_ptr(_VSTD::move(__r)).swap(*this);
3973    return *this;
3974}
3975#endif
3976
3977template<class _Tp>
3978template <class _Yp, class _Dp>
3979inline
3980typename enable_if
3981<
3982    !is_array<_Yp>::value &&
3983    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
3984                   typename shared_ptr<_Tp>::element_type*>::value,
3985    shared_ptr<_Tp>&
3986>::type
3987shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3988{
3989    shared_ptr(_VSTD::move(__r)).swap(*this);
3990    return *this;
3991}
3992
3993template<class _Tp>
3994inline
3995void
3996shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
3997{
3998    _VSTD::swap(__ptr_, __r.__ptr_);
3999    _VSTD::swap(__cntrl_, __r.__cntrl_);
4000}
4001
4002template<class _Tp>
4003inline
4004void
4005shared_ptr<_Tp>::reset() _NOEXCEPT
4006{
4007    shared_ptr().swap(*this);
4008}
4009
4010template<class _Tp>
4011template<class _Yp>
4012inline
4013typename enable_if
4014<
4015    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
4016    void
4017>::type
4018shared_ptr<_Tp>::reset(_Yp* __p)
4019{
4020    shared_ptr(__p).swap(*this);
4021}
4022
4023template<class _Tp>
4024template<class _Yp, class _Dp>
4025inline
4026typename enable_if
4027<
4028    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
4029    void
4030>::type
4031shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4032{
4033    shared_ptr(__p, __d).swap(*this);
4034}
4035
4036template<class _Tp>
4037template<class _Yp, class _Dp, class _Alloc>
4038inline
4039typename enable_if
4040<
4041    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
4042    void
4043>::type
4044shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4045{
4046    shared_ptr(__p, __d, __a).swap(*this);
4047}
4048
4049template<class _Tp, class ..._Args>
4050inline _LIBCPP_INLINE_VISIBILITY
4051typename enable_if
4052<
4053    !is_array<_Tp>::value,
4054    shared_ptr<_Tp>
4055>::type
4056make_shared(_Args&& ...__args)
4057{
4058    static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4059    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4060    typedef allocator<_CntrlBlk> _A2;
4061    typedef __allocator_destructor<_A2> _D2;
4062
4063    _A2 __a2;
4064    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4065    ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4066
4067    _Tp *__ptr = __hold2->__get_elem();
4068    return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
4069}
4070
4071template<class _Tp, class _Alloc, class ..._Args>
4072inline _LIBCPP_INLINE_VISIBILITY
4073typename enable_if
4074<
4075    !is_array<_Tp>::value,
4076    shared_ptr<_Tp>
4077>::type
4078allocate_shared(const _Alloc& __a, _Args&& ...__args)
4079{
4080    static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4081
4082    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4083    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4084    typedef __allocator_destructor<_A2> _D2;
4085
4086    _A2 __a2(__a);
4087    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4088    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4089        _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4090
4091    typename shared_ptr<_Tp>::element_type *__p = __hold2->__get_elem();
4092    return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
4093}
4094
4095template<class _Tp, class _Up>
4096inline _LIBCPP_INLINE_VISIBILITY
4097bool
4098operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4099{
4100    return __x.get() == __y.get();
4101}
4102
4103template<class _Tp, class _Up>
4104inline _LIBCPP_INLINE_VISIBILITY
4105bool
4106operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4107{
4108    return !(__x == __y);
4109}
4110
4111template<class _Tp, class _Up>
4112inline _LIBCPP_INLINE_VISIBILITY
4113bool
4114operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4115{
4116#if _LIBCPP_STD_VER <= 11
4117    typedef typename common_type<_Tp*, _Up*>::type _Vp;
4118    return less<_Vp>()(__x.get(), __y.get());
4119#else
4120    return less<>()(__x.get(), __y.get());
4121#endif
4122
4123}
4124
4125template<class _Tp, class _Up>
4126inline _LIBCPP_INLINE_VISIBILITY
4127bool
4128operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4129{
4130    return __y < __x;
4131}
4132
4133template<class _Tp, class _Up>
4134inline _LIBCPP_INLINE_VISIBILITY
4135bool
4136operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4137{
4138    return !(__y < __x);
4139}
4140
4141template<class _Tp, class _Up>
4142inline _LIBCPP_INLINE_VISIBILITY
4143bool
4144operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4145{
4146    return !(__x < __y);
4147}
4148
4149template<class _Tp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151bool
4152operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4153{
4154    return !__x;
4155}
4156
4157template<class _Tp>
4158inline _LIBCPP_INLINE_VISIBILITY
4159bool
4160operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4161{
4162    return !__x;
4163}
4164
4165template<class _Tp>
4166inline _LIBCPP_INLINE_VISIBILITY
4167bool
4168operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4169{
4170    return static_cast<bool>(__x);
4171}
4172
4173template<class _Tp>
4174inline _LIBCPP_INLINE_VISIBILITY
4175bool
4176operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4177{
4178    return static_cast<bool>(__x);
4179}
4180
4181template<class _Tp>
4182inline _LIBCPP_INLINE_VISIBILITY
4183bool
4184operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4185{
4186    return less<_Tp*>()(__x.get(), nullptr);
4187}
4188
4189template<class _Tp>
4190inline _LIBCPP_INLINE_VISIBILITY
4191bool
4192operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4193{
4194    return less<_Tp*>()(nullptr, __x.get());
4195}
4196
4197template<class _Tp>
4198inline _LIBCPP_INLINE_VISIBILITY
4199bool
4200operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4201{
4202    return nullptr < __x;
4203}
4204
4205template<class _Tp>
4206inline _LIBCPP_INLINE_VISIBILITY
4207bool
4208operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4209{
4210    return __x < nullptr;
4211}
4212
4213template<class _Tp>
4214inline _LIBCPP_INLINE_VISIBILITY
4215bool
4216operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4217{
4218    return !(nullptr < __x);
4219}
4220
4221template<class _Tp>
4222inline _LIBCPP_INLINE_VISIBILITY
4223bool
4224operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4225{
4226    return !(__x < nullptr);
4227}
4228
4229template<class _Tp>
4230inline _LIBCPP_INLINE_VISIBILITY
4231bool
4232operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4233{
4234    return !(__x < nullptr);
4235}
4236
4237template<class _Tp>
4238inline _LIBCPP_INLINE_VISIBILITY
4239bool
4240operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4241{
4242    return !(nullptr < __x);
4243}
4244
4245template<class _Tp>
4246inline _LIBCPP_INLINE_VISIBILITY
4247void
4248swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
4249{
4250    __x.swap(__y);
4251}
4252
4253template<class _Tp, class _Up>
4254inline _LIBCPP_INLINE_VISIBILITY
4255shared_ptr<_Tp>
4256static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4257{
4258    return shared_ptr<_Tp>(__r,
4259                           static_cast<
4260                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
4261}
4262
4263template<class _Tp, class _Up>
4264inline _LIBCPP_INLINE_VISIBILITY
4265shared_ptr<_Tp>
4266dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4267{
4268    typedef typename shared_ptr<_Tp>::element_type _ET;
4269    _ET* __p = dynamic_cast<_ET*>(__r.get());
4270    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4271}
4272
4273template<class _Tp, class _Up>
4274shared_ptr<_Tp>
4275const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4276{
4277    typedef typename shared_ptr<_Tp>::element_type _RTp;
4278    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
4279}
4280
4281template<class _Tp, class _Up>
4282shared_ptr<_Tp>
4283reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4284{
4285    return shared_ptr<_Tp>(__r,
4286                           reinterpret_cast<
4287                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
4288}
4289
4290#ifndef _LIBCPP_NO_RTTI
4291
4292template<class _Dp, class _Tp>
4293inline _LIBCPP_INLINE_VISIBILITY
4294_Dp*
4295get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
4296{
4297    return __p.template __get_deleter<_Dp>();
4298}
4299
4300#endif  // _LIBCPP_NO_RTTI
4301
4302template<class _Tp>
4303class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
4304{
4305public:
4306    typedef _Tp element_type;
4307private:
4308    element_type*        __ptr_;
4309    __shared_weak_count* __cntrl_;
4310
4311public:
4312    _LIBCPP_INLINE_VISIBILITY
4313    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
4314    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
4315                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4316                        _NOEXCEPT;
4317    _LIBCPP_INLINE_VISIBILITY
4318    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
4319    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
4320                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4321                         _NOEXCEPT;
4322
4323    _LIBCPP_INLINE_VISIBILITY
4324    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4325    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
4326                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4327                         _NOEXCEPT;
4328    ~weak_ptr();
4329
4330    _LIBCPP_INLINE_VISIBILITY
4331    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4332    template<class _Yp>
4333        typename enable_if
4334        <
4335            is_convertible<_Yp*, element_type*>::value,
4336            weak_ptr&
4337        >::type
4338        _LIBCPP_INLINE_VISIBILITY
4339        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4340
4341    _LIBCPP_INLINE_VISIBILITY
4342    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4343    template<class _Yp>
4344        typename enable_if
4345        <
4346            is_convertible<_Yp*, element_type*>::value,
4347            weak_ptr&
4348        >::type
4349        _LIBCPP_INLINE_VISIBILITY
4350        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4351
4352    template<class _Yp>
4353        typename enable_if
4354        <
4355            is_convertible<_Yp*, element_type*>::value,
4356            weak_ptr&
4357        >::type
4358        _LIBCPP_INLINE_VISIBILITY
4359        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
4360
4361    _LIBCPP_INLINE_VISIBILITY
4362    void swap(weak_ptr& __r) _NOEXCEPT;
4363    _LIBCPP_INLINE_VISIBILITY
4364    void reset() _NOEXCEPT;
4365
4366    _LIBCPP_INLINE_VISIBILITY
4367    long use_count() const _NOEXCEPT
4368        {return __cntrl_ ? __cntrl_->use_count() : 0;}
4369    _LIBCPP_INLINE_VISIBILITY
4370    bool expired() const _NOEXCEPT
4371        {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
4372    shared_ptr<_Tp> lock() const _NOEXCEPT;
4373    template<class _Up>
4374        _LIBCPP_INLINE_VISIBILITY
4375        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
4376        {return __cntrl_ < __r.__cntrl_;}
4377    template<class _Up>
4378        _LIBCPP_INLINE_VISIBILITY
4379        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
4380        {return __cntrl_ < __r.__cntrl_;}
4381
4382    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4383    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4384};
4385
4386#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4387template<class _Tp>
4388weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4389#endif
4390
4391template<class _Tp>
4392inline
4393_LIBCPP_CONSTEXPR
4394weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
4395    : __ptr_(nullptr),
4396      __cntrl_(nullptr)
4397{
4398}
4399
4400template<class _Tp>
4401inline
4402weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
4403    : __ptr_(__r.__ptr_),
4404      __cntrl_(__r.__cntrl_)
4405{
4406    if (__cntrl_)
4407        __cntrl_->__add_weak();
4408}
4409
4410template<class _Tp>
4411template<class _Yp>
4412inline
4413weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
4414                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4415                         _NOEXCEPT
4416    : __ptr_(__r.__ptr_),
4417      __cntrl_(__r.__cntrl_)
4418{
4419    if (__cntrl_)
4420        __cntrl_->__add_weak();
4421}
4422
4423template<class _Tp>
4424template<class _Yp>
4425inline
4426weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
4427                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4428         _NOEXCEPT
4429    : __ptr_(__r.__ptr_),
4430      __cntrl_(__r.__cntrl_)
4431{
4432    if (__cntrl_)
4433        __cntrl_->__add_weak();
4434}
4435
4436template<class _Tp>
4437inline
4438weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4439    : __ptr_(__r.__ptr_),
4440      __cntrl_(__r.__cntrl_)
4441{
4442    __r.__ptr_ = nullptr;
4443    __r.__cntrl_ = nullptr;
4444}
4445
4446template<class _Tp>
4447template<class _Yp>
4448inline
4449weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4450                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4451         _NOEXCEPT
4452    : __ptr_(__r.__ptr_),
4453      __cntrl_(__r.__cntrl_)
4454{
4455    __r.__ptr_ = nullptr;
4456    __r.__cntrl_ = nullptr;
4457}
4458
4459template<class _Tp>
4460weak_ptr<_Tp>::~weak_ptr()
4461{
4462    if (__cntrl_)
4463        __cntrl_->__release_weak();
4464}
4465
4466template<class _Tp>
4467inline
4468weak_ptr<_Tp>&
4469weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
4470{
4471    weak_ptr(__r).swap(*this);
4472    return *this;
4473}
4474
4475template<class _Tp>
4476template<class _Yp>
4477inline
4478typename enable_if
4479<
4480    is_convertible<_Yp*, _Tp*>::value,
4481    weak_ptr<_Tp>&
4482>::type
4483weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
4484{
4485    weak_ptr(__r).swap(*this);
4486    return *this;
4487}
4488
4489template<class _Tp>
4490inline
4491weak_ptr<_Tp>&
4492weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4493{
4494    weak_ptr(_VSTD::move(__r)).swap(*this);
4495    return *this;
4496}
4497
4498template<class _Tp>
4499template<class _Yp>
4500inline
4501typename enable_if
4502<
4503    is_convertible<_Yp*, _Tp*>::value,
4504    weak_ptr<_Tp>&
4505>::type
4506weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4507{
4508    weak_ptr(_VSTD::move(__r)).swap(*this);
4509    return *this;
4510}
4511
4512template<class _Tp>
4513template<class _Yp>
4514inline
4515typename enable_if
4516<
4517    is_convertible<_Yp*, _Tp*>::value,
4518    weak_ptr<_Tp>&
4519>::type
4520weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
4521{
4522    weak_ptr(__r).swap(*this);
4523    return *this;
4524}
4525
4526template<class _Tp>
4527inline
4528void
4529weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
4530{
4531    _VSTD::swap(__ptr_, __r.__ptr_);
4532    _VSTD::swap(__cntrl_, __r.__cntrl_);
4533}
4534
4535template<class _Tp>
4536inline _LIBCPP_INLINE_VISIBILITY
4537void
4538swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
4539{
4540    __x.swap(__y);
4541}
4542
4543template<class _Tp>
4544inline
4545void
4546weak_ptr<_Tp>::reset() _NOEXCEPT
4547{
4548    weak_ptr().swap(*this);
4549}
4550
4551template<class _Tp>
4552template<class _Yp>
4553shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4554                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4555    : __ptr_(__r.__ptr_),
4556      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4557{
4558    if (__cntrl_ == nullptr)
4559        __throw_bad_weak_ptr();
4560}
4561
4562template<class _Tp>
4563shared_ptr<_Tp>
4564weak_ptr<_Tp>::lock() const _NOEXCEPT
4565{
4566    shared_ptr<_Tp> __r;
4567    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4568    if (__r.__cntrl_)
4569        __r.__ptr_ = __ptr_;
4570    return __r;
4571}
4572
4573#if _LIBCPP_STD_VER > 14
4574template <class _Tp = void> struct owner_less;
4575#else
4576template <class _Tp> struct owner_less;
4577#endif
4578
4579template <class _Tp>
4580struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
4581    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
4582{
4583    typedef bool result_type;
4584    _LIBCPP_INLINE_VISIBILITY
4585    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4586        {return __x.owner_before(__y);}
4587    _LIBCPP_INLINE_VISIBILITY
4588    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4589        {return __x.owner_before(__y);}
4590    _LIBCPP_INLINE_VISIBILITY
4591    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4592        {return __x.owner_before(__y);}
4593};
4594
4595template <class _Tp>
4596struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
4597    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4598{
4599    typedef bool result_type;
4600    _LIBCPP_INLINE_VISIBILITY
4601    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4602        {return __x.owner_before(__y);}
4603    _LIBCPP_INLINE_VISIBILITY
4604    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4605        {return __x.owner_before(__y);}
4606    _LIBCPP_INLINE_VISIBILITY
4607    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4608        {return __x.owner_before(__y);}
4609};
4610
4611#if _LIBCPP_STD_VER > 14
4612template <>
4613struct _LIBCPP_TEMPLATE_VIS owner_less<void>
4614{
4615    template <class _Tp, class _Up>
4616    _LIBCPP_INLINE_VISIBILITY
4617    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
4618        {return __x.owner_before(__y);}
4619    template <class _Tp, class _Up>
4620    _LIBCPP_INLINE_VISIBILITY
4621    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
4622        {return __x.owner_before(__y);}
4623    template <class _Tp, class _Up>
4624    _LIBCPP_INLINE_VISIBILITY
4625    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
4626        {return __x.owner_before(__y);}
4627    template <class _Tp, class _Up>
4628    _LIBCPP_INLINE_VISIBILITY
4629    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
4630        {return __x.owner_before(__y);}
4631    typedef void is_transparent;
4632};
4633#endif
4634
4635template<class _Tp>
4636class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
4637{
4638    mutable weak_ptr<_Tp> __weak_this_;
4639protected:
4640    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
4641    enable_shared_from_this() _NOEXCEPT {}
4642    _LIBCPP_INLINE_VISIBILITY
4643    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
4644    _LIBCPP_INLINE_VISIBILITY
4645    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4646        {return *this;}
4647    _LIBCPP_INLINE_VISIBILITY
4648    ~enable_shared_from_this() {}
4649public:
4650    _LIBCPP_INLINE_VISIBILITY
4651    shared_ptr<_Tp> shared_from_this()
4652        {return shared_ptr<_Tp>(__weak_this_);}
4653    _LIBCPP_INLINE_VISIBILITY
4654    shared_ptr<_Tp const> shared_from_this() const
4655        {return shared_ptr<const _Tp>(__weak_this_);}
4656
4657#if _LIBCPP_STD_VER > 14
4658    _LIBCPP_INLINE_VISIBILITY
4659    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4660       { return __weak_this_; }
4661
4662    _LIBCPP_INLINE_VISIBILITY
4663    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4664        { return __weak_this_; }
4665#endif // _LIBCPP_STD_VER > 14
4666
4667    template <class _Up> friend class shared_ptr;
4668};
4669
4670template <class _Tp>
4671struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
4672{
4673    typedef shared_ptr<_Tp>      argument_type;
4674    typedef size_t               result_type;
4675
4676    _LIBCPP_INLINE_VISIBILITY
4677    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
4678    {
4679        return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
4680    }
4681};
4682
4683template<class _CharT, class _Traits, class _Yp>
4684inline _LIBCPP_INLINE_VISIBILITY
4685basic_ostream<_CharT, _Traits>&
4686operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
4687
4688
4689#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
4690
4691class _LIBCPP_TYPE_VIS __sp_mut
4692{
4693    void* __lx;
4694public:
4695    void lock() _NOEXCEPT;
4696    void unlock() _NOEXCEPT;
4697
4698private:
4699    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4700    __sp_mut(const __sp_mut&);
4701    __sp_mut& operator=(const __sp_mut&);
4702
4703    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
4704};
4705
4706_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4707__sp_mut& __get_sp_mut(const void*);
4708
4709template <class _Tp>
4710inline _LIBCPP_INLINE_VISIBILITY
4711bool
4712atomic_is_lock_free(const shared_ptr<_Tp>*)
4713{
4714    return false;
4715}
4716
4717template <class _Tp>
4718_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4719shared_ptr<_Tp>
4720atomic_load(const shared_ptr<_Tp>* __p)
4721{
4722    __sp_mut& __m = __get_sp_mut(__p);
4723    __m.lock();
4724    shared_ptr<_Tp> __q = *__p;
4725    __m.unlock();
4726    return __q;
4727}
4728
4729template <class _Tp>
4730inline _LIBCPP_INLINE_VISIBILITY
4731_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4732shared_ptr<_Tp>
4733atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4734{
4735    return atomic_load(__p);
4736}
4737
4738template <class _Tp>
4739_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4740void
4741atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4742{
4743    __sp_mut& __m = __get_sp_mut(__p);
4744    __m.lock();
4745    __p->swap(__r);
4746    __m.unlock();
4747}
4748
4749template <class _Tp>
4750inline _LIBCPP_INLINE_VISIBILITY
4751_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4752void
4753atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4754{
4755    atomic_store(__p, __r);
4756}
4757
4758template <class _Tp>
4759_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4760shared_ptr<_Tp>
4761atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4762{
4763    __sp_mut& __m = __get_sp_mut(__p);
4764    __m.lock();
4765    __p->swap(__r);
4766    __m.unlock();
4767    return __r;
4768}
4769
4770template <class _Tp>
4771inline _LIBCPP_INLINE_VISIBILITY
4772_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4773shared_ptr<_Tp>
4774atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4775{
4776    return atomic_exchange(__p, __r);
4777}
4778
4779template <class _Tp>
4780_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4781bool
4782atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4783{
4784    shared_ptr<_Tp> __temp;
4785    __sp_mut& __m = __get_sp_mut(__p);
4786    __m.lock();
4787    if (__p->__owner_equivalent(*__v))
4788    {
4789        _VSTD::swap(__temp, *__p);
4790        *__p = __w;
4791        __m.unlock();
4792        return true;
4793    }
4794    _VSTD::swap(__temp, *__v);
4795    *__v = *__p;
4796    __m.unlock();
4797    return false;
4798}
4799
4800template <class _Tp>
4801inline _LIBCPP_INLINE_VISIBILITY
4802_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4803bool
4804atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4805{
4806    return atomic_compare_exchange_strong(__p, __v, __w);
4807}
4808
4809template <class _Tp>
4810inline _LIBCPP_INLINE_VISIBILITY
4811_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4812bool
4813atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4814                                        shared_ptr<_Tp> __w, memory_order, memory_order)
4815{
4816    return atomic_compare_exchange_strong(__p, __v, __w);
4817}
4818
4819template <class _Tp>
4820inline _LIBCPP_INLINE_VISIBILITY
4821_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4822bool
4823atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4824                                      shared_ptr<_Tp> __w, memory_order, memory_order)
4825{
4826    return atomic_compare_exchange_weak(__p, __v, __w);
4827}
4828
4829#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
4830
4831//enum class
4832#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4833# ifndef _LIBCPP_CXX03_LANG
4834enum class pointer_safety : unsigned char {
4835  relaxed,
4836  preferred,
4837  strict
4838};
4839# endif
4840#else
4841struct _LIBCPP_TYPE_VIS pointer_safety
4842{
4843    enum __lx
4844    {
4845        relaxed,
4846        preferred,
4847        strict
4848    };
4849
4850    __lx __v_;
4851
4852    _LIBCPP_INLINE_VISIBILITY
4853    pointer_safety() : __v_() {}
4854
4855    _LIBCPP_INLINE_VISIBILITY
4856    pointer_safety(__lx __v) : __v_(__v) {}
4857    _LIBCPP_INLINE_VISIBILITY
4858    operator int() const {return __v_;}
4859};
4860#endif
4861
4862#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
4863    defined(_LIBCPP_BUILDING_LIBRARY)
4864_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4865#else
4866// This function is only offered in C++03 under ABI v1.
4867# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4868inline _LIBCPP_INLINE_VISIBILITY
4869pointer_safety get_pointer_safety() _NOEXCEPT {
4870  return pointer_safety::relaxed;
4871}
4872# endif
4873#endif
4874
4875
4876_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4877_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4878_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
4879_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
4880
4881template <class _Tp>
4882inline _LIBCPP_INLINE_VISIBILITY
4883_Tp*
4884undeclare_reachable(_Tp* __p)
4885{
4886    return static_cast<_Tp*>(__undeclare_reachable(__p));
4887}
4888
4889_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
4890
4891// --- Helper for container swap --
4892template <typename _Alloc>
4893_LIBCPP_INLINE_VISIBILITY
4894void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4895#if _LIBCPP_STD_VER >= 14
4896    _NOEXCEPT
4897#else
4898    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4899#endif
4900{
4901    using _VSTD::swap;
4902    swap(__a1, __a2);
4903}
4904
4905template <typename _Alloc>
4906inline _LIBCPP_INLINE_VISIBILITY
4907void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4908
4909template <typename _Alloc>
4910inline _LIBCPP_INLINE_VISIBILITY
4911void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4912#if _LIBCPP_STD_VER >= 14
4913    _NOEXCEPT
4914#else
4915    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4916#endif
4917{
4918    _VSTD::__swap_allocator(__a1, __a2,
4919      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4920}
4921
4922template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
4923struct __noexcept_move_assign_container : public integral_constant<bool,
4924    _Traits::propagate_on_container_move_assignment::value
4925#if _LIBCPP_STD_VER > 14
4926        || _Traits::is_always_equal::value
4927#else
4928        && is_nothrow_move_assignable<_Alloc>::value
4929#endif
4930    > {};
4931
4932
4933template <class _Tp, class _Alloc>
4934struct __temp_value {
4935    typedef allocator_traits<_Alloc> _Traits;
4936
4937    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
4938    _Alloc &__a;
4939
4940    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4941    _Tp &   get() { return *__addr(); }
4942
4943    template<class... _Args>
4944    _LIBCPP_NO_CFI
4945    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4946      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4947                         _VSTD::forward<_Args>(__args)...);
4948    }
4949
4950    ~__temp_value() { _Traits::destroy(__a, __addr()); }
4951    };
4952
4953template<typename _Alloc, typename = void, typename = void>
4954struct __is_allocator : false_type {};
4955
4956template<typename _Alloc>
4957struct __is_allocator<_Alloc,
4958       typename __void_t<typename _Alloc::value_type>::type,
4959       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4960     >
4961   : true_type {};
4962
4963// __builtin_new_allocator -- A non-templated helper for allocating and
4964// deallocating memory using __builtin_operator_new and
4965// __builtin_operator_delete. It should be used in preference to
4966// `std::allocator<T>` to avoid additional instantiations.
4967struct __builtin_new_allocator {
4968  struct __builtin_new_deleter {
4969    typedef void* pointer_type;
4970
4971    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4972        : __size_(__size), __align_(__align) {}
4973
4974    void operator()(void* p) const _NOEXCEPT {
4975        _VSTD::__libcpp_deallocate(p, __size_, __align_);
4976    }
4977
4978   private:
4979    size_t __size_;
4980    size_t __align_;
4981  };
4982
4983  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4984
4985  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
4986      return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
4987                     __builtin_new_deleter(__s, __align));
4988  }
4989
4990  static void __deallocate_bytes(void* __p, size_t __s,
4991                                 size_t __align) _NOEXCEPT {
4992      _VSTD::__libcpp_deallocate(__p, __s, __align);
4993  }
4994
4995  template <class _Tp>
4996  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4997  static __holder_t __allocate_type(size_t __n) {
4998      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4999  }
5000
5001  template <class _Tp>
5002  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5003  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5004      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5005  }
5006};
5007
5008
5009_LIBCPP_END_NAMESPACE_STD
5010
5011_LIBCPP_POP_MACROS
5012
5013#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5014#   include <__pstl_memory>
5015#endif
5016
5017#endif  // _LIBCPP_MEMORY
5018