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