xref: /llvm-project-15.0.7/libcxx/include/memory (revision eb066bc2)
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 && __e->__weak_this_.expired())
4124            {
4125                weak_ptr<_Yp> __tmp;
4126                __tmp.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4127                __tmp.__cntrl_ = __cntrl_;
4128                __cntrl_->__add_weak();
4129                __e->__weak_this_.swap(__tmp);
4130            }
4131        }
4132
4133    _LIBCPP_INLINE_VISIBILITY
4134    void __enable_weak_this(const volatile void*) _NOEXCEPT {}
4135
4136    template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4137    template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
4138};
4139
4140template<class _Tp>
4141inline
4142_LIBCPP_CONSTEXPR
4143shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
4144    : __ptr_(0),
4145      __cntrl_(0)
4146{
4147}
4148
4149template<class _Tp>
4150inline
4151_LIBCPP_CONSTEXPR
4152shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
4153    : __ptr_(0),
4154      __cntrl_(0)
4155{
4156}
4157
4158template<class _Tp>
4159template<class _Yp>
4160shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4161                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4162    : __ptr_(__p)
4163{
4164    unique_ptr<_Yp> __hold(__p);
4165    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4166    __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4167    __hold.release();
4168    __enable_weak_this(__p);
4169}
4170
4171template<class _Tp>
4172template<class _Yp, class _Dp>
4173shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4174                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4175    : __ptr_(__p)
4176{
4177#ifndef _LIBCPP_NO_EXCEPTIONS
4178    try
4179    {
4180#endif  // _LIBCPP_NO_EXCEPTIONS
4181        typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4182        __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4183        __enable_weak_this(__p);
4184#ifndef _LIBCPP_NO_EXCEPTIONS
4185    }
4186    catch (...)
4187    {
4188        __d(__p);
4189        throw;
4190    }
4191#endif  // _LIBCPP_NO_EXCEPTIONS
4192}
4193
4194template<class _Tp>
4195template<class _Dp>
4196shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4197    : __ptr_(0)
4198{
4199#ifndef _LIBCPP_NO_EXCEPTIONS
4200    try
4201    {
4202#endif  // _LIBCPP_NO_EXCEPTIONS
4203        typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4204        __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4205#ifndef _LIBCPP_NO_EXCEPTIONS
4206    }
4207    catch (...)
4208    {
4209        __d(__p);
4210        throw;
4211    }
4212#endif  // _LIBCPP_NO_EXCEPTIONS
4213}
4214
4215template<class _Tp>
4216template<class _Yp, class _Dp, class _Alloc>
4217shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4218                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4219    : __ptr_(__p)
4220{
4221#ifndef _LIBCPP_NO_EXCEPTIONS
4222    try
4223    {
4224#endif  // _LIBCPP_NO_EXCEPTIONS
4225        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4226        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4227        typedef __allocator_destructor<_A2> _D2;
4228        _A2 __a2(__a);
4229        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4230        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4231            _CntrlBlk(__p, __d, __a);
4232        __cntrl_ = _VSTD::addressof(*__hold2.release());
4233        __enable_weak_this(__p);
4234#ifndef _LIBCPP_NO_EXCEPTIONS
4235    }
4236    catch (...)
4237    {
4238        __d(__p);
4239        throw;
4240    }
4241#endif  // _LIBCPP_NO_EXCEPTIONS
4242}
4243
4244template<class _Tp>
4245template<class _Dp, class _Alloc>
4246shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4247    : __ptr_(0)
4248{
4249#ifndef _LIBCPP_NO_EXCEPTIONS
4250    try
4251    {
4252#endif  // _LIBCPP_NO_EXCEPTIONS
4253        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4254        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4255        typedef __allocator_destructor<_A2> _D2;
4256        _A2 __a2(__a);
4257        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4258        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4259            _CntrlBlk(__p, __d, __a);
4260        __cntrl_ = _VSTD::addressof(*__hold2.release());
4261#ifndef _LIBCPP_NO_EXCEPTIONS
4262    }
4263    catch (...)
4264    {
4265        __d(__p);
4266        throw;
4267    }
4268#endif  // _LIBCPP_NO_EXCEPTIONS
4269}
4270
4271template<class _Tp>
4272template<class _Yp>
4273inline
4274shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
4275    : __ptr_(__p),
4276      __cntrl_(__r.__cntrl_)
4277{
4278    if (__cntrl_)
4279        __cntrl_->__add_shared();
4280}
4281
4282template<class _Tp>
4283inline
4284shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
4285    : __ptr_(__r.__ptr_),
4286      __cntrl_(__r.__cntrl_)
4287{
4288    if (__cntrl_)
4289        __cntrl_->__add_shared();
4290}
4291
4292template<class _Tp>
4293template<class _Yp>
4294inline
4295shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4296                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4297         _NOEXCEPT
4298    : __ptr_(__r.__ptr_),
4299      __cntrl_(__r.__cntrl_)
4300{
4301    if (__cntrl_)
4302        __cntrl_->__add_shared();
4303}
4304
4305#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4306
4307template<class _Tp>
4308inline
4309shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
4310    : __ptr_(__r.__ptr_),
4311      __cntrl_(__r.__cntrl_)
4312{
4313    __r.__ptr_ = 0;
4314    __r.__cntrl_ = 0;
4315}
4316
4317template<class _Tp>
4318template<class _Yp>
4319inline
4320shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4321                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4322         _NOEXCEPT
4323    : __ptr_(__r.__ptr_),
4324      __cntrl_(__r.__cntrl_)
4325{
4326    __r.__ptr_ = 0;
4327    __r.__cntrl_ = 0;
4328}
4329
4330#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4331
4332template<class _Tp>
4333template<class _Yp>
4334#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4335shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
4336#else
4337shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
4338#endif
4339                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
4340    : __ptr_(__r.get())
4341{
4342    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4343    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4344    __enable_weak_this(__r.get());
4345    __r.release();
4346}
4347
4348template<class _Tp>
4349template <class _Yp, class _Dp>
4350#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4351shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4352#else
4353shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4354#endif
4355                            typename enable_if
4356                            <
4357                                !is_lvalue_reference<_Dp>::value &&
4358                                !is_array<_Yp>::value &&
4359                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4360                                __nat
4361                            >::type)
4362    : __ptr_(__r.get())
4363{
4364#if _LIBCPP_STD_VER > 11
4365    if (__ptr_ == nullptr)
4366        __cntrl_ = nullptr;
4367    else
4368#endif
4369    {
4370        typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4371        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4372        __enable_weak_this(__r.get());
4373    }
4374    __r.release();
4375}
4376
4377template<class _Tp>
4378template <class _Yp, class _Dp>
4379#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4380shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4381#else
4382shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4383#endif
4384                            typename enable_if
4385                            <
4386                                is_lvalue_reference<_Dp>::value &&
4387                                !is_array<_Yp>::value &&
4388                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4389                                __nat
4390                            >::type)
4391    : __ptr_(__r.get())
4392{
4393#if _LIBCPP_STD_VER > 11
4394    if (__ptr_ == nullptr)
4395        __cntrl_ = nullptr;
4396    else
4397#endif
4398    {
4399        typedef __shared_ptr_pointer<_Yp*,
4400                                     reference_wrapper<typename remove_reference<_Dp>::type>,
4401                                     allocator<_Yp> > _CntrlBlk;
4402        __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4403        __enable_weak_this(__r.get());
4404    }
4405    __r.release();
4406}
4407
4408#ifndef _LIBCPP_HAS_NO_VARIADICS
4409
4410template<class _Tp>
4411template<class ..._Args>
4412shared_ptr<_Tp>
4413shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4414{
4415    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4416    typedef allocator<_CntrlBlk> _A2;
4417    typedef __allocator_destructor<_A2> _D2;
4418    _A2 __a2;
4419    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4420    ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4421    shared_ptr<_Tp> __r;
4422    __r.__ptr_ = __hold2.get()->get();
4423    __r.__cntrl_ = __hold2.release();
4424    __r.__enable_weak_this(__r.__ptr_);
4425    return __r;
4426}
4427
4428template<class _Tp>
4429template<class _Alloc, class ..._Args>
4430shared_ptr<_Tp>
4431shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4432{
4433    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4434    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4435    typedef __allocator_destructor<_A2> _D2;
4436    _A2 __a2(__a);
4437    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4438    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4439        _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4440    shared_ptr<_Tp> __r;
4441    __r.__ptr_ = __hold2.get()->get();
4442    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4443    __r.__enable_weak_this(__r.__ptr_);
4444    return __r;
4445}
4446
4447#else  // _LIBCPP_HAS_NO_VARIADICS
4448
4449template<class _Tp>
4450shared_ptr<_Tp>
4451shared_ptr<_Tp>::make_shared()
4452{
4453    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4454    typedef allocator<_CntrlBlk> _Alloc2;
4455    typedef __allocator_destructor<_Alloc2> _D2;
4456    _Alloc2 __alloc2;
4457    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4458    ::new(__hold2.get()) _CntrlBlk(__alloc2);
4459    shared_ptr<_Tp> __r;
4460    __r.__ptr_ = __hold2.get()->get();
4461    __r.__cntrl_ = __hold2.release();
4462    __r.__enable_weak_this(__r.__ptr_);
4463    return __r;
4464}
4465
4466template<class _Tp>
4467template<class _A0>
4468shared_ptr<_Tp>
4469shared_ptr<_Tp>::make_shared(_A0& __a0)
4470{
4471    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4472    typedef allocator<_CntrlBlk> _Alloc2;
4473    typedef __allocator_destructor<_Alloc2> _D2;
4474    _Alloc2 __alloc2;
4475    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4476    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4477    shared_ptr<_Tp> __r;
4478    __r.__ptr_ = __hold2.get()->get();
4479    __r.__cntrl_ = __hold2.release();
4480    __r.__enable_weak_this(__r.__ptr_);
4481    return __r;
4482}
4483
4484template<class _Tp>
4485template<class _A0, class _A1>
4486shared_ptr<_Tp>
4487shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4488{
4489    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4490    typedef allocator<_CntrlBlk> _Alloc2;
4491    typedef __allocator_destructor<_Alloc2> _D2;
4492    _Alloc2 __alloc2;
4493    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4494    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4495    shared_ptr<_Tp> __r;
4496    __r.__ptr_ = __hold2.get()->get();
4497    __r.__cntrl_ = __hold2.release();
4498    __r.__enable_weak_this(__r.__ptr_);
4499    return __r;
4500}
4501
4502template<class _Tp>
4503template<class _A0, class _A1, class _A2>
4504shared_ptr<_Tp>
4505shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4506{
4507    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4508    typedef allocator<_CntrlBlk> _Alloc2;
4509    typedef __allocator_destructor<_Alloc2> _D2;
4510    _Alloc2 __alloc2;
4511    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4512    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4513    shared_ptr<_Tp> __r;
4514    __r.__ptr_ = __hold2.get()->get();
4515    __r.__cntrl_ = __hold2.release();
4516    __r.__enable_weak_this(__r.__ptr_);
4517    return __r;
4518}
4519
4520template<class _Tp>
4521template<class _Alloc>
4522shared_ptr<_Tp>
4523shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4524{
4525    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4526    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4527    typedef __allocator_destructor<_Alloc2> _D2;
4528    _Alloc2 __alloc2(__a);
4529    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4530    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4531        _CntrlBlk(__a);
4532    shared_ptr<_Tp> __r;
4533    __r.__ptr_ = __hold2.get()->get();
4534    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4535    __r.__enable_weak_this(__r.__ptr_);
4536    return __r;
4537}
4538
4539template<class _Tp>
4540template<class _Alloc, class _A0>
4541shared_ptr<_Tp>
4542shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4543{
4544    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4545    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4546    typedef __allocator_destructor<_Alloc2> _D2;
4547    _Alloc2 __alloc2(__a);
4548    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4549    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4550        _CntrlBlk(__a, __a0);
4551    shared_ptr<_Tp> __r;
4552    __r.__ptr_ = __hold2.get()->get();
4553    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4554    __r.__enable_weak_this(__r.__ptr_);
4555    return __r;
4556}
4557
4558template<class _Tp>
4559template<class _Alloc, class _A0, class _A1>
4560shared_ptr<_Tp>
4561shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4562{
4563    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4564    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4565    typedef __allocator_destructor<_Alloc2> _D2;
4566    _Alloc2 __alloc2(__a);
4567    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4568    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4569        _CntrlBlk(__a, __a0, __a1);
4570    shared_ptr<_Tp> __r;
4571    __r.__ptr_ = __hold2.get()->get();
4572    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4573    __r.__enable_weak_this(__r.__ptr_);
4574    return __r;
4575}
4576
4577template<class _Tp>
4578template<class _Alloc, class _A0, class _A1, class _A2>
4579shared_ptr<_Tp>
4580shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4581{
4582    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4583    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
4584    typedef __allocator_destructor<_Alloc2> _D2;
4585    _Alloc2 __alloc2(__a);
4586    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4587    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4588        _CntrlBlk(__a, __a0, __a1, __a2);
4589    shared_ptr<_Tp> __r;
4590    __r.__ptr_ = __hold2.get()->get();
4591    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4592    __r.__enable_weak_this(__r.__ptr_);
4593    return __r;
4594}
4595
4596#endif  // _LIBCPP_HAS_NO_VARIADICS
4597
4598template<class _Tp>
4599shared_ptr<_Tp>::~shared_ptr()
4600{
4601    if (__cntrl_)
4602        __cntrl_->__release_shared();
4603}
4604
4605template<class _Tp>
4606inline
4607shared_ptr<_Tp>&
4608shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
4609{
4610    shared_ptr(__r).swap(*this);
4611    return *this;
4612}
4613
4614template<class _Tp>
4615template<class _Yp>
4616inline
4617typename enable_if
4618<
4619    is_convertible<_Yp*, _Tp*>::value,
4620    shared_ptr<_Tp>&
4621>::type
4622shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
4623{
4624    shared_ptr(__r).swap(*this);
4625    return *this;
4626}
4627
4628#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4629
4630template<class _Tp>
4631inline
4632shared_ptr<_Tp>&
4633shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
4634{
4635    shared_ptr(_VSTD::move(__r)).swap(*this);
4636    return *this;
4637}
4638
4639template<class _Tp>
4640template<class _Yp>
4641inline
4642typename enable_if
4643<
4644    is_convertible<_Yp*, _Tp*>::value,
4645    shared_ptr<_Tp>&
4646>::type
4647shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4648{
4649    shared_ptr(_VSTD::move(__r)).swap(*this);
4650    return *this;
4651}
4652
4653template<class _Tp>
4654template<class _Yp>
4655inline
4656typename enable_if
4657<
4658    !is_array<_Yp>::value &&
4659    is_convertible<_Yp*, _Tp*>::value,
4660    shared_ptr<_Tp>
4661>::type&
4662shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4663{
4664    shared_ptr(_VSTD::move(__r)).swap(*this);
4665    return *this;
4666}
4667
4668template<class _Tp>
4669template <class _Yp, class _Dp>
4670inline
4671typename enable_if
4672<
4673    !is_array<_Yp>::value &&
4674    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4675    shared_ptr<_Tp>&
4676>::type
4677shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4678{
4679    shared_ptr(_VSTD::move(__r)).swap(*this);
4680    return *this;
4681}
4682
4683#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4684
4685template<class _Tp>
4686template<class _Yp>
4687inline _LIBCPP_INLINE_VISIBILITY
4688typename enable_if
4689<
4690    !is_array<_Yp>::value &&
4691    is_convertible<_Yp*, _Tp*>::value,
4692    shared_ptr<_Tp>&
4693>::type
4694shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
4695{
4696    shared_ptr(__r).swap(*this);
4697    return *this;
4698}
4699
4700template<class _Tp>
4701template <class _Yp, class _Dp>
4702inline _LIBCPP_INLINE_VISIBILITY
4703typename enable_if
4704<
4705    !is_array<_Yp>::value &&
4706    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4707    shared_ptr<_Tp>&
4708>::type
4709shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4710{
4711    shared_ptr(_VSTD::move(__r)).swap(*this);
4712    return *this;
4713}
4714
4715#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4716
4717template<class _Tp>
4718inline
4719void
4720shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
4721{
4722    _VSTD::swap(__ptr_, __r.__ptr_);
4723    _VSTD::swap(__cntrl_, __r.__cntrl_);
4724}
4725
4726template<class _Tp>
4727inline
4728void
4729shared_ptr<_Tp>::reset() _NOEXCEPT
4730{
4731    shared_ptr().swap(*this);
4732}
4733
4734template<class _Tp>
4735template<class _Yp>
4736inline
4737typename enable_if
4738<
4739    is_convertible<_Yp*, _Tp*>::value,
4740    void
4741>::type
4742shared_ptr<_Tp>::reset(_Yp* __p)
4743{
4744    shared_ptr(__p).swap(*this);
4745}
4746
4747template<class _Tp>
4748template<class _Yp, class _Dp>
4749inline
4750typename enable_if
4751<
4752    is_convertible<_Yp*, _Tp*>::value,
4753    void
4754>::type
4755shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4756{
4757    shared_ptr(__p, __d).swap(*this);
4758}
4759
4760template<class _Tp>
4761template<class _Yp, class _Dp, class _Alloc>
4762inline
4763typename enable_if
4764<
4765    is_convertible<_Yp*, _Tp*>::value,
4766    void
4767>::type
4768shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4769{
4770    shared_ptr(__p, __d, __a).swap(*this);
4771}
4772
4773#ifndef _LIBCPP_HAS_NO_VARIADICS
4774
4775template<class _Tp, class ..._Args>
4776inline _LIBCPP_INLINE_VISIBILITY
4777typename enable_if
4778<
4779    !is_array<_Tp>::value,
4780    shared_ptr<_Tp>
4781>::type
4782make_shared(_Args&& ...__args)
4783{
4784    return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
4785}
4786
4787template<class _Tp, class _Alloc, class ..._Args>
4788inline _LIBCPP_INLINE_VISIBILITY
4789typename enable_if
4790<
4791    !is_array<_Tp>::value,
4792    shared_ptr<_Tp>
4793>::type
4794allocate_shared(const _Alloc& __a, _Args&& ...__args)
4795{
4796    return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
4797}
4798
4799#else  // _LIBCPP_HAS_NO_VARIADICS
4800
4801template<class _Tp>
4802inline _LIBCPP_INLINE_VISIBILITY
4803shared_ptr<_Tp>
4804make_shared()
4805{
4806    return shared_ptr<_Tp>::make_shared();
4807}
4808
4809template<class _Tp, class _A0>
4810inline _LIBCPP_INLINE_VISIBILITY
4811shared_ptr<_Tp>
4812make_shared(_A0& __a0)
4813{
4814    return shared_ptr<_Tp>::make_shared(__a0);
4815}
4816
4817template<class _Tp, class _A0, class _A1>
4818inline _LIBCPP_INLINE_VISIBILITY
4819shared_ptr<_Tp>
4820make_shared(_A0& __a0, _A1& __a1)
4821{
4822    return shared_ptr<_Tp>::make_shared(__a0, __a1);
4823}
4824
4825template<class _Tp, class _A0, class _A1, class _A2>
4826inline _LIBCPP_INLINE_VISIBILITY
4827shared_ptr<_Tp>
4828make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4829{
4830    return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4831}
4832
4833template<class _Tp, class _Alloc>
4834inline _LIBCPP_INLINE_VISIBILITY
4835shared_ptr<_Tp>
4836allocate_shared(const _Alloc& __a)
4837{
4838    return shared_ptr<_Tp>::allocate_shared(__a);
4839}
4840
4841template<class _Tp, class _Alloc, class _A0>
4842inline _LIBCPP_INLINE_VISIBILITY
4843shared_ptr<_Tp>
4844allocate_shared(const _Alloc& __a, _A0& __a0)
4845{
4846    return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4847}
4848
4849template<class _Tp, class _Alloc, class _A0, class _A1>
4850inline _LIBCPP_INLINE_VISIBILITY
4851shared_ptr<_Tp>
4852allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4853{
4854    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4855}
4856
4857template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4858inline _LIBCPP_INLINE_VISIBILITY
4859shared_ptr<_Tp>
4860allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4861{
4862    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4863}
4864
4865#endif  // _LIBCPP_HAS_NO_VARIADICS
4866
4867template<class _Tp, class _Up>
4868inline _LIBCPP_INLINE_VISIBILITY
4869bool
4870operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4871{
4872    return __x.get() == __y.get();
4873}
4874
4875template<class _Tp, class _Up>
4876inline _LIBCPP_INLINE_VISIBILITY
4877bool
4878operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4879{
4880    return !(__x == __y);
4881}
4882
4883template<class _Tp, class _Up>
4884inline _LIBCPP_INLINE_VISIBILITY
4885bool
4886operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4887{
4888    typedef typename common_type<_Tp*, _Up*>::type _Vp;
4889    return less<_Vp>()(__x.get(), __y.get());
4890}
4891
4892template<class _Tp, class _Up>
4893inline _LIBCPP_INLINE_VISIBILITY
4894bool
4895operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4896{
4897    return __y < __x;
4898}
4899
4900template<class _Tp, class _Up>
4901inline _LIBCPP_INLINE_VISIBILITY
4902bool
4903operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4904{
4905    return !(__y < __x);
4906}
4907
4908template<class _Tp, class _Up>
4909inline _LIBCPP_INLINE_VISIBILITY
4910bool
4911operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4912{
4913    return !(__x < __y);
4914}
4915
4916template<class _Tp>
4917inline _LIBCPP_INLINE_VISIBILITY
4918bool
4919operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4920{
4921    return !__x;
4922}
4923
4924template<class _Tp>
4925inline _LIBCPP_INLINE_VISIBILITY
4926bool
4927operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4928{
4929    return !__x;
4930}
4931
4932template<class _Tp>
4933inline _LIBCPP_INLINE_VISIBILITY
4934bool
4935operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4936{
4937    return static_cast<bool>(__x);
4938}
4939
4940template<class _Tp>
4941inline _LIBCPP_INLINE_VISIBILITY
4942bool
4943operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4944{
4945    return static_cast<bool>(__x);
4946}
4947
4948template<class _Tp>
4949inline _LIBCPP_INLINE_VISIBILITY
4950bool
4951operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4952{
4953    return less<_Tp*>()(__x.get(), nullptr);
4954}
4955
4956template<class _Tp>
4957inline _LIBCPP_INLINE_VISIBILITY
4958bool
4959operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4960{
4961    return less<_Tp*>()(nullptr, __x.get());
4962}
4963
4964template<class _Tp>
4965inline _LIBCPP_INLINE_VISIBILITY
4966bool
4967operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4968{
4969    return nullptr < __x;
4970}
4971
4972template<class _Tp>
4973inline _LIBCPP_INLINE_VISIBILITY
4974bool
4975operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4976{
4977    return __x < nullptr;
4978}
4979
4980template<class _Tp>
4981inline _LIBCPP_INLINE_VISIBILITY
4982bool
4983operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4984{
4985    return !(nullptr < __x);
4986}
4987
4988template<class _Tp>
4989inline _LIBCPP_INLINE_VISIBILITY
4990bool
4991operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4992{
4993    return !(__x < nullptr);
4994}
4995
4996template<class _Tp>
4997inline _LIBCPP_INLINE_VISIBILITY
4998bool
4999operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5000{
5001    return !(__x < nullptr);
5002}
5003
5004template<class _Tp>
5005inline _LIBCPP_INLINE_VISIBILITY
5006bool
5007operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5008{
5009    return !(nullptr < __x);
5010}
5011
5012template<class _Tp>
5013inline _LIBCPP_INLINE_VISIBILITY
5014void
5015swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
5016{
5017    __x.swap(__y);
5018}
5019
5020template<class _Tp, class _Up>
5021inline _LIBCPP_INLINE_VISIBILITY
5022typename enable_if
5023<
5024    !is_array<_Tp>::value && !is_array<_Up>::value,
5025    shared_ptr<_Tp>
5026>::type
5027static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
5028{
5029    return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5030}
5031
5032template<class _Tp, class _Up>
5033inline _LIBCPP_INLINE_VISIBILITY
5034typename enable_if
5035<
5036    !is_array<_Tp>::value && !is_array<_Up>::value,
5037    shared_ptr<_Tp>
5038>::type
5039dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
5040{
5041    _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5042    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5043}
5044
5045template<class _Tp, class _Up>
5046typename enable_if
5047<
5048    is_array<_Tp>::value == is_array<_Up>::value,
5049    shared_ptr<_Tp>
5050>::type
5051const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
5052{
5053    typedef typename remove_extent<_Tp>::type _RTp;
5054    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
5055}
5056
5057#ifndef _LIBCPP_NO_RTTI
5058
5059template<class _Dp, class _Tp>
5060inline _LIBCPP_INLINE_VISIBILITY
5061_Dp*
5062get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
5063{
5064    return __p.template __get_deleter<_Dp>();
5065}
5066
5067#endif  // _LIBCPP_NO_RTTI
5068
5069template<class _Tp>
5070class _LIBCPP_TYPE_VIS_ONLY weak_ptr
5071{
5072public:
5073    typedef _Tp element_type;
5074private:
5075    element_type*        __ptr_;
5076    __shared_weak_count* __cntrl_;
5077
5078public:
5079    _LIBCPP_INLINE_VISIBILITY
5080    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
5081    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
5082                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5083                        _NOEXCEPT;
5084    _LIBCPP_INLINE_VISIBILITY
5085    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
5086    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
5087                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5088                         _NOEXCEPT;
5089
5090#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5091    _LIBCPP_INLINE_VISIBILITY
5092    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5093    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
5094                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5095                         _NOEXCEPT;
5096#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5097    ~weak_ptr();
5098
5099    _LIBCPP_INLINE_VISIBILITY
5100    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
5101    template<class _Yp>
5102        typename enable_if
5103        <
5104            is_convertible<_Yp*, element_type*>::value,
5105            weak_ptr&
5106        >::type
5107        _LIBCPP_INLINE_VISIBILITY
5108        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5109
5110#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5111
5112    _LIBCPP_INLINE_VISIBILITY
5113    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5114    template<class _Yp>
5115        typename enable_if
5116        <
5117            is_convertible<_Yp*, element_type*>::value,
5118            weak_ptr&
5119        >::type
5120        _LIBCPP_INLINE_VISIBILITY
5121        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5122
5123#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5124
5125    template<class _Yp>
5126        typename enable_if
5127        <
5128            is_convertible<_Yp*, element_type*>::value,
5129            weak_ptr&
5130        >::type
5131        _LIBCPP_INLINE_VISIBILITY
5132        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
5133
5134    _LIBCPP_INLINE_VISIBILITY
5135    void swap(weak_ptr& __r) _NOEXCEPT;
5136    _LIBCPP_INLINE_VISIBILITY
5137    void reset() _NOEXCEPT;
5138
5139    _LIBCPP_INLINE_VISIBILITY
5140    long use_count() const _NOEXCEPT
5141        {return __cntrl_ ? __cntrl_->use_count() : 0;}
5142    _LIBCPP_INLINE_VISIBILITY
5143    bool expired() const _NOEXCEPT
5144        {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5145    shared_ptr<_Tp> lock() const _NOEXCEPT;
5146    template<class _Up>
5147        _LIBCPP_INLINE_VISIBILITY
5148        bool owner_before(const shared_ptr<_Up>& __r) const
5149        {return __cntrl_ < __r.__cntrl_;}
5150    template<class _Up>
5151        _LIBCPP_INLINE_VISIBILITY
5152        bool owner_before(const weak_ptr<_Up>& __r) const
5153        {return __cntrl_ < __r.__cntrl_;}
5154
5155    template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5156    template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
5157};
5158
5159template<class _Tp>
5160inline
5161_LIBCPP_CONSTEXPR
5162weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
5163    : __ptr_(0),
5164      __cntrl_(0)
5165{
5166}
5167
5168template<class _Tp>
5169inline
5170weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
5171    : __ptr_(__r.__ptr_),
5172      __cntrl_(__r.__cntrl_)
5173{
5174    if (__cntrl_)
5175        __cntrl_->__add_weak();
5176}
5177
5178template<class _Tp>
5179template<class _Yp>
5180inline
5181weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
5182                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5183                         _NOEXCEPT
5184    : __ptr_(__r.__ptr_),
5185      __cntrl_(__r.__cntrl_)
5186{
5187    if (__cntrl_)
5188        __cntrl_->__add_weak();
5189}
5190
5191template<class _Tp>
5192template<class _Yp>
5193inline
5194weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
5195                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5196         _NOEXCEPT
5197    : __ptr_(__r.__ptr_),
5198      __cntrl_(__r.__cntrl_)
5199{
5200    if (__cntrl_)
5201        __cntrl_->__add_weak();
5202}
5203
5204#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5205
5206template<class _Tp>
5207inline
5208weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5209    : __ptr_(__r.__ptr_),
5210      __cntrl_(__r.__cntrl_)
5211{
5212    __r.__ptr_ = 0;
5213    __r.__cntrl_ = 0;
5214}
5215
5216template<class _Tp>
5217template<class _Yp>
5218inline
5219weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5220                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5221         _NOEXCEPT
5222    : __ptr_(__r.__ptr_),
5223      __cntrl_(__r.__cntrl_)
5224{
5225    __r.__ptr_ = 0;
5226    __r.__cntrl_ = 0;
5227}
5228
5229#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5230
5231template<class _Tp>
5232weak_ptr<_Tp>::~weak_ptr()
5233{
5234    if (__cntrl_)
5235        __cntrl_->__release_weak();
5236}
5237
5238template<class _Tp>
5239inline
5240weak_ptr<_Tp>&
5241weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
5242{
5243    weak_ptr(__r).swap(*this);
5244    return *this;
5245}
5246
5247template<class _Tp>
5248template<class _Yp>
5249inline
5250typename enable_if
5251<
5252    is_convertible<_Yp*, _Tp*>::value,
5253    weak_ptr<_Tp>&
5254>::type
5255weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
5256{
5257    weak_ptr(__r).swap(*this);
5258    return *this;
5259}
5260
5261#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5262
5263template<class _Tp>
5264inline
5265weak_ptr<_Tp>&
5266weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5267{
5268    weak_ptr(_VSTD::move(__r)).swap(*this);
5269    return *this;
5270}
5271
5272template<class _Tp>
5273template<class _Yp>
5274inline
5275typename enable_if
5276<
5277    is_convertible<_Yp*, _Tp*>::value,
5278    weak_ptr<_Tp>&
5279>::type
5280weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5281{
5282    weak_ptr(_VSTD::move(__r)).swap(*this);
5283    return *this;
5284}
5285
5286#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5287
5288template<class _Tp>
5289template<class _Yp>
5290inline
5291typename enable_if
5292<
5293    is_convertible<_Yp*, _Tp*>::value,
5294    weak_ptr<_Tp>&
5295>::type
5296weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
5297{
5298    weak_ptr(__r).swap(*this);
5299    return *this;
5300}
5301
5302template<class _Tp>
5303inline
5304void
5305weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
5306{
5307    _VSTD::swap(__ptr_, __r.__ptr_);
5308    _VSTD::swap(__cntrl_, __r.__cntrl_);
5309}
5310
5311template<class _Tp>
5312inline _LIBCPP_INLINE_VISIBILITY
5313void
5314swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
5315{
5316    __x.swap(__y);
5317}
5318
5319template<class _Tp>
5320inline
5321void
5322weak_ptr<_Tp>::reset() _NOEXCEPT
5323{
5324    weak_ptr().swap(*this);
5325}
5326
5327template<class _Tp>
5328template<class _Yp>
5329shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5330                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5331    : __ptr_(__r.__ptr_),
5332      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5333{
5334    if (__cntrl_ == 0)
5335#ifndef _LIBCPP_NO_EXCEPTIONS
5336        throw bad_weak_ptr();
5337#else
5338        assert(!"bad_weak_ptr");
5339#endif
5340}
5341
5342template<class _Tp>
5343shared_ptr<_Tp>
5344weak_ptr<_Tp>::lock() const _NOEXCEPT
5345{
5346    shared_ptr<_Tp> __r;
5347    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5348    if (__r.__cntrl_)
5349        __r.__ptr_ = __ptr_;
5350    return __r;
5351}
5352
5353#if _LIBCPP_STD_VER > 14
5354template <class _Tp = void> struct owner_less;
5355#else
5356template <class _Tp> struct owner_less;
5357#endif
5358
5359template <class _Tp>
5360struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
5361    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
5362{
5363    typedef bool result_type;
5364    _LIBCPP_INLINE_VISIBILITY
5365    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5366        {return __x.owner_before(__y);}
5367    _LIBCPP_INLINE_VISIBILITY
5368    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
5369        {return __x.owner_before(__y);}
5370    _LIBCPP_INLINE_VISIBILITY
5371    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5372        {return __x.owner_before(__y);}
5373};
5374
5375template <class _Tp>
5376struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
5377    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5378{
5379    typedef bool result_type;
5380    _LIBCPP_INLINE_VISIBILITY
5381    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
5382        {return __x.owner_before(__y);}
5383    _LIBCPP_INLINE_VISIBILITY
5384    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
5385        {return __x.owner_before(__y);}
5386    _LIBCPP_INLINE_VISIBILITY
5387    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5388        {return __x.owner_before(__y);}
5389};
5390
5391#if _LIBCPP_STD_VER > 14
5392template <>
5393struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5394{
5395    template <class _Tp, class _Up>
5396    _LIBCPP_INLINE_VISIBILITY
5397    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5398        {return __x.owner_before(__y);}
5399    template <class _Tp, class _Up>
5400    _LIBCPP_INLINE_VISIBILITY
5401    bool operator()( shared_ptr<_Tp> const& __x,  weak_ptr<_Up> const& __y) const
5402        {return __x.owner_before(__y);}
5403    template <class _Tp, class _Up>
5404    _LIBCPP_INLINE_VISIBILITY
5405    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5406        {return __x.owner_before(__y);}
5407    template <class _Tp, class _Up>
5408    _LIBCPP_INLINE_VISIBILITY
5409    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const
5410        {return __x.owner_before(__y);}
5411    typedef void is_transparent;
5412};
5413#endif
5414
5415template<class _Tp>
5416class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
5417{
5418    mutable weak_ptr<_Tp> __weak_this_;
5419protected:
5420    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
5421    enable_shared_from_this() _NOEXCEPT {}
5422    _LIBCPP_INLINE_VISIBILITY
5423    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
5424    _LIBCPP_INLINE_VISIBILITY
5425    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5426        {return *this;}
5427    _LIBCPP_INLINE_VISIBILITY
5428    ~enable_shared_from_this() {}
5429public:
5430    _LIBCPP_INLINE_VISIBILITY
5431    shared_ptr<_Tp> shared_from_this()
5432        {return shared_ptr<_Tp>(__weak_this_);}
5433    _LIBCPP_INLINE_VISIBILITY
5434    shared_ptr<_Tp const> shared_from_this() const
5435        {return shared_ptr<const _Tp>(__weak_this_);}
5436
5437#if _LIBCPP_STD_VER > 14
5438    _LIBCPP_INLINE_VISIBILITY
5439    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5440       { return __weak_this_; }
5441
5442    _LIBCPP_INLINE_VISIBILITY
5443    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5444        { return __weak_this_; }
5445#endif // _LIBCPP_STD_VER > 14
5446
5447    template <class _Up> friend class shared_ptr;
5448};
5449
5450template <class _Tp>
5451struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
5452{
5453    typedef shared_ptr<_Tp>      argument_type;
5454    typedef size_t               result_type;
5455    _LIBCPP_INLINE_VISIBILITY
5456    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
5457    {
5458        return hash<_Tp*>()(__ptr.get());
5459    }
5460};
5461
5462template<class _CharT, class _Traits, class _Yp>
5463inline _LIBCPP_INLINE_VISIBILITY
5464basic_ostream<_CharT, _Traits>&
5465operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
5466
5467// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
5468// enabled with clang.
5469#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
5470
5471class _LIBCPP_TYPE_VIS __sp_mut
5472{
5473    void* __lx;
5474public:
5475    void lock() _NOEXCEPT;
5476    void unlock() _NOEXCEPT;
5477
5478private:
5479    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5480    __sp_mut(const __sp_mut&);
5481    __sp_mut& operator=(const __sp_mut&);
5482
5483    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
5484};
5485
5486_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
5487
5488template <class _Tp>
5489inline _LIBCPP_INLINE_VISIBILITY
5490bool
5491atomic_is_lock_free(const shared_ptr<_Tp>*)
5492{
5493    return false;
5494}
5495
5496template <class _Tp>
5497shared_ptr<_Tp>
5498atomic_load(const shared_ptr<_Tp>* __p)
5499{
5500    __sp_mut& __m = __get_sp_mut(__p);
5501    __m.lock();
5502    shared_ptr<_Tp> __q = *__p;
5503    __m.unlock();
5504    return __q;
5505}
5506
5507template <class _Tp>
5508inline _LIBCPP_INLINE_VISIBILITY
5509shared_ptr<_Tp>
5510atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5511{
5512    return atomic_load(__p);
5513}
5514
5515template <class _Tp>
5516void
5517atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5518{
5519    __sp_mut& __m = __get_sp_mut(__p);
5520    __m.lock();
5521    __p->swap(__r);
5522    __m.unlock();
5523}
5524
5525template <class _Tp>
5526inline _LIBCPP_INLINE_VISIBILITY
5527void
5528atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5529{
5530    atomic_store(__p, __r);
5531}
5532
5533template <class _Tp>
5534shared_ptr<_Tp>
5535atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5536{
5537    __sp_mut& __m = __get_sp_mut(__p);
5538    __m.lock();
5539    __p->swap(__r);
5540    __m.unlock();
5541    return __r;
5542}
5543
5544template <class _Tp>
5545inline _LIBCPP_INLINE_VISIBILITY
5546shared_ptr<_Tp>
5547atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5548{
5549    return atomic_exchange(__p, __r);
5550}
5551
5552template <class _Tp>
5553bool
5554atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5555{
5556    shared_ptr<_Tp> __temp;
5557    __sp_mut& __m = __get_sp_mut(__p);
5558    __m.lock();
5559    if (__p->__owner_equivalent(*__v))
5560    {
5561        _VSTD::swap(__temp, *__p);
5562        *__p = __w;
5563        __m.unlock();
5564        return true;
5565    }
5566    _VSTD::swap(__temp, *__v);
5567    *__v = *__p;
5568    __m.unlock();
5569    return false;
5570}
5571
5572template <class _Tp>
5573inline _LIBCPP_INLINE_VISIBILITY
5574bool
5575atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5576{
5577    return atomic_compare_exchange_strong(__p, __v, __w);
5578}
5579
5580template <class _Tp>
5581inline _LIBCPP_INLINE_VISIBILITY
5582bool
5583atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5584                                        shared_ptr<_Tp> __w, memory_order, memory_order)
5585{
5586    return atomic_compare_exchange_strong(__p, __v, __w);
5587}
5588
5589template <class _Tp>
5590inline _LIBCPP_INLINE_VISIBILITY
5591bool
5592atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5593                                      shared_ptr<_Tp> __w, memory_order, memory_order)
5594{
5595    return atomic_compare_exchange_weak(__p, __v, __w);
5596}
5597
5598#endif  // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
5599
5600//enum class
5601struct _LIBCPP_TYPE_VIS pointer_safety
5602{
5603    enum __lx
5604    {
5605        relaxed,
5606        preferred,
5607        strict
5608    };
5609
5610    __lx __v_;
5611
5612    _LIBCPP_INLINE_VISIBILITY
5613    pointer_safety(__lx __v) : __v_(__v) {}
5614    _LIBCPP_INLINE_VISIBILITY
5615    operator int() const {return __v_;}
5616};
5617
5618_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5619_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5620_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5621_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5622_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
5623
5624template <class _Tp>
5625inline _LIBCPP_INLINE_VISIBILITY
5626_Tp*
5627undeclare_reachable(_Tp* __p)
5628{
5629    return static_cast<_Tp*>(__undeclare_reachable(__p));
5630}
5631
5632_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
5633
5634// --- Helper for container swap --
5635template <typename _Alloc>
5636inline _LIBCPP_INLINE_VISIBILITY
5637void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5638#if _LIBCPP_STD_VER >= 14
5639    _NOEXCEPT
5640#else
5641    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5642#endif
5643{
5644    __swap_allocator(__a1, __a2,
5645      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5646}
5647
5648template <typename _Alloc>
5649_LIBCPP_INLINE_VISIBILITY
5650void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5651#if _LIBCPP_STD_VER >= 14
5652    _NOEXCEPT
5653#else
5654    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5655#endif
5656{
5657    using _VSTD::swap;
5658    swap(__a1, __a2);
5659}
5660
5661template <typename _Alloc>
5662inline _LIBCPP_INLINE_VISIBILITY
5663void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5664
5665template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
5666struct __noexcept_move_assign_container : public integral_constant<bool,
5667    _Traits::propagate_on_container_move_assignment::value
5668#if _LIBCPP_STD_VER > 14
5669        || _Traits::is_always_equal::value
5670#else
5671        && is_nothrow_move_assignable<_Alloc>::value
5672#endif
5673    > {};
5674
5675_LIBCPP_END_NAMESPACE_STD
5676
5677#endif  // _LIBCPP_MEMORY
5678