xref: /freebsd/contrib/llvm-project/libcxx/include/memory (revision 8c2dd68caa963f1900a8228b0732b04f5d530ffa)
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> constexpr 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> // removed in C++20
103{
104public:
105    typedef void*                                 pointer;          // deprecated in C++17
106    typedef const void*                           const_pointer;    // deprecated in C++17
107    typedef void                                  value_type;       // deprecated in C++17
108
109    template <class _Up> struct rebind {typedef allocator<_Up> other;}; // deprecated in C++17
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 <__availability>
669#include <type_traits>
670#include <typeinfo>
671#include <cstddef>
672#include <cstdint>
673#include <new>
674#include <utility>
675#include <limits>
676#include <iterator>
677#include <__functional_base>
678#include <iosfwd>
679#include <tuple>
680#include <stdexcept>
681#include <cstring>
682#include <__memory/allocator_traits.h>
683#include <__memory/base.h>
684#include <__memory/pointer_traits.h>
685#include <__memory/utilities.h>
686#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
687#  include <atomic>
688#endif
689#include <version>
690
691#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
692#pragma GCC system_header
693#endif
694
695_LIBCPP_PUSH_MACROS
696#include <__undef_macros>
697
698
699_LIBCPP_BEGIN_NAMESPACE_STD
700
701template <class _ValueType>
702inline _LIBCPP_INLINE_VISIBILITY
703_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
704#if !defined(_LIBCPP_HAS_NO_THREADS) && \
705    defined(__ATOMIC_RELAXED) &&        \
706    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
707    return __atomic_load_n(__value, __ATOMIC_RELAXED);
708#else
709    return *__value;
710#endif
711}
712
713template <class _ValueType>
714inline _LIBCPP_INLINE_VISIBILITY
715_ValueType __libcpp_acquire_load(_ValueType const* __value) {
716#if !defined(_LIBCPP_HAS_NO_THREADS) && \
717    defined(__ATOMIC_ACQUIRE) &&        \
718    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
719    return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
720#else
721    return *__value;
722#endif
723}
724
725template <bool _UsePointerTraits> struct __to_address_helper;
726
727template <> struct __to_address_helper<true> {
728    template <class _Pointer>
729    using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
730
731    template <class _Pointer>
732    _LIBCPP_CONSTEXPR
733    static __return_type<_Pointer>
734    __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
735};
736
737template <class _Pointer, bool _Dummy = true>
738using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
739
740
741template <class _Tp>
742inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
743_Tp*
744__to_address(_Tp* __p) _NOEXCEPT
745{
746    static_assert(!is_function<_Tp>::value, "_Tp is a function type");
747    return __p;
748}
749
750template <class _Pointer>
751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
752typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
753__to_address(const _Pointer& __p) _NOEXCEPT {
754  return __choose_to_address<_Pointer>::__do_it(__p);
755}
756
757template <> struct __to_address_helper<false> {
758    template <class _Pointer>
759    using __return_type = typename pointer_traits<_Pointer>::element_type*;
760
761    template <class _Pointer>
762    _LIBCPP_CONSTEXPR
763    static __return_type<_Pointer>
764    __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
765};
766
767
768#if _LIBCPP_STD_VER > 17
769template <class _Tp>
770inline _LIBCPP_INLINE_VISIBILITY constexpr
771_Tp*
772to_address(_Tp* __p) _NOEXCEPT
773{
774    static_assert(!is_function_v<_Tp>, "_Tp is a function type");
775    return __p;
776}
777
778template <class _Pointer>
779inline _LIBCPP_INLINE_VISIBILITY constexpr
780auto
781to_address(const _Pointer& __p) _NOEXCEPT
782{
783    return _VSTD::__to_address(__p);
784}
785#endif
786
787template <class _Tp> class allocator;
788
789#if _LIBCPP_STD_VER <= 17
790template <>
791class _LIBCPP_TEMPLATE_VIS allocator<void>
792{
793public:
794    _LIBCPP_DEPRECATED_IN_CXX17 typedef void*             pointer;
795    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       const_pointer;
796    _LIBCPP_DEPRECATED_IN_CXX17 typedef void              value_type;
797
798    template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
799};
800
801template <>
802class _LIBCPP_TEMPLATE_VIS allocator<const void>
803{
804public:
805    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       pointer;
806    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       const_pointer;
807    _LIBCPP_DEPRECATED_IN_CXX17 typedef const void        value_type;
808
809    template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
810};
811#endif
812
813// allocator
814
815template <class _Tp>
816class _LIBCPP_TEMPLATE_VIS allocator
817{
818public:
819    typedef size_t      size_type;
820    typedef ptrdiff_t   difference_type;
821    typedef _Tp         value_type;
822    typedef true_type   propagate_on_container_move_assignment;
823    typedef true_type   is_always_equal;
824
825    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
826    allocator() _NOEXCEPT { }
827
828    template <class _Up>
829    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
830    allocator(const allocator<_Up>&) _NOEXCEPT { }
831
832    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
833    _Tp* allocate(size_t __n) {
834        if (__n > allocator_traits<allocator>::max_size(*this))
835            __throw_length_error("allocator<T>::allocate(size_t n)"
836                                 " 'n' exceeds maximum supported size");
837        if (__libcpp_is_constant_evaluated()) {
838            return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
839        } else {
840            return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
841        }
842    }
843
844    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
845    void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
846        if (__libcpp_is_constant_evaluated()) {
847            ::operator delete(__p);
848        } else {
849            _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
850        }
851    }
852
853    // C++20 Removed members
854#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
855    _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp*       pointer;
856    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
857    _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp&       reference;
858    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
859
860    template <class _Up>
861    struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
862        typedef allocator<_Up> other;
863    };
864
865    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
866    pointer address(reference __x) const _NOEXCEPT {
867        return _VSTD::addressof(__x);
868    }
869    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
870    const_pointer address(const_reference __x) const _NOEXCEPT {
871        return _VSTD::addressof(__x);
872    }
873
874    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
875    _Tp* allocate(size_t __n, const void*) {
876        return allocate(__n);
877    }
878
879    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
880        return size_type(~0) / sizeof(_Tp);
881    }
882
883    template <class _Up, class... _Args>
884    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
885    void construct(_Up* __p, _Args&&... __args) {
886        ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
887    }
888
889    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
890    void destroy(pointer __p) {
891        __p->~_Tp();
892    }
893#endif
894};
895
896template <class _Tp>
897class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
898{
899public:
900    typedef size_t      size_type;
901    typedef ptrdiff_t   difference_type;
902    typedef const _Tp   value_type;
903    typedef true_type   propagate_on_container_move_assignment;
904    typedef true_type   is_always_equal;
905
906    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
907    allocator() _NOEXCEPT { }
908
909    template <class _Up>
910    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
911    allocator(const allocator<_Up>&) _NOEXCEPT { }
912
913    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
914    const _Tp* allocate(size_t __n) {
915        if (__n > allocator_traits<allocator>::max_size(*this))
916            __throw_length_error("allocator<const T>::allocate(size_t n)"
917                                 " 'n' exceeds maximum supported size");
918        if (__libcpp_is_constant_evaluated()) {
919            return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
920        } else {
921            return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
922        }
923    }
924
925    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
926    void deallocate(const _Tp* __p, size_t __n) {
927        if (__libcpp_is_constant_evaluated()) {
928            ::operator delete(const_cast<_Tp*>(__p));
929        } else {
930            _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
931        }
932    }
933
934    // C++20 Removed members
935#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
936    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
937    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
938    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
939    _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
940
941    template <class _Up>
942    struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
943        typedef allocator<_Up> other;
944    };
945
946    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
947    const_pointer address(const_reference __x) const _NOEXCEPT {
948        return _VSTD::addressof(__x);
949    }
950
951    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
952    const _Tp* allocate(size_t __n, const void*) {
953        return allocate(__n);
954    }
955
956    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
957        return size_type(~0) / sizeof(_Tp);
958    }
959
960    template <class _Up, class... _Args>
961    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
962    void construct(_Up* __p, _Args&&... __args) {
963        ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
964    }
965
966    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
967    void destroy(pointer __p) {
968        __p->~_Tp();
969    }
970#endif
971};
972
973template <class _Tp, class _Up>
974inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
975bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
976
977template <class _Tp, class _Up>
978inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
979bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
980
981template <class _Alloc, class _Ptr>
982_LIBCPP_INLINE_VISIBILITY
983void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
984    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
985        "The specified type does not meet the requirements of Cpp17MoveInsertable");
986    typedef allocator_traits<_Alloc> _Traits;
987    for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
988        _Traits::construct(__a, _VSTD::__to_address(__begin2),
989#ifdef _LIBCPP_NO_EXCEPTIONS
990            _VSTD::move(*__begin1)
991#else
992            _VSTD::move_if_noexcept(*__begin1)
993#endif
994        );
995    }
996}
997
998template <class _Alloc, class _Tp, typename enable_if<
999    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1000    is_trivially_move_constructible<_Tp>::value
1001>::type>
1002_LIBCPP_INLINE_VISIBILITY
1003void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1004    ptrdiff_t _Np = __end1 - __begin1;
1005    if (_Np > 0) {
1006        _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1007        __begin2 += _Np;
1008    }
1009}
1010
1011template <class _Alloc, class _Iter, class _Ptr>
1012_LIBCPP_INLINE_VISIBILITY
1013void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1014    typedef allocator_traits<_Alloc> _Traits;
1015    for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1016        _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1017    }
1018}
1019
1020template <class _Alloc, class _Source, class _Dest,
1021          class _RawSource = typename remove_const<_Source>::type,
1022          class _RawDest = typename remove_const<_Dest>::type,
1023          class =
1024    typename enable_if<
1025        is_trivially_copy_constructible<_Dest>::value &&
1026        is_same<_RawSource, _RawDest>::value &&
1027        (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1028    >::type>
1029_LIBCPP_INLINE_VISIBILITY
1030void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1031    ptrdiff_t _Np = __end1 - __begin1;
1032    if (_Np > 0) {
1033        _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1034        __begin2 += _Np;
1035    }
1036}
1037
1038template <class _Alloc, class _Ptr>
1039_LIBCPP_INLINE_VISIBILITY
1040void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1041    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1042        "The specified type does not meet the requirements of Cpp17MoveInsertable");
1043    typedef allocator_traits<_Alloc> _Traits;
1044    while (__end1 != __begin1) {
1045        _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1046#ifdef _LIBCPP_NO_EXCEPTIONS
1047            _VSTD::move(*--__end1)
1048#else
1049            _VSTD::move_if_noexcept(*--__end1)
1050#endif
1051        );
1052        --__end2;
1053    }
1054}
1055
1056template <class _Alloc, class _Tp, class = typename enable_if<
1057    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1058    is_trivially_move_constructible<_Tp>::value
1059>::type>
1060_LIBCPP_INLINE_VISIBILITY
1061void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1062    ptrdiff_t _Np = __end1 - __begin1;
1063    __end2 -= _Np;
1064    if (_Np > 0)
1065        _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1066}
1067
1068template <class _OutputIterator, class _Tp>
1069class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
1070    : public iterator<output_iterator_tag,
1071                      _Tp,                                         // purposefully not C++03
1072                      ptrdiff_t,                                   // purposefully not C++03
1073                      _Tp*,                                        // purposefully not C++03
1074                      raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1075{
1076private:
1077    _OutputIterator __x_;
1078public:
1079    _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1080    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1081    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1082        {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
1083#if _LIBCPP_STD_VER >= 14
1084    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1085        {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
1086#endif
1087    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1088    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator  operator++(int)
1089        {raw_storage_iterator __t(*this); ++__x_; return __t;}
1090#if _LIBCPP_STD_VER >= 14
1091    _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1092#endif
1093};
1094
1095template <class _Tp>
1096_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
1097pair<_Tp*, ptrdiff_t>
1098get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
1099{
1100    pair<_Tp*, ptrdiff_t> __r(0, 0);
1101    const ptrdiff_t __m = (~ptrdiff_t(0) ^
1102                           ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1103                           / sizeof(_Tp);
1104    if (__n > __m)
1105        __n = __m;
1106    while (__n > 0)
1107    {
1108#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
1109    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
1110        {
1111            align_val_t __al =
1112                align_val_t(alignment_of<_Tp>::value);
1113            __r.first = static_cast<_Tp*>(::operator new(
1114                __n * sizeof(_Tp), __al, nothrow));
1115        } else {
1116            __r.first = static_cast<_Tp*>(::operator new(
1117                __n * sizeof(_Tp), nothrow));
1118        }
1119#else
1120    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
1121        {
1122            // Since aligned operator new is unavailable, return an empty
1123            // buffer rather than one with invalid alignment.
1124            return __r;
1125        }
1126
1127        __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1128#endif
1129
1130        if (__r.first)
1131        {
1132            __r.second = __n;
1133            break;
1134        }
1135        __n /= 2;
1136    }
1137    return __r;
1138}
1139
1140template <class _Tp>
1141inline _LIBCPP_INLINE_VISIBILITY
1142void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1143{
1144  _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
1145}
1146
1147#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1148template <class _Tp>
1149struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
1150{
1151    _Tp* __ptr_;
1152};
1153
1154template<class _Tp>
1155class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
1156{
1157private:
1158    _Tp* __ptr_;
1159public:
1160    typedef _Tp element_type;
1161
1162    _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1163    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1164    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
1165        : __ptr_(__p.release()) {}
1166    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
1167        {reset(__p.release()); return *this;}
1168    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
1169        {reset(__p.release()); return *this;}
1170    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
1171        {reset(__p.__ptr_); return *this;}
1172    _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
1173
1174    _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
1175        {return *__ptr_;}
1176    _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1177    _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1178    _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
1179    {
1180        _Tp* __t = __ptr_;
1181        __ptr_ = nullptr;
1182        return __t;
1183    }
1184    _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
1185    {
1186        if (__ptr_ != __p)
1187            delete __ptr_;
1188        __ptr_ = __p;
1189    }
1190
1191    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1192    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
1193        {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1194    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
1195        {return auto_ptr<_Up>(release());}
1196};
1197
1198template <>
1199class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
1200{
1201public:
1202    typedef void element_type;
1203};
1204#endif
1205
1206// Tag used to default initialize one or both of the pair's elements.
1207struct __default_init_tag {};
1208struct __value_init_tag {};
1209
1210template <class _Tp, int _Idx,
1211          bool _CanBeEmptyBase =
1212              is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1213struct __compressed_pair_elem {
1214  typedef _Tp _ParamT;
1215  typedef _Tp& reference;
1216  typedef const _Tp& const_reference;
1217
1218  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1219  __compressed_pair_elem(__default_init_tag) {}
1220  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1221  __compressed_pair_elem(__value_init_tag) : __value_() {}
1222
1223  template <class _Up, class = typename enable_if<
1224      !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1225  >::type>
1226  _LIBCPP_INLINE_VISIBILITY
1227  _LIBCPP_CONSTEXPR explicit
1228  __compressed_pair_elem(_Up&& __u)
1229      : __value_(_VSTD::forward<_Up>(__u))
1230    {
1231    }
1232
1233
1234#ifndef _LIBCPP_CXX03_LANG
1235  template <class... _Args, size_t... _Indexes>
1236  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1237  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1238                         __tuple_indices<_Indexes...>)
1239      : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
1240#endif
1241
1242
1243  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1244  _LIBCPP_INLINE_VISIBILITY
1245  const_reference __get() const _NOEXCEPT { return __value_; }
1246
1247private:
1248  _Tp __value_;
1249};
1250
1251template <class _Tp, int _Idx>
1252struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1253  typedef _Tp _ParamT;
1254  typedef _Tp& reference;
1255  typedef const _Tp& const_reference;
1256  typedef _Tp __value_type;
1257
1258  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1259  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1260  __compressed_pair_elem(__default_init_tag) {}
1261  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1262  __compressed_pair_elem(__value_init_tag) : __value_type() {}
1263
1264  template <class _Up, class = typename enable_if<
1265        !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1266  >::type>
1267  _LIBCPP_INLINE_VISIBILITY
1268  _LIBCPP_CONSTEXPR explicit
1269  __compressed_pair_elem(_Up&& __u)
1270      : __value_type(_VSTD::forward<_Up>(__u))
1271  {}
1272
1273#ifndef _LIBCPP_CXX03_LANG
1274  template <class... _Args, size_t... _Indexes>
1275  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1276  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1277                         __tuple_indices<_Indexes...>)
1278      : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
1279#endif
1280
1281  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1282  _LIBCPP_INLINE_VISIBILITY
1283  const_reference __get() const _NOEXCEPT { return *this; }
1284};
1285
1286template <class _T1, class _T2>
1287class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1288                          private __compressed_pair_elem<_T2, 1> {
1289public:
1290  // NOTE: This static assert should never fire because __compressed_pair
1291  // is *almost never* used in a scenario where it's possible for T1 == T2.
1292  // (The exception is std::function where it is possible that the function
1293  //  object and the allocator have the same type).
1294  static_assert((!is_same<_T1, _T2>::value),
1295    "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
1296    "The current implementation is NOT ABI-compatible with the previous "
1297    "implementation for this configuration");
1298
1299    typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1300    typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1301
1302    template <bool _Dummy = true,
1303      class = typename enable_if<
1304          __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1305          __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1306      >::type
1307  >
1308  _LIBCPP_INLINE_VISIBILITY
1309  _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
1310
1311  template <class _U1, class _U2>
1312  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1313  __compressed_pair(_U1&& __t1, _U2&& __t2)
1314      : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
1315
1316#ifndef _LIBCPP_CXX03_LANG
1317  template <class... _Args1, class... _Args2>
1318  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1319  __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1320                    tuple<_Args2...> __second_args)
1321      : _Base1(__pc, _VSTD::move(__first_args),
1322               typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1323        _Base2(__pc, _VSTD::move(__second_args),
1324               typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
1325#endif
1326
1327  _LIBCPP_INLINE_VISIBILITY
1328  typename _Base1::reference first() _NOEXCEPT {
1329    return static_cast<_Base1&>(*this).__get();
1330  }
1331
1332  _LIBCPP_INLINE_VISIBILITY
1333  typename _Base1::const_reference first() const _NOEXCEPT {
1334    return static_cast<_Base1 const&>(*this).__get();
1335  }
1336
1337  _LIBCPP_INLINE_VISIBILITY
1338  typename _Base2::reference second() _NOEXCEPT {
1339    return static_cast<_Base2&>(*this).__get();
1340  }
1341
1342  _LIBCPP_INLINE_VISIBILITY
1343  typename _Base2::const_reference second() const _NOEXCEPT {
1344    return static_cast<_Base2 const&>(*this).__get();
1345  }
1346
1347  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1348  static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1349    return static_cast<_Base1*>(__pair);
1350  }
1351  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1352  static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1353    return static_cast<_Base2*>(__pair);
1354  }
1355
1356  _LIBCPP_INLINE_VISIBILITY
1357  void swap(__compressed_pair& __x)
1358    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1359               __is_nothrow_swappable<_T2>::value)
1360  {
1361    using _VSTD::swap;
1362    swap(first(), __x.first());
1363    swap(second(), __x.second());
1364  }
1365};
1366
1367template <class _T1, class _T2>
1368inline _LIBCPP_INLINE_VISIBILITY
1369void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1370    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1371               __is_nothrow_swappable<_T2>::value) {
1372  __x.swap(__y);
1373}
1374
1375// default_delete
1376
1377template <class _Tp>
1378struct _LIBCPP_TEMPLATE_VIS default_delete {
1379    static_assert(!is_function<_Tp>::value,
1380                  "default_delete cannot be instantiated for function types");
1381#ifndef _LIBCPP_CXX03_LANG
1382  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
1383#else
1384  _LIBCPP_INLINE_VISIBILITY default_delete() {}
1385#endif
1386  template <class _Up>
1387  _LIBCPP_INLINE_VISIBILITY
1388  default_delete(const default_delete<_Up>&,
1389                 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1390                     0) _NOEXCEPT {}
1391
1392  _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1393    static_assert(sizeof(_Tp) > 0,
1394                  "default_delete can not delete incomplete type");
1395    static_assert(!is_void<_Tp>::value,
1396                  "default_delete can not delete incomplete type");
1397    delete __ptr;
1398  }
1399};
1400
1401template <class _Tp>
1402struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1403private:
1404  template <class _Up>
1405  struct _EnableIfConvertible
1406      : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1407
1408public:
1409#ifndef _LIBCPP_CXX03_LANG
1410  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
1411#else
1412  _LIBCPP_INLINE_VISIBILITY default_delete() {}
1413#endif
1414
1415  template <class _Up>
1416  _LIBCPP_INLINE_VISIBILITY
1417  default_delete(const default_delete<_Up[]>&,
1418                 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1419
1420  template <class _Up>
1421  _LIBCPP_INLINE_VISIBILITY
1422  typename _EnableIfConvertible<_Up>::type
1423  operator()(_Up* __ptr) const _NOEXCEPT {
1424    static_assert(sizeof(_Tp) > 0,
1425                  "default_delete can not delete incomplete type");
1426    static_assert(!is_void<_Tp>::value,
1427                  "default_delete can not delete void type");
1428    delete[] __ptr;
1429  }
1430};
1431
1432template <class _Deleter>
1433struct __unique_ptr_deleter_sfinae {
1434  static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1435  typedef const _Deleter& __lval_ref_type;
1436  typedef _Deleter&& __good_rval_ref_type;
1437  typedef true_type __enable_rval_overload;
1438};
1439
1440template <class _Deleter>
1441struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1442  typedef const _Deleter& __lval_ref_type;
1443  typedef const _Deleter&& __bad_rval_ref_type;
1444  typedef false_type __enable_rval_overload;
1445};
1446
1447template <class _Deleter>
1448struct __unique_ptr_deleter_sfinae<_Deleter&> {
1449  typedef _Deleter& __lval_ref_type;
1450  typedef _Deleter&& __bad_rval_ref_type;
1451  typedef false_type __enable_rval_overload;
1452};
1453
1454#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1455#  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1456#else
1457#  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1458#endif
1459
1460template <class _Tp, class _Dp = default_delete<_Tp> >
1461class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
1462public:
1463  typedef _Tp element_type;
1464  typedef _Dp deleter_type;
1465  typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
1466
1467  static_assert(!is_rvalue_reference<deleter_type>::value,
1468                "the specified deleter type cannot be an rvalue reference");
1469
1470private:
1471  __compressed_pair<pointer, deleter_type> __ptr_;
1472
1473  struct __nat { int __for_bool_; };
1474
1475  typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
1476
1477  template <bool _Dummy>
1478  using _LValRefType _LIBCPP_NODEBUG_TYPE =
1479      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
1480
1481  template <bool _Dummy>
1482  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
1483      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
1484
1485  template <bool _Dummy>
1486  using _BadRValRefType _LIBCPP_NODEBUG_TYPE  =
1487      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
1488
1489  template <bool _Dummy, class _Deleter = typename __dependent_type<
1490                             __identity<deleter_type>, _Dummy>::type>
1491  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
1492      typename enable_if<is_default_constructible<_Deleter>::value &&
1493                         !is_pointer<_Deleter>::value>::type;
1494
1495  template <class _ArgType>
1496  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
1497      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1498
1499  template <class _UPtr, class _Up>
1500  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1501      is_convertible<typename _UPtr::pointer, pointer>::value &&
1502      !is_array<_Up>::value
1503  >::type;
1504
1505  template <class _UDel>
1506  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1507      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1508      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1509    >::type;
1510
1511  template <class _UDel>
1512  using _EnableIfDeleterAssignable = typename enable_if<
1513      is_assignable<_Dp&, _UDel&&>::value
1514    >::type;
1515
1516public:
1517  template <bool _Dummy = true,
1518            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1519  _LIBCPP_INLINE_VISIBILITY
1520  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1521
1522  template <bool _Dummy = true,
1523            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1524  _LIBCPP_INLINE_VISIBILITY
1525  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1526
1527  template <bool _Dummy = true,
1528            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1529  _LIBCPP_INLINE_VISIBILITY
1530  explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
1531
1532  template <bool _Dummy = true,
1533            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
1534  _LIBCPP_INLINE_VISIBILITY
1535  unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
1536      : __ptr_(__p, __d) {}
1537
1538  template <bool _Dummy = true,
1539            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
1540  _LIBCPP_INLINE_VISIBILITY
1541  unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
1542      : __ptr_(__p, _VSTD::move(__d)) {
1543    static_assert(!is_reference<deleter_type>::value,
1544                  "rvalue deleter bound to reference");
1545  }
1546
1547  template <bool _Dummy = true,
1548            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
1549  _LIBCPP_INLINE_VISIBILITY
1550  unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1551
1552  _LIBCPP_INLINE_VISIBILITY
1553  unique_ptr(unique_ptr&& __u) _NOEXCEPT
1554      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1555  }
1556
1557  template <class _Up, class _Ep,
1558      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1559      class = _EnableIfDeleterConvertible<_Ep>
1560  >
1561  _LIBCPP_INLINE_VISIBILITY
1562  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1563      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1564
1565#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1566  template <class _Up>
1567  _LIBCPP_INLINE_VISIBILITY
1568  unique_ptr(auto_ptr<_Up>&& __p,
1569             typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1570                                    is_same<_Dp, default_delete<_Tp> >::value,
1571                                __nat>::type = __nat()) _NOEXCEPT
1572      : __ptr_(__p.release(), __default_init_tag()) {}
1573#endif
1574
1575  _LIBCPP_INLINE_VISIBILITY
1576  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1577    reset(__u.release());
1578    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1579    return *this;
1580  }
1581
1582  template <class _Up, class _Ep,
1583      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1584      class = _EnableIfDeleterAssignable<_Ep>
1585  >
1586  _LIBCPP_INLINE_VISIBILITY
1587  unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1588    reset(__u.release());
1589    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1590    return *this;
1591  }
1592
1593#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1594  template <class _Up>
1595  _LIBCPP_INLINE_VISIBILITY
1596      typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1597                             is_same<_Dp, default_delete<_Tp> >::value,
1598                         unique_ptr&>::type
1599      operator=(auto_ptr<_Up> __p) {
1600    reset(__p.release());
1601    return *this;
1602  }
1603#endif
1604
1605#ifdef _LIBCPP_CXX03_LANG
1606  unique_ptr(unique_ptr const&) = delete;
1607  unique_ptr& operator=(unique_ptr const&) = delete;
1608#endif
1609
1610
1611  _LIBCPP_INLINE_VISIBILITY
1612  ~unique_ptr() { reset(); }
1613
1614  _LIBCPP_INLINE_VISIBILITY
1615  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1616    reset();
1617    return *this;
1618  }
1619
1620  _LIBCPP_INLINE_VISIBILITY
1621  typename add_lvalue_reference<_Tp>::type
1622  operator*() const {
1623    return *__ptr_.first();
1624  }
1625  _LIBCPP_INLINE_VISIBILITY
1626  pointer operator->() const _NOEXCEPT {
1627    return __ptr_.first();
1628  }
1629  _LIBCPP_INLINE_VISIBILITY
1630  pointer get() const _NOEXCEPT {
1631    return __ptr_.first();
1632  }
1633  _LIBCPP_INLINE_VISIBILITY
1634  deleter_type& get_deleter() _NOEXCEPT {
1635    return __ptr_.second();
1636  }
1637  _LIBCPP_INLINE_VISIBILITY
1638  const deleter_type& get_deleter() const _NOEXCEPT {
1639    return __ptr_.second();
1640  }
1641  _LIBCPP_INLINE_VISIBILITY
1642  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1643    return __ptr_.first() != nullptr;
1644  }
1645
1646  _LIBCPP_INLINE_VISIBILITY
1647  pointer release() _NOEXCEPT {
1648    pointer __t = __ptr_.first();
1649    __ptr_.first() = pointer();
1650    return __t;
1651  }
1652
1653  _LIBCPP_INLINE_VISIBILITY
1654  void reset(pointer __p = pointer()) _NOEXCEPT {
1655    pointer __tmp = __ptr_.first();
1656    __ptr_.first() = __p;
1657    if (__tmp)
1658      __ptr_.second()(__tmp);
1659  }
1660
1661  _LIBCPP_INLINE_VISIBILITY
1662  void swap(unique_ptr& __u) _NOEXCEPT {
1663    __ptr_.swap(__u.__ptr_);
1664  }
1665};
1666
1667
1668template <class _Tp, class _Dp>
1669class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
1670public:
1671  typedef _Tp element_type;
1672  typedef _Dp deleter_type;
1673  typedef typename __pointer<_Tp, deleter_type>::type pointer;
1674
1675private:
1676  __compressed_pair<pointer, deleter_type> __ptr_;
1677
1678  template <class _From>
1679  struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
1680
1681  template <class _FromElem>
1682  struct _CheckArrayPointerConversion<_FromElem*>
1683      : integral_constant<bool,
1684          is_same<_FromElem*, pointer>::value ||
1685            (is_same<pointer, element_type*>::value &&
1686             is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1687      >
1688  {};
1689
1690  typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
1691
1692  template <bool _Dummy>
1693  using _LValRefType _LIBCPP_NODEBUG_TYPE =
1694      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
1695
1696  template <bool _Dummy>
1697  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
1698      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
1699
1700  template <bool _Dummy>
1701  using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
1702      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
1703
1704  template <bool _Dummy, class _Deleter = typename __dependent_type<
1705                             __identity<deleter_type>, _Dummy>::type>
1706  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE  =
1707      typename enable_if<is_default_constructible<_Deleter>::value &&
1708                         !is_pointer<_Deleter>::value>::type;
1709
1710  template <class _ArgType>
1711  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
1712      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1713
1714  template <class _Pp>
1715  using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1716      _CheckArrayPointerConversion<_Pp>::value
1717  >::type;
1718
1719  template <class _UPtr, class _Up,
1720        class _ElemT = typename _UPtr::element_type>
1721  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1722      is_array<_Up>::value &&
1723      is_same<pointer, element_type*>::value &&
1724      is_same<typename _UPtr::pointer, _ElemT*>::value &&
1725      is_convertible<_ElemT(*)[], element_type(*)[]>::value
1726    >::type;
1727
1728  template <class _UDel>
1729  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1730      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1731      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1732    >::type;
1733
1734  template <class _UDel>
1735  using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE  = typename enable_if<
1736      is_assignable<_Dp&, _UDel&&>::value
1737    >::type;
1738
1739public:
1740  template <bool _Dummy = true,
1741            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1742  _LIBCPP_INLINE_VISIBILITY
1743  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1744
1745  template <bool _Dummy = true,
1746            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
1747  _LIBCPP_INLINE_VISIBILITY
1748  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
1749
1750  template <class _Pp, bool _Dummy = true,
1751            class = _EnableIfDeleterDefaultConstructible<_Dummy>,
1752            class = _EnableIfPointerConvertible<_Pp> >
1753  _LIBCPP_INLINE_VISIBILITY
1754  explicit unique_ptr(_Pp __p) _NOEXCEPT
1755      : __ptr_(__p, __default_init_tag()) {}
1756
1757  template <class _Pp, bool _Dummy = true,
1758            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1759            class = _EnableIfPointerConvertible<_Pp> >
1760  _LIBCPP_INLINE_VISIBILITY
1761  unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
1762      : __ptr_(__p, __d) {}
1763
1764  template <bool _Dummy = true,
1765            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
1766  _LIBCPP_INLINE_VISIBILITY
1767  unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
1768      : __ptr_(nullptr, __d) {}
1769
1770  template <class _Pp, bool _Dummy = true,
1771            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1772            class = _EnableIfPointerConvertible<_Pp> >
1773  _LIBCPP_INLINE_VISIBILITY
1774  unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
1775      : __ptr_(__p, _VSTD::move(__d)) {
1776    static_assert(!is_reference<deleter_type>::value,
1777                  "rvalue deleter bound to reference");
1778  }
1779
1780  template <bool _Dummy = true,
1781            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
1782  _LIBCPP_INLINE_VISIBILITY
1783  unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
1784      : __ptr_(nullptr, _VSTD::move(__d)) {
1785    static_assert(!is_reference<deleter_type>::value,
1786                  "rvalue deleter bound to reference");
1787  }
1788
1789  template <class _Pp, bool _Dummy = true,
1790            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1791            class = _EnableIfPointerConvertible<_Pp> >
1792  _LIBCPP_INLINE_VISIBILITY
1793  unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
1794
1795  _LIBCPP_INLINE_VISIBILITY
1796  unique_ptr(unique_ptr&& __u) _NOEXCEPT
1797      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1798  }
1799
1800  _LIBCPP_INLINE_VISIBILITY
1801  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1802    reset(__u.release());
1803    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1804    return *this;
1805  }
1806
1807  template <class _Up, class _Ep,
1808      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1809      class = _EnableIfDeleterConvertible<_Ep>
1810  >
1811  _LIBCPP_INLINE_VISIBILITY
1812  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1813      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
1814  }
1815
1816  template <class _Up, class _Ep,
1817      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1818      class = _EnableIfDeleterAssignable<_Ep>
1819  >
1820  _LIBCPP_INLINE_VISIBILITY
1821  unique_ptr&
1822  operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1823    reset(__u.release());
1824    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1825    return *this;
1826  }
1827
1828#ifdef _LIBCPP_CXX03_LANG
1829  unique_ptr(unique_ptr const&) = delete;
1830  unique_ptr& operator=(unique_ptr const&) = delete;
1831#endif
1832
1833public:
1834  _LIBCPP_INLINE_VISIBILITY
1835  ~unique_ptr() { reset(); }
1836
1837  _LIBCPP_INLINE_VISIBILITY
1838  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1839    reset();
1840    return *this;
1841  }
1842
1843  _LIBCPP_INLINE_VISIBILITY
1844  typename add_lvalue_reference<_Tp>::type
1845  operator[](size_t __i) const {
1846    return __ptr_.first()[__i];
1847  }
1848  _LIBCPP_INLINE_VISIBILITY
1849  pointer get() const _NOEXCEPT {
1850    return __ptr_.first();
1851  }
1852
1853  _LIBCPP_INLINE_VISIBILITY
1854  deleter_type& get_deleter() _NOEXCEPT {
1855    return __ptr_.second();
1856  }
1857
1858  _LIBCPP_INLINE_VISIBILITY
1859  const deleter_type& get_deleter() const _NOEXCEPT {
1860    return __ptr_.second();
1861  }
1862  _LIBCPP_INLINE_VISIBILITY
1863  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1864    return __ptr_.first() != nullptr;
1865  }
1866
1867  _LIBCPP_INLINE_VISIBILITY
1868  pointer release() _NOEXCEPT {
1869    pointer __t = __ptr_.first();
1870    __ptr_.first() = pointer();
1871    return __t;
1872  }
1873
1874  template <class _Pp>
1875  _LIBCPP_INLINE_VISIBILITY
1876  typename enable_if<
1877      _CheckArrayPointerConversion<_Pp>::value
1878  >::type
1879  reset(_Pp __p) _NOEXCEPT {
1880    pointer __tmp = __ptr_.first();
1881    __ptr_.first() = __p;
1882    if (__tmp)
1883      __ptr_.second()(__tmp);
1884  }
1885
1886  _LIBCPP_INLINE_VISIBILITY
1887  void reset(nullptr_t = nullptr) _NOEXCEPT {
1888    pointer __tmp = __ptr_.first();
1889    __ptr_.first() = nullptr;
1890    if (__tmp)
1891      __ptr_.second()(__tmp);
1892  }
1893
1894  _LIBCPP_INLINE_VISIBILITY
1895  void swap(unique_ptr& __u) _NOEXCEPT {
1896    __ptr_.swap(__u.__ptr_);
1897  }
1898
1899};
1900
1901template <class _Tp, class _Dp>
1902inline _LIBCPP_INLINE_VISIBILITY
1903typename enable_if<
1904    __is_swappable<_Dp>::value,
1905    void
1906>::type
1907swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
1908
1909template <class _T1, class _D1, class _T2, class _D2>
1910inline _LIBCPP_INLINE_VISIBILITY
1911bool
1912operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1913
1914template <class _T1, class _D1, class _T2, class _D2>
1915inline _LIBCPP_INLINE_VISIBILITY
1916bool
1917operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1918
1919template <class _T1, class _D1, class _T2, class _D2>
1920inline _LIBCPP_INLINE_VISIBILITY
1921bool
1922operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1923{
1924    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1925    typedef typename unique_ptr<_T2, _D2>::pointer _P2;
1926    typedef typename common_type<_P1, _P2>::type _Vp;
1927    return less<_Vp>()(__x.get(), __y.get());
1928}
1929
1930template <class _T1, class _D1, class _T2, class _D2>
1931inline _LIBCPP_INLINE_VISIBILITY
1932bool
1933operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1934
1935template <class _T1, class _D1, class _T2, class _D2>
1936inline _LIBCPP_INLINE_VISIBILITY
1937bool
1938operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1939
1940template <class _T1, class _D1, class _T2, class _D2>
1941inline _LIBCPP_INLINE_VISIBILITY
1942bool
1943operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1944
1945template <class _T1, class _D1>
1946inline _LIBCPP_INLINE_VISIBILITY
1947bool
1948operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
1949{
1950    return !__x;
1951}
1952
1953template <class _T1, class _D1>
1954inline _LIBCPP_INLINE_VISIBILITY
1955bool
1956operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
1957{
1958    return !__x;
1959}
1960
1961template <class _T1, class _D1>
1962inline _LIBCPP_INLINE_VISIBILITY
1963bool
1964operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
1965{
1966    return static_cast<bool>(__x);
1967}
1968
1969template <class _T1, class _D1>
1970inline _LIBCPP_INLINE_VISIBILITY
1971bool
1972operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
1973{
1974    return static_cast<bool>(__x);
1975}
1976
1977template <class _T1, class _D1>
1978inline _LIBCPP_INLINE_VISIBILITY
1979bool
1980operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1981{
1982    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1983    return less<_P1>()(__x.get(), nullptr);
1984}
1985
1986template <class _T1, class _D1>
1987inline _LIBCPP_INLINE_VISIBILITY
1988bool
1989operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1990{
1991    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1992    return less<_P1>()(nullptr, __x.get());
1993}
1994
1995template <class _T1, class _D1>
1996inline _LIBCPP_INLINE_VISIBILITY
1997bool
1998operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1999{
2000    return nullptr < __x;
2001}
2002
2003template <class _T1, class _D1>
2004inline _LIBCPP_INLINE_VISIBILITY
2005bool
2006operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2007{
2008    return __x < nullptr;
2009}
2010
2011template <class _T1, class _D1>
2012inline _LIBCPP_INLINE_VISIBILITY
2013bool
2014operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2015{
2016    return !(nullptr < __x);
2017}
2018
2019template <class _T1, class _D1>
2020inline _LIBCPP_INLINE_VISIBILITY
2021bool
2022operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2023{
2024    return !(__x < nullptr);
2025}
2026
2027template <class _T1, class _D1>
2028inline _LIBCPP_INLINE_VISIBILITY
2029bool
2030operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2031{
2032    return !(__x < nullptr);
2033}
2034
2035template <class _T1, class _D1>
2036inline _LIBCPP_INLINE_VISIBILITY
2037bool
2038operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2039{
2040    return !(nullptr < __x);
2041}
2042
2043#if _LIBCPP_STD_VER > 11
2044
2045template<class _Tp>
2046struct __unique_if
2047{
2048    typedef unique_ptr<_Tp> __unique_single;
2049};
2050
2051template<class _Tp>
2052struct __unique_if<_Tp[]>
2053{
2054    typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2055};
2056
2057template<class _Tp, size_t _Np>
2058struct __unique_if<_Tp[_Np]>
2059{
2060    typedef void __unique_array_known_bound;
2061};
2062
2063template<class _Tp, class... _Args>
2064inline _LIBCPP_INLINE_VISIBILITY
2065typename __unique_if<_Tp>::__unique_single
2066make_unique(_Args&&... __args)
2067{
2068    return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2069}
2070
2071template<class _Tp>
2072inline _LIBCPP_INLINE_VISIBILITY
2073typename __unique_if<_Tp>::__unique_array_unknown_bound
2074make_unique(size_t __n)
2075{
2076    typedef typename remove_extent<_Tp>::type _Up;
2077    return unique_ptr<_Tp>(new _Up[__n]());
2078}
2079
2080template<class _Tp, class... _Args>
2081    typename __unique_if<_Tp>::__unique_array_known_bound
2082    make_unique(_Args&&...) = delete;
2083
2084#endif  // _LIBCPP_STD_VER > 11
2085
2086template <class _Tp, class _Dp>
2087#ifdef _LIBCPP_CXX03_LANG
2088struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
2089#else
2090struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
2091    unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
2092#endif
2093{
2094    typedef unique_ptr<_Tp, _Dp> argument_type;
2095    typedef size_t               result_type;
2096    _LIBCPP_INLINE_VISIBILITY
2097    result_type operator()(const argument_type& __ptr) const
2098    {
2099        typedef typename argument_type::pointer pointer;
2100        return hash<pointer>()(__ptr.get());
2101    }
2102};
2103
2104struct __destruct_n
2105{
2106private:
2107    size_t __size_;
2108
2109    template <class _Tp>
2110    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
2111        {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
2112
2113    template <class _Tp>
2114    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
2115        {}
2116
2117    _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
2118        {++__size_;}
2119    _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
2120        {}
2121
2122    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
2123        {__size_ = __s;}
2124    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
2125        {}
2126public:
2127    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2128        : __size_(__s) {}
2129
2130    template <class _Tp>
2131    _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
2132        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2133
2134    template <class _Tp>
2135    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
2136        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2137
2138    template <class _Tp>
2139    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
2140        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2141};
2142
2143template <class _Alloc>
2144class __allocator_destructor
2145{
2146    typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
2147public:
2148    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2149    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
2150private:
2151    _Alloc& __alloc_;
2152    size_type __s_;
2153public:
2154    _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2155             _NOEXCEPT
2156        : __alloc_(__a), __s_(__s) {}
2157    _LIBCPP_INLINE_VISIBILITY
2158    void operator()(pointer __p) _NOEXCEPT
2159        {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2160};
2161
2162template <class _InputIterator, class _ForwardIterator>
2163_ForwardIterator
2164uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2165{
2166    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2167#ifndef _LIBCPP_NO_EXCEPTIONS
2168    _ForwardIterator __s = __r;
2169    try
2170    {
2171#endif
2172        for (; __f != __l; ++__f, (void) ++__r)
2173            ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
2174#ifndef _LIBCPP_NO_EXCEPTIONS
2175    }
2176    catch (...)
2177    {
2178        for (; __s != __r; ++__s)
2179            __s->~value_type();
2180        throw;
2181    }
2182#endif
2183    return __r;
2184}
2185
2186template <class _InputIterator, class _Size, class _ForwardIterator>
2187_ForwardIterator
2188uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2189{
2190    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2191#ifndef _LIBCPP_NO_EXCEPTIONS
2192    _ForwardIterator __s = __r;
2193    try
2194    {
2195#endif
2196        for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2197            ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
2198#ifndef _LIBCPP_NO_EXCEPTIONS
2199    }
2200    catch (...)
2201    {
2202        for (; __s != __r; ++__s)
2203            __s->~value_type();
2204        throw;
2205    }
2206#endif
2207    return __r;
2208}
2209
2210template <class _ForwardIterator, class _Tp>
2211void
2212uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2213{
2214    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2215#ifndef _LIBCPP_NO_EXCEPTIONS
2216    _ForwardIterator __s = __f;
2217    try
2218    {
2219#endif
2220        for (; __f != __l; ++__f)
2221            ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
2222#ifndef _LIBCPP_NO_EXCEPTIONS
2223    }
2224    catch (...)
2225    {
2226        for (; __s != __f; ++__s)
2227            __s->~value_type();
2228        throw;
2229    }
2230#endif
2231}
2232
2233template <class _ForwardIterator, class _Size, class _Tp>
2234_ForwardIterator
2235uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2236{
2237    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2238#ifndef _LIBCPP_NO_EXCEPTIONS
2239    _ForwardIterator __s = __f;
2240    try
2241    {
2242#endif
2243        for (; __n > 0; ++__f, (void) --__n)
2244            ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
2245#ifndef _LIBCPP_NO_EXCEPTIONS
2246    }
2247    catch (...)
2248    {
2249        for (; __s != __f; ++__s)
2250            __s->~value_type();
2251        throw;
2252    }
2253#endif
2254    return __f;
2255}
2256
2257#if _LIBCPP_STD_VER > 14
2258
2259template <class _ForwardIterator>
2260inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2261void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2262    for (; __first != __last; ++__first)
2263        _VSTD::destroy_at(_VSTD::addressof(*__first));
2264}
2265
2266template <class _ForwardIterator, class _Size>
2267inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2268_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2269    for (; __n > 0; (void)++__first, --__n)
2270        _VSTD::destroy_at(_VSTD::addressof(*__first));
2271    return __first;
2272}
2273
2274template <class _ForwardIterator>
2275inline _LIBCPP_INLINE_VISIBILITY
2276void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2277    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2278    auto __idx = __first;
2279#ifndef _LIBCPP_NO_EXCEPTIONS
2280    try {
2281#endif
2282    for (; __idx != __last; ++__idx)
2283        ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
2284#ifndef _LIBCPP_NO_EXCEPTIONS
2285    } catch (...) {
2286        _VSTD::destroy(__first, __idx);
2287        throw;
2288    }
2289#endif
2290}
2291
2292template <class _ForwardIterator, class _Size>
2293inline _LIBCPP_INLINE_VISIBILITY
2294_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2295    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2296    auto __idx = __first;
2297#ifndef _LIBCPP_NO_EXCEPTIONS
2298    try {
2299#endif
2300    for (; __n > 0; (void)++__idx, --__n)
2301        ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
2302    return __idx;
2303#ifndef _LIBCPP_NO_EXCEPTIONS
2304    } catch (...) {
2305        _VSTD::destroy(__first, __idx);
2306        throw;
2307    }
2308#endif
2309}
2310
2311
2312template <class _ForwardIterator>
2313inline _LIBCPP_INLINE_VISIBILITY
2314void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2315    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2316    auto __idx = __first;
2317#ifndef _LIBCPP_NO_EXCEPTIONS
2318    try {
2319#endif
2320    for (; __idx != __last; ++__idx)
2321        ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
2322#ifndef _LIBCPP_NO_EXCEPTIONS
2323    } catch (...) {
2324        _VSTD::destroy(__first, __idx);
2325        throw;
2326    }
2327#endif
2328}
2329
2330template <class _ForwardIterator, class _Size>
2331inline _LIBCPP_INLINE_VISIBILITY
2332_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2333    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2334    auto __idx = __first;
2335#ifndef _LIBCPP_NO_EXCEPTIONS
2336    try {
2337#endif
2338    for (; __n > 0; (void)++__idx, --__n)
2339        ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
2340    return __idx;
2341#ifndef _LIBCPP_NO_EXCEPTIONS
2342    } catch (...) {
2343        _VSTD::destroy(__first, __idx);
2344        throw;
2345    }
2346#endif
2347}
2348
2349
2350template <class _InputIt, class _ForwardIt>
2351inline _LIBCPP_INLINE_VISIBILITY
2352_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2353    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2354    auto __idx = __first_res;
2355#ifndef _LIBCPP_NO_EXCEPTIONS
2356    try {
2357#endif
2358    for (; __first != __last; (void)++__idx, ++__first)
2359        ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
2360    return __idx;
2361#ifndef _LIBCPP_NO_EXCEPTIONS
2362    } catch (...) {
2363        _VSTD::destroy(__first_res, __idx);
2364        throw;
2365    }
2366#endif
2367}
2368
2369template <class _InputIt, class _Size, class _ForwardIt>
2370inline _LIBCPP_INLINE_VISIBILITY
2371pair<_InputIt, _ForwardIt>
2372uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2373    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2374    auto __idx = __first_res;
2375#ifndef _LIBCPP_NO_EXCEPTIONS
2376    try {
2377#endif
2378    for (; __n > 0; ++__idx, (void)++__first, --__n)
2379        ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
2380    return {__first, __idx};
2381#ifndef _LIBCPP_NO_EXCEPTIONS
2382    } catch (...) {
2383        _VSTD::destroy(__first_res, __idx);
2384        throw;
2385    }
2386#endif
2387}
2388
2389
2390#endif // _LIBCPP_STD_VER > 14
2391
2392// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2393// should be sufficient for thread safety.
2394// See https://bugs.llvm.org/show_bug.cgi?id=22803
2395#if defined(__clang__) && __has_builtin(__atomic_add_fetch)          \
2396                       && defined(__ATOMIC_RELAXED)                  \
2397                       && defined(__ATOMIC_ACQ_REL)
2398#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2399#elif defined(_LIBCPP_COMPILER_GCC)
2400#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2401#endif
2402
2403template <class _Tp>
2404inline _LIBCPP_INLINE_VISIBILITY _Tp
2405__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2406{
2407#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2408    return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2409#else
2410    return __t += 1;
2411#endif
2412}
2413
2414template <class _Tp>
2415inline _LIBCPP_INLINE_VISIBILITY _Tp
2416__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2417{
2418#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2419    return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2420#else
2421    return __t -= 1;
2422#endif
2423}
2424
2425class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
2426    : public std::exception
2427{
2428public:
2429    bad_weak_ptr() _NOEXCEPT = default;
2430    bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
2431    virtual ~bad_weak_ptr() _NOEXCEPT;
2432    virtual const char* what() const  _NOEXCEPT;
2433};
2434
2435_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
2436void __throw_bad_weak_ptr()
2437{
2438#ifndef _LIBCPP_NO_EXCEPTIONS
2439    throw bad_weak_ptr();
2440#else
2441    _VSTD::abort();
2442#endif
2443}
2444
2445template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
2446
2447class _LIBCPP_TYPE_VIS __shared_count
2448{
2449    __shared_count(const __shared_count&);
2450    __shared_count& operator=(const __shared_count&);
2451
2452protected:
2453    long __shared_owners_;
2454    virtual ~__shared_count();
2455private:
2456    virtual void __on_zero_shared() _NOEXCEPT = 0;
2457
2458public:
2459    _LIBCPP_INLINE_VISIBILITY
2460    explicit __shared_count(long __refs = 0) _NOEXCEPT
2461        : __shared_owners_(__refs) {}
2462
2463#if defined(_LIBCPP_BUILDING_LIBRARY) && \
2464    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
2465    void __add_shared() _NOEXCEPT;
2466    bool __release_shared() _NOEXCEPT;
2467#else
2468    _LIBCPP_INLINE_VISIBILITY
2469    void __add_shared() _NOEXCEPT {
2470      __libcpp_atomic_refcount_increment(__shared_owners_);
2471    }
2472    _LIBCPP_INLINE_VISIBILITY
2473    bool __release_shared() _NOEXCEPT {
2474      if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2475        __on_zero_shared();
2476        return true;
2477      }
2478      return false;
2479    }
2480#endif
2481    _LIBCPP_INLINE_VISIBILITY
2482    long use_count() const _NOEXCEPT {
2483        return __libcpp_relaxed_load(&__shared_owners_) + 1;
2484    }
2485};
2486
2487class _LIBCPP_TYPE_VIS __shared_weak_count
2488    : private __shared_count
2489{
2490    long __shared_weak_owners_;
2491
2492public:
2493    _LIBCPP_INLINE_VISIBILITY
2494    explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
2495        : __shared_count(__refs),
2496          __shared_weak_owners_(__refs) {}
2497protected:
2498    virtual ~__shared_weak_count();
2499
2500public:
2501#if defined(_LIBCPP_BUILDING_LIBRARY) && \
2502    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
2503    void __add_shared() _NOEXCEPT;
2504    void __add_weak() _NOEXCEPT;
2505    void __release_shared() _NOEXCEPT;
2506#else
2507    _LIBCPP_INLINE_VISIBILITY
2508    void __add_shared() _NOEXCEPT {
2509      __shared_count::__add_shared();
2510    }
2511    _LIBCPP_INLINE_VISIBILITY
2512    void __add_weak() _NOEXCEPT {
2513      __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2514    }
2515    _LIBCPP_INLINE_VISIBILITY
2516    void __release_shared() _NOEXCEPT {
2517      if (__shared_count::__release_shared())
2518        __release_weak();
2519    }
2520#endif
2521    void __release_weak() _NOEXCEPT;
2522    _LIBCPP_INLINE_VISIBILITY
2523    long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2524    __shared_weak_count* lock() _NOEXCEPT;
2525
2526    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
2527private:
2528    virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
2529};
2530
2531template <class _Tp, class _Dp, class _Alloc>
2532class __shared_ptr_pointer
2533    : public __shared_weak_count
2534{
2535    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2536public:
2537    _LIBCPP_INLINE_VISIBILITY
2538    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2539        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
2540
2541#ifndef _LIBCPP_NO_RTTI
2542    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
2543#endif
2544
2545private:
2546    virtual void __on_zero_shared() _NOEXCEPT;
2547    virtual void __on_zero_shared_weak() _NOEXCEPT;
2548};
2549
2550#ifndef _LIBCPP_NO_RTTI
2551
2552template <class _Tp, class _Dp, class _Alloc>
2553const void*
2554__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
2555{
2556    return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
2557}
2558
2559#endif  // _LIBCPP_NO_RTTI
2560
2561template <class _Tp, class _Dp, class _Alloc>
2562void
2563__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
2564{
2565    __data_.first().second()(__data_.first().first());
2566    __data_.first().second().~_Dp();
2567}
2568
2569template <class _Tp, class _Dp, class _Alloc>
2570void
2571__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
2572{
2573    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2574    typedef allocator_traits<_Al> _ATraits;
2575    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2576
2577    _Al __a(__data_.second());
2578    __data_.second().~_Alloc();
2579    __a.deallocate(_PTraits::pointer_to(*this), 1);
2580}
2581
2582template <class _Tp, class _Alloc>
2583struct __shared_ptr_emplace
2584    : __shared_weak_count
2585{
2586    template<class ..._Args>
2587    _LIBCPP_HIDE_FROM_ABI
2588    explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2589        : __storage_(_VSTD::move(__a))
2590    {
2591#if _LIBCPP_STD_VER > 17
2592        using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2593        _TpAlloc __tmp(*__get_alloc());
2594        allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
2595#else
2596        ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
2597#endif
2598    }
2599
2600    _LIBCPP_HIDE_FROM_ABI
2601    _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
2602
2603    _LIBCPP_HIDE_FROM_ABI
2604    _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
2605
2606private:
2607    virtual void __on_zero_shared() _NOEXCEPT {
2608#if _LIBCPP_STD_VER > 17
2609        using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2610        _TpAlloc __tmp(*__get_alloc());
2611        allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2612#else
2613        __get_elem()->~_Tp();
2614#endif
2615    }
2616
2617    virtual void __on_zero_shared_weak() _NOEXCEPT {
2618        using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2619        using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
2620        _ControlBlockAlloc __tmp(*__get_alloc());
2621        __storage_.~_Storage();
2622        allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2623            pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2624    }
2625
2626    // This class implements the control block for non-array shared pointers created
2627    // through `std::allocate_shared` and `std::make_shared`.
2628    //
2629    // In previous versions of the library, we used a compressed pair to store
2630    // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2631    // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2632    // we now use a properly aligned char buffer while making sure that we maintain
2633    // the same layout that we had when we used a compressed pair.
2634    using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2635    struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2636        char __blob_[sizeof(_CompressedPair)];
2637
2638        _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2639            ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2640        }
2641        _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2642            __get_alloc()->~_Alloc();
2643        }
2644        _Alloc* __get_alloc() _NOEXCEPT {
2645            _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2646            typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2647            _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2648            return __alloc;
2649        }
2650        _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
2651            _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2652            typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2653            _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2654            return __elem;
2655        }
2656    };
2657
2658    static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2659    static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2660    _Storage __storage_;
2661};
2662
2663struct __shared_ptr_dummy_rebind_allocator_type;
2664template <>
2665class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2666{
2667public:
2668    template <class _Other>
2669    struct rebind
2670    {
2671        typedef allocator<_Other> other;
2672    };
2673};
2674
2675template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
2676
2677template<class _Tp, class _Up>
2678struct __compatible_with
2679#if _LIBCPP_STD_VER > 14
2680    : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2681#else
2682    : is_convertible<_Tp*, _Up*> {};
2683#endif // _LIBCPP_STD_VER > 14
2684
2685#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2686#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2687#else
2688#  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2689#endif
2690
2691template<class _Tp>
2692class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
2693{
2694public:
2695#if _LIBCPP_STD_VER > 14
2696    typedef weak_ptr<_Tp> weak_type;
2697    typedef remove_extent_t<_Tp> element_type;
2698#else
2699    typedef _Tp element_type;
2700#endif
2701
2702private:
2703    element_type*      __ptr_;
2704    __shared_weak_count* __cntrl_;
2705
2706    struct __nat {int __for_bool_;};
2707public:
2708    _LIBCPP_INLINE_VISIBILITY
2709    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
2710    _LIBCPP_INLINE_VISIBILITY
2711    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
2712    template<class _Yp>
2713        explicit shared_ptr(_Yp* __p,
2714                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
2715    template<class _Yp, class _Dp>
2716        shared_ptr(_Yp* __p, _Dp __d,
2717                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
2718    template<class _Yp, class _Dp, class _Alloc>
2719        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
2720                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
2721    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2722    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
2723    template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2724    _LIBCPP_INLINE_VISIBILITY
2725    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
2726    template<class _Yp>
2727        _LIBCPP_INLINE_VISIBILITY
2728        shared_ptr(const shared_ptr<_Yp>& __r,
2729                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
2730                       _NOEXCEPT;
2731    _LIBCPP_INLINE_VISIBILITY
2732    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
2733    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
2734                   typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
2735                       _NOEXCEPT;
2736    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
2737                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
2738#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2739    template<class _Yp>
2740        shared_ptr(auto_ptr<_Yp>&& __r,
2741                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
2742#endif
2743    template <class _Yp, class _Dp>
2744        shared_ptr(unique_ptr<_Yp, _Dp>&&,
2745                   typename enable_if
2746                   <
2747                       !is_lvalue_reference<_Dp>::value &&
2748                       !is_array<_Yp>::value &&
2749                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2750                       __nat
2751                   >::type = __nat());
2752    template <class _Yp, class _Dp>
2753        shared_ptr(unique_ptr<_Yp, _Dp>&&,
2754                   typename enable_if
2755                   <
2756                       is_lvalue_reference<_Dp>::value &&
2757                       !is_array<_Yp>::value &&
2758                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2759                       __nat
2760                   >::type = __nat());
2761
2762    ~shared_ptr();
2763
2764    _LIBCPP_INLINE_VISIBILITY
2765    shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
2766    template<class _Yp>
2767        typename enable_if
2768        <
2769            __compatible_with<_Yp, element_type>::value,
2770            shared_ptr&
2771        >::type
2772        _LIBCPP_INLINE_VISIBILITY
2773        operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
2774    _LIBCPP_INLINE_VISIBILITY
2775    shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
2776    template<class _Yp>
2777        typename enable_if
2778        <
2779            __compatible_with<_Yp, element_type>::value,
2780            shared_ptr&
2781        >::type
2782        _LIBCPP_INLINE_VISIBILITY
2783        operator=(shared_ptr<_Yp>&& __r);
2784#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2785    template<class _Yp>
2786        _LIBCPP_INLINE_VISIBILITY
2787        typename enable_if
2788        <
2789            !is_array<_Yp>::value &&
2790            is_convertible<_Yp*, element_type*>::value,
2791            shared_ptr
2792        >::type&
2793        operator=(auto_ptr<_Yp>&& __r);
2794#endif
2795    template <class _Yp, class _Dp>
2796        typename enable_if
2797        <
2798            !is_array<_Yp>::value &&
2799            is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2800            shared_ptr&
2801        >::type
2802        _LIBCPP_INLINE_VISIBILITY
2803        operator=(unique_ptr<_Yp, _Dp>&& __r);
2804
2805    _LIBCPP_INLINE_VISIBILITY
2806    void swap(shared_ptr& __r) _NOEXCEPT;
2807    _LIBCPP_INLINE_VISIBILITY
2808    void reset() _NOEXCEPT;
2809    template<class _Yp>
2810        typename enable_if
2811        <
2812            __compatible_with<_Yp, element_type>::value,
2813            void
2814        >::type
2815        _LIBCPP_INLINE_VISIBILITY
2816        reset(_Yp* __p);
2817    template<class _Yp, class _Dp>
2818        typename enable_if
2819        <
2820            __compatible_with<_Yp, element_type>::value,
2821            void
2822        >::type
2823        _LIBCPP_INLINE_VISIBILITY
2824        reset(_Yp* __p, _Dp __d);
2825    template<class _Yp, class _Dp, class _Alloc>
2826        typename enable_if
2827        <
2828            __compatible_with<_Yp, element_type>::value,
2829            void
2830        >::type
2831        _LIBCPP_INLINE_VISIBILITY
2832        reset(_Yp* __p, _Dp __d, _Alloc __a);
2833
2834    _LIBCPP_INLINE_VISIBILITY
2835    element_type* get() const _NOEXCEPT {return __ptr_;}
2836    _LIBCPP_INLINE_VISIBILITY
2837    typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2838        {return *__ptr_;}
2839    _LIBCPP_INLINE_VISIBILITY
2840    element_type* operator->() const _NOEXCEPT
2841    {
2842        static_assert(!_VSTD::is_array<_Tp>::value,
2843                      "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2844        return __ptr_;
2845    }
2846    _LIBCPP_INLINE_VISIBILITY
2847    long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
2848    _LIBCPP_INLINE_VISIBILITY
2849    bool unique() const _NOEXCEPT {return use_count() == 1;}
2850    _LIBCPP_INLINE_VISIBILITY
2851    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
2852    template <class _Up>
2853        _LIBCPP_INLINE_VISIBILITY
2854        bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
2855        {return __cntrl_ < __p.__cntrl_;}
2856    template <class _Up>
2857        _LIBCPP_INLINE_VISIBILITY
2858        bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
2859        {return __cntrl_ < __p.__cntrl_;}
2860    _LIBCPP_INLINE_VISIBILITY
2861    bool
2862    __owner_equivalent(const shared_ptr& __p) const
2863        {return __cntrl_ == __p.__cntrl_;}
2864
2865#if _LIBCPP_STD_VER > 14
2866    typename add_lvalue_reference<element_type>::type
2867    _LIBCPP_INLINE_VISIBILITY
2868    operator[](ptrdiff_t __i) const
2869    {
2870            static_assert(_VSTD::is_array<_Tp>::value,
2871                          "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2872            return __ptr_[__i];
2873    }
2874#endif
2875
2876#ifndef _LIBCPP_NO_RTTI
2877    template <class _Dp>
2878        _LIBCPP_INLINE_VISIBILITY
2879        _Dp* __get_deleter() const _NOEXCEPT
2880            {return static_cast<_Dp*>(__cntrl_
2881                    ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
2882                      : nullptr);}
2883#endif  // _LIBCPP_NO_RTTI
2884
2885    template<class _Yp, class _CntrlBlk>
2886    static shared_ptr<_Tp>
2887    __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
2888    {
2889        shared_ptr<_Tp> __r;
2890        __r.__ptr_ = __p;
2891        __r.__cntrl_ = __cntrl;
2892        __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2893        return __r;
2894    }
2895
2896private:
2897    template <class _Yp, bool = is_function<_Yp>::value>
2898        struct __shared_ptr_default_allocator
2899        {
2900            typedef allocator<_Yp> type;
2901        };
2902
2903    template <class _Yp>
2904        struct __shared_ptr_default_allocator<_Yp, true>
2905        {
2906            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2907        };
2908
2909    template <class _Yp, class _OrigPtr>
2910        _LIBCPP_INLINE_VISIBILITY
2911        typename enable_if<is_convertible<_OrigPtr*,
2912                                          const enable_shared_from_this<_Yp>*
2913        >::value,
2914            void>::type
2915        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2916                           _OrigPtr* __ptr) _NOEXCEPT
2917        {
2918            typedef typename remove_cv<_Yp>::type _RawYp;
2919            if (__e && __e->__weak_this_.expired())
2920            {
2921                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2922                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
2923            }
2924        }
2925
2926    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
2927
2928    template <class, class _Yp>
2929        struct __shared_ptr_default_delete
2930            : default_delete<_Yp> {};
2931
2932    template <class _Yp, class _Un, size_t _Sz>
2933        struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2934            : default_delete<_Yp[]> {};
2935
2936    template <class _Yp, class _Un>
2937        struct __shared_ptr_default_delete<_Yp[], _Un>
2938            : default_delete<_Yp[]> {};
2939
2940    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2941    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
2942};
2943
2944#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2945template<class _Tp>
2946shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2947template<class _Tp, class _Dp>
2948shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2949#endif
2950
2951template<class _Tp>
2952inline
2953_LIBCPP_CONSTEXPR
2954shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
2955    : __ptr_(nullptr),
2956      __cntrl_(nullptr)
2957{
2958}
2959
2960template<class _Tp>
2961inline
2962_LIBCPP_CONSTEXPR
2963shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
2964    : __ptr_(nullptr),
2965      __cntrl_(nullptr)
2966{
2967}
2968
2969template<class _Tp>
2970template<class _Yp>
2971shared_ptr<_Tp>::shared_ptr(_Yp* __p,
2972                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
2973    : __ptr_(__p)
2974{
2975    unique_ptr<_Yp> __hold(__p);
2976    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2977    typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2978    __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
2979    __hold.release();
2980    __enable_weak_this(__p, __p);
2981}
2982
2983template<class _Tp>
2984template<class _Yp, class _Dp>
2985shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
2986                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
2987    : __ptr_(__p)
2988{
2989#ifndef _LIBCPP_NO_EXCEPTIONS
2990    try
2991    {
2992#endif  // _LIBCPP_NO_EXCEPTIONS
2993        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2994        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
2995        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
2996        __enable_weak_this(__p, __p);
2997#ifndef _LIBCPP_NO_EXCEPTIONS
2998    }
2999    catch (...)
3000    {
3001        __d(__p);
3002        throw;
3003    }
3004#endif  // _LIBCPP_NO_EXCEPTIONS
3005}
3006
3007template<class _Tp>
3008template<class _Dp>
3009shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3010    : __ptr_(nullptr)
3011{
3012#ifndef _LIBCPP_NO_EXCEPTIONS
3013    try
3014    {
3015#endif  // _LIBCPP_NO_EXCEPTIONS
3016        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3017        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3018        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
3019#ifndef _LIBCPP_NO_EXCEPTIONS
3020    }
3021    catch (...)
3022    {
3023        __d(__p);
3024        throw;
3025    }
3026#endif  // _LIBCPP_NO_EXCEPTIONS
3027}
3028
3029template<class _Tp>
3030template<class _Yp, class _Dp, class _Alloc>
3031shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3032                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3033    : __ptr_(__p)
3034{
3035#ifndef _LIBCPP_NO_EXCEPTIONS
3036    try
3037    {
3038#endif  // _LIBCPP_NO_EXCEPTIONS
3039        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3040        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3041        typedef __allocator_destructor<_A2> _D2;
3042        _A2 __a2(__a);
3043        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3044        ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
3045        __cntrl_ = _VSTD::addressof(*__hold2.release());
3046        __enable_weak_this(__p, __p);
3047#ifndef _LIBCPP_NO_EXCEPTIONS
3048    }
3049    catch (...)
3050    {
3051        __d(__p);
3052        throw;
3053    }
3054#endif  // _LIBCPP_NO_EXCEPTIONS
3055}
3056
3057template<class _Tp>
3058template<class _Dp, class _Alloc>
3059shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3060    : __ptr_(nullptr)
3061{
3062#ifndef _LIBCPP_NO_EXCEPTIONS
3063    try
3064    {
3065#endif  // _LIBCPP_NO_EXCEPTIONS
3066        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3067        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
3068        typedef __allocator_destructor<_A2> _D2;
3069        _A2 __a2(__a);
3070        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3071        ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
3072        __cntrl_ = _VSTD::addressof(*__hold2.release());
3073#ifndef _LIBCPP_NO_EXCEPTIONS
3074    }
3075    catch (...)
3076    {
3077        __d(__p);
3078        throw;
3079    }
3080#endif  // _LIBCPP_NO_EXCEPTIONS
3081}
3082
3083template<class _Tp>
3084template<class _Yp>
3085inline
3086shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
3087    : __ptr_(__p),
3088      __cntrl_(__r.__cntrl_)
3089{
3090    if (__cntrl_)
3091        __cntrl_->__add_shared();
3092}
3093
3094template<class _Tp>
3095inline
3096shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
3097    : __ptr_(__r.__ptr_),
3098      __cntrl_(__r.__cntrl_)
3099{
3100    if (__cntrl_)
3101        __cntrl_->__add_shared();
3102}
3103
3104template<class _Tp>
3105template<class _Yp>
3106inline
3107shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3108                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3109         _NOEXCEPT
3110    : __ptr_(__r.__ptr_),
3111      __cntrl_(__r.__cntrl_)
3112{
3113    if (__cntrl_)
3114        __cntrl_->__add_shared();
3115}
3116
3117template<class _Tp>
3118inline
3119shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
3120    : __ptr_(__r.__ptr_),
3121      __cntrl_(__r.__cntrl_)
3122{
3123    __r.__ptr_ = nullptr;
3124    __r.__cntrl_ = nullptr;
3125}
3126
3127template<class _Tp>
3128template<class _Yp>
3129inline
3130shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3131                            typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
3132         _NOEXCEPT
3133    : __ptr_(__r.__ptr_),
3134      __cntrl_(__r.__cntrl_)
3135{
3136    __r.__ptr_ = nullptr;
3137    __r.__cntrl_ = nullptr;
3138}
3139
3140#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3141template<class _Tp>
3142template<class _Yp>
3143shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
3144                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3145    : __ptr_(__r.get())
3146{
3147    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3148    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3149    __enable_weak_this(__r.get(), __r.get());
3150    __r.release();
3151}
3152#endif
3153
3154template<class _Tp>
3155template <class _Yp, class _Dp>
3156shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3157                            typename enable_if
3158                            <
3159                                !is_lvalue_reference<_Dp>::value &&
3160                                !is_array<_Yp>::value &&
3161                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3162                                __nat
3163                            >::type)
3164    : __ptr_(__r.get())
3165{
3166#if _LIBCPP_STD_VER > 11
3167    if (__ptr_ == nullptr)
3168        __cntrl_ = nullptr;
3169    else
3170#endif
3171    {
3172        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3173        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3174        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
3175        __enable_weak_this(__r.get(), __r.get());
3176    }
3177    __r.release();
3178}
3179
3180template<class _Tp>
3181template <class _Yp, class _Dp>
3182shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3183                            typename enable_if
3184                            <
3185                                is_lvalue_reference<_Dp>::value &&
3186                                !is_array<_Yp>::value &&
3187                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3188                                __nat
3189                            >::type)
3190    : __ptr_(__r.get())
3191{
3192#if _LIBCPP_STD_VER > 11
3193    if (__ptr_ == nullptr)
3194        __cntrl_ = nullptr;
3195    else
3196#endif
3197    {
3198        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3199        typedef __shared_ptr_pointer<_Yp*,
3200                                     reference_wrapper<typename remove_reference<_Dp>::type>,
3201                                     _AllocT > _CntrlBlk;
3202        __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
3203        __enable_weak_this(__r.get(), __r.get());
3204    }
3205    __r.release();
3206}
3207
3208template<class _Tp>
3209shared_ptr<_Tp>::~shared_ptr()
3210{
3211    if (__cntrl_)
3212        __cntrl_->__release_shared();
3213}
3214
3215template<class _Tp>
3216inline
3217shared_ptr<_Tp>&
3218shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
3219{
3220    shared_ptr(__r).swap(*this);
3221    return *this;
3222}
3223
3224template<class _Tp>
3225template<class _Yp>
3226inline
3227typename enable_if
3228<
3229    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3230    shared_ptr<_Tp>&
3231>::type
3232shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
3233{
3234    shared_ptr(__r).swap(*this);
3235    return *this;
3236}
3237
3238template<class _Tp>
3239inline
3240shared_ptr<_Tp>&
3241shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
3242{
3243    shared_ptr(_VSTD::move(__r)).swap(*this);
3244    return *this;
3245}
3246
3247template<class _Tp>
3248template<class _Yp>
3249inline
3250typename enable_if
3251<
3252    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3253    shared_ptr<_Tp>&
3254>::type
3255shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3256{
3257    shared_ptr(_VSTD::move(__r)).swap(*this);
3258    return *this;
3259}
3260
3261#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
3262template<class _Tp>
3263template<class _Yp>
3264inline
3265typename enable_if
3266<
3267    !is_array<_Yp>::value &&
3268    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
3269    shared_ptr<_Tp>
3270>::type&
3271shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3272{
3273    shared_ptr(_VSTD::move(__r)).swap(*this);
3274    return *this;
3275}
3276#endif
3277
3278template<class _Tp>
3279template <class _Yp, class _Dp>
3280inline
3281typename enable_if
3282<
3283    !is_array<_Yp>::value &&
3284    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
3285                   typename shared_ptr<_Tp>::element_type*>::value,
3286    shared_ptr<_Tp>&
3287>::type
3288shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3289{
3290    shared_ptr(_VSTD::move(__r)).swap(*this);
3291    return *this;
3292}
3293
3294template<class _Tp>
3295inline
3296void
3297shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
3298{
3299    _VSTD::swap(__ptr_, __r.__ptr_);
3300    _VSTD::swap(__cntrl_, __r.__cntrl_);
3301}
3302
3303template<class _Tp>
3304inline
3305void
3306shared_ptr<_Tp>::reset() _NOEXCEPT
3307{
3308    shared_ptr().swap(*this);
3309}
3310
3311template<class _Tp>
3312template<class _Yp>
3313inline
3314typename enable_if
3315<
3316    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3317    void
3318>::type
3319shared_ptr<_Tp>::reset(_Yp* __p)
3320{
3321    shared_ptr(__p).swap(*this);
3322}
3323
3324template<class _Tp>
3325template<class _Yp, class _Dp>
3326inline
3327typename enable_if
3328<
3329    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3330    void
3331>::type
3332shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3333{
3334    shared_ptr(__p, __d).swap(*this);
3335}
3336
3337template<class _Tp>
3338template<class _Yp, class _Dp, class _Alloc>
3339inline
3340typename enable_if
3341<
3342    __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
3343    void
3344>::type
3345shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3346{
3347    shared_ptr(__p, __d, __a).swap(*this);
3348}
3349
3350//
3351// std::allocate_shared and std::make_shared
3352//
3353template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3354_LIBCPP_HIDE_FROM_ABI
3355shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
3356{
3357    using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3358    using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3359    __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3360    ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3361    auto __control_block = __guard.__release_ptr();
3362    return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
3363}
3364
3365template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3366_LIBCPP_HIDE_FROM_ABI
3367shared_ptr<_Tp> make_shared(_Args&& ...__args)
3368{
3369    return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3370}
3371
3372template<class _Tp, class _Up>
3373inline _LIBCPP_INLINE_VISIBILITY
3374bool
3375operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3376{
3377    return __x.get() == __y.get();
3378}
3379
3380template<class _Tp, class _Up>
3381inline _LIBCPP_INLINE_VISIBILITY
3382bool
3383operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3384{
3385    return !(__x == __y);
3386}
3387
3388template<class _Tp, class _Up>
3389inline _LIBCPP_INLINE_VISIBILITY
3390bool
3391operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3392{
3393#if _LIBCPP_STD_VER <= 11
3394    typedef typename common_type<_Tp*, _Up*>::type _Vp;
3395    return less<_Vp>()(__x.get(), __y.get());
3396#else
3397    return less<>()(__x.get(), __y.get());
3398#endif
3399
3400}
3401
3402template<class _Tp, class _Up>
3403inline _LIBCPP_INLINE_VISIBILITY
3404bool
3405operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3406{
3407    return __y < __x;
3408}
3409
3410template<class _Tp, class _Up>
3411inline _LIBCPP_INLINE_VISIBILITY
3412bool
3413operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3414{
3415    return !(__y < __x);
3416}
3417
3418template<class _Tp, class _Up>
3419inline _LIBCPP_INLINE_VISIBILITY
3420bool
3421operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3422{
3423    return !(__x < __y);
3424}
3425
3426template<class _Tp>
3427inline _LIBCPP_INLINE_VISIBILITY
3428bool
3429operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3430{
3431    return !__x;
3432}
3433
3434template<class _Tp>
3435inline _LIBCPP_INLINE_VISIBILITY
3436bool
3437operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3438{
3439    return !__x;
3440}
3441
3442template<class _Tp>
3443inline _LIBCPP_INLINE_VISIBILITY
3444bool
3445operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3446{
3447    return static_cast<bool>(__x);
3448}
3449
3450template<class _Tp>
3451inline _LIBCPP_INLINE_VISIBILITY
3452bool
3453operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3454{
3455    return static_cast<bool>(__x);
3456}
3457
3458template<class _Tp>
3459inline _LIBCPP_INLINE_VISIBILITY
3460bool
3461operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3462{
3463    return less<_Tp*>()(__x.get(), nullptr);
3464}
3465
3466template<class _Tp>
3467inline _LIBCPP_INLINE_VISIBILITY
3468bool
3469operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3470{
3471    return less<_Tp*>()(nullptr, __x.get());
3472}
3473
3474template<class _Tp>
3475inline _LIBCPP_INLINE_VISIBILITY
3476bool
3477operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3478{
3479    return nullptr < __x;
3480}
3481
3482template<class _Tp>
3483inline _LIBCPP_INLINE_VISIBILITY
3484bool
3485operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3486{
3487    return __x < nullptr;
3488}
3489
3490template<class _Tp>
3491inline _LIBCPP_INLINE_VISIBILITY
3492bool
3493operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3494{
3495    return !(nullptr < __x);
3496}
3497
3498template<class _Tp>
3499inline _LIBCPP_INLINE_VISIBILITY
3500bool
3501operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3502{
3503    return !(__x < nullptr);
3504}
3505
3506template<class _Tp>
3507inline _LIBCPP_INLINE_VISIBILITY
3508bool
3509operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3510{
3511    return !(__x < nullptr);
3512}
3513
3514template<class _Tp>
3515inline _LIBCPP_INLINE_VISIBILITY
3516bool
3517operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3518{
3519    return !(nullptr < __x);
3520}
3521
3522template<class _Tp>
3523inline _LIBCPP_INLINE_VISIBILITY
3524void
3525swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
3526{
3527    __x.swap(__y);
3528}
3529
3530template<class _Tp, class _Up>
3531inline _LIBCPP_INLINE_VISIBILITY
3532shared_ptr<_Tp>
3533static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3534{
3535    return shared_ptr<_Tp>(__r,
3536                           static_cast<
3537                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
3538}
3539
3540template<class _Tp, class _Up>
3541inline _LIBCPP_INLINE_VISIBILITY
3542shared_ptr<_Tp>
3543dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3544{
3545    typedef typename shared_ptr<_Tp>::element_type _ET;
3546    _ET* __p = dynamic_cast<_ET*>(__r.get());
3547    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3548}
3549
3550template<class _Tp, class _Up>
3551shared_ptr<_Tp>
3552const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3553{
3554    typedef typename shared_ptr<_Tp>::element_type _RTp;
3555    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
3556}
3557
3558template<class _Tp, class _Up>
3559shared_ptr<_Tp>
3560reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3561{
3562    return shared_ptr<_Tp>(__r,
3563                           reinterpret_cast<
3564                               typename shared_ptr<_Tp>::element_type*>(__r.get()));
3565}
3566
3567#ifndef _LIBCPP_NO_RTTI
3568
3569template<class _Dp, class _Tp>
3570inline _LIBCPP_INLINE_VISIBILITY
3571_Dp*
3572get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
3573{
3574    return __p.template __get_deleter<_Dp>();
3575}
3576
3577#endif  // _LIBCPP_NO_RTTI
3578
3579template<class _Tp>
3580class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
3581{
3582public:
3583    typedef _Tp element_type;
3584private:
3585    element_type*        __ptr_;
3586    __shared_weak_count* __cntrl_;
3587
3588public:
3589    _LIBCPP_INLINE_VISIBILITY
3590    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
3591    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
3592                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3593                        _NOEXCEPT;
3594    _LIBCPP_INLINE_VISIBILITY
3595    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
3596    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
3597                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3598                         _NOEXCEPT;
3599
3600    _LIBCPP_INLINE_VISIBILITY
3601    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
3602    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
3603                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3604                         _NOEXCEPT;
3605    ~weak_ptr();
3606
3607    _LIBCPP_INLINE_VISIBILITY
3608    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3609    template<class _Yp>
3610        typename enable_if
3611        <
3612            is_convertible<_Yp*, element_type*>::value,
3613            weak_ptr&
3614        >::type
3615        _LIBCPP_INLINE_VISIBILITY
3616        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3617
3618    _LIBCPP_INLINE_VISIBILITY
3619    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3620    template<class _Yp>
3621        typename enable_if
3622        <
3623            is_convertible<_Yp*, element_type*>::value,
3624            weak_ptr&
3625        >::type
3626        _LIBCPP_INLINE_VISIBILITY
3627        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3628
3629    template<class _Yp>
3630        typename enable_if
3631        <
3632            is_convertible<_Yp*, element_type*>::value,
3633            weak_ptr&
3634        >::type
3635        _LIBCPP_INLINE_VISIBILITY
3636        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
3637
3638    _LIBCPP_INLINE_VISIBILITY
3639    void swap(weak_ptr& __r) _NOEXCEPT;
3640    _LIBCPP_INLINE_VISIBILITY
3641    void reset() _NOEXCEPT;
3642
3643    _LIBCPP_INLINE_VISIBILITY
3644    long use_count() const _NOEXCEPT
3645        {return __cntrl_ ? __cntrl_->use_count() : 0;}
3646    _LIBCPP_INLINE_VISIBILITY
3647    bool expired() const _NOEXCEPT
3648        {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
3649    shared_ptr<_Tp> lock() const _NOEXCEPT;
3650    template<class _Up>
3651        _LIBCPP_INLINE_VISIBILITY
3652        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
3653        {return __cntrl_ < __r.__cntrl_;}
3654    template<class _Up>
3655        _LIBCPP_INLINE_VISIBILITY
3656        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
3657        {return __cntrl_ < __r.__cntrl_;}
3658
3659    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3660    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3661};
3662
3663#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3664template<class _Tp>
3665weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3666#endif
3667
3668template<class _Tp>
3669inline
3670_LIBCPP_CONSTEXPR
3671weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
3672    : __ptr_(nullptr),
3673      __cntrl_(nullptr)
3674{
3675}
3676
3677template<class _Tp>
3678inline
3679weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
3680    : __ptr_(__r.__ptr_),
3681      __cntrl_(__r.__cntrl_)
3682{
3683    if (__cntrl_)
3684        __cntrl_->__add_weak();
3685}
3686
3687template<class _Tp>
3688template<class _Yp>
3689inline
3690weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3691                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3692                         _NOEXCEPT
3693    : __ptr_(__r.__ptr_),
3694      __cntrl_(__r.__cntrl_)
3695{
3696    if (__cntrl_)
3697        __cntrl_->__add_weak();
3698}
3699
3700template<class _Tp>
3701template<class _Yp>
3702inline
3703weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3704                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3705         _NOEXCEPT
3706    : __ptr_(__r.__ptr_),
3707      __cntrl_(__r.__cntrl_)
3708{
3709    if (__cntrl_)
3710        __cntrl_->__add_weak();
3711}
3712
3713template<class _Tp>
3714inline
3715weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3716    : __ptr_(__r.__ptr_),
3717      __cntrl_(__r.__cntrl_)
3718{
3719    __r.__ptr_ = nullptr;
3720    __r.__cntrl_ = nullptr;
3721}
3722
3723template<class _Tp>
3724template<class _Yp>
3725inline
3726weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3727                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3728         _NOEXCEPT
3729    : __ptr_(__r.__ptr_),
3730      __cntrl_(__r.__cntrl_)
3731{
3732    __r.__ptr_ = nullptr;
3733    __r.__cntrl_ = nullptr;
3734}
3735
3736template<class _Tp>
3737weak_ptr<_Tp>::~weak_ptr()
3738{
3739    if (__cntrl_)
3740        __cntrl_->__release_weak();
3741}
3742
3743template<class _Tp>
3744inline
3745weak_ptr<_Tp>&
3746weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
3747{
3748    weak_ptr(__r).swap(*this);
3749    return *this;
3750}
3751
3752template<class _Tp>
3753template<class _Yp>
3754inline
3755typename enable_if
3756<
3757    is_convertible<_Yp*, _Tp*>::value,
3758    weak_ptr<_Tp>&
3759>::type
3760weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
3761{
3762    weak_ptr(__r).swap(*this);
3763    return *this;
3764}
3765
3766template<class _Tp>
3767inline
3768weak_ptr<_Tp>&
3769weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3770{
3771    weak_ptr(_VSTD::move(__r)).swap(*this);
3772    return *this;
3773}
3774
3775template<class _Tp>
3776template<class _Yp>
3777inline
3778typename enable_if
3779<
3780    is_convertible<_Yp*, _Tp*>::value,
3781    weak_ptr<_Tp>&
3782>::type
3783weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3784{
3785    weak_ptr(_VSTD::move(__r)).swap(*this);
3786    return *this;
3787}
3788
3789template<class _Tp>
3790template<class _Yp>
3791inline
3792typename enable_if
3793<
3794    is_convertible<_Yp*, _Tp*>::value,
3795    weak_ptr<_Tp>&
3796>::type
3797weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
3798{
3799    weak_ptr(__r).swap(*this);
3800    return *this;
3801}
3802
3803template<class _Tp>
3804inline
3805void
3806weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
3807{
3808    _VSTD::swap(__ptr_, __r.__ptr_);
3809    _VSTD::swap(__cntrl_, __r.__cntrl_);
3810}
3811
3812template<class _Tp>
3813inline _LIBCPP_INLINE_VISIBILITY
3814void
3815swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
3816{
3817    __x.swap(__y);
3818}
3819
3820template<class _Tp>
3821inline
3822void
3823weak_ptr<_Tp>::reset() _NOEXCEPT
3824{
3825    weak_ptr().swap(*this);
3826}
3827
3828template<class _Tp>
3829template<class _Yp>
3830shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3831                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
3832    : __ptr_(__r.__ptr_),
3833      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3834{
3835    if (__cntrl_ == nullptr)
3836        __throw_bad_weak_ptr();
3837}
3838
3839template<class _Tp>
3840shared_ptr<_Tp>
3841weak_ptr<_Tp>::lock() const _NOEXCEPT
3842{
3843    shared_ptr<_Tp> __r;
3844    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3845    if (__r.__cntrl_)
3846        __r.__ptr_ = __ptr_;
3847    return __r;
3848}
3849
3850#if _LIBCPP_STD_VER > 14
3851template <class _Tp = void> struct owner_less;
3852#else
3853template <class _Tp> struct owner_less;
3854#endif
3855
3856template <class _Tp>
3857struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
3858    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
3859{
3860    typedef bool result_type;
3861    _LIBCPP_INLINE_VISIBILITY
3862    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
3863        {return __x.owner_before(__y);}
3864    _LIBCPP_INLINE_VISIBILITY
3865    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
3866        {return __x.owner_before(__y);}
3867    _LIBCPP_INLINE_VISIBILITY
3868    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
3869        {return __x.owner_before(__y);}
3870};
3871
3872template <class _Tp>
3873struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
3874    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3875{
3876    typedef bool result_type;
3877    _LIBCPP_INLINE_VISIBILITY
3878    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
3879        {return __x.owner_before(__y);}
3880    _LIBCPP_INLINE_VISIBILITY
3881    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
3882        {return __x.owner_before(__y);}
3883    _LIBCPP_INLINE_VISIBILITY
3884    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
3885        {return __x.owner_before(__y);}
3886};
3887
3888#if _LIBCPP_STD_VER > 14
3889template <>
3890struct _LIBCPP_TEMPLATE_VIS owner_less<void>
3891{
3892    template <class _Tp, class _Up>
3893    _LIBCPP_INLINE_VISIBILITY
3894    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
3895        {return __x.owner_before(__y);}
3896    template <class _Tp, class _Up>
3897    _LIBCPP_INLINE_VISIBILITY
3898    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
3899        {return __x.owner_before(__y);}
3900    template <class _Tp, class _Up>
3901    _LIBCPP_INLINE_VISIBILITY
3902    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
3903        {return __x.owner_before(__y);}
3904    template <class _Tp, class _Up>
3905    _LIBCPP_INLINE_VISIBILITY
3906    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
3907        {return __x.owner_before(__y);}
3908    typedef void is_transparent;
3909};
3910#endif
3911
3912template<class _Tp>
3913class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
3914{
3915    mutable weak_ptr<_Tp> __weak_this_;
3916protected:
3917    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
3918    enable_shared_from_this() _NOEXCEPT {}
3919    _LIBCPP_INLINE_VISIBILITY
3920    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
3921    _LIBCPP_INLINE_VISIBILITY
3922    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3923        {return *this;}
3924    _LIBCPP_INLINE_VISIBILITY
3925    ~enable_shared_from_this() {}
3926public:
3927    _LIBCPP_INLINE_VISIBILITY
3928    shared_ptr<_Tp> shared_from_this()
3929        {return shared_ptr<_Tp>(__weak_this_);}
3930    _LIBCPP_INLINE_VISIBILITY
3931    shared_ptr<_Tp const> shared_from_this() const
3932        {return shared_ptr<const _Tp>(__weak_this_);}
3933
3934#if _LIBCPP_STD_VER > 14
3935    _LIBCPP_INLINE_VISIBILITY
3936    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3937       { return __weak_this_; }
3938
3939    _LIBCPP_INLINE_VISIBILITY
3940    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3941        { return __weak_this_; }
3942#endif // _LIBCPP_STD_VER > 14
3943
3944    template <class _Up> friend class shared_ptr;
3945};
3946
3947template <class _Tp>
3948struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
3949{
3950    typedef shared_ptr<_Tp>      argument_type;
3951    typedef size_t               result_type;
3952
3953    _LIBCPP_INLINE_VISIBILITY
3954    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
3955    {
3956        return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
3957    }
3958};
3959
3960template<class _CharT, class _Traits, class _Yp>
3961inline _LIBCPP_INLINE_VISIBILITY
3962basic_ostream<_CharT, _Traits>&
3963operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
3964
3965
3966#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
3967
3968class _LIBCPP_TYPE_VIS __sp_mut
3969{
3970    void* __lx;
3971public:
3972    void lock() _NOEXCEPT;
3973    void unlock() _NOEXCEPT;
3974
3975private:
3976    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3977    __sp_mut(const __sp_mut&);
3978    __sp_mut& operator=(const __sp_mut&);
3979
3980    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
3981};
3982
3983_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3984__sp_mut& __get_sp_mut(const void*);
3985
3986template <class _Tp>
3987inline _LIBCPP_INLINE_VISIBILITY
3988bool
3989atomic_is_lock_free(const shared_ptr<_Tp>*)
3990{
3991    return false;
3992}
3993
3994template <class _Tp>
3995_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3996shared_ptr<_Tp>
3997atomic_load(const shared_ptr<_Tp>* __p)
3998{
3999    __sp_mut& __m = __get_sp_mut(__p);
4000    __m.lock();
4001    shared_ptr<_Tp> __q = *__p;
4002    __m.unlock();
4003    return __q;
4004}
4005
4006template <class _Tp>
4007inline _LIBCPP_INLINE_VISIBILITY
4008_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4009shared_ptr<_Tp>
4010atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4011{
4012    return atomic_load(__p);
4013}
4014
4015template <class _Tp>
4016_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4017void
4018atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4019{
4020    __sp_mut& __m = __get_sp_mut(__p);
4021    __m.lock();
4022    __p->swap(__r);
4023    __m.unlock();
4024}
4025
4026template <class _Tp>
4027inline _LIBCPP_INLINE_VISIBILITY
4028_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4029void
4030atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4031{
4032    atomic_store(__p, __r);
4033}
4034
4035template <class _Tp>
4036_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4037shared_ptr<_Tp>
4038atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4039{
4040    __sp_mut& __m = __get_sp_mut(__p);
4041    __m.lock();
4042    __p->swap(__r);
4043    __m.unlock();
4044    return __r;
4045}
4046
4047template <class _Tp>
4048inline _LIBCPP_INLINE_VISIBILITY
4049_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4050shared_ptr<_Tp>
4051atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4052{
4053    return atomic_exchange(__p, __r);
4054}
4055
4056template <class _Tp>
4057_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4058bool
4059atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4060{
4061    shared_ptr<_Tp> __temp;
4062    __sp_mut& __m = __get_sp_mut(__p);
4063    __m.lock();
4064    if (__p->__owner_equivalent(*__v))
4065    {
4066        _VSTD::swap(__temp, *__p);
4067        *__p = __w;
4068        __m.unlock();
4069        return true;
4070    }
4071    _VSTD::swap(__temp, *__v);
4072    *__v = *__p;
4073    __m.unlock();
4074    return false;
4075}
4076
4077template <class _Tp>
4078inline _LIBCPP_INLINE_VISIBILITY
4079_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4080bool
4081atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4082{
4083    return atomic_compare_exchange_strong(__p, __v, __w);
4084}
4085
4086template <class _Tp>
4087inline _LIBCPP_INLINE_VISIBILITY
4088_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4089bool
4090atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4091                                        shared_ptr<_Tp> __w, memory_order, memory_order)
4092{
4093    return atomic_compare_exchange_strong(__p, __v, __w);
4094}
4095
4096template <class _Tp>
4097inline _LIBCPP_INLINE_VISIBILITY
4098_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4099bool
4100atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4101                                      shared_ptr<_Tp> __w, memory_order, memory_order)
4102{
4103    return atomic_compare_exchange_weak(__p, __v, __w);
4104}
4105
4106#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
4107
4108//enum class
4109#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4110# ifndef _LIBCPP_CXX03_LANG
4111enum class pointer_safety : unsigned char {
4112  relaxed,
4113  preferred,
4114  strict
4115};
4116# endif
4117#else
4118struct _LIBCPP_TYPE_VIS pointer_safety
4119{
4120    enum __lx
4121    {
4122        relaxed,
4123        preferred,
4124        strict
4125    };
4126
4127    __lx __v_;
4128
4129    _LIBCPP_INLINE_VISIBILITY
4130    pointer_safety() : __v_() {}
4131
4132    _LIBCPP_INLINE_VISIBILITY
4133    pointer_safety(__lx __v) : __v_(__v) {}
4134    _LIBCPP_INLINE_VISIBILITY
4135    operator int() const {return __v_;}
4136};
4137#endif
4138
4139#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
4140    defined(_LIBCPP_BUILDING_LIBRARY)
4141_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4142#else
4143// This function is only offered in C++03 under ABI v1.
4144# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4145inline _LIBCPP_INLINE_VISIBILITY
4146pointer_safety get_pointer_safety() _NOEXCEPT {
4147  return pointer_safety::relaxed;
4148}
4149# endif
4150#endif
4151
4152
4153_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4154_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4155_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
4156_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
4157
4158template <class _Tp>
4159inline _LIBCPP_INLINE_VISIBILITY
4160_Tp*
4161undeclare_reachable(_Tp* __p)
4162{
4163    return static_cast<_Tp*>(__undeclare_reachable(__p));
4164}
4165
4166_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
4167
4168// --- Helper for container swap --
4169template <typename _Alloc>
4170_LIBCPP_INLINE_VISIBILITY
4171void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4172#if _LIBCPP_STD_VER >= 14
4173    _NOEXCEPT
4174#else
4175    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4176#endif
4177{
4178    using _VSTD::swap;
4179    swap(__a1, __a2);
4180}
4181
4182template <typename _Alloc>
4183inline _LIBCPP_INLINE_VISIBILITY
4184void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4185
4186template <typename _Alloc>
4187inline _LIBCPP_INLINE_VISIBILITY
4188void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4189#if _LIBCPP_STD_VER >= 14
4190    _NOEXCEPT
4191#else
4192    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4193#endif
4194{
4195    _VSTD::__swap_allocator(__a1, __a2,
4196      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4197}
4198
4199template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
4200struct __noexcept_move_assign_container : public integral_constant<bool,
4201    _Traits::propagate_on_container_move_assignment::value
4202#if _LIBCPP_STD_VER > 14
4203        || _Traits::is_always_equal::value
4204#else
4205        && is_nothrow_move_assignable<_Alloc>::value
4206#endif
4207    > {};
4208
4209
4210template <class _Tp, class _Alloc>
4211struct __temp_value {
4212    typedef allocator_traits<_Alloc> _Traits;
4213
4214    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
4215    _Alloc &__a;
4216
4217    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4218    _Tp &   get() { return *__addr(); }
4219
4220    template<class... _Args>
4221    _LIBCPP_NO_CFI
4222    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4223      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4224                         _VSTD::forward<_Args>(__args)...);
4225    }
4226
4227    ~__temp_value() { _Traits::destroy(__a, __addr()); }
4228    };
4229
4230template<typename _Alloc, typename = void, typename = void>
4231struct __is_allocator : false_type {};
4232
4233template<typename _Alloc>
4234struct __is_allocator<_Alloc,
4235       typename __void_t<typename _Alloc::value_type>::type,
4236       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4237     >
4238   : true_type {};
4239
4240// __builtin_new_allocator -- A non-templated helper for allocating and
4241// deallocating memory using __builtin_operator_new and
4242// __builtin_operator_delete. It should be used in preference to
4243// `std::allocator<T>` to avoid additional instantiations.
4244struct __builtin_new_allocator {
4245  struct __builtin_new_deleter {
4246    typedef void* pointer_type;
4247
4248    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4249        : __size_(__size), __align_(__align) {}
4250
4251    void operator()(void* p) const _NOEXCEPT {
4252        _VSTD::__libcpp_deallocate(p, __size_, __align_);
4253    }
4254
4255   private:
4256    size_t __size_;
4257    size_t __align_;
4258  };
4259
4260  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4261
4262  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
4263      return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
4264                     __builtin_new_deleter(__s, __align));
4265  }
4266
4267  static void __deallocate_bytes(void* __p, size_t __s,
4268                                 size_t __align) _NOEXCEPT {
4269      _VSTD::__libcpp_deallocate(__p, __s, __align);
4270  }
4271
4272  template <class _Tp>
4273  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4274  static __holder_t __allocate_type(size_t __n) {
4275      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4276  }
4277
4278  template <class _Tp>
4279  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4280  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4281      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4282  }
4283};
4284
4285
4286_LIBCPP_END_NAMESPACE_STD
4287
4288_LIBCPP_POP_MACROS
4289
4290#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
4291#   include <__pstl_memory>
4292#endif
4293
4294#endif  // _LIBCPP_MEMORY
4295