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