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