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