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