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