xref: /freebsd/contrib/llvm-project/libcxx/include/memory (revision e40139ff33b48b56a24c808b166b04b8ee6f5b21)
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 _Yp, class _CntrlBlk>
3875    static shared_ptr<_Tp>
3876    __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3877    {
3878        shared_ptr<_Tp> __r;
3879        __r.__ptr_ = __p;
3880        __r.__cntrl_ = __cntrl;
3881        __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3882        return __r;
3883    }
3884
3885    template<class _Alloc, class ..._Args>
3886        static
3887        shared_ptr<_Tp>
3888        allocate_shared(const _Alloc& __a, _Args&& ...__args);
3889
3890private:
3891    template <class _Yp, bool = is_function<_Yp>::value>
3892        struct __shared_ptr_default_allocator
3893        {
3894            typedef allocator<_Yp> type;
3895        };
3896
3897    template <class _Yp>
3898        struct __shared_ptr_default_allocator<_Yp, true>
3899        {
3900            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3901        };
3902
3903    template <class _Yp, class _OrigPtr>
3904        _LIBCPP_INLINE_VISIBILITY
3905        typename enable_if<is_convertible<_OrigPtr*,
3906                                          const enable_shared_from_this<_Yp>*
3907        >::value,
3908            void>::type
3909        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3910                           _OrigPtr* __ptr) _NOEXCEPT
3911        {
3912            typedef typename remove_cv<_Yp>::type _RawYp;
3913            if (__e && __e->__weak_this_.expired())
3914            {
3915                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3916                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
3917            }
3918        }
3919
3920    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
3921
3922    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3923    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3924};
3925
3926
3927template<class _Tp>
3928inline
3929_LIBCPP_CONSTEXPR
3930shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
3931    : __ptr_(0),
3932      __cntrl_(0)
3933{
3934}
3935
3936template<class _Tp>
3937inline
3938_LIBCPP_CONSTEXPR
3939shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
3940    : __ptr_(0),
3941      __cntrl_(0)
3942{
3943}
3944
3945template<class _Tp>
3946template<class _Yp>
3947shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3948                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3949    : __ptr_(__p)
3950{
3951    unique_ptr<_Yp> __hold(__p);
3952    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3953    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3954    __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
3955    __hold.release();
3956    __enable_weak_this(__p, __p);
3957}
3958
3959template<class _Tp>
3960template<class _Yp, class _Dp>
3961shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3962                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3963    : __ptr_(__p)
3964{
3965#ifndef _LIBCPP_NO_EXCEPTIONS
3966    try
3967    {
3968#endif  // _LIBCPP_NO_EXCEPTIONS
3969        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3970        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3971        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3972        __enable_weak_this(__p, __p);
3973#ifndef _LIBCPP_NO_EXCEPTIONS
3974    }
3975    catch (...)
3976    {
3977        __d(__p);
3978        throw;
3979    }
3980#endif  // _LIBCPP_NO_EXCEPTIONS
3981}
3982
3983template<class _Tp>
3984template<class _Dp>
3985shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3986    : __ptr_(0)
3987{
3988#ifndef _LIBCPP_NO_EXCEPTIONS
3989    try
3990    {
3991#endif  // _LIBCPP_NO_EXCEPTIONS
3992        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3993        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3994        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3995#ifndef _LIBCPP_NO_EXCEPTIONS
3996    }
3997    catch (...)
3998    {
3999        __d(__p);
4000        throw;
4001    }
4002#endif  // _LIBCPP_NO_EXCEPTIONS
4003}
4004
4005template<class _Tp>
4006template<class _Yp, class _Dp, class _Alloc>
4007shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4008                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4009    : __ptr_(__p)
4010{
4011#ifndef _LIBCPP_NO_EXCEPTIONS
4012    try
4013    {
4014#endif  // _LIBCPP_NO_EXCEPTIONS
4015        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4016        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4017        typedef __allocator_destructor<_A2> _D2;
4018        _A2 __a2(__a);
4019        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4020        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4021            _CntrlBlk(__p, __d, __a);
4022        __cntrl_ = _VSTD::addressof(*__hold2.release());
4023        __enable_weak_this(__p, __p);
4024#ifndef _LIBCPP_NO_EXCEPTIONS
4025    }
4026    catch (...)
4027    {
4028        __d(__p);
4029        throw;
4030    }
4031#endif  // _LIBCPP_NO_EXCEPTIONS
4032}
4033
4034template<class _Tp>
4035template<class _Dp, class _Alloc>
4036shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4037    : __ptr_(0)
4038{
4039#ifndef _LIBCPP_NO_EXCEPTIONS
4040    try
4041    {
4042#endif  // _LIBCPP_NO_EXCEPTIONS
4043        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4044        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4045        typedef __allocator_destructor<_A2> _D2;
4046        _A2 __a2(__a);
4047        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4048        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4049            _CntrlBlk(__p, __d, __a);
4050        __cntrl_ = _VSTD::addressof(*__hold2.release());
4051#ifndef _LIBCPP_NO_EXCEPTIONS
4052    }
4053    catch (...)
4054    {
4055        __d(__p);
4056        throw;
4057    }
4058#endif  // _LIBCPP_NO_EXCEPTIONS
4059}
4060
4061template<class _Tp>
4062template<class _Yp>
4063inline
4064shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
4065    : __ptr_(__p),
4066      __cntrl_(__r.__cntrl_)
4067{
4068    if (__cntrl_)
4069        __cntrl_->__add_shared();
4070}
4071
4072template<class _Tp>
4073inline
4074shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
4075    : __ptr_(__r.__ptr_),
4076      __cntrl_(__r.__cntrl_)
4077{
4078    if (__cntrl_)
4079        __cntrl_->__add_shared();
4080}
4081
4082template<class _Tp>
4083template<class _Yp>
4084inline
4085shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4086                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4087         _NOEXCEPT
4088    : __ptr_(__r.__ptr_),
4089      __cntrl_(__r.__cntrl_)
4090{
4091    if (__cntrl_)
4092        __cntrl_->__add_shared();
4093}
4094
4095#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4096
4097template<class _Tp>
4098inline
4099shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
4100    : __ptr_(__r.__ptr_),
4101      __cntrl_(__r.__cntrl_)
4102{
4103    __r.__ptr_ = 0;
4104    __r.__cntrl_ = 0;
4105}
4106
4107template<class _Tp>
4108template<class _Yp>
4109inline
4110shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4111                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4112         _NOEXCEPT
4113    : __ptr_(__r.__ptr_),
4114      __cntrl_(__r.__cntrl_)
4115{
4116    __r.__ptr_ = 0;
4117    __r.__cntrl_ = 0;
4118}
4119
4120#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4121
4122#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
4123template<class _Tp>
4124template<class _Yp>
4125#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4126shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
4127#else
4128shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
4129#endif
4130                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4131    : __ptr_(__r.get())
4132{
4133    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4134    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4135    __enable_weak_this(__r.get(), __r.get());
4136    __r.release();
4137}
4138#endif
4139
4140template<class _Tp>
4141template <class _Yp, class _Dp>
4142#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4143shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4144#else
4145shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4146#endif
4147                            typename enable_if
4148                            <
4149                                !is_lvalue_reference<_Dp>::value &&
4150                                !is_array<_Yp>::value &&
4151                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4152                                __nat
4153                            >::type)
4154    : __ptr_(__r.get())
4155{
4156#if _LIBCPP_STD_VER > 11
4157    if (__ptr_ == nullptr)
4158        __cntrl_ = nullptr;
4159    else
4160#endif
4161    {
4162        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4163        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4164        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
4165        __enable_weak_this(__r.get(), __r.get());
4166    }
4167    __r.release();
4168}
4169
4170template<class _Tp>
4171template <class _Yp, class _Dp>
4172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4173shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4174#else
4175shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4176#endif
4177                            typename enable_if
4178                            <
4179                                is_lvalue_reference<_Dp>::value &&
4180                                !is_array<_Yp>::value &&
4181                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4182                                __nat
4183                            >::type)
4184    : __ptr_(__r.get())
4185{
4186#if _LIBCPP_STD_VER > 11
4187    if (__ptr_ == nullptr)
4188        __cntrl_ = nullptr;
4189    else
4190#endif
4191    {
4192        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4193        typedef __shared_ptr_pointer<_Yp*,
4194                                     reference_wrapper<typename remove_reference<_Dp>::type>,
4195                                     _AllocT > _CntrlBlk;
4196        __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
4197        __enable_weak_this(__r.get(), __r.get());
4198    }
4199    __r.release();
4200}
4201
4202template<class _Tp>
4203template<class _Alloc, class ..._Args>
4204shared_ptr<_Tp>
4205shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4206{
4207    static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
4208    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4209    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4210    typedef __allocator_destructor<_A2> _D2;
4211    _A2 __a2(__a);
4212    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4213    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4214        _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4215    shared_ptr<_Tp> __r;
4216    __r.__ptr_ = __hold2.get()->get();
4217    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4218    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4219    return __r;
4220}
4221
4222template<class _Tp>
4223shared_ptr<_Tp>::~shared_ptr()
4224{
4225    if (__cntrl_)
4226        __cntrl_->__release_shared();
4227}
4228
4229template<class _Tp>
4230inline
4231shared_ptr<_Tp>&
4232shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
4233{
4234    shared_ptr(__r).swap(*this);
4235    return *this;
4236}
4237
4238template<class _Tp>
4239template<class _Yp>
4240inline
4241typename enable_if
4242<
4243    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4244    shared_ptr<_Tp>&
4245>::type
4246shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
4247{
4248    shared_ptr(__r).swap(*this);
4249    return *this;
4250}
4251
4252#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4253
4254template<class _Tp>
4255inline
4256shared_ptr<_Tp>&
4257shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
4258{
4259    shared_ptr(_VSTD::move(__r)).swap(*this);
4260    return *this;
4261}
4262
4263template<class _Tp>
4264template<class _Yp>
4265inline
4266typename enable_if
4267<
4268    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4269    shared_ptr<_Tp>&
4270>::type
4271shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4272{
4273    shared_ptr(_VSTD::move(__r)).swap(*this);
4274    return *this;
4275}
4276
4277#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
4278template<class _Tp>
4279template<class _Yp>
4280inline
4281typename enable_if
4282<
4283    !is_array<_Yp>::value &&
4284    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4285    shared_ptr<_Tp>
4286>::type&
4287shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4288{
4289    shared_ptr(_VSTD::move(__r)).swap(*this);
4290    return *this;
4291}
4292#endif
4293
4294template<class _Tp>
4295template <class _Yp, class _Dp>
4296inline
4297typename enable_if
4298<
4299    !is_array<_Yp>::value &&
4300    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4301                   typename shared_ptr<_Tp>::element_type*>::value,
4302    shared_ptr<_Tp>&
4303>::type
4304shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4305{
4306    shared_ptr(_VSTD::move(__r)).swap(*this);
4307    return *this;
4308}
4309
4310#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4311
4312#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
4313template<class _Tp>
4314template<class _Yp>
4315inline _LIBCPP_INLINE_VISIBILITY
4316typename enable_if
4317<
4318    !is_array<_Yp>::value &&
4319    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4320    shared_ptr<_Tp>&
4321>::type
4322shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
4323{
4324    shared_ptr(__r).swap(*this);
4325    return *this;
4326}
4327#endif
4328
4329template<class _Tp>
4330template <class _Yp, class _Dp>
4331inline _LIBCPP_INLINE_VISIBILITY
4332typename enable_if
4333<
4334    !is_array<_Yp>::value &&
4335    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4336                   typename shared_ptr<_Tp>::element_type*>::value,
4337    shared_ptr<_Tp>&
4338>::type
4339shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4340{
4341    shared_ptr(_VSTD::move(__r)).swap(*this);
4342    return *this;
4343}
4344
4345#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4346
4347template<class _Tp>
4348inline
4349void
4350shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
4351{
4352    _VSTD::swap(__ptr_, __r.__ptr_);
4353    _VSTD::swap(__cntrl_, __r.__cntrl_);
4354}
4355
4356template<class _Tp>
4357inline
4358void
4359shared_ptr<_Tp>::reset() _NOEXCEPT
4360{
4361    shared_ptr().swap(*this);
4362}
4363
4364template<class _Tp>
4365template<class _Yp>
4366inline
4367typename enable_if
4368<
4369    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4370    void
4371>::type
4372shared_ptr<_Tp>::reset(_Yp* __p)
4373{
4374    shared_ptr(__p).swap(*this);
4375}
4376
4377template<class _Tp>
4378template<class _Yp, class _Dp>
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, _Dp __d)
4386{
4387    shared_ptr(__p, __d).swap(*this);
4388}
4389
4390template<class _Tp>
4391template<class _Yp, class _Dp, class _Alloc>
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, _Alloc __a)
4399{
4400    shared_ptr(__p, __d, __a).swap(*this);
4401}
4402
4403template<class _Tp, class ..._Args>
4404inline _LIBCPP_INLINE_VISIBILITY
4405typename enable_if
4406<
4407    !is_array<_Tp>::value,
4408    shared_ptr<_Tp>
4409>::type
4410make_shared(_Args&& ...__args)
4411{
4412    static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4413    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4414    typedef allocator<_CntrlBlk> _A2;
4415    typedef __allocator_destructor<_A2> _D2;
4416
4417    _A2 __a2;
4418    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4419    ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4420
4421    _Tp *__ptr = __hold2.get()->get();
4422    return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
4423}
4424
4425template<class _Tp, class _Alloc, class ..._Args>
4426inline _LIBCPP_INLINE_VISIBILITY
4427typename enable_if
4428<
4429    !is_array<_Tp>::value,
4430    shared_ptr<_Tp>
4431>::type
4432allocate_shared(const _Alloc& __a, _Args&& ...__args)
4433{
4434    return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
4435}
4436
4437template<class _Tp, class _Up>
4438inline _LIBCPP_INLINE_VISIBILITY
4439bool
4440operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4441{
4442    return __x.get() == __y.get();
4443}
4444
4445template<class _Tp, class _Up>
4446inline _LIBCPP_INLINE_VISIBILITY
4447bool
4448operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4449{
4450    return !(__x == __y);
4451}
4452
4453template<class _Tp, class _Up>
4454inline _LIBCPP_INLINE_VISIBILITY
4455bool
4456operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4457{
4458#if _LIBCPP_STD_VER <= 11
4459    typedef typename common_type<_Tp*, _Up*>::type _Vp;
4460    return less<_Vp>()(__x.get(), __y.get());
4461#else
4462    return less<>()(__x.get(), __y.get());
4463#endif
4464
4465}
4466
4467template<class _Tp, class _Up>
4468inline _LIBCPP_INLINE_VISIBILITY
4469bool
4470operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4471{
4472    return __y < __x;
4473}
4474
4475template<class _Tp, class _Up>
4476inline _LIBCPP_INLINE_VISIBILITY
4477bool
4478operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4479{
4480    return !(__y < __x);
4481}
4482
4483template<class _Tp, class _Up>
4484inline _LIBCPP_INLINE_VISIBILITY
4485bool
4486operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4487{
4488    return !(__x < __y);
4489}
4490
4491template<class _Tp>
4492inline _LIBCPP_INLINE_VISIBILITY
4493bool
4494operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4495{
4496    return !__x;
4497}
4498
4499template<class _Tp>
4500inline _LIBCPP_INLINE_VISIBILITY
4501bool
4502operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4503{
4504    return !__x;
4505}
4506
4507template<class _Tp>
4508inline _LIBCPP_INLINE_VISIBILITY
4509bool
4510operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4511{
4512    return static_cast<bool>(__x);
4513}
4514
4515template<class _Tp>
4516inline _LIBCPP_INLINE_VISIBILITY
4517bool
4518operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4519{
4520    return static_cast<bool>(__x);
4521}
4522
4523template<class _Tp>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
4526operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4527{
4528    return less<_Tp*>()(__x.get(), nullptr);
4529}
4530
4531template<class _Tp>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
4534operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4535{
4536    return less<_Tp*>()(nullptr, __x.get());
4537}
4538
4539template<class _Tp>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
4542operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4543{
4544    return nullptr < __x;
4545}
4546
4547template<class _Tp>
4548inline _LIBCPP_INLINE_VISIBILITY
4549bool
4550operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4551{
4552    return __x < nullptr;
4553}
4554
4555template<class _Tp>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
4558operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4559{
4560    return !(nullptr < __x);
4561}
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565bool
4566operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4567{
4568    return !(__x < nullptr);
4569}
4570
4571template<class _Tp>
4572inline _LIBCPP_INLINE_VISIBILITY
4573bool
4574operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4575{
4576    return !(__x < nullptr);
4577}
4578
4579template<class _Tp>
4580inline _LIBCPP_INLINE_VISIBILITY
4581bool
4582operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4583{
4584    return !(nullptr < __x);
4585}
4586
4587template<class _Tp>
4588inline _LIBCPP_INLINE_VISIBILITY
4589void
4590swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
4591{
4592    __x.swap(__y);
4593}
4594
4595template<class _Tp, class _Up>
4596inline _LIBCPP_INLINE_VISIBILITY
4597typename enable_if
4598<
4599    !is_array<_Tp>::value && !is_array<_Up>::value,
4600    shared_ptr<_Tp>
4601>::type
4602static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4603{
4604    return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4605}
4606
4607template<class _Tp, class _Up>
4608inline _LIBCPP_INLINE_VISIBILITY
4609typename enable_if
4610<
4611    !is_array<_Tp>::value && !is_array<_Up>::value,
4612    shared_ptr<_Tp>
4613>::type
4614dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4615{
4616    _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4617    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4618}
4619
4620template<class _Tp, class _Up>
4621typename enable_if
4622<
4623    is_array<_Tp>::value == is_array<_Up>::value,
4624    shared_ptr<_Tp>
4625>::type
4626const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4627{
4628    typedef typename remove_extent<_Tp>::type _RTp;
4629    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
4630}
4631
4632#ifndef _LIBCPP_NO_RTTI
4633
4634template<class _Dp, class _Tp>
4635inline _LIBCPP_INLINE_VISIBILITY
4636_Dp*
4637get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
4638{
4639    return __p.template __get_deleter<_Dp>();
4640}
4641
4642#endif  // _LIBCPP_NO_RTTI
4643
4644template<class _Tp>
4645class _LIBCPP_TEMPLATE_VIS weak_ptr
4646{
4647public:
4648    typedef _Tp element_type;
4649private:
4650    element_type*        __ptr_;
4651    __shared_weak_count* __cntrl_;
4652
4653public:
4654    _LIBCPP_INLINE_VISIBILITY
4655    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
4656    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
4657                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4658                        _NOEXCEPT;
4659    _LIBCPP_INLINE_VISIBILITY
4660    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
4661    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
4662                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4663                         _NOEXCEPT;
4664
4665#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4666    _LIBCPP_INLINE_VISIBILITY
4667    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4668    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
4669                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4670                         _NOEXCEPT;
4671#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4672    ~weak_ptr();
4673
4674    _LIBCPP_INLINE_VISIBILITY
4675    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4676    template<class _Yp>
4677        typename enable_if
4678        <
4679            is_convertible<_Yp*, element_type*>::value,
4680            weak_ptr&
4681        >::type
4682        _LIBCPP_INLINE_VISIBILITY
4683        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4684
4685#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4686
4687    _LIBCPP_INLINE_VISIBILITY
4688    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4689    template<class _Yp>
4690        typename enable_if
4691        <
4692            is_convertible<_Yp*, element_type*>::value,
4693            weak_ptr&
4694        >::type
4695        _LIBCPP_INLINE_VISIBILITY
4696        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4697
4698#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4699
4700    template<class _Yp>
4701        typename enable_if
4702        <
4703            is_convertible<_Yp*, element_type*>::value,
4704            weak_ptr&
4705        >::type
4706        _LIBCPP_INLINE_VISIBILITY
4707        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
4708
4709    _LIBCPP_INLINE_VISIBILITY
4710    void swap(weak_ptr& __r) _NOEXCEPT;
4711    _LIBCPP_INLINE_VISIBILITY
4712    void reset() _NOEXCEPT;
4713
4714    _LIBCPP_INLINE_VISIBILITY
4715    long use_count() const _NOEXCEPT
4716        {return __cntrl_ ? __cntrl_->use_count() : 0;}
4717    _LIBCPP_INLINE_VISIBILITY
4718    bool expired() const _NOEXCEPT
4719        {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4720    shared_ptr<_Tp> lock() const _NOEXCEPT;
4721    template<class _Up>
4722        _LIBCPP_INLINE_VISIBILITY
4723        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
4724        {return __cntrl_ < __r.__cntrl_;}
4725    template<class _Up>
4726        _LIBCPP_INLINE_VISIBILITY
4727        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
4728        {return __cntrl_ < __r.__cntrl_;}
4729
4730    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4731    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4732};
4733
4734template<class _Tp>
4735inline
4736_LIBCPP_CONSTEXPR
4737weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
4738    : __ptr_(0),
4739      __cntrl_(0)
4740{
4741}
4742
4743template<class _Tp>
4744inline
4745weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
4746    : __ptr_(__r.__ptr_),
4747      __cntrl_(__r.__cntrl_)
4748{
4749    if (__cntrl_)
4750        __cntrl_->__add_weak();
4751}
4752
4753template<class _Tp>
4754template<class _Yp>
4755inline
4756weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
4757                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4758                         _NOEXCEPT
4759    : __ptr_(__r.__ptr_),
4760      __cntrl_(__r.__cntrl_)
4761{
4762    if (__cntrl_)
4763        __cntrl_->__add_weak();
4764}
4765
4766template<class _Tp>
4767template<class _Yp>
4768inline
4769weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
4770                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4771         _NOEXCEPT
4772    : __ptr_(__r.__ptr_),
4773      __cntrl_(__r.__cntrl_)
4774{
4775    if (__cntrl_)
4776        __cntrl_->__add_weak();
4777}
4778
4779#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4780
4781template<class _Tp>
4782inline
4783weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4784    : __ptr_(__r.__ptr_),
4785      __cntrl_(__r.__cntrl_)
4786{
4787    __r.__ptr_ = 0;
4788    __r.__cntrl_ = 0;
4789}
4790
4791template<class _Tp>
4792template<class _Yp>
4793inline
4794weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4795                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4796         _NOEXCEPT
4797    : __ptr_(__r.__ptr_),
4798      __cntrl_(__r.__cntrl_)
4799{
4800    __r.__ptr_ = 0;
4801    __r.__cntrl_ = 0;
4802}
4803
4804#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4805
4806template<class _Tp>
4807weak_ptr<_Tp>::~weak_ptr()
4808{
4809    if (__cntrl_)
4810        __cntrl_->__release_weak();
4811}
4812
4813template<class _Tp>
4814inline
4815weak_ptr<_Tp>&
4816weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
4817{
4818    weak_ptr(__r).swap(*this);
4819    return *this;
4820}
4821
4822template<class _Tp>
4823template<class _Yp>
4824inline
4825typename enable_if
4826<
4827    is_convertible<_Yp*, _Tp*>::value,
4828    weak_ptr<_Tp>&
4829>::type
4830weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
4831{
4832    weak_ptr(__r).swap(*this);
4833    return *this;
4834}
4835
4836#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4837
4838template<class _Tp>
4839inline
4840weak_ptr<_Tp>&
4841weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4842{
4843    weak_ptr(_VSTD::move(__r)).swap(*this);
4844    return *this;
4845}
4846
4847template<class _Tp>
4848template<class _Yp>
4849inline
4850typename enable_if
4851<
4852    is_convertible<_Yp*, _Tp*>::value,
4853    weak_ptr<_Tp>&
4854>::type
4855weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4856{
4857    weak_ptr(_VSTD::move(__r)).swap(*this);
4858    return *this;
4859}
4860
4861#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4862
4863template<class _Tp>
4864template<class _Yp>
4865inline
4866typename enable_if
4867<
4868    is_convertible<_Yp*, _Tp*>::value,
4869    weak_ptr<_Tp>&
4870>::type
4871weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
4872{
4873    weak_ptr(__r).swap(*this);
4874    return *this;
4875}
4876
4877template<class _Tp>
4878inline
4879void
4880weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
4881{
4882    _VSTD::swap(__ptr_, __r.__ptr_);
4883    _VSTD::swap(__cntrl_, __r.__cntrl_);
4884}
4885
4886template<class _Tp>
4887inline _LIBCPP_INLINE_VISIBILITY
4888void
4889swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
4890{
4891    __x.swap(__y);
4892}
4893
4894template<class _Tp>
4895inline
4896void
4897weak_ptr<_Tp>::reset() _NOEXCEPT
4898{
4899    weak_ptr().swap(*this);
4900}
4901
4902template<class _Tp>
4903template<class _Yp>
4904shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4905                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4906    : __ptr_(__r.__ptr_),
4907      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4908{
4909    if (__cntrl_ == 0)
4910        __throw_bad_weak_ptr();
4911}
4912
4913template<class _Tp>
4914shared_ptr<_Tp>
4915weak_ptr<_Tp>::lock() const _NOEXCEPT
4916{
4917    shared_ptr<_Tp> __r;
4918    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4919    if (__r.__cntrl_)
4920        __r.__ptr_ = __ptr_;
4921    return __r;
4922}
4923
4924#if _LIBCPP_STD_VER > 14
4925template <class _Tp = void> struct owner_less;
4926#else
4927template <class _Tp> struct owner_less;
4928#endif
4929
4930template <class _Tp>
4931struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
4932    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
4933{
4934    typedef bool result_type;
4935    _LIBCPP_INLINE_VISIBILITY
4936    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4937        {return __x.owner_before(__y);}
4938    _LIBCPP_INLINE_VISIBILITY
4939    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4940        {return __x.owner_before(__y);}
4941    _LIBCPP_INLINE_VISIBILITY
4942    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4943        {return __x.owner_before(__y);}
4944};
4945
4946template <class _Tp>
4947struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
4948    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4949{
4950    typedef bool result_type;
4951    _LIBCPP_INLINE_VISIBILITY
4952    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4953        {return __x.owner_before(__y);}
4954    _LIBCPP_INLINE_VISIBILITY
4955    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4956        {return __x.owner_before(__y);}
4957    _LIBCPP_INLINE_VISIBILITY
4958    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4959        {return __x.owner_before(__y);}
4960};
4961
4962#if _LIBCPP_STD_VER > 14
4963template <>
4964struct _LIBCPP_TEMPLATE_VIS owner_less<void>
4965{
4966    template <class _Tp, class _Up>
4967    _LIBCPP_INLINE_VISIBILITY
4968    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
4969        {return __x.owner_before(__y);}
4970    template <class _Tp, class _Up>
4971    _LIBCPP_INLINE_VISIBILITY
4972    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
4973        {return __x.owner_before(__y);}
4974    template <class _Tp, class _Up>
4975    _LIBCPP_INLINE_VISIBILITY
4976    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
4977        {return __x.owner_before(__y);}
4978    template <class _Tp, class _Up>
4979    _LIBCPP_INLINE_VISIBILITY
4980    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
4981        {return __x.owner_before(__y);}
4982    typedef void is_transparent;
4983};
4984#endif
4985
4986template<class _Tp>
4987class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
4988{
4989    mutable weak_ptr<_Tp> __weak_this_;
4990protected:
4991    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
4992    enable_shared_from_this() _NOEXCEPT {}
4993    _LIBCPP_INLINE_VISIBILITY
4994    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
4995    _LIBCPP_INLINE_VISIBILITY
4996    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4997        {return *this;}
4998    _LIBCPP_INLINE_VISIBILITY
4999    ~enable_shared_from_this() {}
5000public:
5001    _LIBCPP_INLINE_VISIBILITY
5002    shared_ptr<_Tp> shared_from_this()
5003        {return shared_ptr<_Tp>(__weak_this_);}
5004    _LIBCPP_INLINE_VISIBILITY
5005    shared_ptr<_Tp const> shared_from_this() const
5006        {return shared_ptr<const _Tp>(__weak_this_);}
5007
5008#if _LIBCPP_STD_VER > 14
5009    _LIBCPP_INLINE_VISIBILITY
5010    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5011       { return __weak_this_; }
5012
5013    _LIBCPP_INLINE_VISIBILITY
5014    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5015        { return __weak_this_; }
5016#endif // _LIBCPP_STD_VER > 14
5017
5018    template <class _Up> friend class shared_ptr;
5019};
5020
5021template <class _Tp>
5022struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
5023{
5024    typedef shared_ptr<_Tp>      argument_type;
5025    typedef size_t               result_type;
5026
5027    _LIBCPP_INLINE_VISIBILITY
5028    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
5029    {
5030        return hash<_Tp*>()(__ptr.get());
5031    }
5032};
5033
5034template<class _CharT, class _Traits, class _Yp>
5035inline _LIBCPP_INLINE_VISIBILITY
5036basic_ostream<_CharT, _Traits>&
5037operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
5038
5039
5040#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
5041
5042class _LIBCPP_TYPE_VIS __sp_mut
5043{
5044    void* __lx;
5045public:
5046    void lock() _NOEXCEPT;
5047    void unlock() _NOEXCEPT;
5048
5049private:
5050    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5051    __sp_mut(const __sp_mut&);
5052    __sp_mut& operator=(const __sp_mut&);
5053
5054    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
5055};
5056
5057_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5058__sp_mut& __get_sp_mut(const void*);
5059
5060template <class _Tp>
5061inline _LIBCPP_INLINE_VISIBILITY
5062bool
5063atomic_is_lock_free(const shared_ptr<_Tp>*)
5064{
5065    return false;
5066}
5067
5068template <class _Tp>
5069_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5070shared_ptr<_Tp>
5071atomic_load(const shared_ptr<_Tp>* __p)
5072{
5073    __sp_mut& __m = __get_sp_mut(__p);
5074    __m.lock();
5075    shared_ptr<_Tp> __q = *__p;
5076    __m.unlock();
5077    return __q;
5078}
5079
5080template <class _Tp>
5081inline _LIBCPP_INLINE_VISIBILITY
5082_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5083shared_ptr<_Tp>
5084atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5085{
5086    return atomic_load(__p);
5087}
5088
5089template <class _Tp>
5090_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5091void
5092atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5093{
5094    __sp_mut& __m = __get_sp_mut(__p);
5095    __m.lock();
5096    __p->swap(__r);
5097    __m.unlock();
5098}
5099
5100template <class _Tp>
5101inline _LIBCPP_INLINE_VISIBILITY
5102_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5103void
5104atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5105{
5106    atomic_store(__p, __r);
5107}
5108
5109template <class _Tp>
5110_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5111shared_ptr<_Tp>
5112atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5113{
5114    __sp_mut& __m = __get_sp_mut(__p);
5115    __m.lock();
5116    __p->swap(__r);
5117    __m.unlock();
5118    return __r;
5119}
5120
5121template <class _Tp>
5122inline _LIBCPP_INLINE_VISIBILITY
5123_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5124shared_ptr<_Tp>
5125atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5126{
5127    return atomic_exchange(__p, __r);
5128}
5129
5130template <class _Tp>
5131_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5132bool
5133atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5134{
5135    shared_ptr<_Tp> __temp;
5136    __sp_mut& __m = __get_sp_mut(__p);
5137    __m.lock();
5138    if (__p->__owner_equivalent(*__v))
5139    {
5140        _VSTD::swap(__temp, *__p);
5141        *__p = __w;
5142        __m.unlock();
5143        return true;
5144    }
5145    _VSTD::swap(__temp, *__v);
5146    *__v = *__p;
5147    __m.unlock();
5148    return false;
5149}
5150
5151template <class _Tp>
5152inline _LIBCPP_INLINE_VISIBILITY
5153_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5154bool
5155atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5156{
5157    return atomic_compare_exchange_strong(__p, __v, __w);
5158}
5159
5160template <class _Tp>
5161inline _LIBCPP_INLINE_VISIBILITY
5162_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5163bool
5164atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5165                                        shared_ptr<_Tp> __w, memory_order, memory_order)
5166{
5167    return atomic_compare_exchange_strong(__p, __v, __w);
5168}
5169
5170template <class _Tp>
5171inline _LIBCPP_INLINE_VISIBILITY
5172_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5173bool
5174atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5175                                      shared_ptr<_Tp> __w, memory_order, memory_order)
5176{
5177    return atomic_compare_exchange_weak(__p, __v, __w);
5178}
5179
5180#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
5181
5182//enum class
5183#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5184# ifndef _LIBCPP_CXX03_LANG
5185enum class pointer_safety : unsigned char {
5186  relaxed,
5187  preferred,
5188  strict
5189};
5190# endif
5191#else
5192struct _LIBCPP_TYPE_VIS pointer_safety
5193{
5194    enum __lx
5195    {
5196        relaxed,
5197        preferred,
5198        strict
5199    };
5200
5201    __lx __v_;
5202
5203    _LIBCPP_INLINE_VISIBILITY
5204    pointer_safety() : __v_() {}
5205
5206    _LIBCPP_INLINE_VISIBILITY
5207    pointer_safety(__lx __v) : __v_(__v) {}
5208    _LIBCPP_INLINE_VISIBILITY
5209    operator int() const {return __v_;}
5210};
5211#endif
5212
5213#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5214    defined(_LIBCPP_BUILDING_LIBRARY)
5215_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5216#else
5217// This function is only offered in C++03 under ABI v1.
5218# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5219inline _LIBCPP_INLINE_VISIBILITY
5220pointer_safety get_pointer_safety() _NOEXCEPT {
5221  return pointer_safety::relaxed;
5222}
5223# endif
5224#endif
5225
5226
5227_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5228_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5229_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5230_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
5231
5232template <class _Tp>
5233inline _LIBCPP_INLINE_VISIBILITY
5234_Tp*
5235undeclare_reachable(_Tp* __p)
5236{
5237    return static_cast<_Tp*>(__undeclare_reachable(__p));
5238}
5239
5240_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
5241
5242// --- Helper for container swap --
5243template <typename _Alloc>
5244inline _LIBCPP_INLINE_VISIBILITY
5245void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5246#if _LIBCPP_STD_VER >= 14
5247    _NOEXCEPT
5248#else
5249    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5250#endif
5251{
5252    __swap_allocator(__a1, __a2,
5253      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5254}
5255
5256template <typename _Alloc>
5257_LIBCPP_INLINE_VISIBILITY
5258void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5259#if _LIBCPP_STD_VER >= 14
5260    _NOEXCEPT
5261#else
5262    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5263#endif
5264{
5265    using _VSTD::swap;
5266    swap(__a1, __a2);
5267}
5268
5269template <typename _Alloc>
5270inline _LIBCPP_INLINE_VISIBILITY
5271void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5272
5273template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
5274struct __noexcept_move_assign_container : public integral_constant<bool,
5275    _Traits::propagate_on_container_move_assignment::value
5276#if _LIBCPP_STD_VER > 14
5277        || _Traits::is_always_equal::value
5278#else
5279        && is_nothrow_move_assignable<_Alloc>::value
5280#endif
5281    > {};
5282
5283
5284#ifndef _LIBCPP_HAS_NO_VARIADICS
5285template <class _Tp, class _Alloc>
5286struct __temp_value {
5287    typedef allocator_traits<_Alloc> _Traits;
5288
5289    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
5290    _Alloc &__a;
5291
5292    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5293    _Tp &   get() { return *__addr(); }
5294
5295    template<class... _Args>
5296    _LIBCPP_NO_CFI
5297    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5298      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5299                         _VSTD::forward<_Args>(__args)...);
5300    }
5301
5302    ~__temp_value() { _Traits::destroy(__a, __addr()); }
5303    };
5304#endif
5305
5306template<typename _Alloc, typename = void, typename = void>
5307struct __is_allocator : false_type {};
5308
5309template<typename _Alloc>
5310struct __is_allocator<_Alloc,
5311       typename __void_t<typename _Alloc::value_type>::type,
5312       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5313     >
5314   : true_type {};
5315
5316// __builtin_new_allocator -- A non-templated helper for allocating and
5317// deallocating memory using __builtin_operator_new and
5318// __builtin_operator_delete. It should be used in preference to
5319// `std::allocator<T>` to avoid additional instantiations.
5320struct __builtin_new_allocator {
5321  struct __builtin_new_deleter {
5322    typedef void* pointer_type;
5323
5324    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5325        : __size_(__size), __align_(__align) {}
5326
5327    void operator()(void* p) const _NOEXCEPT {
5328        std::__libcpp_deallocate(p, __size_, __align_);
5329    }
5330
5331   private:
5332    size_t __size_;
5333    size_t __align_;
5334  };
5335
5336  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5337
5338  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5339      return __holder_t(std::__libcpp_allocate(__s, __align),
5340                     __builtin_new_deleter(__s, __align));
5341  }
5342
5343  static void __deallocate_bytes(void* __p, size_t __s,
5344                                 size_t __align) _NOEXCEPT {
5345      std::__libcpp_deallocate(__p, __s, __align);
5346  }
5347
5348  template <class _Tp>
5349  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5350  static __holder_t __allocate_type(size_t __n) {
5351      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5352  }
5353
5354  template <class _Tp>
5355  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5356  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5357      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5358  }
5359};
5360
5361
5362_LIBCPP_END_NAMESPACE_STD
5363
5364_LIBCPP_POP_MACROS
5365
5366#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5367#   include <__pstl_memory>
5368#endif
5369
5370#endif  // _LIBCPP_MEMORY
5371