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