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