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