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