xref: /llvm-project-15.0.7/libcxx/include/memory (revision a852cf66)
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> 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(std::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 std::__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
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> : std::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> : std::true_type {};
1377template <class _Alloc>
1378struct __is_cpp17_move_insertable<_Alloc, false> : std::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    std::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            std::align_val_t __al =
1846                std::align_val_t(std::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_ = 0;
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 instantated 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(std::forward<_U1>(__t1)), _Base2(std::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 std::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(_Tp*) _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(std::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(std::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    _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
3337};
3338
3339template <class _Tp, class _Alloc>
3340void
3341__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
3342{
3343    __data_.second().~_Tp();
3344}
3345
3346template <class _Tp, class _Alloc>
3347void
3348__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
3349{
3350    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3351    typedef allocator_traits<_Al> _ATraits;
3352    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3353    _Al __a(__data_.first());
3354    __data_.first().~_Alloc();
3355    __a.deallocate(_PTraits::pointer_to(*this), 1);
3356}
3357
3358struct __shared_ptr_dummy_rebind_allocator_type;
3359template <>
3360class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3361{
3362public:
3363    template <class _Other>
3364    struct rebind
3365    {
3366        typedef allocator<_Other> other;
3367    };
3368};
3369
3370template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
3371
3372template<class _Tp, class _Up>
3373struct __compatible_with
3374#if _LIBCPP_STD_VER > 14
3375    : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3376#else
3377    : is_convertible<_Tp*, _Up*> {};
3378#endif // _LIBCPP_STD_VER > 14
3379
3380#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3381#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3382#else
3383#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3384#endif
3385
3386template<class _Tp>
3387class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
3388{
3389public:
3390#if _LIBCPP_STD_VER > 14
3391    typedef weak_ptr<_Tp> weak_type;
3392    typedef remove_extent_t<_Tp> element_type;
3393#else
3394    typedef _Tp element_type;
3395#endif
3396
3397private:
3398    element_type*      __ptr_;
3399    __shared_weak_count* __cntrl_;
3400
3401    struct __nat {int __for_bool_;};
3402public:
3403    _LIBCPP_INLINE_VISIBILITY
3404    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3405    _LIBCPP_INLINE_VISIBILITY
3406    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
3407    template<class _Yp>
3408        explicit shared_ptr(_Yp* __p,
3409                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
3410    template<class _Yp, class _Dp>
3411        shared_ptr(_Yp* __p, _Dp __d,
3412                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
3413    template<class _Yp, class _Dp, class _Alloc>
3414        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3415                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
3416    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3417    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
3418    template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3419    _LIBCPP_INLINE_VISIBILITY
3420    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
3421    template<class _Yp>
3422        _LIBCPP_INLINE_VISIBILITY
3423        shared_ptr(const shared_ptr<_Yp>& __r,
3424                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
3425                       _NOEXCEPT;
3426    _LIBCPP_INLINE_VISIBILITY
3427    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
3428    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
3429                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
3430                       _NOEXCEPT;
3431    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
3432                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
3433#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3434    template<class _Yp>
3435        shared_ptr(auto_ptr<_Yp>&& __r,
3436                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3437#endif
3438    template <class _Yp, class _Dp>
3439        shared_ptr(unique_ptr<_Yp, _Dp>&&,
3440                   typename enable_if
3441                   <
3442                       !is_lvalue_reference<_Dp>::value &&
3443                       !is_array<_Yp>::value &&
3444                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3445                       __nat
3446                   >::type = __nat());
3447    template <class _Yp, class _Dp>
3448        shared_ptr(unique_ptr<_Yp, _Dp>&&,
3449                   typename enable_if
3450                   <
3451                       is_lvalue_reference<_Dp>::value &&
3452                       !is_array<_Yp>::value &&
3453                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3454                       __nat
3455                   >::type = __nat());
3456
3457    ~shared_ptr();
3458
3459    _LIBCPP_INLINE_VISIBILITY
3460    shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3461    template<class _Yp>
3462        typename enable_if
3463        <
3464            __compatible_with<_Yp, element_type>::value,
3465            shared_ptr&
3466        >::type
3467        _LIBCPP_INLINE_VISIBILITY
3468        operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
3469    _LIBCPP_INLINE_VISIBILITY
3470    shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
3471    template<class _Yp>
3472        typename enable_if
3473        <
3474            __compatible_with<_Yp, element_type>::value,
3475            shared_ptr&
3476        >::type
3477        _LIBCPP_INLINE_VISIBILITY
3478        operator=(shared_ptr<_Yp>&& __r);
3479#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3480    template<class _Yp>
3481        _LIBCPP_INLINE_VISIBILITY
3482        typename enable_if
3483        <
3484            !is_array<_Yp>::value &&
3485            is_convertible<_Yp*, element_type*>::value,
3486            shared_ptr
3487        >::type&
3488        operator=(auto_ptr<_Yp>&& __r);
3489#endif
3490    template <class _Yp, class _Dp>
3491        typename enable_if
3492        <
3493            !is_array<_Yp>::value &&
3494            is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3495            shared_ptr&
3496        >::type
3497        _LIBCPP_INLINE_VISIBILITY
3498        operator=(unique_ptr<_Yp, _Dp>&& __r);
3499
3500    _LIBCPP_INLINE_VISIBILITY
3501    void swap(shared_ptr& __r) _NOEXCEPT;
3502    _LIBCPP_INLINE_VISIBILITY
3503    void reset() _NOEXCEPT;
3504    template<class _Yp>
3505        typename enable_if
3506        <
3507            __compatible_with<_Yp, element_type>::value,
3508            void
3509        >::type
3510        _LIBCPP_INLINE_VISIBILITY
3511        reset(_Yp* __p);
3512    template<class _Yp, class _Dp>
3513        typename enable_if
3514        <
3515            __compatible_with<_Yp, element_type>::value,
3516            void
3517        >::type
3518        _LIBCPP_INLINE_VISIBILITY
3519        reset(_Yp* __p, _Dp __d);
3520    template<class _Yp, class _Dp, class _Alloc>
3521        typename enable_if
3522        <
3523            __compatible_with<_Yp, element_type>::value,
3524            void
3525        >::type
3526        _LIBCPP_INLINE_VISIBILITY
3527        reset(_Yp* __p, _Dp __d, _Alloc __a);
3528
3529    _LIBCPP_INLINE_VISIBILITY
3530    element_type* get() const _NOEXCEPT {return __ptr_;}
3531    _LIBCPP_INLINE_VISIBILITY
3532    typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3533        {return *__ptr_;}
3534    _LIBCPP_INLINE_VISIBILITY
3535    element_type* operator->() const _NOEXCEPT
3536    {
3537        static_assert(!_VSTD::is_array<_Tp>::value,
3538                      "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3539        return __ptr_;
3540    }
3541    _LIBCPP_INLINE_VISIBILITY
3542    long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
3543    _LIBCPP_INLINE_VISIBILITY
3544    bool unique() const _NOEXCEPT {return use_count() == 1;}
3545    _LIBCPP_INLINE_VISIBILITY
3546    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
3547    template <class _Up>
3548        _LIBCPP_INLINE_VISIBILITY
3549        bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
3550        {return __cntrl_ < __p.__cntrl_;}
3551    template <class _Up>
3552        _LIBCPP_INLINE_VISIBILITY
3553        bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
3554        {return __cntrl_ < __p.__cntrl_;}
3555    _LIBCPP_INLINE_VISIBILITY
3556    bool
3557    __owner_equivalent(const shared_ptr& __p) const
3558        {return __cntrl_ == __p.__cntrl_;}
3559
3560#if _LIBCPP_STD_VER > 14
3561    typename add_lvalue_reference<element_type>::type
3562    _LIBCPP_INLINE_VISIBILITY
3563    operator[](ptrdiff_t __i) const
3564    {
3565            static_assert(_VSTD::is_array<_Tp>::value,
3566                          "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3567            return __ptr_[__i];
3568    }
3569#endif
3570
3571#ifndef _LIBCPP_NO_RTTI
3572    template <class _Dp>
3573        _LIBCPP_INLINE_VISIBILITY
3574        _Dp* __get_deleter() const _NOEXCEPT
3575            {return static_cast<_Dp*>(__cntrl_
3576                    ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
3577                      : nullptr);}
3578#endif  // _LIBCPP_NO_RTTI
3579
3580    template<class _Yp, class _CntrlBlk>
3581    static shared_ptr<_Tp>
3582    __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
3583    {
3584        shared_ptr<_Tp> __r;
3585        __r.__ptr_ = __p;
3586        __r.__cntrl_ = __cntrl;
3587        __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3588        return __r;
3589    }
3590
3591private:
3592    template <class _Yp, bool = is_function<_Yp>::value>
3593        struct __shared_ptr_default_allocator
3594        {
3595            typedef allocator<_Yp> type;
3596        };
3597
3598    template <class _Yp>
3599        struct __shared_ptr_default_allocator<_Yp, true>
3600        {
3601            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3602        };
3603
3604    template <class _Yp, class _OrigPtr>
3605        _LIBCPP_INLINE_VISIBILITY
3606        typename enable_if<is_convertible<_OrigPtr*,
3607                                          const enable_shared_from_this<_Yp>*
3608        >::value,
3609            void>::type
3610        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3611                           _OrigPtr* __ptr) _NOEXCEPT
3612        {
3613            typedef typename remove_cv<_Yp>::type _RawYp;
3614            if (__e && __e->__weak_this_.expired())
3615            {
3616                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3617                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
3618            }
3619        }
3620
3621    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
3622
3623    template <class, class _Yp>
3624        struct __shared_ptr_default_delete
3625            : default_delete<_Yp> {};
3626
3627    template <class _Yp, class _Un, size_t _Sz>
3628        struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3629            : default_delete<_Yp[]> {};
3630
3631    template <class _Yp, class _Un>
3632        struct __shared_ptr_default_delete<_Yp[], _Un>
3633            : default_delete<_Yp[]> {};
3634
3635    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3636    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3637};
3638
3639#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3640template<class _Tp>
3641shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3642template<class _Tp, class _Dp>
3643shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3644#endif
3645
3646template<class _Tp>
3647inline
3648_LIBCPP_CONSTEXPR
3649shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
3650    : __ptr_(0),
3651      __cntrl_(0)
3652{
3653}
3654
3655template<class _Tp>
3656inline
3657_LIBCPP_CONSTEXPR
3658shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
3659    : __ptr_(0),
3660      __cntrl_(0)
3661{
3662}
3663
3664template<class _Tp>
3665template<class _Yp>
3666shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3667                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3668    : __ptr_(__p)
3669{
3670    unique_ptr<_Yp> __hold(__p);
3671    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3672    typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3673    __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
3674    __hold.release();
3675    __enable_weak_this(__p, __p);
3676}
3677
3678template<class _Tp>
3679template<class _Yp, class _Dp>
3680shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3681                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3682    : __ptr_(__p)
3683{
3684#ifndef _LIBCPP_NO_EXCEPTIONS
3685    try
3686    {
3687#endif  // _LIBCPP_NO_EXCEPTIONS
3688        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3689        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3690        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3691        __enable_weak_this(__p, __p);
3692#ifndef _LIBCPP_NO_EXCEPTIONS
3693    }
3694    catch (...)
3695    {
3696        __d(__p);
3697        throw;
3698    }
3699#endif  // _LIBCPP_NO_EXCEPTIONS
3700}
3701
3702template<class _Tp>
3703template<class _Dp>
3704shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3705    : __ptr_(0)
3706{
3707#ifndef _LIBCPP_NO_EXCEPTIONS
3708    try
3709    {
3710#endif  // _LIBCPP_NO_EXCEPTIONS
3711        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3712        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3713        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3714#ifndef _LIBCPP_NO_EXCEPTIONS
3715    }
3716    catch (...)
3717    {
3718        __d(__p);
3719        throw;
3720    }
3721#endif  // _LIBCPP_NO_EXCEPTIONS
3722}
3723
3724template<class _Tp>
3725template<class _Yp, class _Dp, class _Alloc>
3726shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3727                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3728    : __ptr_(__p)
3729{
3730#ifndef _LIBCPP_NO_EXCEPTIONS
3731    try
3732    {
3733#endif  // _LIBCPP_NO_EXCEPTIONS
3734        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3735        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3736        typedef __allocator_destructor<_A2> _D2;
3737        _A2 __a2(__a);
3738        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3739        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3740            _CntrlBlk(__p, __d, __a);
3741        __cntrl_ = _VSTD::addressof(*__hold2.release());
3742        __enable_weak_this(__p, __p);
3743#ifndef _LIBCPP_NO_EXCEPTIONS
3744    }
3745    catch (...)
3746    {
3747        __d(__p);
3748        throw;
3749    }
3750#endif  // _LIBCPP_NO_EXCEPTIONS
3751}
3752
3753template<class _Tp>
3754template<class _Dp, class _Alloc>
3755shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3756    : __ptr_(0)
3757{
3758#ifndef _LIBCPP_NO_EXCEPTIONS
3759    try
3760    {
3761#endif  // _LIBCPP_NO_EXCEPTIONS
3762        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3763        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3764        typedef __allocator_destructor<_A2> _D2;
3765        _A2 __a2(__a);
3766        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3767        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3768            _CntrlBlk(__p, __d, __a);
3769        __cntrl_ = _VSTD::addressof(*__hold2.release());
3770#ifndef _LIBCPP_NO_EXCEPTIONS
3771    }
3772    catch (...)
3773    {
3774        __d(__p);
3775        throw;
3776    }
3777#endif  // _LIBCPP_NO_EXCEPTIONS
3778}
3779
3780template<class _Tp>
3781template<class _Yp>
3782inline
3783shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
3784    : __ptr_(__p),
3785      __cntrl_(__r.__cntrl_)
3786{
3787    if (__cntrl_)
3788        __cntrl_->__add_shared();
3789}
3790
3791template<class _Tp>
3792inline
3793shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
3794    : __ptr_(__r.__ptr_),
3795      __cntrl_(__r.__cntrl_)
3796{
3797    if (__cntrl_)
3798        __cntrl_->__add_shared();
3799}
3800
3801template<class _Tp>
3802template<class _Yp>
3803inline
3804shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3805                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3806         _NOEXCEPT
3807    : __ptr_(__r.__ptr_),
3808      __cntrl_(__r.__cntrl_)
3809{
3810    if (__cntrl_)
3811        __cntrl_->__add_shared();
3812}
3813
3814template<class _Tp>
3815inline
3816shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
3817    : __ptr_(__r.__ptr_),
3818      __cntrl_(__r.__cntrl_)
3819{
3820    __r.__ptr_ = 0;
3821    __r.__cntrl_ = 0;
3822}
3823
3824template<class _Tp>
3825template<class _Yp>
3826inline
3827shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3828                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3829         _NOEXCEPT
3830    : __ptr_(__r.__ptr_),
3831      __cntrl_(__r.__cntrl_)
3832{
3833    __r.__ptr_ = 0;
3834    __r.__cntrl_ = 0;
3835}
3836
3837#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3838template<class _Tp>
3839template<class _Yp>
3840shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
3841                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3842    : __ptr_(__r.get())
3843{
3844    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3845    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3846    __enable_weak_this(__r.get(), __r.get());
3847    __r.release();
3848}
3849#endif
3850
3851template<class _Tp>
3852template <class _Yp, class _Dp>
3853shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3854                            typename enable_if
3855                            <
3856                                !is_lvalue_reference<_Dp>::value &&
3857                                !is_array<_Yp>::value &&
3858                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3859                                __nat
3860                            >::type)
3861    : __ptr_(__r.get())
3862{
3863#if _LIBCPP_STD_VER > 11
3864    if (__ptr_ == nullptr)
3865        __cntrl_ = nullptr;
3866    else
3867#endif
3868    {
3869        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3870        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3871        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
3872        __enable_weak_this(__r.get(), __r.get());
3873    }
3874    __r.release();
3875}
3876
3877template<class _Tp>
3878template <class _Yp, class _Dp>
3879shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3880                            typename enable_if
3881                            <
3882                                is_lvalue_reference<_Dp>::value &&
3883                                !is_array<_Yp>::value &&
3884                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3885                                __nat
3886                            >::type)
3887    : __ptr_(__r.get())
3888{
3889#if _LIBCPP_STD_VER > 11
3890    if (__ptr_ == nullptr)
3891        __cntrl_ = nullptr;
3892    else
3893#endif
3894    {
3895        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3896        typedef __shared_ptr_pointer<_Yp*,
3897                                     reference_wrapper<typename remove_reference<_Dp>::type>,
3898                                     _AllocT > _CntrlBlk;
3899        __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
3900        __enable_weak_this(__r.get(), __r.get());
3901    }
3902    __r.release();
3903}
3904
3905template<class _Tp>
3906shared_ptr<_Tp>::~shared_ptr()
3907{
3908    if (__cntrl_)
3909        __cntrl_->__release_shared();
3910}
3911
3912template<class _Tp>
3913inline
3914shared_ptr<_Tp>&
3915shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
3916{
3917    shared_ptr(__r).swap(*this);
3918    return *this;
3919}
3920
3921template<class _Tp>
3922template<class _Yp>
3923inline
3924typename enable_if
3925<
3926    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3927    shared_ptr<_Tp>&
3928>::type
3929shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
3930{
3931    shared_ptr(__r).swap(*this);
3932    return *this;
3933}
3934
3935template<class _Tp>
3936inline
3937shared_ptr<_Tp>&
3938shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
3939{
3940    shared_ptr(_VSTD::move(__r)).swap(*this);
3941    return *this;
3942}
3943
3944template<class _Tp>
3945template<class _Yp>
3946inline
3947typename enable_if
3948<
3949    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3950    shared_ptr<_Tp>&
3951>::type
3952shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3953{
3954    shared_ptr(_VSTD::move(__r)).swap(*this);
3955    return *this;
3956}
3957
3958#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3959template<class _Tp>
3960template<class _Yp>
3961inline
3962typename enable_if
3963<
3964    !is_array<_Yp>::value &&
3965    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
3966    shared_ptr<_Tp>
3967>::type&
3968shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3969{
3970    shared_ptr(_VSTD::move(__r)).swap(*this);
3971    return *this;
3972}
3973#endif
3974
3975template<class _Tp>
3976template <class _Yp, class _Dp>
3977inline
3978typename enable_if
3979<
3980    !is_array<_Yp>::value &&
3981    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
3982                   typename shared_ptr<_Tp>::element_type*>::value,
3983    shared_ptr<_Tp>&
3984>::type
3985shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3986{
3987    shared_ptr(_VSTD::move(__r)).swap(*this);
3988    return *this;
3989}
3990
3991template<class _Tp>
3992inline
3993void
3994shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
3995{
3996    _VSTD::swap(__ptr_, __r.__ptr_);
3997    _VSTD::swap(__cntrl_, __r.__cntrl_);
3998}
3999
4000template<class _Tp>
4001inline
4002void
4003shared_ptr<_Tp>::reset() _NOEXCEPT
4004{
4005    shared_ptr().swap(*this);
4006}
4007
4008template<class _Tp>
4009template<class _Yp>
4010inline
4011typename enable_if
4012<
4013    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
4014    void
4015>::type
4016shared_ptr<_Tp>::reset(_Yp* __p)
4017{
4018    shared_ptr(__p).swap(*this);
4019}
4020
4021template<class _Tp>
4022template<class _Yp, class _Dp>
4023inline
4024typename enable_if
4025<
4026    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
4027    void
4028>::type
4029shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4030{
4031    shared_ptr(__p, __d).swap(*this);
4032}
4033
4034template<class _Tp>
4035template<class _Yp, class _Dp, class _Alloc>
4036inline
4037typename enable_if
4038<
4039    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
4040    void
4041>::type
4042shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4043{
4044    shared_ptr(__p, __d, __a).swap(*this);
4045}
4046
4047template<class _Tp, class ..._Args>
4048inline _LIBCPP_INLINE_VISIBILITY
4049typename enable_if
4050<
4051    !is_array<_Tp>::value,
4052    shared_ptr<_Tp>
4053>::type
4054make_shared(_Args&& ...__args)
4055{
4056    static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4057    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4058    typedef allocator<_CntrlBlk> _A2;
4059    typedef __allocator_destructor<_A2> _D2;
4060
4061    _A2 __a2;
4062    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4063    ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4064
4065    _Tp *__ptr = __hold2.get()->get();
4066    return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
4067}
4068
4069template<class _Tp, class _Alloc, class ..._Args>
4070inline _LIBCPP_INLINE_VISIBILITY
4071typename enable_if
4072<
4073    !is_array<_Tp>::value,
4074    shared_ptr<_Tp>
4075>::type
4076allocate_shared(const _Alloc& __a, _Args&& ...__args)
4077{
4078    static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4079
4080    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4081    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4082    typedef __allocator_destructor<_A2> _D2;
4083
4084    _A2 __a2(__a);
4085    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4086    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4087        _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4088
4089    typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4090    return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
4091}
4092
4093template<class _Tp, class _Up>
4094inline _LIBCPP_INLINE_VISIBILITY
4095bool
4096operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4097{
4098    return __x.get() == __y.get();
4099}
4100
4101template<class _Tp, class _Up>
4102inline _LIBCPP_INLINE_VISIBILITY
4103bool
4104operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4105{
4106    return !(__x == __y);
4107}
4108
4109template<class _Tp, class _Up>
4110inline _LIBCPP_INLINE_VISIBILITY
4111bool
4112operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4113{
4114#if _LIBCPP_STD_VER <= 11
4115    typedef typename common_type<_Tp*, _Up*>::type _Vp;
4116    return less<_Vp>()(__x.get(), __y.get());
4117#else
4118    return less<>()(__x.get(), __y.get());
4119#endif
4120
4121}
4122
4123template<class _Tp, class _Up>
4124inline _LIBCPP_INLINE_VISIBILITY
4125bool
4126operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4127{
4128    return __y < __x;
4129}
4130
4131template<class _Tp, class _Up>
4132inline _LIBCPP_INLINE_VISIBILITY
4133bool
4134operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4135{
4136    return !(__y < __x);
4137}
4138
4139template<class _Tp, class _Up>
4140inline _LIBCPP_INLINE_VISIBILITY
4141bool
4142operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4143{
4144    return !(__x < __y);
4145}
4146
4147template<class _Tp>
4148inline _LIBCPP_INLINE_VISIBILITY
4149bool
4150operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4151{
4152    return !__x;
4153}
4154
4155template<class _Tp>
4156inline _LIBCPP_INLINE_VISIBILITY
4157bool
4158operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4159{
4160    return !__x;
4161}
4162
4163template<class _Tp>
4164inline _LIBCPP_INLINE_VISIBILITY
4165bool
4166operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4167{
4168    return static_cast<bool>(__x);
4169}
4170
4171template<class _Tp>
4172inline _LIBCPP_INLINE_VISIBILITY
4173bool
4174operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4175{
4176    return static_cast<bool>(__x);
4177}
4178
4179template<class _Tp>
4180inline _LIBCPP_INLINE_VISIBILITY
4181bool
4182operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4183{
4184    return less<_Tp*>()(__x.get(), nullptr);
4185}
4186
4187template<class _Tp>
4188inline _LIBCPP_INLINE_VISIBILITY
4189bool
4190operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4191{
4192    return less<_Tp*>()(nullptr, __x.get());
4193}
4194
4195template<class _Tp>
4196inline _LIBCPP_INLINE_VISIBILITY
4197bool
4198operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4199{
4200    return nullptr < __x;
4201}
4202
4203template<class _Tp>
4204inline _LIBCPP_INLINE_VISIBILITY
4205bool
4206operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4207{
4208    return __x < nullptr;
4209}
4210
4211template<class _Tp>
4212inline _LIBCPP_INLINE_VISIBILITY
4213bool
4214operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4215{
4216    return !(nullptr < __x);
4217}
4218
4219template<class _Tp>
4220inline _LIBCPP_INLINE_VISIBILITY
4221bool
4222operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4223{
4224    return !(__x < nullptr);
4225}
4226
4227template<class _Tp>
4228inline _LIBCPP_INLINE_VISIBILITY
4229bool
4230operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4231{
4232    return !(__x < nullptr);
4233}
4234
4235template<class _Tp>
4236inline _LIBCPP_INLINE_VISIBILITY
4237bool
4238operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4239{
4240    return !(nullptr < __x);
4241}
4242
4243template<class _Tp>
4244inline _LIBCPP_INLINE_VISIBILITY
4245void
4246swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
4247{
4248    __x.swap(__y);
4249}
4250
4251template<class _Tp, class _Up>
4252inline _LIBCPP_INLINE_VISIBILITY
4253shared_ptr<_Tp>
4254static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4255{
4256    return shared_ptr<_Tp>(__r,
4257                           static_cast<
4258                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
4259}
4260
4261template<class _Tp, class _Up>
4262inline _LIBCPP_INLINE_VISIBILITY
4263shared_ptr<_Tp>
4264dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4265{
4266    typedef typename shared_ptr<_Tp>::element_type _ET;
4267    _ET* __p = dynamic_cast<_ET*>(__r.get());
4268    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4269}
4270
4271template<class _Tp, class _Up>
4272shared_ptr<_Tp>
4273const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4274{
4275    typedef typename shared_ptr<_Tp>::element_type _RTp;
4276    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
4277}
4278
4279template<class _Tp, class _Up>
4280shared_ptr<_Tp>
4281reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4282{
4283    return shared_ptr<_Tp>(__r,
4284                           reinterpret_cast<
4285                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
4286}
4287
4288#ifndef _LIBCPP_NO_RTTI
4289
4290template<class _Dp, class _Tp>
4291inline _LIBCPP_INLINE_VISIBILITY
4292_Dp*
4293get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
4294{
4295    return __p.template __get_deleter<_Dp>();
4296}
4297
4298#endif  // _LIBCPP_NO_RTTI
4299
4300template<class _Tp>
4301class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
4302{
4303public:
4304    typedef _Tp element_type;
4305private:
4306    element_type*        __ptr_;
4307    __shared_weak_count* __cntrl_;
4308
4309public:
4310    _LIBCPP_INLINE_VISIBILITY
4311    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
4312    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
4313                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4314                        _NOEXCEPT;
4315    _LIBCPP_INLINE_VISIBILITY
4316    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
4317    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
4318                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4319                         _NOEXCEPT;
4320
4321    _LIBCPP_INLINE_VISIBILITY
4322    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4323    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
4324                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4325                         _NOEXCEPT;
4326    ~weak_ptr();
4327
4328    _LIBCPP_INLINE_VISIBILITY
4329    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4330    template<class _Yp>
4331        typename enable_if
4332        <
4333            is_convertible<_Yp*, element_type*>::value,
4334            weak_ptr&
4335        >::type
4336        _LIBCPP_INLINE_VISIBILITY
4337        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4338
4339    _LIBCPP_INLINE_VISIBILITY
4340    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4341    template<class _Yp>
4342        typename enable_if
4343        <
4344            is_convertible<_Yp*, element_type*>::value,
4345            weak_ptr&
4346        >::type
4347        _LIBCPP_INLINE_VISIBILITY
4348        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4349
4350    template<class _Yp>
4351        typename enable_if
4352        <
4353            is_convertible<_Yp*, element_type*>::value,
4354            weak_ptr&
4355        >::type
4356        _LIBCPP_INLINE_VISIBILITY
4357        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
4358
4359    _LIBCPP_INLINE_VISIBILITY
4360    void swap(weak_ptr& __r) _NOEXCEPT;
4361    _LIBCPP_INLINE_VISIBILITY
4362    void reset() _NOEXCEPT;
4363
4364    _LIBCPP_INLINE_VISIBILITY
4365    long use_count() const _NOEXCEPT
4366        {return __cntrl_ ? __cntrl_->use_count() : 0;}
4367    _LIBCPP_INLINE_VISIBILITY
4368    bool expired() const _NOEXCEPT
4369        {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4370    shared_ptr<_Tp> lock() const _NOEXCEPT;
4371    template<class _Up>
4372        _LIBCPP_INLINE_VISIBILITY
4373        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
4374        {return __cntrl_ < __r.__cntrl_;}
4375    template<class _Up>
4376        _LIBCPP_INLINE_VISIBILITY
4377        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
4378        {return __cntrl_ < __r.__cntrl_;}
4379
4380    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4381    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4382};
4383
4384#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4385template<class _Tp>
4386weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4387#endif
4388
4389template<class _Tp>
4390inline
4391_LIBCPP_CONSTEXPR
4392weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
4393    : __ptr_(0),
4394      __cntrl_(0)
4395{
4396}
4397
4398template<class _Tp>
4399inline
4400weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
4401    : __ptr_(__r.__ptr_),
4402      __cntrl_(__r.__cntrl_)
4403{
4404    if (__cntrl_)
4405        __cntrl_->__add_weak();
4406}
4407
4408template<class _Tp>
4409template<class _Yp>
4410inline
4411weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
4412                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4413                         _NOEXCEPT
4414    : __ptr_(__r.__ptr_),
4415      __cntrl_(__r.__cntrl_)
4416{
4417    if (__cntrl_)
4418        __cntrl_->__add_weak();
4419}
4420
4421template<class _Tp>
4422template<class _Yp>
4423inline
4424weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
4425                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4426         _NOEXCEPT
4427    : __ptr_(__r.__ptr_),
4428      __cntrl_(__r.__cntrl_)
4429{
4430    if (__cntrl_)
4431        __cntrl_->__add_weak();
4432}
4433
4434template<class _Tp>
4435inline
4436weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4437    : __ptr_(__r.__ptr_),
4438      __cntrl_(__r.__cntrl_)
4439{
4440    __r.__ptr_ = 0;
4441    __r.__cntrl_ = 0;
4442}
4443
4444template<class _Tp>
4445template<class _Yp>
4446inline
4447weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4448                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4449         _NOEXCEPT
4450    : __ptr_(__r.__ptr_),
4451      __cntrl_(__r.__cntrl_)
4452{
4453    __r.__ptr_ = 0;
4454    __r.__cntrl_ = 0;
4455}
4456
4457template<class _Tp>
4458weak_ptr<_Tp>::~weak_ptr()
4459{
4460    if (__cntrl_)
4461        __cntrl_->__release_weak();
4462}
4463
4464template<class _Tp>
4465inline
4466weak_ptr<_Tp>&
4467weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
4468{
4469    weak_ptr(__r).swap(*this);
4470    return *this;
4471}
4472
4473template<class _Tp>
4474template<class _Yp>
4475inline
4476typename enable_if
4477<
4478    is_convertible<_Yp*, _Tp*>::value,
4479    weak_ptr<_Tp>&
4480>::type
4481weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
4482{
4483    weak_ptr(__r).swap(*this);
4484    return *this;
4485}
4486
4487template<class _Tp>
4488inline
4489weak_ptr<_Tp>&
4490weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4491{
4492    weak_ptr(_VSTD::move(__r)).swap(*this);
4493    return *this;
4494}
4495
4496template<class _Tp>
4497template<class _Yp>
4498inline
4499typename enable_if
4500<
4501    is_convertible<_Yp*, _Tp*>::value,
4502    weak_ptr<_Tp>&
4503>::type
4504weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4505{
4506    weak_ptr(_VSTD::move(__r)).swap(*this);
4507    return *this;
4508}
4509
4510template<class _Tp>
4511template<class _Yp>
4512inline
4513typename enable_if
4514<
4515    is_convertible<_Yp*, _Tp*>::value,
4516    weak_ptr<_Tp>&
4517>::type
4518weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
4519{
4520    weak_ptr(__r).swap(*this);
4521    return *this;
4522}
4523
4524template<class _Tp>
4525inline
4526void
4527weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
4528{
4529    _VSTD::swap(__ptr_, __r.__ptr_);
4530    _VSTD::swap(__cntrl_, __r.__cntrl_);
4531}
4532
4533template<class _Tp>
4534inline _LIBCPP_INLINE_VISIBILITY
4535void
4536swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
4537{
4538    __x.swap(__y);
4539}
4540
4541template<class _Tp>
4542inline
4543void
4544weak_ptr<_Tp>::reset() _NOEXCEPT
4545{
4546    weak_ptr().swap(*this);
4547}
4548
4549template<class _Tp>
4550template<class _Yp>
4551shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4552                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4553    : __ptr_(__r.__ptr_),
4554      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4555{
4556    if (__cntrl_ == 0)
4557        __throw_bad_weak_ptr();
4558}
4559
4560template<class _Tp>
4561shared_ptr<_Tp>
4562weak_ptr<_Tp>::lock() const _NOEXCEPT
4563{
4564    shared_ptr<_Tp> __r;
4565    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4566    if (__r.__cntrl_)
4567        __r.__ptr_ = __ptr_;
4568    return __r;
4569}
4570
4571#if _LIBCPP_STD_VER > 14
4572template <class _Tp = void> struct owner_less;
4573#else
4574template <class _Tp> struct owner_less;
4575#endif
4576
4577template <class _Tp>
4578struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
4579    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
4580{
4581    typedef bool result_type;
4582    _LIBCPP_INLINE_VISIBILITY
4583    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4584        {return __x.owner_before(__y);}
4585    _LIBCPP_INLINE_VISIBILITY
4586    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4587        {return __x.owner_before(__y);}
4588    _LIBCPP_INLINE_VISIBILITY
4589    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4590        {return __x.owner_before(__y);}
4591};
4592
4593template <class _Tp>
4594struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
4595    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4596{
4597    typedef bool result_type;
4598    _LIBCPP_INLINE_VISIBILITY
4599    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4600        {return __x.owner_before(__y);}
4601    _LIBCPP_INLINE_VISIBILITY
4602    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4603        {return __x.owner_before(__y);}
4604    _LIBCPP_INLINE_VISIBILITY
4605    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4606        {return __x.owner_before(__y);}
4607};
4608
4609#if _LIBCPP_STD_VER > 14
4610template <>
4611struct _LIBCPP_TEMPLATE_VIS owner_less<void>
4612{
4613    template <class _Tp, class _Up>
4614    _LIBCPP_INLINE_VISIBILITY
4615    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
4616        {return __x.owner_before(__y);}
4617    template <class _Tp, class _Up>
4618    _LIBCPP_INLINE_VISIBILITY
4619    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
4620        {return __x.owner_before(__y);}
4621    template <class _Tp, class _Up>
4622    _LIBCPP_INLINE_VISIBILITY
4623    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
4624        {return __x.owner_before(__y);}
4625    template <class _Tp, class _Up>
4626    _LIBCPP_INLINE_VISIBILITY
4627    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
4628        {return __x.owner_before(__y);}
4629    typedef void is_transparent;
4630};
4631#endif
4632
4633template<class _Tp>
4634class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
4635{
4636    mutable weak_ptr<_Tp> __weak_this_;
4637protected:
4638    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
4639    enable_shared_from_this() _NOEXCEPT {}
4640    _LIBCPP_INLINE_VISIBILITY
4641    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
4642    _LIBCPP_INLINE_VISIBILITY
4643    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4644        {return *this;}
4645    _LIBCPP_INLINE_VISIBILITY
4646    ~enable_shared_from_this() {}
4647public:
4648    _LIBCPP_INLINE_VISIBILITY
4649    shared_ptr<_Tp> shared_from_this()
4650        {return shared_ptr<_Tp>(__weak_this_);}
4651    _LIBCPP_INLINE_VISIBILITY
4652    shared_ptr<_Tp const> shared_from_this() const
4653        {return shared_ptr<const _Tp>(__weak_this_);}
4654
4655#if _LIBCPP_STD_VER > 14
4656    _LIBCPP_INLINE_VISIBILITY
4657    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4658       { return __weak_this_; }
4659
4660    _LIBCPP_INLINE_VISIBILITY
4661    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4662        { return __weak_this_; }
4663#endif // _LIBCPP_STD_VER > 14
4664
4665    template <class _Up> friend class shared_ptr;
4666};
4667
4668template <class _Tp>
4669struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
4670{
4671    typedef shared_ptr<_Tp>      argument_type;
4672    typedef size_t               result_type;
4673
4674    _LIBCPP_INLINE_VISIBILITY
4675    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
4676    {
4677        return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
4678    }
4679};
4680
4681template<class _CharT, class _Traits, class _Yp>
4682inline _LIBCPP_INLINE_VISIBILITY
4683basic_ostream<_CharT, _Traits>&
4684operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
4685
4686
4687#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
4688
4689class _LIBCPP_TYPE_VIS __sp_mut
4690{
4691    void* __lx;
4692public:
4693    void lock() _NOEXCEPT;
4694    void unlock() _NOEXCEPT;
4695
4696private:
4697    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4698    __sp_mut(const __sp_mut&);
4699    __sp_mut& operator=(const __sp_mut&);
4700
4701    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
4702};
4703
4704_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4705__sp_mut& __get_sp_mut(const void*);
4706
4707template <class _Tp>
4708inline _LIBCPP_INLINE_VISIBILITY
4709bool
4710atomic_is_lock_free(const shared_ptr<_Tp>*)
4711{
4712    return false;
4713}
4714
4715template <class _Tp>
4716_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4717shared_ptr<_Tp>
4718atomic_load(const shared_ptr<_Tp>* __p)
4719{
4720    __sp_mut& __m = __get_sp_mut(__p);
4721    __m.lock();
4722    shared_ptr<_Tp> __q = *__p;
4723    __m.unlock();
4724    return __q;
4725}
4726
4727template <class _Tp>
4728inline _LIBCPP_INLINE_VISIBILITY
4729_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4730shared_ptr<_Tp>
4731atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4732{
4733    return atomic_load(__p);
4734}
4735
4736template <class _Tp>
4737_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4738void
4739atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4740{
4741    __sp_mut& __m = __get_sp_mut(__p);
4742    __m.lock();
4743    __p->swap(__r);
4744    __m.unlock();
4745}
4746
4747template <class _Tp>
4748inline _LIBCPP_INLINE_VISIBILITY
4749_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4750void
4751atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4752{
4753    atomic_store(__p, __r);
4754}
4755
4756template <class _Tp>
4757_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4758shared_ptr<_Tp>
4759atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4760{
4761    __sp_mut& __m = __get_sp_mut(__p);
4762    __m.lock();
4763    __p->swap(__r);
4764    __m.unlock();
4765    return __r;
4766}
4767
4768template <class _Tp>
4769inline _LIBCPP_INLINE_VISIBILITY
4770_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4771shared_ptr<_Tp>
4772atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4773{
4774    return atomic_exchange(__p, __r);
4775}
4776
4777template <class _Tp>
4778_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4779bool
4780atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4781{
4782    shared_ptr<_Tp> __temp;
4783    __sp_mut& __m = __get_sp_mut(__p);
4784    __m.lock();
4785    if (__p->__owner_equivalent(*__v))
4786    {
4787        _VSTD::swap(__temp, *__p);
4788        *__p = __w;
4789        __m.unlock();
4790        return true;
4791    }
4792    _VSTD::swap(__temp, *__v);
4793    *__v = *__p;
4794    __m.unlock();
4795    return false;
4796}
4797
4798template <class _Tp>
4799inline _LIBCPP_INLINE_VISIBILITY
4800_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4801bool
4802atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4803{
4804    return atomic_compare_exchange_strong(__p, __v, __w);
4805}
4806
4807template <class _Tp>
4808inline _LIBCPP_INLINE_VISIBILITY
4809_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4810bool
4811atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4812                                        shared_ptr<_Tp> __w, memory_order, memory_order)
4813{
4814    return atomic_compare_exchange_strong(__p, __v, __w);
4815}
4816
4817template <class _Tp>
4818inline _LIBCPP_INLINE_VISIBILITY
4819_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4820bool
4821atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4822                                      shared_ptr<_Tp> __w, memory_order, memory_order)
4823{
4824    return atomic_compare_exchange_weak(__p, __v, __w);
4825}
4826
4827#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
4828
4829//enum class
4830#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4831# ifndef _LIBCPP_CXX03_LANG
4832enum class pointer_safety : unsigned char {
4833  relaxed,
4834  preferred,
4835  strict
4836};
4837# endif
4838#else
4839struct _LIBCPP_TYPE_VIS pointer_safety
4840{
4841    enum __lx
4842    {
4843        relaxed,
4844        preferred,
4845        strict
4846    };
4847
4848    __lx __v_;
4849
4850    _LIBCPP_INLINE_VISIBILITY
4851    pointer_safety() : __v_() {}
4852
4853    _LIBCPP_INLINE_VISIBILITY
4854    pointer_safety(__lx __v) : __v_(__v) {}
4855    _LIBCPP_INLINE_VISIBILITY
4856    operator int() const {return __v_;}
4857};
4858#endif
4859
4860#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
4861    defined(_LIBCPP_BUILDING_LIBRARY)
4862_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4863#else
4864// This function is only offered in C++03 under ABI v1.
4865# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4866inline _LIBCPP_INLINE_VISIBILITY
4867pointer_safety get_pointer_safety() _NOEXCEPT {
4868  return pointer_safety::relaxed;
4869}
4870# endif
4871#endif
4872
4873
4874_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4875_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4876_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
4877_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
4878
4879template <class _Tp>
4880inline _LIBCPP_INLINE_VISIBILITY
4881_Tp*
4882undeclare_reachable(_Tp* __p)
4883{
4884    return static_cast<_Tp*>(__undeclare_reachable(__p));
4885}
4886
4887_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
4888
4889// --- Helper for container swap --
4890template <typename _Alloc>
4891inline _LIBCPP_INLINE_VISIBILITY
4892void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4893#if _LIBCPP_STD_VER >= 14
4894    _NOEXCEPT
4895#else
4896    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4897#endif
4898{
4899    __swap_allocator(__a1, __a2,
4900      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4901}
4902
4903template <typename _Alloc>
4904_LIBCPP_INLINE_VISIBILITY
4905void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4906#if _LIBCPP_STD_VER >= 14
4907    _NOEXCEPT
4908#else
4909    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4910#endif
4911{
4912    using _VSTD::swap;
4913    swap(__a1, __a2);
4914}
4915
4916template <typename _Alloc>
4917inline _LIBCPP_INLINE_VISIBILITY
4918void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4919
4920template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
4921struct __noexcept_move_assign_container : public integral_constant<bool,
4922    _Traits::propagate_on_container_move_assignment::value
4923#if _LIBCPP_STD_VER > 14
4924        || _Traits::is_always_equal::value
4925#else
4926        && is_nothrow_move_assignable<_Alloc>::value
4927#endif
4928    > {};
4929
4930
4931template <class _Tp, class _Alloc>
4932struct __temp_value {
4933    typedef allocator_traits<_Alloc> _Traits;
4934
4935    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
4936    _Alloc &__a;
4937
4938    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4939    _Tp &   get() { return *__addr(); }
4940
4941    template<class... _Args>
4942    _LIBCPP_NO_CFI
4943    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4944      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4945                         _VSTD::forward<_Args>(__args)...);
4946    }
4947
4948    ~__temp_value() { _Traits::destroy(__a, __addr()); }
4949    };
4950
4951template<typename _Alloc, typename = void, typename = void>
4952struct __is_allocator : false_type {};
4953
4954template<typename _Alloc>
4955struct __is_allocator<_Alloc,
4956       typename __void_t<typename _Alloc::value_type>::type,
4957       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4958     >
4959   : true_type {};
4960
4961// __builtin_new_allocator -- A non-templated helper for allocating and
4962// deallocating memory using __builtin_operator_new and
4963// __builtin_operator_delete. It should be used in preference to
4964// `std::allocator<T>` to avoid additional instantiations.
4965struct __builtin_new_allocator {
4966  struct __builtin_new_deleter {
4967    typedef void* pointer_type;
4968
4969    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4970        : __size_(__size), __align_(__align) {}
4971
4972    void operator()(void* p) const _NOEXCEPT {
4973        std::__libcpp_deallocate(p, __size_, __align_);
4974    }
4975
4976   private:
4977    size_t __size_;
4978    size_t __align_;
4979  };
4980
4981  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4982
4983  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
4984      return __holder_t(std::__libcpp_allocate(__s, __align),
4985                     __builtin_new_deleter(__s, __align));
4986  }
4987
4988  static void __deallocate_bytes(void* __p, size_t __s,
4989                                 size_t __align) _NOEXCEPT {
4990      std::__libcpp_deallocate(__p, __s, __align);
4991  }
4992
4993  template <class _Tp>
4994  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4995  static __holder_t __allocate_type(size_t __n) {
4996      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4997  }
4998
4999  template <class _Tp>
5000  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5001  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5002      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5003  }
5004};
5005
5006
5007_LIBCPP_END_NAMESPACE_STD
5008
5009_LIBCPP_POP_MACROS
5010
5011#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5012#   include <__pstl_memory>
5013#endif
5014
5015#endif  // _LIBCPP_MEMORY
5016