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