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