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