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