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