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