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