xref: /llvm-project-15.0.7/libcxx/include/memory (revision 57a2eaf3)
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) throw() : __ptr_(__p) {}
2202    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2203    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2204        : __ptr_(__p.release()) {}
2205    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2206        {reset(__p.release()); return *this;}
2207    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2208        {reset(__p.release()); return *this;}
2209    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2210        {reset(__p.__ptr_); return *this;}
2211    _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2212
2213    _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2214        {return *__ptr_;}
2215    _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2216    _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2217    _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2218    {
2219        _Tp* __t = __ptr_;
2220        __ptr_ = 0;
2221        return __t;
2222    }
2223    _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2224    {
2225        if (__ptr_ != __p)
2226            delete __ptr_;
2227        __ptr_ = __p;
2228    }
2229
2230    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2231    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2232        {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2233    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
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    virtual ~bad_weak_ptr() _NOEXCEPT;
3461    virtual const char* what() const  _NOEXCEPT;
3462};
3463
3464_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
3465void __throw_bad_weak_ptr()
3466{
3467#ifndef _LIBCPP_NO_EXCEPTIONS
3468    throw bad_weak_ptr();
3469#else
3470    _VSTD::abort();
3471#endif
3472}
3473
3474template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
3475
3476class _LIBCPP_TYPE_VIS __shared_count
3477{
3478    __shared_count(const __shared_count&);
3479    __shared_count& operator=(const __shared_count&);
3480
3481protected:
3482    long __shared_owners_;
3483    virtual ~__shared_count();
3484private:
3485    virtual void __on_zero_shared() _NOEXCEPT = 0;
3486
3487public:
3488    _LIBCPP_INLINE_VISIBILITY
3489    explicit __shared_count(long __refs = 0) _NOEXCEPT
3490        : __shared_owners_(__refs) {}
3491
3492#if defined(_LIBCPP_BUILDING_LIBRARY) && \
3493    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
3494    void __add_shared() _NOEXCEPT;
3495    bool __release_shared() _NOEXCEPT;
3496#else
3497    _LIBCPP_INLINE_VISIBILITY
3498    void __add_shared() _NOEXCEPT {
3499      __libcpp_atomic_refcount_increment(__shared_owners_);
3500    }
3501    _LIBCPP_INLINE_VISIBILITY
3502    bool __release_shared() _NOEXCEPT {
3503      if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3504        __on_zero_shared();
3505        return true;
3506      }
3507      return false;
3508    }
3509#endif
3510    _LIBCPP_INLINE_VISIBILITY
3511    long use_count() const _NOEXCEPT {
3512        return __libcpp_relaxed_load(&__shared_owners_) + 1;
3513    }
3514};
3515
3516class _LIBCPP_TYPE_VIS __shared_weak_count
3517    : private __shared_count
3518{
3519    long __shared_weak_owners_;
3520
3521public:
3522    _LIBCPP_INLINE_VISIBILITY
3523    explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
3524        : __shared_count(__refs),
3525          __shared_weak_owners_(__refs) {}
3526protected:
3527    virtual ~__shared_weak_count();
3528
3529public:
3530#if defined(_LIBCPP_BUILDING_LIBRARY) && \
3531    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
3532    void __add_shared() _NOEXCEPT;
3533    void __add_weak() _NOEXCEPT;
3534    void __release_shared() _NOEXCEPT;
3535#else
3536    _LIBCPP_INLINE_VISIBILITY
3537    void __add_shared() _NOEXCEPT {
3538      __shared_count::__add_shared();
3539    }
3540    _LIBCPP_INLINE_VISIBILITY
3541    void __add_weak() _NOEXCEPT {
3542      __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3543    }
3544    _LIBCPP_INLINE_VISIBILITY
3545    void __release_shared() _NOEXCEPT {
3546      if (__shared_count::__release_shared())
3547        __release_weak();
3548    }
3549#endif
3550    void __release_weak() _NOEXCEPT;
3551    _LIBCPP_INLINE_VISIBILITY
3552    long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3553    __shared_weak_count* lock() _NOEXCEPT;
3554
3555    // Define the function out only if we build static libc++ without RTTI.
3556    // Otherwise we may break clients who need to compile their projects with
3557    // -fno-rtti and yet link against a libc++.dylib compiled
3558    // without -fno-rtti.
3559#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
3560    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
3561#endif
3562private:
3563    virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
3564};
3565
3566template <class _Tp, class _Dp, class _Alloc>
3567class __shared_ptr_pointer
3568    : public __shared_weak_count
3569{
3570    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3571public:
3572    _LIBCPP_INLINE_VISIBILITY
3573    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
3574        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
3575
3576#ifndef _LIBCPP_NO_RTTI
3577    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
3578#endif
3579
3580private:
3581    virtual void __on_zero_shared() _NOEXCEPT;
3582    virtual void __on_zero_shared_weak() _NOEXCEPT;
3583};
3584
3585#ifndef _LIBCPP_NO_RTTI
3586
3587template <class _Tp, class _Dp, class _Alloc>
3588const void*
3589__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
3590{
3591    return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
3592}
3593
3594#endif  // _LIBCPP_NO_RTTI
3595
3596template <class _Tp, class _Dp, class _Alloc>
3597void
3598__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
3599{
3600    __data_.first().second()(__data_.first().first());
3601    __data_.first().second().~_Dp();
3602}
3603
3604template <class _Tp, class _Dp, class _Alloc>
3605void
3606__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
3607{
3608    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3609    typedef allocator_traits<_Al> _ATraits;
3610    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3611
3612    _Al __a(__data_.second());
3613    __data_.second().~_Alloc();
3614    __a.deallocate(_PTraits::pointer_to(*this), 1);
3615}
3616
3617template <class _Tp, class _Alloc>
3618class __shared_ptr_emplace
3619    : public __shared_weak_count
3620{
3621    __compressed_pair<_Alloc, _Tp> __data_;
3622public:
3623
3624    _LIBCPP_INLINE_VISIBILITY
3625    __shared_ptr_emplace(_Alloc __a)
3626        :  __data_(_VSTD::move(__a), __value_init_tag()) {}
3627
3628
3629#ifndef _LIBCPP_HAS_NO_VARIADICS
3630    template <class ..._Args>
3631        _LIBCPP_INLINE_VISIBILITY
3632        __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3633            :  __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3634                   _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
3635#else  // _LIBCPP_HAS_NO_VARIADICS
3636
3637    template <class _A0>
3638        _LIBCPP_INLINE_VISIBILITY
3639        __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3640            :  __data_(__a, _Tp(__a0)) {}
3641
3642    template <class _A0, class _A1>
3643        _LIBCPP_INLINE_VISIBILITY
3644        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3645            :  __data_(__a, _Tp(__a0, __a1)) {}
3646
3647    template <class _A0, class _A1, class _A2>
3648        _LIBCPP_INLINE_VISIBILITY
3649        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3650            :  __data_(__a, _Tp(__a0, __a1, __a2)) {}
3651
3652#endif  // _LIBCPP_HAS_NO_VARIADICS
3653
3654private:
3655    virtual void __on_zero_shared() _NOEXCEPT;
3656    virtual void __on_zero_shared_weak() _NOEXCEPT;
3657public:
3658    _LIBCPP_INLINE_VISIBILITY
3659    _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
3660};
3661
3662template <class _Tp, class _Alloc>
3663void
3664__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
3665{
3666    __data_.second().~_Tp();
3667}
3668
3669template <class _Tp, class _Alloc>
3670void
3671__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
3672{
3673    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3674    typedef allocator_traits<_Al> _ATraits;
3675    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3676    _Al __a(__data_.first());
3677    __data_.first().~_Alloc();
3678    __a.deallocate(_PTraits::pointer_to(*this), 1);
3679}
3680
3681struct __shared_ptr_dummy_rebind_allocator_type;
3682template <>
3683class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3684{
3685public:
3686    template <class _Other>
3687    struct rebind
3688    {
3689        typedef allocator<_Other> other;
3690    };
3691};
3692
3693template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
3694
3695template<class _Tp>
3696class _LIBCPP_TEMPLATE_VIS shared_ptr
3697{
3698public:
3699    typedef _Tp element_type;
3700
3701#if _LIBCPP_STD_VER > 14
3702    typedef weak_ptr<_Tp> weak_type;
3703#endif
3704private:
3705    element_type*      __ptr_;
3706    __shared_weak_count* __cntrl_;
3707
3708    struct __nat {int __for_bool_;};
3709public:
3710    _LIBCPP_INLINE_VISIBILITY
3711    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3712    _LIBCPP_INLINE_VISIBILITY
3713    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
3714    template<class _Yp>
3715        explicit shared_ptr(_Yp* __p,
3716                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3717    template<class _Yp, class _Dp>
3718        shared_ptr(_Yp* __p, _Dp __d,
3719                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3720    template<class _Yp, class _Dp, class _Alloc>
3721        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3722                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3723    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3724    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
3725    template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3726    _LIBCPP_INLINE_VISIBILITY
3727    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
3728    template<class _Yp>
3729        _LIBCPP_INLINE_VISIBILITY
3730        shared_ptr(const shared_ptr<_Yp>& __r,
3731                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
3732                       _NOEXCEPT;
3733#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3734    _LIBCPP_INLINE_VISIBILITY
3735    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
3736    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
3737                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
3738                       _NOEXCEPT;
3739#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3740    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
3741                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
3742#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3743#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3744    template<class _Yp>
3745        shared_ptr(auto_ptr<_Yp>&& __r,
3746                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3747#else
3748    template<class _Yp>
3749        shared_ptr(auto_ptr<_Yp> __r,
3750                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3751#endif
3752#endif
3753#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3754    template <class _Yp, class _Dp>
3755        shared_ptr(unique_ptr<_Yp, _Dp>&&,
3756                   typename enable_if
3757                   <
3758                       !is_lvalue_reference<_Dp>::value &&
3759                       !is_array<_Yp>::value &&
3760                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3761                       __nat
3762                   >::type = __nat());
3763    template <class _Yp, class _Dp>
3764        shared_ptr(unique_ptr<_Yp, _Dp>&&,
3765                   typename enable_if
3766                   <
3767                       is_lvalue_reference<_Dp>::value &&
3768                       !is_array<_Yp>::value &&
3769                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3770                       __nat
3771                   >::type = __nat());
3772#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3773    template <class _Yp, class _Dp>
3774        shared_ptr(unique_ptr<_Yp, _Dp>,
3775                   typename enable_if
3776                   <
3777                       !is_lvalue_reference<_Dp>::value &&
3778                       !is_array<_Yp>::value &&
3779                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3780                       __nat
3781                   >::type = __nat());
3782    template <class _Yp, class _Dp>
3783        shared_ptr(unique_ptr<_Yp, _Dp>,
3784                   typename enable_if
3785                   <
3786                       is_lvalue_reference<_Dp>::value &&
3787                       !is_array<_Yp>::value &&
3788                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3789                       __nat
3790                   >::type = __nat());
3791#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3792
3793    ~shared_ptr();
3794
3795    _LIBCPP_INLINE_VISIBILITY
3796    shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3797    template<class _Yp>
3798        typename enable_if
3799        <
3800            is_convertible<_Yp*, element_type*>::value,
3801            shared_ptr&
3802        >::type
3803        _LIBCPP_INLINE_VISIBILITY
3804        operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
3805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3806    _LIBCPP_INLINE_VISIBILITY
3807    shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
3808    template<class _Yp>
3809        typename enable_if
3810        <
3811            is_convertible<_Yp*, element_type*>::value,
3812            shared_ptr<_Tp>&
3813        >::type
3814        _LIBCPP_INLINE_VISIBILITY
3815        operator=(shared_ptr<_Yp>&& __r);
3816#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3817    template<class _Yp>
3818        _LIBCPP_INLINE_VISIBILITY
3819        typename enable_if
3820        <
3821            !is_array<_Yp>::value &&
3822            is_convertible<_Yp*, element_type*>::value,
3823            shared_ptr
3824        >::type&
3825        operator=(auto_ptr<_Yp>&& __r);
3826#endif
3827#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3828#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3829    template<class _Yp>
3830        _LIBCPP_INLINE_VISIBILITY
3831        typename enable_if
3832        <
3833            !is_array<_Yp>::value &&
3834            is_convertible<_Yp*, element_type*>::value,
3835            shared_ptr&
3836        >::type
3837        operator=(auto_ptr<_Yp> __r);
3838#endif
3839#endif
3840    template <class _Yp, class _Dp>
3841        typename enable_if
3842        <
3843            !is_array<_Yp>::value &&
3844            is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3845            shared_ptr&
3846        >::type
3847#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3848        _LIBCPP_INLINE_VISIBILITY
3849        operator=(unique_ptr<_Yp, _Dp>&& __r);
3850#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3851        _LIBCPP_INLINE_VISIBILITY
3852        operator=(unique_ptr<_Yp, _Dp> __r);
3853#endif
3854
3855    _LIBCPP_INLINE_VISIBILITY
3856    void swap(shared_ptr& __r) _NOEXCEPT;
3857    _LIBCPP_INLINE_VISIBILITY
3858    void reset() _NOEXCEPT;
3859    template<class _Yp>
3860        typename enable_if
3861        <
3862            is_convertible<_Yp*, element_type*>::value,
3863            void
3864        >::type
3865        _LIBCPP_INLINE_VISIBILITY
3866        reset(_Yp* __p);
3867    template<class _Yp, class _Dp>
3868        typename enable_if
3869        <
3870            is_convertible<_Yp*, element_type*>::value,
3871            void
3872        >::type
3873        _LIBCPP_INLINE_VISIBILITY
3874        reset(_Yp* __p, _Dp __d);
3875    template<class _Yp, class _Dp, class _Alloc>
3876        typename enable_if
3877        <
3878            is_convertible<_Yp*, element_type*>::value,
3879            void
3880        >::type
3881        _LIBCPP_INLINE_VISIBILITY
3882        reset(_Yp* __p, _Dp __d, _Alloc __a);
3883
3884    _LIBCPP_INLINE_VISIBILITY
3885    element_type* get() const _NOEXCEPT {return __ptr_;}
3886    _LIBCPP_INLINE_VISIBILITY
3887    typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3888        {return *__ptr_;}
3889    _LIBCPP_INLINE_VISIBILITY
3890    element_type* operator->() const _NOEXCEPT {return __ptr_;}
3891    _LIBCPP_INLINE_VISIBILITY
3892    long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
3893    _LIBCPP_INLINE_VISIBILITY
3894    bool unique() const _NOEXCEPT {return use_count() == 1;}
3895    _LIBCPP_INLINE_VISIBILITY
3896    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
3897    template <class _Up>
3898        _LIBCPP_INLINE_VISIBILITY
3899        bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
3900        {return __cntrl_ < __p.__cntrl_;}
3901    template <class _Up>
3902        _LIBCPP_INLINE_VISIBILITY
3903        bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
3904        {return __cntrl_ < __p.__cntrl_;}
3905    _LIBCPP_INLINE_VISIBILITY
3906    bool
3907    __owner_equivalent(const shared_ptr& __p) const
3908        {return __cntrl_ == __p.__cntrl_;}
3909
3910#ifndef _LIBCPP_NO_RTTI
3911    template <class _Dp>
3912        _LIBCPP_INLINE_VISIBILITY
3913        _Dp* __get_deleter() const _NOEXCEPT
3914            {return static_cast<_Dp*>(__cntrl_
3915                    ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
3916                      : nullptr);}
3917#endif  // _LIBCPP_NO_RTTI
3918
3919    template<class _Yp, class _CntrlBlk>
3920    static shared_ptr<_Tp>
3921    __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3922    {
3923        shared_ptr<_Tp> __r;
3924        __r.__ptr_ = __p;
3925        __r.__cntrl_ = __cntrl;
3926        __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3927        return __r;
3928    }
3929
3930private:
3931    template <class _Yp, bool = is_function<_Yp>::value>
3932        struct __shared_ptr_default_allocator
3933        {
3934            typedef allocator<_Yp> type;
3935        };
3936
3937    template <class _Yp>
3938        struct __shared_ptr_default_allocator<_Yp, true>
3939        {
3940            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3941        };
3942
3943    template <class _Yp, class _OrigPtr>
3944        _LIBCPP_INLINE_VISIBILITY
3945        typename enable_if<is_convertible<_OrigPtr*,
3946                                          const enable_shared_from_this<_Yp>*
3947        >::value,
3948            void>::type
3949        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3950                           _OrigPtr* __ptr) _NOEXCEPT
3951        {
3952            typedef typename remove_cv<_Yp>::type _RawYp;
3953            if (__e && __e->__weak_this_.expired())
3954            {
3955                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3956                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
3957            }
3958        }
3959
3960    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
3961
3962    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3963    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3964};
3965
3966
3967template<class _Tp>
3968inline
3969_LIBCPP_CONSTEXPR
3970shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
3971    : __ptr_(0),
3972      __cntrl_(0)
3973{
3974}
3975
3976template<class _Tp>
3977inline
3978_LIBCPP_CONSTEXPR
3979shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
3980    : __ptr_(0),
3981      __cntrl_(0)
3982{
3983}
3984
3985template<class _Tp>
3986template<class _Yp>
3987shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3988                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3989    : __ptr_(__p)
3990{
3991    unique_ptr<_Yp> __hold(__p);
3992    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3993    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3994    __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
3995    __hold.release();
3996    __enable_weak_this(__p, __p);
3997}
3998
3999template<class _Tp>
4000template<class _Yp, class _Dp>
4001shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4002                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4003    : __ptr_(__p)
4004{
4005#ifndef _LIBCPP_NO_EXCEPTIONS
4006    try
4007    {
4008#endif  // _LIBCPP_NO_EXCEPTIONS
4009        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4010        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4011        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
4012        __enable_weak_this(__p, __p);
4013#ifndef _LIBCPP_NO_EXCEPTIONS
4014    }
4015    catch (...)
4016    {
4017        __d(__p);
4018        throw;
4019    }
4020#endif  // _LIBCPP_NO_EXCEPTIONS
4021}
4022
4023template<class _Tp>
4024template<class _Dp>
4025shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4026    : __ptr_(0)
4027{
4028#ifndef _LIBCPP_NO_EXCEPTIONS
4029    try
4030    {
4031#endif  // _LIBCPP_NO_EXCEPTIONS
4032        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4033        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4034        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
4035#ifndef _LIBCPP_NO_EXCEPTIONS
4036    }
4037    catch (...)
4038    {
4039        __d(__p);
4040        throw;
4041    }
4042#endif  // _LIBCPP_NO_EXCEPTIONS
4043}
4044
4045template<class _Tp>
4046template<class _Yp, class _Dp, class _Alloc>
4047shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4048                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4049    : __ptr_(__p)
4050{
4051#ifndef _LIBCPP_NO_EXCEPTIONS
4052    try
4053    {
4054#endif  // _LIBCPP_NO_EXCEPTIONS
4055        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4056        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4057        typedef __allocator_destructor<_A2> _D2;
4058        _A2 __a2(__a);
4059        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4060        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4061            _CntrlBlk(__p, __d, __a);
4062        __cntrl_ = _VSTD::addressof(*__hold2.release());
4063        __enable_weak_this(__p, __p);
4064#ifndef _LIBCPP_NO_EXCEPTIONS
4065    }
4066    catch (...)
4067    {
4068        __d(__p);
4069        throw;
4070    }
4071#endif  // _LIBCPP_NO_EXCEPTIONS
4072}
4073
4074template<class _Tp>
4075template<class _Dp, class _Alloc>
4076shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4077    : __ptr_(0)
4078{
4079#ifndef _LIBCPP_NO_EXCEPTIONS
4080    try
4081    {
4082#endif  // _LIBCPP_NO_EXCEPTIONS
4083        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4084        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4085        typedef __allocator_destructor<_A2> _D2;
4086        _A2 __a2(__a);
4087        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4088        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4089            _CntrlBlk(__p, __d, __a);
4090        __cntrl_ = _VSTD::addressof(*__hold2.release());
4091#ifndef _LIBCPP_NO_EXCEPTIONS
4092    }
4093    catch (...)
4094    {
4095        __d(__p);
4096        throw;
4097    }
4098#endif  // _LIBCPP_NO_EXCEPTIONS
4099}
4100
4101template<class _Tp>
4102template<class _Yp>
4103inline
4104shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
4105    : __ptr_(__p),
4106      __cntrl_(__r.__cntrl_)
4107{
4108    if (__cntrl_)
4109        __cntrl_->__add_shared();
4110}
4111
4112template<class _Tp>
4113inline
4114shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
4115    : __ptr_(__r.__ptr_),
4116      __cntrl_(__r.__cntrl_)
4117{
4118    if (__cntrl_)
4119        __cntrl_->__add_shared();
4120}
4121
4122template<class _Tp>
4123template<class _Yp>
4124inline
4125shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4126                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4127         _NOEXCEPT
4128    : __ptr_(__r.__ptr_),
4129      __cntrl_(__r.__cntrl_)
4130{
4131    if (__cntrl_)
4132        __cntrl_->__add_shared();
4133}
4134
4135#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4136
4137template<class _Tp>
4138inline
4139shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
4140    : __ptr_(__r.__ptr_),
4141      __cntrl_(__r.__cntrl_)
4142{
4143    __r.__ptr_ = 0;
4144    __r.__cntrl_ = 0;
4145}
4146
4147template<class _Tp>
4148template<class _Yp>
4149inline
4150shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4151                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4152         _NOEXCEPT
4153    : __ptr_(__r.__ptr_),
4154      __cntrl_(__r.__cntrl_)
4155{
4156    __r.__ptr_ = 0;
4157    __r.__cntrl_ = 0;
4158}
4159
4160#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4161
4162#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
4163template<class _Tp>
4164template<class _Yp>
4165#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4166shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
4167#else
4168shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
4169#endif
4170                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4171    : __ptr_(__r.get())
4172{
4173    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4174    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4175    __enable_weak_this(__r.get(), __r.get());
4176    __r.release();
4177}
4178#endif
4179
4180template<class _Tp>
4181template <class _Yp, class _Dp>
4182#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4183shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4184#else
4185shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4186#endif
4187                            typename enable_if
4188                            <
4189                                !is_lvalue_reference<_Dp>::value &&
4190                                !is_array<_Yp>::value &&
4191                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4192                                __nat
4193                            >::type)
4194    : __ptr_(__r.get())
4195{
4196#if _LIBCPP_STD_VER > 11
4197    if (__ptr_ == nullptr)
4198        __cntrl_ = nullptr;
4199    else
4200#endif
4201    {
4202        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4203        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4204        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
4205        __enable_weak_this(__r.get(), __r.get());
4206    }
4207    __r.release();
4208}
4209
4210template<class _Tp>
4211template <class _Yp, class _Dp>
4212#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4213shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4214#else
4215shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4216#endif
4217                            typename enable_if
4218                            <
4219                                is_lvalue_reference<_Dp>::value &&
4220                                !is_array<_Yp>::value &&
4221                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4222                                __nat
4223                            >::type)
4224    : __ptr_(__r.get())
4225{
4226#if _LIBCPP_STD_VER > 11
4227    if (__ptr_ == nullptr)
4228        __cntrl_ = nullptr;
4229    else
4230#endif
4231    {
4232        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4233        typedef __shared_ptr_pointer<_Yp*,
4234                                     reference_wrapper<typename remove_reference<_Dp>::type>,
4235                                     _AllocT > _CntrlBlk;
4236        __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
4237        __enable_weak_this(__r.get(), __r.get());
4238    }
4239    __r.release();
4240}
4241
4242template<class _Tp>
4243shared_ptr<_Tp>::~shared_ptr()
4244{
4245    if (__cntrl_)
4246        __cntrl_->__release_shared();
4247}
4248
4249template<class _Tp>
4250inline
4251shared_ptr<_Tp>&
4252shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
4253{
4254    shared_ptr(__r).swap(*this);
4255    return *this;
4256}
4257
4258template<class _Tp>
4259template<class _Yp>
4260inline
4261typename enable_if
4262<
4263    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4264    shared_ptr<_Tp>&
4265>::type
4266shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
4267{
4268    shared_ptr(__r).swap(*this);
4269    return *this;
4270}
4271
4272#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4273
4274template<class _Tp>
4275inline
4276shared_ptr<_Tp>&
4277shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
4278{
4279    shared_ptr(_VSTD::move(__r)).swap(*this);
4280    return *this;
4281}
4282
4283template<class _Tp>
4284template<class _Yp>
4285inline
4286typename enable_if
4287<
4288    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4289    shared_ptr<_Tp>&
4290>::type
4291shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4292{
4293    shared_ptr(_VSTD::move(__r)).swap(*this);
4294    return *this;
4295}
4296
4297#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
4298template<class _Tp>
4299template<class _Yp>
4300inline
4301typename enable_if
4302<
4303    !is_array<_Yp>::value &&
4304    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4305    shared_ptr<_Tp>
4306>::type&
4307shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4308{
4309    shared_ptr(_VSTD::move(__r)).swap(*this);
4310    return *this;
4311}
4312#endif
4313
4314template<class _Tp>
4315template <class _Yp, class _Dp>
4316inline
4317typename enable_if
4318<
4319    !is_array<_Yp>::value &&
4320    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4321                   typename shared_ptr<_Tp>::element_type*>::value,
4322    shared_ptr<_Tp>&
4323>::type
4324shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4325{
4326    shared_ptr(_VSTD::move(__r)).swap(*this);
4327    return *this;
4328}
4329
4330#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4331
4332#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
4333template<class _Tp>
4334template<class _Yp>
4335inline _LIBCPP_INLINE_VISIBILITY
4336typename enable_if
4337<
4338    !is_array<_Yp>::value &&
4339    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4340    shared_ptr<_Tp>&
4341>::type
4342shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
4343{
4344    shared_ptr(__r).swap(*this);
4345    return *this;
4346}
4347#endif
4348
4349template<class _Tp>
4350template <class _Yp, class _Dp>
4351inline _LIBCPP_INLINE_VISIBILITY
4352typename enable_if
4353<
4354    !is_array<_Yp>::value &&
4355    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4356                   typename shared_ptr<_Tp>::element_type*>::value,
4357    shared_ptr<_Tp>&
4358>::type
4359shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4360{
4361    shared_ptr(_VSTD::move(__r)).swap(*this);
4362    return *this;
4363}
4364
4365#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4366
4367template<class _Tp>
4368inline
4369void
4370shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
4371{
4372    _VSTD::swap(__ptr_, __r.__ptr_);
4373    _VSTD::swap(__cntrl_, __r.__cntrl_);
4374}
4375
4376template<class _Tp>
4377inline
4378void
4379shared_ptr<_Tp>::reset() _NOEXCEPT
4380{
4381    shared_ptr().swap(*this);
4382}
4383
4384template<class _Tp>
4385template<class _Yp>
4386inline
4387typename enable_if
4388<
4389    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4390    void
4391>::type
4392shared_ptr<_Tp>::reset(_Yp* __p)
4393{
4394    shared_ptr(__p).swap(*this);
4395}
4396
4397template<class _Tp>
4398template<class _Yp, class _Dp>
4399inline
4400typename enable_if
4401<
4402    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4403    void
4404>::type
4405shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4406{
4407    shared_ptr(__p, __d).swap(*this);
4408}
4409
4410template<class _Tp>
4411template<class _Yp, class _Dp, class _Alloc>
4412inline
4413typename enable_if
4414<
4415    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
4416    void
4417>::type
4418shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4419{
4420    shared_ptr(__p, __d, __a).swap(*this);
4421}
4422
4423template<class _Tp, class ..._Args>
4424inline _LIBCPP_INLINE_VISIBILITY
4425typename enable_if
4426<
4427    !is_array<_Tp>::value,
4428    shared_ptr<_Tp>
4429>::type
4430make_shared(_Args&& ...__args)
4431{
4432    static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4433    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4434    typedef allocator<_CntrlBlk> _A2;
4435    typedef __allocator_destructor<_A2> _D2;
4436
4437    _A2 __a2;
4438    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4439    ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4440
4441    _Tp *__ptr = __hold2.get()->get();
4442    return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
4443}
4444
4445template<class _Tp, class _Alloc, class ..._Args>
4446inline _LIBCPP_INLINE_VISIBILITY
4447typename enable_if
4448<
4449    !is_array<_Tp>::value,
4450    shared_ptr<_Tp>
4451>::type
4452allocate_shared(const _Alloc& __a, _Args&& ...__args)
4453{
4454    static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4455
4456    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4457    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4458    typedef __allocator_destructor<_A2> _D2;
4459
4460    _A2 __a2(__a);
4461    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4462    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4463        _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4464
4465    typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4466    return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
4467}
4468
4469template<class _Tp, class _Up>
4470inline _LIBCPP_INLINE_VISIBILITY
4471bool
4472operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4473{
4474    return __x.get() == __y.get();
4475}
4476
4477template<class _Tp, class _Up>
4478inline _LIBCPP_INLINE_VISIBILITY
4479bool
4480operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4481{
4482    return !(__x == __y);
4483}
4484
4485template<class _Tp, class _Up>
4486inline _LIBCPP_INLINE_VISIBILITY
4487bool
4488operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4489{
4490#if _LIBCPP_STD_VER <= 11
4491    typedef typename common_type<_Tp*, _Up*>::type _Vp;
4492    return less<_Vp>()(__x.get(), __y.get());
4493#else
4494    return less<>()(__x.get(), __y.get());
4495#endif
4496
4497}
4498
4499template<class _Tp, class _Up>
4500inline _LIBCPP_INLINE_VISIBILITY
4501bool
4502operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4503{
4504    return __y < __x;
4505}
4506
4507template<class _Tp, class _Up>
4508inline _LIBCPP_INLINE_VISIBILITY
4509bool
4510operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4511{
4512    return !(__y < __x);
4513}
4514
4515template<class _Tp, class _Up>
4516inline _LIBCPP_INLINE_VISIBILITY
4517bool
4518operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4519{
4520    return !(__x < __y);
4521}
4522
4523template<class _Tp>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
4526operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4527{
4528    return !__x;
4529}
4530
4531template<class _Tp>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
4534operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4535{
4536    return !__x;
4537}
4538
4539template<class _Tp>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
4542operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4543{
4544    return static_cast<bool>(__x);
4545}
4546
4547template<class _Tp>
4548inline _LIBCPP_INLINE_VISIBILITY
4549bool
4550operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4551{
4552    return static_cast<bool>(__x);
4553}
4554
4555template<class _Tp>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
4558operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4559{
4560    return less<_Tp*>()(__x.get(), nullptr);
4561}
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565bool
4566operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4567{
4568    return less<_Tp*>()(nullptr, __x.get());
4569}
4570
4571template<class _Tp>
4572inline _LIBCPP_INLINE_VISIBILITY
4573bool
4574operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4575{
4576    return nullptr < __x;
4577}
4578
4579template<class _Tp>
4580inline _LIBCPP_INLINE_VISIBILITY
4581bool
4582operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4583{
4584    return __x < nullptr;
4585}
4586
4587template<class _Tp>
4588inline _LIBCPP_INLINE_VISIBILITY
4589bool
4590operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4591{
4592    return !(nullptr < __x);
4593}
4594
4595template<class _Tp>
4596inline _LIBCPP_INLINE_VISIBILITY
4597bool
4598operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4599{
4600    return !(__x < nullptr);
4601}
4602
4603template<class _Tp>
4604inline _LIBCPP_INLINE_VISIBILITY
4605bool
4606operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4607{
4608    return !(__x < nullptr);
4609}
4610
4611template<class _Tp>
4612inline _LIBCPP_INLINE_VISIBILITY
4613bool
4614operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4615{
4616    return !(nullptr < __x);
4617}
4618
4619template<class _Tp>
4620inline _LIBCPP_INLINE_VISIBILITY
4621void
4622swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
4623{
4624    __x.swap(__y);
4625}
4626
4627template<class _Tp, class _Up>
4628inline _LIBCPP_INLINE_VISIBILITY
4629typename enable_if
4630<
4631    !is_array<_Tp>::value && !is_array<_Up>::value,
4632    shared_ptr<_Tp>
4633>::type
4634static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4635{
4636    return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4637}
4638
4639template<class _Tp, class _Up>
4640inline _LIBCPP_INLINE_VISIBILITY
4641typename enable_if
4642<
4643    !is_array<_Tp>::value && !is_array<_Up>::value,
4644    shared_ptr<_Tp>
4645>::type
4646dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4647{
4648    _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4649    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4650}
4651
4652template<class _Tp, class _Up>
4653typename enable_if
4654<
4655    is_array<_Tp>::value == is_array<_Up>::value,
4656    shared_ptr<_Tp>
4657>::type
4658const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4659{
4660    typedef typename remove_extent<_Tp>::type _RTp;
4661    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
4662}
4663
4664#ifndef _LIBCPP_NO_RTTI
4665
4666template<class _Dp, class _Tp>
4667inline _LIBCPP_INLINE_VISIBILITY
4668_Dp*
4669get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
4670{
4671    return __p.template __get_deleter<_Dp>();
4672}
4673
4674#endif  // _LIBCPP_NO_RTTI
4675
4676template<class _Tp>
4677class _LIBCPP_TEMPLATE_VIS weak_ptr
4678{
4679public:
4680    typedef _Tp element_type;
4681private:
4682    element_type*        __ptr_;
4683    __shared_weak_count* __cntrl_;
4684
4685public:
4686    _LIBCPP_INLINE_VISIBILITY
4687    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
4688    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
4689                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4690                        _NOEXCEPT;
4691    _LIBCPP_INLINE_VISIBILITY
4692    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
4693    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
4694                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4695                         _NOEXCEPT;
4696
4697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4698    _LIBCPP_INLINE_VISIBILITY
4699    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4700    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
4701                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4702                         _NOEXCEPT;
4703#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4704    ~weak_ptr();
4705
4706    _LIBCPP_INLINE_VISIBILITY
4707    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4708    template<class _Yp>
4709        typename enable_if
4710        <
4711            is_convertible<_Yp*, element_type*>::value,
4712            weak_ptr&
4713        >::type
4714        _LIBCPP_INLINE_VISIBILITY
4715        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4716
4717#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4718
4719    _LIBCPP_INLINE_VISIBILITY
4720    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4721    template<class _Yp>
4722        typename enable_if
4723        <
4724            is_convertible<_Yp*, element_type*>::value,
4725            weak_ptr&
4726        >::type
4727        _LIBCPP_INLINE_VISIBILITY
4728        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4729
4730#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4731
4732    template<class _Yp>
4733        typename enable_if
4734        <
4735            is_convertible<_Yp*, element_type*>::value,
4736            weak_ptr&
4737        >::type
4738        _LIBCPP_INLINE_VISIBILITY
4739        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
4740
4741    _LIBCPP_INLINE_VISIBILITY
4742    void swap(weak_ptr& __r) _NOEXCEPT;
4743    _LIBCPP_INLINE_VISIBILITY
4744    void reset() _NOEXCEPT;
4745
4746    _LIBCPP_INLINE_VISIBILITY
4747    long use_count() const _NOEXCEPT
4748        {return __cntrl_ ? __cntrl_->use_count() : 0;}
4749    _LIBCPP_INLINE_VISIBILITY
4750    bool expired() const _NOEXCEPT
4751        {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4752    shared_ptr<_Tp> lock() const _NOEXCEPT;
4753    template<class _Up>
4754        _LIBCPP_INLINE_VISIBILITY
4755        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
4756        {return __cntrl_ < __r.__cntrl_;}
4757    template<class _Up>
4758        _LIBCPP_INLINE_VISIBILITY
4759        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
4760        {return __cntrl_ < __r.__cntrl_;}
4761
4762    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4763    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4764};
4765
4766template<class _Tp>
4767inline
4768_LIBCPP_CONSTEXPR
4769weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
4770    : __ptr_(0),
4771      __cntrl_(0)
4772{
4773}
4774
4775template<class _Tp>
4776inline
4777weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
4778    : __ptr_(__r.__ptr_),
4779      __cntrl_(__r.__cntrl_)
4780{
4781    if (__cntrl_)
4782        __cntrl_->__add_weak();
4783}
4784
4785template<class _Tp>
4786template<class _Yp>
4787inline
4788weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
4789                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4790                         _NOEXCEPT
4791    : __ptr_(__r.__ptr_),
4792      __cntrl_(__r.__cntrl_)
4793{
4794    if (__cntrl_)
4795        __cntrl_->__add_weak();
4796}
4797
4798template<class _Tp>
4799template<class _Yp>
4800inline
4801weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
4802                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4803         _NOEXCEPT
4804    : __ptr_(__r.__ptr_),
4805      __cntrl_(__r.__cntrl_)
4806{
4807    if (__cntrl_)
4808        __cntrl_->__add_weak();
4809}
4810
4811#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4812
4813template<class _Tp>
4814inline
4815weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4816    : __ptr_(__r.__ptr_),
4817      __cntrl_(__r.__cntrl_)
4818{
4819    __r.__ptr_ = 0;
4820    __r.__cntrl_ = 0;
4821}
4822
4823template<class _Tp>
4824template<class _Yp>
4825inline
4826weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4827                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4828         _NOEXCEPT
4829    : __ptr_(__r.__ptr_),
4830      __cntrl_(__r.__cntrl_)
4831{
4832    __r.__ptr_ = 0;
4833    __r.__cntrl_ = 0;
4834}
4835
4836#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4837
4838template<class _Tp>
4839weak_ptr<_Tp>::~weak_ptr()
4840{
4841    if (__cntrl_)
4842        __cntrl_->__release_weak();
4843}
4844
4845template<class _Tp>
4846inline
4847weak_ptr<_Tp>&
4848weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
4849{
4850    weak_ptr(__r).swap(*this);
4851    return *this;
4852}
4853
4854template<class _Tp>
4855template<class _Yp>
4856inline
4857typename enable_if
4858<
4859    is_convertible<_Yp*, _Tp*>::value,
4860    weak_ptr<_Tp>&
4861>::type
4862weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
4863{
4864    weak_ptr(__r).swap(*this);
4865    return *this;
4866}
4867
4868#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4869
4870template<class _Tp>
4871inline
4872weak_ptr<_Tp>&
4873weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4874{
4875    weak_ptr(_VSTD::move(__r)).swap(*this);
4876    return *this;
4877}
4878
4879template<class _Tp>
4880template<class _Yp>
4881inline
4882typename enable_if
4883<
4884    is_convertible<_Yp*, _Tp*>::value,
4885    weak_ptr<_Tp>&
4886>::type
4887weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4888{
4889    weak_ptr(_VSTD::move(__r)).swap(*this);
4890    return *this;
4891}
4892
4893#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4894
4895template<class _Tp>
4896template<class _Yp>
4897inline
4898typename enable_if
4899<
4900    is_convertible<_Yp*, _Tp*>::value,
4901    weak_ptr<_Tp>&
4902>::type
4903weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
4904{
4905    weak_ptr(__r).swap(*this);
4906    return *this;
4907}
4908
4909template<class _Tp>
4910inline
4911void
4912weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
4913{
4914    _VSTD::swap(__ptr_, __r.__ptr_);
4915    _VSTD::swap(__cntrl_, __r.__cntrl_);
4916}
4917
4918template<class _Tp>
4919inline _LIBCPP_INLINE_VISIBILITY
4920void
4921swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
4922{
4923    __x.swap(__y);
4924}
4925
4926template<class _Tp>
4927inline
4928void
4929weak_ptr<_Tp>::reset() _NOEXCEPT
4930{
4931    weak_ptr().swap(*this);
4932}
4933
4934template<class _Tp>
4935template<class _Yp>
4936shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4937                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4938    : __ptr_(__r.__ptr_),
4939      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4940{
4941    if (__cntrl_ == 0)
4942        __throw_bad_weak_ptr();
4943}
4944
4945template<class _Tp>
4946shared_ptr<_Tp>
4947weak_ptr<_Tp>::lock() const _NOEXCEPT
4948{
4949    shared_ptr<_Tp> __r;
4950    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4951    if (__r.__cntrl_)
4952        __r.__ptr_ = __ptr_;
4953    return __r;
4954}
4955
4956#if _LIBCPP_STD_VER > 14
4957template <class _Tp = void> struct owner_less;
4958#else
4959template <class _Tp> struct owner_less;
4960#endif
4961
4962template <class _Tp>
4963struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
4964    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
4965{
4966    typedef bool result_type;
4967    _LIBCPP_INLINE_VISIBILITY
4968    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4969        {return __x.owner_before(__y);}
4970    _LIBCPP_INLINE_VISIBILITY
4971    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4972        {return __x.owner_before(__y);}
4973    _LIBCPP_INLINE_VISIBILITY
4974    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4975        {return __x.owner_before(__y);}
4976};
4977
4978template <class _Tp>
4979struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
4980    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4981{
4982    typedef bool result_type;
4983    _LIBCPP_INLINE_VISIBILITY
4984    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4985        {return __x.owner_before(__y);}
4986    _LIBCPP_INLINE_VISIBILITY
4987    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
4988        {return __x.owner_before(__y);}
4989    _LIBCPP_INLINE_VISIBILITY
4990    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
4991        {return __x.owner_before(__y);}
4992};
4993
4994#if _LIBCPP_STD_VER > 14
4995template <>
4996struct _LIBCPP_TEMPLATE_VIS owner_less<void>
4997{
4998    template <class _Tp, class _Up>
4999    _LIBCPP_INLINE_VISIBILITY
5000    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
5001        {return __x.owner_before(__y);}
5002    template <class _Tp, class _Up>
5003    _LIBCPP_INLINE_VISIBILITY
5004    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
5005        {return __x.owner_before(__y);}
5006    template <class _Tp, class _Up>
5007    _LIBCPP_INLINE_VISIBILITY
5008    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
5009        {return __x.owner_before(__y);}
5010    template <class _Tp, class _Up>
5011    _LIBCPP_INLINE_VISIBILITY
5012    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
5013        {return __x.owner_before(__y);}
5014    typedef void is_transparent;
5015};
5016#endif
5017
5018template<class _Tp>
5019class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
5020{
5021    mutable weak_ptr<_Tp> __weak_this_;
5022protected:
5023    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
5024    enable_shared_from_this() _NOEXCEPT {}
5025    _LIBCPP_INLINE_VISIBILITY
5026    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
5027    _LIBCPP_INLINE_VISIBILITY
5028    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5029        {return *this;}
5030    _LIBCPP_INLINE_VISIBILITY
5031    ~enable_shared_from_this() {}
5032public:
5033    _LIBCPP_INLINE_VISIBILITY
5034    shared_ptr<_Tp> shared_from_this()
5035        {return shared_ptr<_Tp>(__weak_this_);}
5036    _LIBCPP_INLINE_VISIBILITY
5037    shared_ptr<_Tp const> shared_from_this() const
5038        {return shared_ptr<const _Tp>(__weak_this_);}
5039
5040#if _LIBCPP_STD_VER > 14
5041    _LIBCPP_INLINE_VISIBILITY
5042    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5043       { return __weak_this_; }
5044
5045    _LIBCPP_INLINE_VISIBILITY
5046    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5047        { return __weak_this_; }
5048#endif // _LIBCPP_STD_VER > 14
5049
5050    template <class _Up> friend class shared_ptr;
5051};
5052
5053template <class _Tp>
5054struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
5055{
5056    typedef shared_ptr<_Tp>      argument_type;
5057    typedef size_t               result_type;
5058
5059    _LIBCPP_INLINE_VISIBILITY
5060    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
5061    {
5062        return hash<_Tp*>()(__ptr.get());
5063    }
5064};
5065
5066template<class _CharT, class _Traits, class _Yp>
5067inline _LIBCPP_INLINE_VISIBILITY
5068basic_ostream<_CharT, _Traits>&
5069operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
5070
5071
5072#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
5073
5074class _LIBCPP_TYPE_VIS __sp_mut
5075{
5076    void* __lx;
5077public:
5078    void lock() _NOEXCEPT;
5079    void unlock() _NOEXCEPT;
5080
5081private:
5082    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5083    __sp_mut(const __sp_mut&);
5084    __sp_mut& operator=(const __sp_mut&);
5085
5086    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
5087};
5088
5089_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5090__sp_mut& __get_sp_mut(const void*);
5091
5092template <class _Tp>
5093inline _LIBCPP_INLINE_VISIBILITY
5094bool
5095atomic_is_lock_free(const shared_ptr<_Tp>*)
5096{
5097    return false;
5098}
5099
5100template <class _Tp>
5101_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5102shared_ptr<_Tp>
5103atomic_load(const shared_ptr<_Tp>* __p)
5104{
5105    __sp_mut& __m = __get_sp_mut(__p);
5106    __m.lock();
5107    shared_ptr<_Tp> __q = *__p;
5108    __m.unlock();
5109    return __q;
5110}
5111
5112template <class _Tp>
5113inline _LIBCPP_INLINE_VISIBILITY
5114_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5115shared_ptr<_Tp>
5116atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5117{
5118    return atomic_load(__p);
5119}
5120
5121template <class _Tp>
5122_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5123void
5124atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5125{
5126    __sp_mut& __m = __get_sp_mut(__p);
5127    __m.lock();
5128    __p->swap(__r);
5129    __m.unlock();
5130}
5131
5132template <class _Tp>
5133inline _LIBCPP_INLINE_VISIBILITY
5134_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5135void
5136atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5137{
5138    atomic_store(__p, __r);
5139}
5140
5141template <class _Tp>
5142_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5143shared_ptr<_Tp>
5144atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5145{
5146    __sp_mut& __m = __get_sp_mut(__p);
5147    __m.lock();
5148    __p->swap(__r);
5149    __m.unlock();
5150    return __r;
5151}
5152
5153template <class _Tp>
5154inline _LIBCPP_INLINE_VISIBILITY
5155_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5156shared_ptr<_Tp>
5157atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5158{
5159    return atomic_exchange(__p, __r);
5160}
5161
5162template <class _Tp>
5163_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5164bool
5165atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5166{
5167    shared_ptr<_Tp> __temp;
5168    __sp_mut& __m = __get_sp_mut(__p);
5169    __m.lock();
5170    if (__p->__owner_equivalent(*__v))
5171    {
5172        _VSTD::swap(__temp, *__p);
5173        *__p = __w;
5174        __m.unlock();
5175        return true;
5176    }
5177    _VSTD::swap(__temp, *__v);
5178    *__v = *__p;
5179    __m.unlock();
5180    return false;
5181}
5182
5183template <class _Tp>
5184inline _LIBCPP_INLINE_VISIBILITY
5185_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5186bool
5187atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5188{
5189    return atomic_compare_exchange_strong(__p, __v, __w);
5190}
5191
5192template <class _Tp>
5193inline _LIBCPP_INLINE_VISIBILITY
5194_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5195bool
5196atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5197                                        shared_ptr<_Tp> __w, memory_order, memory_order)
5198{
5199    return atomic_compare_exchange_strong(__p, __v, __w);
5200}
5201
5202template <class _Tp>
5203inline _LIBCPP_INLINE_VISIBILITY
5204_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5205bool
5206atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5207                                      shared_ptr<_Tp> __w, memory_order, memory_order)
5208{
5209    return atomic_compare_exchange_weak(__p, __v, __w);
5210}
5211
5212#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
5213
5214//enum class
5215#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5216# ifndef _LIBCPP_CXX03_LANG
5217enum class pointer_safety : unsigned char {
5218  relaxed,
5219  preferred,
5220  strict
5221};
5222# endif
5223#else
5224struct _LIBCPP_TYPE_VIS pointer_safety
5225{
5226    enum __lx
5227    {
5228        relaxed,
5229        preferred,
5230        strict
5231    };
5232
5233    __lx __v_;
5234
5235    _LIBCPP_INLINE_VISIBILITY
5236    pointer_safety() : __v_() {}
5237
5238    _LIBCPP_INLINE_VISIBILITY
5239    pointer_safety(__lx __v) : __v_(__v) {}
5240    _LIBCPP_INLINE_VISIBILITY
5241    operator int() const {return __v_;}
5242};
5243#endif
5244
5245#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5246    defined(_LIBCPP_BUILDING_LIBRARY)
5247_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5248#else
5249// This function is only offered in C++03 under ABI v1.
5250# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5251inline _LIBCPP_INLINE_VISIBILITY
5252pointer_safety get_pointer_safety() _NOEXCEPT {
5253  return pointer_safety::relaxed;
5254}
5255# endif
5256#endif
5257
5258
5259_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5260_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5261_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5262_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
5263
5264template <class _Tp>
5265inline _LIBCPP_INLINE_VISIBILITY
5266_Tp*
5267undeclare_reachable(_Tp* __p)
5268{
5269    return static_cast<_Tp*>(__undeclare_reachable(__p));
5270}
5271
5272_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
5273
5274// --- Helper for container swap --
5275template <typename _Alloc>
5276inline _LIBCPP_INLINE_VISIBILITY
5277void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5278#if _LIBCPP_STD_VER >= 14
5279    _NOEXCEPT
5280#else
5281    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5282#endif
5283{
5284    __swap_allocator(__a1, __a2,
5285      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5286}
5287
5288template <typename _Alloc>
5289_LIBCPP_INLINE_VISIBILITY
5290void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5291#if _LIBCPP_STD_VER >= 14
5292    _NOEXCEPT
5293#else
5294    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5295#endif
5296{
5297    using _VSTD::swap;
5298    swap(__a1, __a2);
5299}
5300
5301template <typename _Alloc>
5302inline _LIBCPP_INLINE_VISIBILITY
5303void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5304
5305template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
5306struct __noexcept_move_assign_container : public integral_constant<bool,
5307    _Traits::propagate_on_container_move_assignment::value
5308#if _LIBCPP_STD_VER > 14
5309        || _Traits::is_always_equal::value
5310#else
5311        && is_nothrow_move_assignable<_Alloc>::value
5312#endif
5313    > {};
5314
5315
5316#ifndef _LIBCPP_HAS_NO_VARIADICS
5317template <class _Tp, class _Alloc>
5318struct __temp_value {
5319    typedef allocator_traits<_Alloc> _Traits;
5320
5321    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
5322    _Alloc &__a;
5323
5324    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5325    _Tp &   get() { return *__addr(); }
5326
5327    template<class... _Args>
5328    _LIBCPP_NO_CFI
5329    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5330      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5331                         _VSTD::forward<_Args>(__args)...);
5332    }
5333
5334    ~__temp_value() { _Traits::destroy(__a, __addr()); }
5335    };
5336#endif
5337
5338template<typename _Alloc, typename = void, typename = void>
5339struct __is_allocator : false_type {};
5340
5341template<typename _Alloc>
5342struct __is_allocator<_Alloc,
5343       typename __void_t<typename _Alloc::value_type>::type,
5344       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5345     >
5346   : true_type {};
5347
5348// __builtin_new_allocator -- A non-templated helper for allocating and
5349// deallocating memory using __builtin_operator_new and
5350// __builtin_operator_delete. It should be used in preference to
5351// `std::allocator<T>` to avoid additional instantiations.
5352struct __builtin_new_allocator {
5353  struct __builtin_new_deleter {
5354    typedef void* pointer_type;
5355
5356    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5357        : __size_(__size), __align_(__align) {}
5358
5359    void operator()(void* p) const _NOEXCEPT {
5360        std::__libcpp_deallocate(p, __size_, __align_);
5361    }
5362
5363   private:
5364    size_t __size_;
5365    size_t __align_;
5366  };
5367
5368  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5369
5370  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5371      return __holder_t(std::__libcpp_allocate(__s, __align),
5372                     __builtin_new_deleter(__s, __align));
5373  }
5374
5375  static void __deallocate_bytes(void* __p, size_t __s,
5376                                 size_t __align) _NOEXCEPT {
5377      std::__libcpp_deallocate(__p, __s, __align);
5378  }
5379
5380  template <class _Tp>
5381  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5382  static __holder_t __allocate_type(size_t __n) {
5383      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5384  }
5385
5386  template <class _Tp>
5387  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5388  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5389      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5390  }
5391};
5392
5393
5394_LIBCPP_END_NAMESPACE_STD
5395
5396_LIBCPP_POP_MACROS
5397
5398#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
5399#   include <__pstl_memory>
5400#endif
5401
5402#endif  // _LIBCPP_MEMORY
5403