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