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