xref: /llvm-project-15.0.7/libcxx/include/memory (revision a462f5cc)
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 Alloc>
38struct allocator_traits
39{
40    typedef Alloc                        allocator_type;
41    typedef typename allocator_type::value_type
42                                         value_type;
43
44    typedef Alloc::pointer | value_type* pointer;
45    typedef Alloc::const_pointer
46          | pointer_traits<pointer>::rebind<const value_type>
47                                         const_pointer;
48    typedef Alloc::void_pointer
49          | pointer_traits<pointer>::rebind<void>
50                                         void_pointer;
51    typedef Alloc::const_void_pointer
52          | pointer_traits<pointer>::rebind<const void>
53                                         const_void_pointer;
54    typedef Alloc::difference_type
55          | pointer_traits<pointer>::difference_type
56                                         difference_type;
57    typedef Alloc::size_type
58          | make_unsigned<difference_type>::type
59                                         size_type;
60    typedef Alloc::propagate_on_container_copy_assignment
61          | false_type                   propagate_on_container_copy_assignment;
62    typedef Alloc::propagate_on_container_move_assignment
63          | false_type                   propagate_on_container_move_assignment;
64    typedef Alloc::propagate_on_container_swap
65          | false_type                   propagate_on_container_swap;
66
67    template <class T> using rebind_alloc  = Alloc::rebind<U>::other | Alloc<T, Args...>;
68    template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
69
70    static pointer allocate(allocator_type& a, size_type n);
71    static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
72
73    static void deallocate(allocator_type& a, pointer p, size_type n);
74
75    template <class T, class... Args>
76        static void construct(allocator_type& a, T* p, Args&&... args);
77
78    template <class T>
79        static void destroy(allocator_type& a, T* p);
80
81    static size_type max_size(const allocator_type& a);
82
83    static allocator_type
84        select_on_container_copy_construction(const allocator_type& a);
85};
86
87template <>
88class allocator<void>
89{
90public:
91    typedef void*                                 pointer;
92    typedef const void*                           const_pointer;
93    typedef void                                  value_type;
94
95    template <class _Up> struct rebind {typedef allocator<_Up> other;};
96};
97
98template <class T>
99class allocator
100{
101public:
102    typedef size_t                                size_type;
103    typedef ptrdiff_t                             difference_type;
104    typedef T*                                    pointer;
105    typedef const T*                              const_pointer;
106    typedef typename add_lvalue_reference<T>::type       reference;
107    typedef typename add_lvalue_reference<const T>::type const_reference;
108    typedef T                                     value_type;
109
110    template <class U> struct rebind {typedef allocator<U> other;};
111
112    allocator() throw();
113    allocator(const allocator&) throw();
114    template <class U> allocator(const allocator<U>&) throw();
115    ~allocator() throw();
116    pointer address(reference x) const;
117    const_pointer address(const_reference x) const;
118    pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
119    void deallocate(pointer p, size_type n);
120    size_type max_size() const throw();
121    void construct(pointer p, const T& val);
122    void destroy(pointer p);
123};
124
125template <class T, class U>
126bool operator==(const allocator<T>&, const allocator<U>&) throw();
127
128template <class T, class U>
129bool operator!=(const allocator<T>&, const allocator<U>&) throw();
130
131template <class OutputIterator, class T>
132class raw_storage_iterator
133    : public iterator<output_iterator_tag,
134                      T,                               // purposefully not C++03
135                      ptrdiff_t,                       // purposefully not C++03
136                      T*,                              // purposefully not C++03
137                      raw_storage_iterator&>           // purposefully not C++03
138{
139public:
140    explicit raw_storage_iterator(OutputIterator x);
141    raw_storage_iterator& operator*();
142    raw_storage_iterator& operator=(const T& element);
143    raw_storage_iterator& operator++();
144    raw_storage_iterator  operator++(int);
145};
146
147template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n);
148template <class T> void               return_temporary_buffer(T* p);
149
150template <class InputIterator, class ForwardIterator>
151ForwardIterator
152uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
153
154template <class ForwardIterator, class T>
155void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
156
157template <class ForwardIterator, class Size, class T>
158ForwardIterator
159uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
160
161template <class Y> struct auto_ptr_ref {};
162
163template<class X>
164class auto_ptr
165{
166public:
167    typedef X element_type;
168
169    explicit auto_ptr(X* p =0) throw();
170    auto_ptr(auto_ptr&) throw();
171    template<class Y> auto_ptr(auto_ptr<Y>&) throw();
172    auto_ptr& operator=(auto_ptr&) throw();
173    template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
174    auto_ptr& operator=(auto_ptr_ref<X> r) throw();
175    ~auto_ptr() throw();
176
177    typename add_lvalue_reference<X>::type operator*() const throw();
178    X* operator->() const throw();
179    X* get() const throw();
180    X* release() throw();
181    void reset(X* p =0) throw();
182
183    auto_ptr(auto_ptr_ref<X>) throw();
184    template<class Y> operator auto_ptr_ref<Y>() throw();
185    template<class Y> operator auto_ptr<Y>() throw();
186};
187
188template <class T>
189struct default_delete
190{
191    constexpr default_delete();
192    template <class U> default_delete(const default_delete<U>&);
193
194    void operator()(T*) const;
195};
196
197template <class T>
198struct default_delete<T[]>
199{
200    constexpr default_delete();
201    void operator()(T*) const;
202    template <class U> void operator()(U*) const = delete;
203};
204
205template <class T, class D = default_delete<T>> class unique_ptr;
206
207template <class T, class D = default_delete<T>>
208class unique_ptr
209{
210public:
211    typedef see below pointer;
212    typedef T element_type;
213    typedef D deleter_type;
214
215    // constructors
216    constexpr unique_ptr();
217    explicit unique_ptr(pointer p);
218    unique_ptr(pointer p, implementation-defined d1);
219    unique_ptr(pointer p, implementation-defined d2);
220    unique_ptr(unique_ptr&& u);
221    unique_ptr(nullptr_t) : unique_ptr() { }
222    template <class U, class E>
223        unique_ptr(unique_ptr<U, E>&& u);
224    template <class U>
225        unique_ptr(auto_ptr<U>&& u);
226
227    // destructor
228    ~unique_ptr();
229
230    // assignment
231    unique_ptr& operator=(unique_ptr&& u);
232    template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u);
233    unique_ptr& operator=(nullptr_t);
234
235    // observers
236    typename add_lvalue_reference<T>::type operator*() const;
237    pointer operator->() const;
238    pointer get() const;
239    deleter_type& get_deleter();
240    const deleter_type& get_deleter() const;
241    explicit operator bool() const;
242
243    // modifiers
244    pointer release();
245    void reset(pointer p = pointer());
246    void swap(unique_ptr& u);
247};
248
249template <class T, class D>
250class unique_ptr<T[], D>
251{
252public:
253    typedef implementation-defined pointer;
254    typedef T element_type;
255    typedef D deleter_type;
256
257    // constructors
258    constexpr unique_ptr();
259    explicit unique_ptr(pointer p);
260    unique_ptr(pointer p, implementation-defined d);
261    unique_ptr(pointer p, implementation-defined d);
262    unique_ptr(unique_ptr&& u);
263    unique_ptr(nullptr_t) : unique_ptr() { }
264
265    // destructor
266    ~unique_ptr();
267
268    // assignment
269    unique_ptr& operator=(unique_ptr&& u);
270    unique_ptr& operator=(nullptr_t);
271
272    // observers
273    T& operator[](size_t i) const;
274    pointer get() const;
275    deleter_type& get_deleter();
276    const deleter_type& get_deleter() const;
277    explicit operator bool() const;
278
279    // modifiers
280    pointer release();
281    void reset(pointer p = pointer());
282    void reset(nullptr_t);
283    template <class U> void reset(U) = delete;
284    void swap(unique_ptr& u);
285};
286
287template <class T, class D>
288    void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y);
289
290template <class T1, class D1, class T2, class D2>
291    bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
292template <class T1, class D1, class T2, class D2>
293    bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
294template <class T1, class D1, class T2, class D2>
295    bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
296template <class T1, class D1, class T2, class D2>
297    bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
298template <class T1, class D1, class T2, class D2>
299    bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
300template <class T1, class D1, class T2, class D2>
301    bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
302
303class bad_weak_ptr
304    : public std::exception
305{
306    bad_weak_ptr();
307};
308
309template<class T>
310class shared_ptr
311{
312public:
313    typedef T element_type;
314
315    // constructors:
316    constexpr shared_ptr();
317    template<class Y> explicit shared_ptr(Y* p);
318    template<class Y, class D> shared_ptr(Y* p, D d);
319    template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
320    template <class D> shared_ptr(nullptr_t p, D d);
321    template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
322    template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p);
323    shared_ptr(const shared_ptr& r);
324    template<class Y> shared_ptr(const shared_ptr<Y>& r);
325    shared_ptr(shared_ptr&& r);
326    template<class Y> shared_ptr(shared_ptr<Y>&& r);
327    template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
328    template<class Y> shared_ptr(auto_ptr<Y>&& r);
329    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
330    shared_ptr(nullptr_t) : shared_ptr() { }
331
332    // destructor:
333    ~shared_ptr();
334
335    // assignment:
336    shared_ptr& operator=(const shared_ptr& r);
337    template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r);
338    shared_ptr& operator=(shared_ptr&& r);
339    template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
340    template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
341    template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
342
343    // modifiers:
344    void swap(shared_ptr& r);
345    void reset();
346    template<class Y> void reset(Y* p);
347    template<class Y, class D> void reset(Y* p, D d);
348    template<class Y, class D, class A> void reset(Y* p, D d, A a);
349
350    // observers: T* get() const;
351    T& operator*() const;
352    T* operator->() const;
353    long use_count() const;
354    bool unique() const;
355    explicit operator bool() const;
356    template<class U> bool owner_before(shared_ptr<U> const& b) const;
357    template<class U> bool owner_before(weak_ptr<U> const& b) const;
358};
359
360// shared_ptr comparisons:
361template<class T, class U>
362    bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b);
363template<class T, class U>
364    bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b);
365template<class T, class U>
366    bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b);
367template<class T, class U>
368    bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b);
369template<class T, class U>
370    bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b);
371template<class T, class U>
372    bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b);
373
374// shared_ptr specialized algorithms:
375template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b);
376
377// shared_ptr casts:
378template<class T, class U>
379    shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r);
380template<class T, class U>
381    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r);
382template<class T, class U>
383    shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r);
384
385// shared_ptr I/O:
386template<class E, class T, class Y>
387    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
388
389// shared_ptr get_deleter:
390template<class D, class T> D* get_deleter(shared_ptr<T> const& p);
391
392template<class T, class... Args>
393    shared_ptr<T> make_shared(Args&&... args);
394template<class T, class A, class... Args>
395    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
396
397template<class T>
398class weak_ptr
399{
400public:
401    typedef T element_type;
402
403    // constructors
404    constexpr weak_ptr();
405    template<class Y> weak_ptr(shared_ptr<Y> const& r);
406    weak_ptr(weak_ptr const& r);
407    template<class Y> weak_ptr(weak_ptr<Y> const& r);
408
409    // destructor
410    ~weak_ptr();
411
412    // assignment
413    weak_ptr& operator=(weak_ptr const& r);
414    template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r);
415    template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r);
416
417    // modifiers
418    void swap(weak_ptr& r);
419    void reset();
420
421    // observers
422    long use_count() const;
423    bool expired() const;
424    shared_ptr<T> lock() const;
425    template<class U> bool owner_before(shared_ptr<U> const& b);
426    template<class U> bool owner_before(weak_ptr<U> const& b);
427};
428
429// weak_ptr specialized algorithms:
430template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b);
431
432// class owner_less:
433template<class T> struct owner_less;
434
435template<class T>
436struct owner_less<shared_ptr<T>>
437    : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
438{
439    typedef bool result_type;
440    bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
441    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
442    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
443};
444
445template<class T>
446struct owner_less<weak_ptr<T>>
447    : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
448{
449    typedef bool result_type;
450    bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
451    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
452    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
453};
454
455template<class T>
456class enable_shared_from_this
457{
458protected:
459    constexpr enable_shared_from_this();
460    enable_shared_from_this(enable_shared_from_this const&);
461    enable_shared_from_this& operator=(enable_shared_from_this const&);
462    ~enable_shared_from_this();
463public:
464    shared_ptr<T> shared_from_this();
465    shared_ptr<T const> shared_from_this() const;
466};
467
468template<class T>
469    bool atomic_is_lock_free(const shared_ptr<T>* p);
470template<class T>
471    shared_ptr<T> atomic_load(const shared_ptr<T>* p);
472template<class T>
473    shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
474template<class T>
475    void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
476template<class T>
477    void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
478template<class T>
479    shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
480template<class T>
481    shared_ptr<T>
482    atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
483template<class T>
484    bool
485    atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
486template<class T>
487    bool
488    atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
489template<class T>
490    bool
491    atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
492                                          shared_ptr<T> w, memory_order success,
493                                          memory_order failure);
494template<class T>
495    bool
496    atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
497                                            shared_ptr<T> w, memory_order success,
498                                            memory_order failure);
499// Hash support
500template <class T> struct hash;
501template <class T, class D> struct hash<unique_ptr<T, D> >;
502template <class T> struct hash<shared_ptr<T> >;
503
504// Pointer safety
505enum class pointer_safety { relaxed, preferred, strict };
506void declare_reachable(void *p);
507template <class T> T *undeclare_reachable(T *p);
508void declare_no_pointers(char *p, size_t n);
509void undeclare_no_pointers(char *p, size_t n);
510pointer_safety get_pointer_safety();
511
512void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
513
514}  // std
515
516*/
517
518#include <__config>
519#include <type_traits>
520#include <typeinfo>
521#include <cstddef>
522#include <cstdint>
523#include <new>
524#include <utility>
525#include <limits>
526#include <iterator>
527#include <__functional_base>
528#if defined(_LIBCPP_NO_EXCEPTIONS)
529    #include <cassert>
530#endif
531
532#pragma GCC system_header
533
534_LIBCPP_BEGIN_NAMESPACE_STD
535
536// allocator_arg_t
537
538struct _LIBCPP_VISIBLE allocator_arg_t { };
539
540extern const allocator_arg_t allocator_arg;
541
542// addressof
543
544template <class _Tp>
545inline _LIBCPP_INLINE_VISIBILITY
546_Tp*
547addressof(_Tp& __x)
548{
549    return (_Tp*)&(char&)__x;
550}
551
552template <class _Tp> class allocator;
553
554template <>
555class _LIBCPP_VISIBLE allocator<void>
556{
557public:
558    typedef void*             pointer;
559    typedef const void*       const_pointer;
560    typedef void              value_type;
561
562    template <class _Up> struct rebind {typedef allocator<_Up> other;};
563};
564
565// pointer_traits
566
567template <class _Tp>
568struct __has_element_type
569{
570private:
571    struct __two {char _; char __;};
572    template <class _Up> static __two __test(...);
573    template <class _Up> static char __test(typename _Up::element_type* = 0);
574public:
575    static const bool value = sizeof(__test<_Tp>(0)) == 1;
576};
577
578template <class _Ptr, bool = __has_element_type<_Ptr>::value>
579struct __pointer_traits_element_type;
580
581template <class _Ptr>
582struct __pointer_traits_element_type<_Ptr, true>
583{
584    typedef typename _Ptr::element_type type;
585};
586
587#ifndef _LIBCPP_HAS_NO_VARIADICS
588
589template <template <class, class...> class _Sp, class _Tp, class ..._Args>
590struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
591{
592    typedef typename _Sp<_Tp, _Args...>::element_type type;
593};
594
595template <template <class, class...> class _Sp, class _Tp, class ..._Args>
596struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
597{
598    typedef _Tp type;
599};
600
601#else  // _LIBCPP_HAS_NO_VARIADICS
602
603template <template <class> class _Sp, class _Tp>
604struct __pointer_traits_element_type<_Sp<_Tp>, true>
605{
606    typedef typename _Sp<_Tp>::element_type type;
607};
608
609template <template <class> class _Sp, class _Tp>
610struct __pointer_traits_element_type<_Sp<_Tp>, false>
611{
612    typedef _Tp type;
613};
614
615template <template <class, class> class _Sp, class _Tp, class _A0>
616struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
617{
618    typedef typename _Sp<_Tp, _A0>::element_type type;
619};
620
621template <template <class, class> class _Sp, class _Tp, class _A0>
622struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
623{
624    typedef _Tp type;
625};
626
627template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
628struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
629{
630    typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
631};
632
633template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
634struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
635{
636    typedef _Tp type;
637};
638
639template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
640                                                           class _A1, class _A2>
641struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
642{
643    typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
644};
645
646template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
647                                                           class _A1, class _A2>
648struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
649{
650    typedef _Tp type;
651};
652
653#endif  // _LIBCPP_HAS_NO_VARIADICS
654
655template <class _Tp>
656struct __has_difference_type
657{
658private:
659    struct __two {char _; char __;};
660    template <class _Up> static __two __test(...);
661    template <class _Up> static char __test(typename _Up::difference_type* = 0);
662public:
663    static const bool value = sizeof(__test<_Tp>(0)) == 1;
664};
665
666template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
667struct __pointer_traits_difference_type
668{
669    typedef ptrdiff_t type;
670};
671
672template <class _Ptr>
673struct __pointer_traits_difference_type<_Ptr, true>
674{
675    typedef typename _Ptr::difference_type type;
676};
677
678template <class _Tp, class _Up>
679struct __has_rebind
680{
681private:
682    struct __two {char _; char __;};
683    template <class _Xp> static __two __test(...);
684    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
685public:
686    static const bool value = sizeof(__test<_Tp>(0)) == 1;
687};
688
689template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
690struct __pointer_traits_rebind
691{
692#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
693    typedef typename _Tp::template rebind<_Up> type;
694#else
695    typedef typename _Tp::template rebind<_Up>::other type;
696#endif
697};
698
699#ifndef _LIBCPP_HAS_NO_VARIADICS
700
701template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
702struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
703{
704#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
705    typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
706#else
707    typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
708#endif
709};
710
711template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
712struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
713{
714    typedef _Sp<_Up, _Args...> type;
715};
716
717#else  // _LIBCPP_HAS_NO_VARIADICS
718
719template <template <class> class _Sp, class _Tp, class _Up>
720struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
721{
722#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
723    typedef typename _Sp<_Tp>::template rebind<_Up> type;
724#else
725    typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
726#endif
727};
728
729template <template <class> class _Sp, class _Tp, class _Up>
730struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
731{
732    typedef _Sp<_Up> type;
733};
734
735template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
736struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
737{
738#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
739    typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
740#else
741    typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
742#endif
743};
744
745template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
746struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
747{
748    typedef _Sp<_Up, _A0> type;
749};
750
751template <template <class, class, class> class _Sp, class _Tp, class _A0,
752                                         class _A1, class _Up>
753struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
754{
755#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
756    typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
757#else
758    typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
759#endif
760};
761
762template <template <class, class, class> class _Sp, class _Tp, class _A0,
763                                         class _A1, class _Up>
764struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
765{
766    typedef _Sp<_Up, _A0, _A1> type;
767};
768
769template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
770                                                class _A1, class _A2, class _Up>
771struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
772{
773#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
774    typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
775#else
776    typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
777#endif
778};
779
780template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
781                                                class _A1, class _A2, class _Up>
782struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
783{
784    typedef _Sp<_Up, _A0, _A1, _A2> type;
785};
786
787#endif  // _LIBCPP_HAS_NO_VARIADICS
788
789template <class _Ptr>
790struct _LIBCPP_VISIBLE pointer_traits
791{
792    typedef _Ptr                                                     pointer;
793    typedef typename __pointer_traits_element_type<pointer>::type    element_type;
794    typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
795
796#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
797    template <class _Up> using rebind = __pointer_traits_rebind<pointer, _Up>::type;
798#else
799    template <class _Up> struct rebind
800        {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
801#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
802
803private:
804    struct __nat {};
805public:
806    _LIBCPP_INLINE_VISIBILITY
807    static pointer pointer_to(typename conditional<is_void<element_type>::value,
808                                           __nat, element_type>::type& __r)
809        {return pointer::pointer_to(__r);}
810};
811
812template <class _Tp>
813struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
814{
815    typedef _Tp*      pointer;
816    typedef _Tp       element_type;
817    typedef ptrdiff_t difference_type;
818
819#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
820    template <class _Up> using rebind = _Up*;
821#else
822    template <class _Up> struct rebind {typedef _Up* other;};
823#endif
824
825private:
826    struct __nat {};
827public:
828    _LIBCPP_INLINE_VISIBILITY
829    static pointer pointer_to(typename conditional<is_void<element_type>::value,
830                                           __nat, element_type>::type& __r)
831        {return _STD::addressof(__r);}
832};
833
834// allocator_traits
835
836namespace __has_pointer_type_imp
837{
838    template <class _Up> static __two test(...);
839    template <class _Up> static char test(typename _Up::pointer* = 0);
840}
841
842template <class _Tp>
843struct __has_pointer_type
844    : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
845{
846};
847
848namespace __pointer_type_imp
849{
850
851template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
852struct __pointer_type
853{
854    typedef typename _Dp::pointer type;
855};
856
857template <class _Tp, class _Dp>
858struct __pointer_type<_Tp, _Dp, false>
859{
860    typedef _Tp* type;
861};
862
863}  // __pointer_type_imp
864
865template <class _Tp, class _Dp>
866struct __pointer_type
867{
868    typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
869};
870
871template <class _Tp>
872struct __has_const_pointer
873{
874private:
875    struct __two {char _; char __;};
876    template <class _Up> static __two __test(...);
877    template <class _Up> static char __test(typename _Up::const_pointer* = 0);
878public:
879    static const bool value = sizeof(__test<_Tp>(0)) == 1;
880};
881
882template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
883struct __const_pointer
884{
885    typedef typename _Alloc::const_pointer type;
886};
887
888template <class _Tp, class _Ptr, class _Alloc>
889struct __const_pointer<_Tp, _Ptr, _Alloc, false>
890{
891#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
892    typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
893#else
894    typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
895#endif
896};
897
898template <class _Tp>
899struct __has_void_pointer
900{
901private:
902    struct __two {char _; char __;};
903    template <class _Up> static __two __test(...);
904    template <class _Up> static char __test(typename _Up::void_pointer* = 0);
905public:
906    static const bool value = sizeof(__test<_Tp>(0)) == 1;
907};
908
909template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
910struct __void_pointer
911{
912    typedef typename _Alloc::void_pointer type;
913};
914
915template <class _Ptr, class _Alloc>
916struct __void_pointer<_Ptr, _Alloc, false>
917{
918#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
919    typedef typename pointer_traits<_Ptr>::template rebind<void> type;
920#else
921    typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
922#endif
923};
924
925template <class _Tp>
926struct __has_const_void_pointer
927{
928private:
929    struct __two {char _; char __;};
930    template <class _Up> static __two __test(...);
931    template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
932public:
933    static const bool value = sizeof(__test<_Tp>(0)) == 1;
934};
935
936template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
937struct __const_void_pointer
938{
939    typedef typename _Alloc::const_void_pointer type;
940};
941
942template <class _Ptr, class _Alloc>
943struct __const_void_pointer<_Ptr, _Alloc, false>
944{
945#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
946    typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
947#else
948    typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
949#endif
950};
951
952template <class _T>
953inline _LIBCPP_INLINE_VISIBILITY
954_T*
955__to_raw_pointer(_T* __p)
956{
957    return __p;
958}
959
960template <class _Pointer>
961inline _LIBCPP_INLINE_VISIBILITY
962typename pointer_traits<_Pointer>::element_type*
963__to_raw_pointer(_Pointer __p)
964{
965    return _STD::__to_raw_pointer(__p.operator->());
966}
967
968template <class _Tp>
969struct __has_size_type
970{
971private:
972    struct __two {char _; char __;};
973    template <class _Up> static __two __test(...);
974    template <class _Up> static char __test(typename _Up::size_type* = 0);
975public:
976    static const bool value = sizeof(__test<_Tp>(0)) == 1;
977};
978
979template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
980struct __size_type
981{
982    typedef typename make_unsigned<_DiffType>::type type;
983};
984
985template <class _Alloc, class _DiffType>
986struct __size_type<_Alloc, _DiffType, true>
987{
988    typedef typename _Alloc::size_type type;
989};
990
991template <class _Tp>
992struct __has_propagate_on_container_copy_assignment
993{
994private:
995    struct __two {char _; char __;};
996    template <class _Up> static __two __test(...);
997    template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
998public:
999    static const bool value = sizeof(__test<_Tp>(0)) == 1;
1000};
1001
1002template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1003struct __propagate_on_container_copy_assignment
1004{
1005    typedef false_type type;
1006};
1007
1008template <class _Alloc>
1009struct __propagate_on_container_copy_assignment<_Alloc, true>
1010{
1011    typedef typename _Alloc::propagate_on_container_copy_assignment type;
1012};
1013
1014template <class _Tp>
1015struct __has_propagate_on_container_move_assignment
1016{
1017private:
1018    struct __two {char _; char __;};
1019    template <class _Up> static __two __test(...);
1020    template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1021public:
1022    static const bool value = sizeof(__test<_Tp>(0)) == 1;
1023};
1024
1025template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1026struct __propagate_on_container_move_assignment
1027{
1028    typedef false_type type;
1029};
1030
1031template <class _Alloc>
1032struct __propagate_on_container_move_assignment<_Alloc, true>
1033{
1034    typedef typename _Alloc::propagate_on_container_move_assignment type;
1035};
1036
1037template <class _Tp>
1038struct __has_propagate_on_container_swap
1039{
1040private:
1041    struct __two {char _; char __;};
1042    template <class _Up> static __two __test(...);
1043    template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1044public:
1045    static const bool value = sizeof(__test<_Tp>(0)) == 1;
1046};
1047
1048template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1049struct __propagate_on_container_swap
1050{
1051    typedef false_type type;
1052};
1053
1054template <class _Alloc>
1055struct __propagate_on_container_swap<_Alloc, true>
1056{
1057    typedef typename _Alloc::propagate_on_container_swap type;
1058};
1059
1060template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1061struct __has_rebind_other
1062{
1063private:
1064    struct __two {char _; char __;};
1065    template <class _Xp> static __two __test(...);
1066    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1067public:
1068    static const bool value = sizeof(__test<_Tp>(0)) == 1;
1069};
1070
1071template <class _Tp, class _Up>
1072struct __has_rebind_other<_Tp, _Up, false>
1073{
1074    static const bool value = false;
1075};
1076
1077template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1078struct __allocator_traits_rebind
1079{
1080    typedef typename _Tp::template rebind<_Up>::other type;
1081};
1082
1083#ifndef _LIBCPP_HAS_NO_VARIADICS
1084
1085template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1086struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1087{
1088    typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1089};
1090
1091template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1092struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1093{
1094    typedef _Alloc<_Up, _Args...> type;
1095};
1096
1097#else  // _LIBCPP_HAS_NO_VARIADICS
1098
1099template <template <class> class _Alloc, class _Tp, class _Up>
1100struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1101{
1102    typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1103};
1104
1105template <template <class> class _Alloc, class _Tp, class _Up>
1106struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1107{
1108    typedef _Alloc<_Up> type;
1109};
1110
1111template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1112struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1113{
1114    typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1115};
1116
1117template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1118struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1119{
1120    typedef _Alloc<_Up, _A0> type;
1121};
1122
1123template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1124                                         class _A1, class _Up>
1125struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1126{
1127    typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1128};
1129
1130template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1131                                         class _A1, class _Up>
1132struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1133{
1134    typedef _Alloc<_Up, _A0, _A1> type;
1135};
1136
1137template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1138                                                class _A1, class _A2, class _Up>
1139struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1140{
1141    typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1142};
1143
1144template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1145                                                class _A1, class _A2, class _Up>
1146struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1147{
1148    typedef _Alloc<_Up, _A0, _A1, _A2> type;
1149};
1150
1151#endif  // _LIBCPP_HAS_NO_VARIADICS
1152
1153#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1154
1155template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1156auto
1157__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1158    -> decltype(__a.allocate(__sz, __p), true_type());
1159
1160template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1161auto
1162__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1163    -> false_type;
1164
1165template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1166struct __has_allocate_hint
1167    : integral_constant<bool,
1168        is_same<
1169            decltype(__has_allocate_hint_test(declval<_Alloc>(),
1170                                          declval<_SizeType>(),
1171                                          declval<_ConstVoidPtr>())),
1172            true_type>::value>
1173{
1174};
1175
1176#else  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
1177
1178template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1179struct __has_allocate_hint
1180    : true_type
1181{
1182};
1183
1184#endif  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
1185
1186#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1187
1188template <class _Alloc, class _Tp, class ..._Args>
1189decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1190                                           _STD::declval<_Args>()...),
1191                                           true_type())
1192__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1193
1194template <class _Alloc, class _Pointer, class ..._Args>
1195false_type
1196__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1197
1198template <class _Alloc, class _Pointer, class ..._Args>
1199struct __has_construct
1200    : integral_constant<bool,
1201        is_same<
1202            decltype(__has_construct_test(declval<_Alloc>(),
1203                                          declval<_Pointer>(),
1204                                          declval<_Args>()...)),
1205            true_type>::value>
1206{
1207};
1208
1209template <class _Alloc, class _Pointer>
1210auto
1211__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1212    -> decltype(__a.destroy(__p), true_type());
1213
1214template <class _Alloc, class _Pointer>
1215auto
1216__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1217    -> false_type;
1218
1219template <class _Alloc, class _Pointer>
1220struct __has_destroy
1221    : integral_constant<bool,
1222        is_same<
1223            decltype(__has_destroy_test(declval<_Alloc>(),
1224                                        declval<_Pointer>())),
1225            true_type>::value>
1226{
1227};
1228
1229template <class _Alloc>
1230auto
1231__has_max_size_test(_Alloc&& __a)
1232    -> decltype(__a.max_size(), true_type());
1233
1234template <class _Alloc>
1235auto
1236__has_max_size_test(const volatile _Alloc& __a)
1237    -> false_type;
1238
1239template <class _Alloc>
1240struct __has_max_size
1241    : integral_constant<bool,
1242        is_same<
1243            decltype(__has_max_size_test(declval<_Alloc&>())),
1244            true_type>::value>
1245{
1246};
1247
1248template <class _Alloc>
1249auto
1250__has_select_on_container_copy_construction_test(_Alloc&& __a)
1251    -> decltype(__a.select_on_container_copy_construction(), true_type());
1252
1253template <class _Alloc>
1254auto
1255__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1256    -> false_type;
1257
1258template <class _Alloc>
1259struct __has_select_on_container_copy_construction
1260    : integral_constant<bool,
1261        is_same<
1262            decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1263            true_type>::value>
1264{
1265};
1266
1267#else  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <class _Alloc, class _Pointer, class ..._Args>
1272struct __has_construct
1273    : false_type
1274{
1275};
1276
1277#endif  // _LIBCPP_HAS_NO_VARIADICS
1278
1279template <class _Alloc, class _Pointer>
1280struct __has_destroy
1281    : false_type
1282{
1283};
1284
1285template <class _Alloc>
1286struct __has_max_size
1287    : true_type
1288{
1289};
1290
1291template <class _Alloc>
1292struct __has_select_on_container_copy_construction
1293    : false_type
1294{
1295};
1296
1297#endif  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
1298
1299template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1300struct __alloc_traits_difference_type
1301{
1302    typedef typename pointer_traits<_Ptr>::difference_type type;
1303};
1304
1305template <class _Alloc, class _Ptr>
1306struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1307{
1308    typedef typename _Alloc::difference_type type;
1309};
1310
1311template <class _Alloc>
1312struct _LIBCPP_VISIBLE allocator_traits
1313{
1314    typedef _Alloc                              allocator_type;
1315    typedef typename allocator_type::value_type value_type;
1316
1317    typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1318    typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1319    typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1320    typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1321
1322    typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1323    typedef typename __size_type<allocator_type, difference_type>::type size_type;
1324
1325    typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1326                     propagate_on_container_copy_assignment;
1327    typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1328                     propagate_on_container_move_assignment;
1329    typedef typename __propagate_on_container_swap<allocator_type>::type
1330                     propagate_on_container_swap;
1331
1332#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1333    template <class _Tp> using rebind_alloc =
1334                           __allocator_traits_rebind<allocator_type, _Tp>::type;
1335    template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
1336#else  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1337    template <class _Tp> struct rebind_alloc
1338        {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1339    template <class _Tp> struct rebind_traits
1340        {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
1341#endif  // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1342
1343    _LIBCPP_INLINE_VISIBILITY
1344    static pointer allocate(allocator_type& __a, size_type __n)
1345        {return __a.allocate(__n);}
1346    _LIBCPP_INLINE_VISIBILITY
1347    static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1348        {return allocate(__a, __n, __hint,
1349            __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1350
1351    _LIBCPP_INLINE_VISIBILITY
1352    static void deallocate(allocator_type& __a, pointer __p, size_type __n)
1353        {__a.deallocate(__p, __n);}
1354
1355#ifndef _LIBCPP_HAS_NO_VARIADICS
1356    template <class _Tp, class... _Args>
1357        _LIBCPP_INLINE_VISIBILITY
1358        static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1359            {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1360                         __a, __p, _STD::forward<_Args>(__args)...);}
1361#else  // _LIBCPP_HAS_NO_VARIADICS
1362    template <class _Tp>
1363        _LIBCPP_INLINE_VISIBILITY
1364        static void construct(allocator_type& __a, _Tp* __p)
1365            {
1366                ::new ((void*)__p) _Tp();
1367            }
1368    template <class _Tp, class _A0>
1369        _LIBCPP_INLINE_VISIBILITY
1370        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1371            {
1372                ::new ((void*)__p) _Tp(__a0);
1373            }
1374    template <class _Tp, class _A0, class _A1>
1375        _LIBCPP_INLINE_VISIBILITY
1376        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1377                              const _A1& __a1)
1378            {
1379                ::new ((void*)__p) _Tp(__a0, __a1);
1380            }
1381    template <class _Tp, class _A0, class _A1, class _A2>
1382        _LIBCPP_INLINE_VISIBILITY
1383        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1384                              const _A1& __a1, const _A2& __a2)
1385            {
1386                ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1387            }
1388#endif  // _LIBCPP_HAS_NO_VARIADICS
1389
1390    template <class _Tp>
1391        _LIBCPP_INLINE_VISIBILITY
1392        static void destroy(allocator_type& __a, _Tp* __p)
1393            {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1394
1395    _LIBCPP_INLINE_VISIBILITY
1396    static size_type max_size(const allocator_type& __a)
1397        {return __max_size(__has_max_size<const allocator_type>(), __a);}
1398
1399    _LIBCPP_INLINE_VISIBILITY
1400    static allocator_type
1401        select_on_container_copy_construction(const allocator_type& __a)
1402            {return select_on_container_copy_construction(
1403                __has_select_on_container_copy_construction<const allocator_type>(),
1404                __a);}
1405
1406private:
1407
1408    _LIBCPP_INLINE_VISIBILITY
1409    static pointer allocate(allocator_type& __a, size_type __n,
1410        const_void_pointer __hint, true_type)
1411        {return __a.allocate(__n, __hint);}
1412    _LIBCPP_INLINE_VISIBILITY
1413    static pointer allocate(allocator_type& __a, size_type __n,
1414        const_void_pointer __hint, false_type)
1415        {return __a.allocate(__n);}
1416
1417#ifndef _LIBCPP_HAS_NO_VARIADICS
1418    template <class _Tp, class... _Args>
1419        _LIBCPP_INLINE_VISIBILITY
1420        static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1421            {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1422    template <class _Tp, class... _Args>
1423        _LIBCPP_INLINE_VISIBILITY
1424        static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1425            {
1426                ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1427            }
1428#endif  // _LIBCPP_HAS_NO_VARIADICS
1429
1430    template <class _Tp>
1431        _LIBCPP_INLINE_VISIBILITY
1432        static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1433            {__a.destroy(__p);}
1434    template <class _Tp>
1435        _LIBCPP_INLINE_VISIBILITY
1436        static void __destroy(false_type, allocator_type&, _Tp* __p)
1437            {
1438                __p->~_Tp();
1439            }
1440
1441    _LIBCPP_INLINE_VISIBILITY
1442    static size_type __max_size(true_type, const allocator_type& __a)
1443            {return __a.max_size();}
1444    _LIBCPP_INLINE_VISIBILITY
1445    static size_type __max_size(false_type, const allocator_type&)
1446            {return numeric_limits<size_type>::max();}
1447
1448    _LIBCPP_INLINE_VISIBILITY
1449    static allocator_type
1450        select_on_container_copy_construction(true_type, const allocator_type& __a)
1451            {return __a.select_on_container_copy_construction();}
1452    _LIBCPP_INLINE_VISIBILITY
1453    static allocator_type
1454        select_on_container_copy_construction(false_type, const allocator_type& __a)
1455            {return __a;}
1456};
1457
1458// uses_allocator
1459
1460template <class _Tp>
1461struct __has_allocator_type
1462{
1463private:
1464    struct __two {char _; char __;};
1465    template <class _Up> static __two __test(...);
1466    template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1467public:
1468    static const bool value = sizeof(__test<_Tp>(0)) == 1;
1469};
1470
1471template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1472struct __uses_allocator
1473    : public integral_constant<bool,
1474        is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1475{
1476};
1477
1478template <class _Tp, class _Alloc>
1479struct __uses_allocator<_Tp, _Alloc, false>
1480    : public false_type
1481{
1482};
1483
1484template <class _Tp, class _Alloc>
1485struct _LIBCPP_VISIBLE uses_allocator
1486    : public __uses_allocator<_Tp, _Alloc>
1487{
1488};
1489
1490#ifndef _LIBCPP_HAS_NO_VARIADICS
1491
1492// uses-allocator construction
1493
1494template <class _Tp, class _Alloc, class ..._Args>
1495struct __uses_alloc_ctor_imp
1496{
1497    static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1498    static const bool __ic =
1499        is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1500    static const int value = __ua ? 2 - __ic : 0;
1501};
1502
1503template <class _Tp, class _Alloc, class ..._Args>
1504struct __uses_alloc_ctor
1505    : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1506    {};
1507
1508#endif  // _LIBCPP_HAS_NO_VARIADICS
1509
1510// allocator
1511
1512template <class _Tp>
1513class _LIBCPP_VISIBLE allocator
1514{
1515public:
1516    typedef size_t            size_type;
1517    typedef ptrdiff_t         difference_type;
1518    typedef _Tp*              pointer;
1519    typedef const _Tp*        const_pointer;
1520    typedef _Tp&              reference;
1521    typedef const _Tp&        const_reference;
1522    typedef _Tp               value_type;
1523
1524    template <class _Up> struct rebind {typedef allocator<_Up> other;};
1525
1526    _LIBCPP_INLINE_VISIBILITY allocator() throw() {}
1527    template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) throw() {}
1528    _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const             {return addressof(__x);}
1529    _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const {return addressof(__x);}
1530    _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1531        {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1532    _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) {::operator delete((void*)__p);}
1533    _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
1534#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1535    template <class _Up, class... _Args>
1536        _LIBCPP_INLINE_VISIBILITY
1537        void
1538        construct(_Up* __p, _Args&&... __args)
1539        {
1540            ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1541        }
1542#else  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1543        _LIBCPP_INLINE_VISIBILITY
1544        void
1545        construct(pointer __p)
1546        {
1547            ::new((void*)__p) _Tp();
1548        }
1549# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1550    template <class _A0>
1551        _LIBCPP_INLINE_VISIBILITY
1552        typename enable_if
1553        <
1554            !is_convertible<_A0, __rv<_A0> >::value,
1555            void
1556        >::type
1557        construct(pointer __p, _A0& __a0)
1558        {
1559            ::new((void*)__p) _Tp(__a0);
1560        }
1561    template <class _A0>
1562        _LIBCPP_INLINE_VISIBILITY
1563        typename enable_if
1564        <
1565            !is_convertible<_A0, __rv<_A0> >::value,
1566            void
1567        >::type
1568        construct(pointer __p, const _A0& __a0)
1569        {
1570            ::new((void*)__p) _Tp(__a0);
1571        }
1572    template <class _A0>
1573        _LIBCPP_INLINE_VISIBILITY
1574        typename enable_if
1575        <
1576            is_convertible<_A0, __rv<_A0> >::value,
1577            void
1578        >::type
1579        construct(pointer __p, _A0 __a0)
1580        {
1581            ::new((void*)__p) _Tp(_STD::move(__a0));
1582        }
1583# endif  // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1584    template <class _A0, class _A1>
1585        _LIBCPP_INLINE_VISIBILITY
1586        void
1587        construct(pointer __p, _A0& __a0, _A1& __a1)
1588        {
1589            ::new((void*)__p) _Tp(__a0, __a1);
1590        }
1591    template <class _A0, class _A1>
1592        _LIBCPP_INLINE_VISIBILITY
1593        void
1594        construct(pointer __p, const _A0& __a0, _A1& __a1)
1595        {
1596            ::new((void*)__p) _Tp(__a0, __a1);
1597        }
1598    template <class _A0, class _A1>
1599        _LIBCPP_INLINE_VISIBILITY
1600        void
1601        construct(pointer __p, _A0& __a0, const _A1& __a1)
1602        {
1603            ::new((void*)__p) _Tp(__a0, __a1);
1604        }
1605    template <class _A0, class _A1>
1606        _LIBCPP_INLINE_VISIBILITY
1607        void
1608        construct(pointer __p, const _A0& __a0, const _A1& __a1)
1609        {
1610            ::new((void*)__p) _Tp(__a0, __a1);
1611        }
1612#endif  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1613    _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1614};
1615
1616template <class _Tp, class _Up>
1617inline _LIBCPP_INLINE_VISIBILITY
1618bool operator==(const allocator<_Tp>&, const allocator<_Up>&) throw() {return true;}
1619
1620template <class _Tp, class _Up>
1621inline _LIBCPP_INLINE_VISIBILITY
1622bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) throw() {return false;}
1623
1624template <class _OutputIterator, class _Tp>
1625class _LIBCPP_VISIBLE raw_storage_iterator
1626    : public iterator<output_iterator_tag,
1627                      _Tp,                                         // purposefully not C++03
1628                      ptrdiff_t,                                   // purposefully not C++03
1629                      _Tp*,                                        // purposefully not C++03
1630                      raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1631{
1632private:
1633    _OutputIterator __x_;
1634public:
1635    _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1636    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1637    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1638        {::new(&*__x_) _Tp(__element); return *this;}
1639    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1640    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator  operator++(int)
1641        {raw_storage_iterator __t(*this); ++__x_; return __t;}
1642};
1643
1644template <class _Tp>
1645pair<_Tp*, ptrdiff_t>
1646get_temporary_buffer(ptrdiff_t __n)
1647{
1648    pair<_Tp*, ptrdiff_t> __r(0, 0);
1649    const ptrdiff_t __m = (~ptrdiff_t(0) ^
1650                           ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1651                           / sizeof(_Tp);
1652    if (__n > __m)
1653        __n = __m;
1654    while (__n > 0)
1655    {
1656        __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1657        if (__r.first)
1658        {
1659            __r.second = __n;
1660            break;
1661        }
1662        __n /= 2;
1663    }
1664    return __r;
1665}
1666
1667template <class _Tp>
1668inline _LIBCPP_INLINE_VISIBILITY
1669void return_temporary_buffer(_Tp* __p) {::operator delete(__p);}
1670
1671template <class _Tp>
1672struct auto_ptr_ref
1673{
1674    _Tp* __ptr_;
1675};
1676
1677template<class _Tp>
1678class _LIBCPP_VISIBLE auto_ptr
1679{
1680private:
1681    _Tp* __ptr_;
1682public:
1683    typedef _Tp element_type;
1684
1685    _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1686    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1687    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1688        : __ptr_(__p.release()) {}
1689    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1690        {reset(__p.release()); return *this;}
1691    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1692        {reset(__p.release()); return *this;}
1693    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1694        {reset(__p.__ptr_); return *this;}
1695    _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1696
1697    _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1698        {return *__ptr_;}
1699    _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1700    _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1701    _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1702    {
1703        _Tp* __t = __ptr_;
1704        __ptr_ = 0;
1705        return __t;
1706    }
1707    _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1708    {
1709        if (__ptr_ != __p)
1710            delete __ptr_;
1711        __ptr_ = __p;
1712    }
1713
1714    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1715    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1716        {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1717    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1718        {return auto_ptr<_Up>(release());}
1719};
1720
1721template <>
1722class _LIBCPP_VISIBLE auto_ptr<void>
1723{
1724public:
1725    typedef void element_type;
1726};
1727
1728template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1729                                                     typename remove_cv<_T2>::type>::value,
1730                                bool = is_empty<_T1>::value,
1731                                bool = is_empty<_T2>::value>
1732struct __libcpp_compressed_pair_switch;
1733
1734template <class _T1, class _T2, bool IsSame>
1735struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1736
1737template <class _T1, class _T2, bool IsSame>
1738struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false>  {enum {value = 1};};
1739
1740template <class _T1, class _T2, bool IsSame>
1741struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true>  {enum {value = 2};};
1742
1743template <class _T1, class _T2>
1744struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true>    {enum {value = 3};};
1745
1746template <class _T1, class _T2>
1747struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true>     {enum {value = 1};};
1748
1749template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1750class __libcpp_compressed_pair_imp;
1751
1752template <class _T1, class _T2>
1753class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1754{
1755private:
1756    _T1 __first_;
1757    _T2 __second_;
1758public:
1759    typedef _T1 _T1_param;
1760    typedef _T2 _T2_param;
1761
1762    typedef typename remove_reference<_T1>::type& _T1_reference;
1763    typedef typename remove_reference<_T2>::type& _T2_reference;
1764
1765    typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1766    typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1767
1768    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1769    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1770        : __first_(_STD::forward<_T1_param>(__t1)) {}
1771    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1772        : __second_(_STD::forward<_T2_param>(__t2)) {}
1773    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1774        : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1775
1776#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1777    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1778        : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
1779#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1780
1781    _LIBCPP_INLINE_VISIBILITY _T1_reference       first()       {return __first_;}
1782    _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1783
1784    _LIBCPP_INLINE_VISIBILITY _T2_reference       second()       {return __second_;}
1785    _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1786
1787    _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1788    {
1789        using _STD::swap;
1790        swap(__first_, __x.__first_);
1791        swap(__second_, __x.__second_);
1792    }
1793};
1794
1795template <class _T1, class _T2>
1796class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1797    : private _T1
1798{
1799private:
1800    _T2 __second_;
1801public:
1802    typedef _T1 _T1_param;
1803    typedef _T2 _T2_param;
1804
1805    typedef _T1&                                        _T1_reference;
1806    typedef typename remove_reference<_T2>::type& _T2_reference;
1807
1808    typedef const _T1&                                        _T1_const_reference;
1809    typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1810
1811    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1812    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1813        : _T1(_STD::forward<_T1_param>(__t1)) {}
1814    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1815        : __second_(_STD::forward<_T2_param>(__t2)) {}
1816    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1817        : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1818
1819#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1820    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1821        : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
1822#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1823
1824    _LIBCPP_INLINE_VISIBILITY _T1_reference       first()       {return *this;}
1825    _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1826
1827    _LIBCPP_INLINE_VISIBILITY _T2_reference       second()       {return __second_;}
1828    _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return __second_;}
1829
1830    _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1831    {
1832        using _STD::swap;
1833        swap(__second_, __x.__second_);
1834    }
1835};
1836
1837template <class _T1, class _T2>
1838class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1839    : private _T2
1840{
1841private:
1842    _T1 __first_;
1843public:
1844    typedef _T1 _T1_param;
1845    typedef _T2 _T2_param;
1846
1847    typedef typename remove_reference<_T1>::type& _T1_reference;
1848    typedef _T2&                                        _T2_reference;
1849
1850    typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1851    typedef const _T2&                                        _T2_const_reference;
1852
1853    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1854    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1855        : __first_(_STD::forward<_T1_param>(__t1)) {}
1856    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1857        : _T2(_STD::forward<_T2_param>(__t2)) {}
1858    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1859        : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1860
1861#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1862    __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1863        : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
1864#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1865
1866    _LIBCPP_INLINE_VISIBILITY _T1_reference       first()       {return __first_;}
1867    _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return __first_;}
1868
1869    _LIBCPP_INLINE_VISIBILITY _T2_reference       second()       {return *this;}
1870    _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1871
1872    _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1873    {
1874        using _STD::swap;
1875        swap(__first_, __x.__first_);
1876    }
1877};
1878
1879template <class _T1, class _T2>
1880class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1881    : private _T1,
1882      private _T2
1883{
1884public:
1885    typedef _T1 _T1_param;
1886    typedef _T2 _T2_param;
1887
1888    typedef _T1& _T1_reference;
1889    typedef _T2& _T2_reference;
1890
1891    typedef const _T1& _T1_const_reference;
1892    typedef const _T2& _T2_const_reference;
1893
1894    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1895    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1896        : _T1(_STD::forward<_T1_param>(__t1)) {}
1897    _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1898        : _T2(_STD::forward<_T2_param>(__t2)) {}
1899    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1900        : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1901
1902#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1903    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1904        : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
1905#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1906
1907    _LIBCPP_INLINE_VISIBILITY _T1_reference       first()       {return *this;}
1908    _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return *this;}
1909
1910    _LIBCPP_INLINE_VISIBILITY _T2_reference       second()       {return *this;}
1911    _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return *this;}
1912
1913    _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
1914    {
1915    }
1916};
1917
1918template <class _T1, class _T2>
1919class __compressed_pair
1920    : private __libcpp_compressed_pair_imp<_T1, _T2>
1921{
1922    typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
1923public:
1924    typedef typename base::_T1_param _T1_param;
1925    typedef typename base::_T2_param _T2_param;
1926
1927    typedef typename base::_T1_reference _T1_reference;
1928    typedef typename base::_T2_reference _T2_reference;
1929
1930    typedef typename base::_T1_const_reference _T1_const_reference;
1931    typedef typename base::_T2_const_reference _T2_const_reference;
1932
1933    _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
1934    _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
1935        : base(_STD::forward<_T1_param>(__t1)) {}
1936    _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
1937        : base(_STD::forward<_T2_param>(__t2)) {}
1938    _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
1939        : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
1940
1941#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1942    __compressed_pair(__compressed_pair&& __p)
1943        : base(_STD::move(__p)) {}
1944#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1945
1946    _LIBCPP_INLINE_VISIBILITY _T1_reference       first()       {return base::first();}
1947    _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const {return base::first();}
1948
1949    _LIBCPP_INLINE_VISIBILITY _T2_reference       second()       {return base::second();}
1950    _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const {return base::second();}
1951
1952    _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x) {base::swap(__x);}
1953};
1954
1955template <class _T1, class _T2>
1956inline _LIBCPP_INLINE_VISIBILITY
1957void
1958swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1959    {__x.swap(__y);}
1960
1961template <class _Tp>
1962struct _LIBCPP_VISIBLE default_delete
1963{
1964    _LIBCPP_INLINE_VISIBILITY default_delete() {}
1965    template <class _Up>
1966        _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
1967                      typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) {}
1968    _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1969        {
1970            static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1971            delete __ptr;
1972        }
1973};
1974
1975template <class _Tp>
1976struct _LIBCPP_VISIBLE default_delete<_Tp[]>
1977{
1978    _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const
1979        {
1980            static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
1981            delete [] __ptr;
1982        }
1983private:
1984    template <class _Up> void operator() (_Up*) const;
1985};
1986
1987template <class _Tp, class _Dp = default_delete<_Tp> >
1988class _LIBCPP_VISIBLE unique_ptr
1989{
1990public:
1991    typedef _Tp element_type;
1992    typedef _Dp deleter_type;
1993    typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
1994private:
1995    __compressed_pair<pointer, deleter_type> __ptr_;
1996
1997#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1998    unique_ptr(const unique_ptr&);
1999    unique_ptr& operator=(const unique_ptr&);
2000    template <class _Up, class _Ep>
2001        unique_ptr(const unique_ptr<_Up, _Ep>&);
2002    template <class _Up, class _Ep>
2003        unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
2004#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2005    unique_ptr(unique_ptr&);
2006    template <class _Up, class _Ep>
2007        unique_ptr(unique_ptr<_Up, _Ep>&);
2008    unique_ptr& operator=(unique_ptr&);
2009    template <class _Up, class _Ep>
2010        unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2011#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2012
2013    struct __nat {int __for_bool_;};
2014
2015    typedef       typename remove_reference<deleter_type>::type& _Dp_reference;
2016    typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2017public:
2018    _LIBCPP_INLINE_VISIBILITY unique_ptr()
2019        : __ptr_(pointer())
2020        {
2021            static_assert(!is_pointer<deleter_type>::value,
2022                "unique_ptr constructed with null function pointer deleter");
2023        }
2024    _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2025        : __ptr_(pointer())
2026        {
2027            static_assert(!is_pointer<deleter_type>::value,
2028                "unique_ptr constructed with null function pointer deleter");
2029        }
2030    _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2031        : __ptr_(_STD::move(__p))
2032        {
2033            static_assert(!is_pointer<deleter_type>::value,
2034                "unique_ptr constructed with null function pointer deleter");
2035        }
2036
2037#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2038    _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2039                                        is_reference<deleter_type>::value,
2040                                        deleter_type,
2041                                        typename add_lvalue_reference<const deleter_type>::type>::type __d)
2042        : __ptr_(__p, __d) {}
2043
2044    _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
2045        : __ptr_(__p, _STD::move(__d))
2046        {
2047            static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2048        }
2049    _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2050        : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2051    template <class _Up, class _Ep>
2052        _LIBCPP_INLINE_VISIBILITY
2053        unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2054                   typename enable_if
2055                      <
2056                        !is_array<_Up>::value &&
2057                         is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2058                         is_convertible<_Ep, deleter_type>::value &&
2059                         (
2060                            !is_reference<deleter_type>::value ||
2061                            is_same<deleter_type, _Ep>::value
2062                         ),
2063                         __nat
2064                      >::type = __nat())
2065            : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2066
2067    template <class _Up>
2068        _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2069                typename enable_if<
2070                                      is_convertible<_Up*, _Tp*>::value &&
2071                                      is_same<_Dp, default_delete<_Tp> >::value,
2072                                      __nat
2073                                  >::type = __nat())
2074            : __ptr_(__p.release())
2075            {
2076            }
2077
2078        _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2079            {
2080                reset(__u.release());
2081                __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2082                return *this;
2083            }
2084
2085        template <class _Up, class _Ep>
2086            _LIBCPP_INLINE_VISIBILITY
2087            typename enable_if
2088            <
2089                !is_array<_Up>::value,
2090                unique_ptr&
2091            >::type
2092            operator=(unique_ptr<_Up, _Ep>&& __u)
2093            {
2094                reset(__u.release());
2095                __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2096                return *this;
2097            }
2098#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2099
2100    _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2101    {
2102        return __rv<unique_ptr>(*this);
2103    }
2104
2105    _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2106        : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2107
2108    template <class _Up, class _Ep>
2109    _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2110    {
2111        reset(__u.release());
2112        __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2113        return *this;
2114    }
2115
2116    _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2117        : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2118
2119    template <class _Up>
2120        _LIBCPP_INLINE_VISIBILITY
2121                typename enable_if<
2122                                      is_convertible<_Up*, _Tp*>::value &&
2123                                      is_same<_Dp, default_delete<_Tp> >::value,
2124                                      unique_ptr&
2125                                  >::type
2126        operator=(auto_ptr<_Up> __p)
2127            {reset(__p.release()); return *this;}
2128
2129#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2130    _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2131
2132    _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2133    {
2134        reset();
2135        return *this;
2136    }
2137
2138    _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2139        {return *__ptr_.first();}
2140    _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_.first();}
2141    _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2142    _LIBCPP_INLINE_VISIBILITY       _Dp_reference get_deleter()       {return __ptr_.second();}
2143    _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2144    _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2145
2146    _LIBCPP_INLINE_VISIBILITY pointer release()
2147    {
2148        pointer __t = __ptr_.first();
2149        __ptr_.first() = pointer();
2150        return __t;
2151    }
2152
2153    _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2154    {
2155        pointer __tmp = __ptr_.first();
2156        __ptr_.first() = __p;
2157        if (__tmp)
2158            __ptr_.second()(__tmp);
2159    }
2160
2161    _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2162};
2163
2164template <class _Tp, class _Dp>
2165class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
2166{
2167public:
2168    typedef _Tp element_type;
2169    typedef _Dp deleter_type;
2170    typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2171private:
2172    __compressed_pair<pointer, deleter_type> __ptr_;
2173
2174#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2175    unique_ptr(const unique_ptr&);
2176    unique_ptr& operator=(const unique_ptr&);
2177#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2178    unique_ptr(unique_ptr&);
2179    template <class _Up>
2180        unique_ptr(unique_ptr<_Up>&);
2181    unique_ptr& operator=(unique_ptr&);
2182    template <class _Up>
2183        unique_ptr& operator=(unique_ptr<_Up>&);
2184#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2185
2186    struct __nat {int __for_bool_;};
2187
2188    typedef       typename remove_reference<deleter_type>::type& _Dp_reference;
2189    typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2190public:
2191    _LIBCPP_INLINE_VISIBILITY unique_ptr()
2192        : __ptr_(pointer())
2193        {
2194            static_assert(!is_pointer<deleter_type>::value,
2195                "unique_ptr constructed with null function pointer deleter");
2196        }
2197    _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t)
2198        : __ptr_(pointer())
2199        {
2200            static_assert(!is_pointer<deleter_type>::value,
2201                "unique_ptr constructed with null function pointer deleter");
2202        }
2203#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2204    template <class _P,
2205              class = typename enable_if<is_same<_P, pointer>::value>::type
2206             >
2207    _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p)
2208        : __ptr_(__p)
2209        {
2210            static_assert(!is_pointer<deleter_type>::value,
2211                "unique_ptr constructed with null function pointer deleter");
2212        }
2213
2214    template <class _P,
2215              class = typename enable_if<is_same<_P, pointer>::value>::type
2216             >
2217    _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2218                                       is_reference<deleter_type>::value,
2219                                       deleter_type,
2220                                       typename add_lvalue_reference<const deleter_type>::type>::type __d)
2221        : __ptr_(__p, __d) {}
2222
2223    _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2224                                       is_reference<deleter_type>::value,
2225                                       deleter_type,
2226                                       typename add_lvalue_reference<const deleter_type>::type>::type __d)
2227        : __ptr_(pointer(), __d) {}
2228
2229    template <class _P,
2230              class = typename enable_if<is_same<_P, pointer>::value ||
2231                                         is_same<_P, nullptr_t>::value>::type
2232             >
2233    _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
2234        : __ptr_(__p, _STD::move(__d))
2235        {
2236            static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2237        }
2238
2239    _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
2240        : __ptr_(pointer(), _STD::move(__d))
2241        {
2242            static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2243        }
2244
2245    _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u)
2246        : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2247
2248    _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u)
2249        {
2250            reset(__u.release());
2251            __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2252            return *this;
2253        }
2254#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2255
2256    _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2257        : __ptr_(__p)
2258        {
2259            static_assert(!is_pointer<deleter_type>::value,
2260                "unique_ptr constructed with null function pointer deleter");
2261        }
2262
2263    _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2264        : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2265
2266    _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2267        : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2268
2269    _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2270    {
2271        return __rv<unique_ptr>(*this);
2272    }
2273
2274    _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2275        : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2276
2277    _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2278    {
2279        reset(__u->release());
2280        __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2281        return *this;
2282    }
2283
2284#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2285    _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2286
2287    _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t)
2288    {
2289        reset();
2290        return *this;
2291    }
2292
2293    _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2294        {return __ptr_.first()[__i];}
2295    _LIBCPP_INLINE_VISIBILITY pointer get() const {return __ptr_.first();}
2296    _LIBCPP_INLINE_VISIBILITY       _Dp_reference get_deleter()       {return __ptr_.second();}
2297    _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const {return __ptr_.second();}
2298    _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
2299
2300    _LIBCPP_INLINE_VISIBILITY pointer release()
2301    {
2302        pointer __t = __ptr_.first();
2303        __ptr_.first() = pointer();
2304        return __t;
2305    }
2306
2307#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2308    template <class _P,
2309              class = typename enable_if<is_same<_P, pointer>::value>::type
2310             >
2311    _LIBCPP_INLINE_VISIBILITY void reset(_P __p)
2312    {
2313        pointer __tmp = __ptr_.first();
2314        __ptr_.first() = __p;
2315        if (__tmp)
2316            __ptr_.second()(__tmp);
2317    }
2318    _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t)
2319    {
2320        pointer __tmp = __ptr_.first();
2321        __ptr_.first() = nullptr;
2322        if (__tmp)
2323            __ptr_.second()(__tmp);
2324    }
2325    _LIBCPP_INLINE_VISIBILITY void reset()
2326    {
2327        pointer __tmp = __ptr_.first();
2328        __ptr_.first() = nullptr;
2329        if (__tmp)
2330            __ptr_.second()(__tmp);
2331    }
2332#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2333    _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2334    {
2335        pointer __tmp = __ptr_.first();
2336        __ptr_.first() = __p;
2337        if (__tmp)
2338            __ptr_.second()(__tmp);
2339    }
2340#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2341
2342    _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2343private:
2344
2345#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2346    template <class _Up>
2347        explicit unique_ptr(_Up);
2348    template <class _Up>
2349        unique_ptr(_Up __u,
2350                   typename conditional<
2351                                       is_reference<deleter_type>::value,
2352                                       deleter_type,
2353                                       typename add_lvalue_reference<const deleter_type>::type>::type,
2354                   typename enable_if
2355                      <
2356                         is_convertible<_Up, pointer>::value,
2357                         __nat
2358                      >::type = __nat());
2359#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2360};
2361
2362template <class _Tp, class _Dp>
2363inline _LIBCPP_INLINE_VISIBILITY
2364void
2365swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) {__x.swap(__y);}
2366
2367template <class _T1, class _D1, class _T2, class _D2>
2368inline _LIBCPP_INLINE_VISIBILITY
2369bool
2370operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2371
2372template <class _T1, class _D1, class _T2, class _D2>
2373inline _LIBCPP_INLINE_VISIBILITY
2374bool
2375operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2376
2377template <class _T1, class _D1, class _T2, class _D2>
2378inline _LIBCPP_INLINE_VISIBILITY
2379bool
2380operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2381
2382template <class _T1, class _D1, class _T2, class _D2>
2383inline _LIBCPP_INLINE_VISIBILITY
2384bool
2385operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2386
2387template <class _T1, class _D1, class _T2, class _D2>
2388inline _LIBCPP_INLINE_VISIBILITY
2389bool
2390operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2391
2392template <class _T1, class _D1, class _T2, class _D2>
2393inline _LIBCPP_INLINE_VISIBILITY
2394bool
2395operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2396
2397template <class> struct hash;
2398
2399template<class _Tp>
2400struct _LIBCPP_VISIBLE hash<_Tp*>
2401    : public unary_function<_Tp*, size_t>
2402{
2403    _LIBCPP_INLINE_VISIBILITY
2404    size_t operator()(_Tp* __v) const
2405    {
2406        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2407        return *__p;
2408    }
2409};
2410
2411template <class _Tp, class _Dp>
2412struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
2413{
2414    typedef unique_ptr<_Tp, _Dp> argument_type;
2415    typedef size_t               result_type;
2416    _LIBCPP_INLINE_VISIBILITY
2417    result_type operator()(const argument_type& __ptr) const
2418    {
2419        typedef typename argument_type::pointer pointer;
2420        return hash<pointer>()(__ptr.get());
2421    }
2422};
2423
2424struct __destruct_n
2425{
2426private:
2427    size_t size;
2428
2429    template <class _Tp>
2430    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2431        {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2432
2433    template <class _Tp>
2434    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2435        {}
2436
2437    _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2438        {++size;}
2439    _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2440        {}
2441
2442    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2443        {size = __s;}
2444    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2445        {}
2446public:
2447    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2448
2449    template <class _Tp>
2450    _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
2451        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2452
2453    template <class _Tp>
2454    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
2455        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2456
2457    template <class _Tp>
2458    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
2459        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2460};
2461
2462template <class _Alloc>
2463class __allocator_destructor
2464{
2465    typedef allocator_traits<_Alloc> __alloc_traits;
2466public:
2467    typedef typename __alloc_traits::pointer pointer;
2468    typedef typename __alloc_traits::size_type size_type;
2469private:
2470    _Alloc& __alloc_;
2471    size_type __s_;
2472public:
2473    _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2474        : __alloc_(__a), __s_(__s) {}
2475    _LIBCPP_INLINE_VISIBILITY
2476    void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2477};
2478
2479template <class _InputIterator, class _ForwardIterator>
2480_ForwardIterator
2481uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2482{
2483    __destruct_n __d(0);
2484    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2485    unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2486    for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2487        ::new(&*__r) value_type(*__f);
2488    __h.release();
2489    return __r;
2490}
2491
2492template <class _InputIterator, class _Size, class _ForwardIterator>
2493_ForwardIterator
2494uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2495{
2496    __destruct_n __d(0);
2497    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2498    unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2499    for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2500        ::new(&*__r) value_type(*__f);
2501    __h.release();
2502    return __r;
2503}
2504
2505template <class _ForwardIterator, class _Tp>
2506void
2507uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2508{
2509    __destruct_n __d(0);
2510    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2511    unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2512    for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2513        ::new(&*__f) value_type(__x);
2514    __h.release();
2515}
2516
2517template <class _ForwardIterator, class _Size, class _Tp>
2518_ForwardIterator
2519uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2520{
2521    __destruct_n __d(0);
2522    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2523    unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2524    for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2525        ::new(&*__f) value_type(__x);
2526    __h.release();
2527    return __f;
2528}
2529
2530class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
2531    : public std::exception
2532{
2533public:
2534    virtual ~bad_weak_ptr() throw();
2535    virtual const char* what() const throw();
2536};
2537
2538template<class _Tp> class weak_ptr;
2539
2540class __shared_count
2541{
2542    __shared_count(const __shared_count&);
2543    __shared_count& operator=(const __shared_count&);
2544
2545protected:
2546    long __shared_owners_;
2547    virtual ~__shared_count();
2548private:
2549    virtual void __on_zero_shared() = 0;
2550
2551public:
2552    _LIBCPP_INLINE_VISIBILITY
2553    explicit __shared_count(long __refs = 0)
2554        : __shared_owners_(__refs) {}
2555
2556    void __add_shared();
2557    bool __release_shared();
2558    _LIBCPP_INLINE_VISIBILITY
2559    long use_count() const {return __shared_owners_ + 1;}
2560};
2561
2562class __shared_weak_count
2563    : private __shared_count
2564{
2565    long __shared_weak_owners_;
2566
2567public:
2568    _LIBCPP_INLINE_VISIBILITY
2569    explicit __shared_weak_count(long __refs = 0)
2570        : __shared_count(__refs),
2571          __shared_weak_owners_(__refs) {}
2572protected:
2573    virtual ~__shared_weak_count();
2574
2575public:
2576    void __add_shared();
2577    void __add_weak();
2578    void __release_shared();
2579    void __release_weak();
2580    _LIBCPP_INLINE_VISIBILITY
2581    long use_count() const {return __shared_count::use_count();}
2582    __shared_weak_count* lock();
2583
2584#ifndef _LIBCPP_NO_RTTI
2585    virtual const void* __get_deleter(const type_info&) const;
2586#endif
2587private:
2588    virtual void __on_zero_shared_weak() = 0;
2589};
2590
2591template <class _Tp, class _Dp, class _Alloc>
2592class __shared_ptr_pointer
2593    : public __shared_weak_count
2594{
2595    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2596public:
2597    _LIBCPP_INLINE_VISIBILITY
2598    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2599        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2600
2601#ifndef _LIBCPP_NO_RTTI
2602    virtual const void* __get_deleter(const type_info&) const;
2603#endif
2604
2605private:
2606    virtual void __on_zero_shared();
2607    virtual void __on_zero_shared_weak();
2608};
2609
2610#ifndef _LIBCPP_NO_RTTI
2611
2612template <class _Tp, class _Dp, class _Alloc>
2613const void*
2614__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2615{
2616    return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2617}
2618
2619#endif  // _LIBCPP_NO_RTTI
2620
2621template <class _Tp, class _Dp, class _Alloc>
2622void
2623__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2624{
2625    __data_.first().second()(__data_.first().first());
2626    __data_.first().second().~_Dp();
2627}
2628
2629template <class _Tp, class _Dp, class _Alloc>
2630void
2631__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2632{
2633    typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2634    __data_.second().~_Alloc();
2635    __a.deallocate(this, 1);
2636}
2637
2638template <class _Tp, class _Alloc>
2639class __shared_ptr_emplace
2640    : public __shared_weak_count
2641{
2642    __compressed_pair<_Alloc, _Tp> __data_;
2643public:
2644#ifndef _LIBCPP_HAS_NO_VARIADICS
2645
2646    _LIBCPP_INLINE_VISIBILITY
2647    __shared_ptr_emplace(_Alloc __a)
2648        :  __data_(_STD::move(__a)) {}
2649
2650    template <class ..._Args>
2651        _LIBCPP_INLINE_VISIBILITY
2652        __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2653            :  __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2654
2655#else  // _LIBCPP_HAS_NO_VARIADICS
2656
2657    _LIBCPP_INLINE_VISIBILITY
2658    __shared_ptr_emplace(_Alloc __a)
2659        :  __data_(__a) {}
2660
2661    template <class _A0>
2662        _LIBCPP_INLINE_VISIBILITY
2663        __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2664            :  __data_(__a, _Tp(__a0)) {}
2665
2666    template <class _A0, class _A1>
2667        _LIBCPP_INLINE_VISIBILITY
2668        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2669            :  __data_(__a, _Tp(__a0, __a1)) {}
2670
2671    template <class _A0, class _A1, class _A2>
2672        _LIBCPP_INLINE_VISIBILITY
2673        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2674            :  __data_(__a, _Tp(__a0, __a1, __a2)) {}
2675
2676#endif  // _LIBCPP_HAS_NO_VARIADICS
2677
2678private:
2679    virtual void __on_zero_shared();
2680    virtual void __on_zero_shared_weak();
2681public:
2682    _LIBCPP_INLINE_VISIBILITY
2683    _Tp* get() {return &__data_.second();}
2684};
2685
2686template <class _Tp, class _Alloc>
2687void
2688__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2689{
2690    __data_.second().~_Tp();
2691}
2692
2693template <class _Tp, class _Alloc>
2694void
2695__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2696{
2697    typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2698    __data_.first().~_Alloc();
2699    __a.deallocate(this, 1);
2700}
2701
2702template<class _Tp> class enable_shared_from_this;
2703
2704template<class _Tp>
2705class _LIBCPP_VISIBLE shared_ptr
2706{
2707public:
2708    typedef _Tp element_type;
2709private:
2710    element_type*      __ptr_;
2711    __shared_weak_count* __cntrl_;
2712
2713    struct __nat {int __for_bool_;};
2714public:
2715    shared_ptr();
2716    shared_ptr(nullptr_t);
2717    template<class _Yp> explicit shared_ptr(_Yp* __p);
2718    template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2719    template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
2720    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2721    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
2722    template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p);
2723    shared_ptr(const shared_ptr& __r);
2724    template<class _Yp>
2725        shared_ptr(const shared_ptr<_Yp>& __r,
2726                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
2727#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2728    shared_ptr(shared_ptr&& __r);
2729    template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
2730                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
2731#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2732    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
2733                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
2734#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2735    template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2736#else
2737    template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
2738#endif
2739#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2740private:
2741    template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
2742public:
2743    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2744       typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2745    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2746       typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2747#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2748    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2749       typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2750    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2751       typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2752#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2753
2754    ~shared_ptr();
2755
2756    shared_ptr& operator=(const shared_ptr& __r);
2757    template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r);
2758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2759    shared_ptr& operator=(shared_ptr&& __r);
2760    template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2761    template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
2762#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2763    template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
2764#endif
2765#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2766private:
2767    template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
2768public:
2769    template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
2770#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2771    template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
2772#endif
2773
2774    void swap(shared_ptr& __r);
2775    void reset();
2776    template<class _Yp> void reset(_Yp* __p);
2777    template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2778    template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2779
2780    _LIBCPP_INLINE_VISIBILITY
2781    element_type* get() const {return __ptr_;}
2782    _LIBCPP_INLINE_VISIBILITY
2783    typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
2784    _LIBCPP_INLINE_VISIBILITY
2785    element_type* operator->() const {return __ptr_;}
2786    _LIBCPP_INLINE_VISIBILITY
2787    long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
2788    _LIBCPP_INLINE_VISIBILITY
2789    bool unique() const {return use_count() == 1;}
2790    _LIBCPP_INLINE_VISIBILITY
2791    bool empty() const {return __cntrl_ == 0;}
2792    _LIBCPP_INLINE_VISIBILITY
2793    /*explicit*/ operator bool() const {return get() != 0;}
2794    template <class _U>
2795        _LIBCPP_INLINE_VISIBILITY
2796        bool owner_before(shared_ptr<_U> const& __p) const
2797        {return __cntrl_ < __p.__cntrl_;}
2798    template <class _U>
2799        _LIBCPP_INLINE_VISIBILITY
2800        bool owner_before(weak_ptr<_U> const& __p) const
2801        {return __cntrl_ < __p.__cntrl_;}
2802
2803#ifndef _LIBCPP_NO_RTTI
2804    template <class _Dp>
2805        _LIBCPP_INLINE_VISIBILITY
2806        _Dp* __get_deleter() const
2807            {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
2808#endif  // _LIBCPP_NO_RTTI
2809
2810#ifndef _LIBCPP_HAS_NO_VARIADICS
2811
2812    template<class ..._Args>
2813        static
2814        shared_ptr<_Tp>
2815        make_shared(_Args&& ...__args);
2816
2817    template<class _Alloc, class ..._Args>
2818        static
2819        shared_ptr<_Tp>
2820        allocate_shared(const _Alloc& __a, _Args&& ...__args);
2821
2822#else  // _LIBCPP_HAS_NO_VARIADICS
2823
2824    static shared_ptr<_Tp> make_shared();
2825
2826    template<class _A0>
2827        static shared_ptr<_Tp> make_shared(_A0&);
2828
2829    template<class _A0, class _A1>
2830        static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2831
2832    template<class _A0, class _A1, class _A2>
2833        static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2834
2835    template<class _Alloc>
2836        static shared_ptr<_Tp>
2837        allocate_shared(const _Alloc& __a);
2838
2839    template<class _Alloc, class _A0>
2840        static shared_ptr<_Tp>
2841        allocate_shared(const _Alloc& __a, _A0& __a0);
2842
2843    template<class _Alloc, class _A0, class _A1>
2844        static shared_ptr<_Tp>
2845        allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2846
2847    template<class _Alloc, class _A0, class _A1, class _A2>
2848        static shared_ptr<_Tp>
2849        allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2850
2851#endif  // _LIBCPP_HAS_NO_VARIADICS
2852
2853private:
2854
2855    template <class _Yp>
2856        _LIBCPP_INLINE_VISIBILITY
2857        void
2858        __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2859        {
2860            if (__e)
2861                __e->__weak_this_ = *this;
2862        }
2863
2864    _LIBCPP_INLINE_VISIBILITY
2865    void __enable_weak_this(const void*) {}
2866
2867    template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2868    template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
2869};
2870
2871template<class _Tp>
2872inline _LIBCPP_INLINE_VISIBILITY
2873shared_ptr<_Tp>::shared_ptr()
2874    : __ptr_(0),
2875      __cntrl_(0)
2876{
2877}
2878
2879template<class _Tp>
2880inline _LIBCPP_INLINE_VISIBILITY
2881shared_ptr<_Tp>::shared_ptr(nullptr_t)
2882    : __ptr_(0),
2883      __cntrl_(0)
2884{
2885}
2886
2887template<class _Tp>
2888template<class _Yp>
2889shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2890    : __ptr_(__p)
2891{
2892    unique_ptr<_Yp> __hold(__p);
2893    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2894    __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2895    __hold.release();
2896    __enable_weak_this(__p);
2897}
2898
2899template<class _Tp>
2900template<class _Yp, class _Dp>
2901shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2902    : __ptr_(__p)
2903{
2904#ifndef _LIBCPP_NO_EXCEPTIONS
2905    try
2906    {
2907#endif  // _LIBCPP_NO_EXCEPTIONS
2908        typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2909        __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2910        __enable_weak_this(__p);
2911#ifndef _LIBCPP_NO_EXCEPTIONS
2912    }
2913    catch (...)
2914    {
2915        __d(__p);
2916        throw;
2917    }
2918#endif  // _LIBCPP_NO_EXCEPTIONS
2919}
2920
2921template<class _Tp>
2922template<class _Dp>
2923shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2924    : __ptr_(0)
2925{
2926#ifndef _LIBCPP_NO_EXCEPTIONS
2927    try
2928    {
2929#endif  // _LIBCPP_NO_EXCEPTIONS
2930        typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2931        __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2932#ifndef _LIBCPP_NO_EXCEPTIONS
2933    }
2934    catch (...)
2935    {
2936        __d(__p);
2937        throw;
2938    }
2939#endif  // _LIBCPP_NO_EXCEPTIONS
2940}
2941
2942template<class _Tp>
2943template<class _Yp, class _Dp, class _Alloc>
2944shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2945    : __ptr_(__p)
2946{
2947#ifndef _LIBCPP_NO_EXCEPTIONS
2948    try
2949    {
2950#endif  // _LIBCPP_NO_EXCEPTIONS
2951        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2952        typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2953        typedef __allocator_destructor<_A2> _D2;
2954        _A2 __a2(__a);
2955        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2956        ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2957        __cntrl_ = __hold2.release();
2958        __enable_weak_this(__p);
2959#ifndef _LIBCPP_NO_EXCEPTIONS
2960    }
2961    catch (...)
2962    {
2963        __d(__p);
2964        throw;
2965    }
2966#endif  // _LIBCPP_NO_EXCEPTIONS
2967}
2968
2969template<class _Tp>
2970template<class _Dp, class _Alloc>
2971shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2972    : __ptr_(0)
2973{
2974#ifndef _LIBCPP_NO_EXCEPTIONS
2975    try
2976    {
2977#endif  // _LIBCPP_NO_EXCEPTIONS
2978        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2979        typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2980        typedef __allocator_destructor<_A2> _D2;
2981        _A2 __a2(__a);
2982        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2983        ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2984        __cntrl_ = __hold2.release();
2985#ifndef _LIBCPP_NO_EXCEPTIONS
2986    }
2987    catch (...)
2988    {
2989        __d(__p);
2990        throw;
2991    }
2992#endif  // _LIBCPP_NO_EXCEPTIONS
2993}
2994
2995template<class _Tp>
2996template<class _Yp>
2997inline _LIBCPP_INLINE_VISIBILITY
2998shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
2999    : __ptr_(__p),
3000      __cntrl_(__r.__cntrl_)
3001{
3002    if (__cntrl_)
3003        __cntrl_->__add_shared();
3004}
3005
3006template<class _Tp>
3007inline _LIBCPP_INLINE_VISIBILITY
3008shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
3009    : __ptr_(__r.__ptr_),
3010      __cntrl_(__r.__cntrl_)
3011{
3012    if (__cntrl_)
3013        __cntrl_->__add_shared();
3014}
3015
3016template<class _Tp>
3017template<class _Yp>
3018inline _LIBCPP_INLINE_VISIBILITY
3019shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3020                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3021    : __ptr_(__r.__ptr_),
3022      __cntrl_(__r.__cntrl_)
3023{
3024    if (__cntrl_)
3025        __cntrl_->__add_shared();
3026}
3027
3028#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3029
3030template<class _Tp>
3031inline _LIBCPP_INLINE_VISIBILITY
3032shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3033    : __ptr_(__r.__ptr_),
3034      __cntrl_(__r.__cntrl_)
3035{
3036    __r.__ptr_ = 0;
3037    __r.__cntrl_ = 0;
3038}
3039
3040template<class _Tp>
3041template<class _Yp>
3042inline _LIBCPP_INLINE_VISIBILITY
3043shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3044                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3045    : __ptr_(__r.__ptr_),
3046      __cntrl_(__r.__cntrl_)
3047{
3048    __r.__ptr_ = 0;
3049    __r.__cntrl_ = 0;
3050}
3051
3052#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3053
3054template<class _Tp>
3055template<class _Yp>
3056#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3057shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3058#else
3059shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
3060#endif
3061    : __ptr_(__r.get())
3062{
3063    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3064    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3065    __enable_weak_this(__r.get());
3066    __r.release();
3067}
3068
3069template<class _Tp>
3070template <class _Yp, class _Dp>
3071#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3072shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3073#else
3074shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3075#endif
3076           typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3077    : __ptr_(__r.get())
3078{
3079    typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3080    __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3081    __enable_weak_this(__r.get());
3082    __r.release();
3083}
3084
3085template<class _Tp>
3086template <class _Yp, class _Dp>
3087#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3088shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3089#else
3090shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3091#endif
3092           typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3093    : __ptr_(__r.get())
3094{
3095    typedef __shared_ptr_pointer<_Yp*,
3096                                 reference_wrapper<typename remove_reference<_Dp>::type>,
3097                                 allocator<_Yp> > _CntrlBlk;
3098    __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3099    __enable_weak_this(__r.get());
3100    __r.release();
3101}
3102
3103#ifndef _LIBCPP_HAS_NO_VARIADICS
3104
3105template<class _Tp>
3106template<class ..._Args>
3107shared_ptr<_Tp>
3108shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3109{
3110    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3111    typedef allocator<_CntrlBlk> _A2;
3112    typedef __allocator_destructor<_A2> _D2;
3113    _A2 __a2;
3114    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3115    ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3116    shared_ptr<_Tp> __r;
3117    __r.__ptr_ = __hold2.get()->get();
3118    __r.__cntrl_ = __hold2.release();
3119    __r.__enable_weak_this(__r.__ptr_);
3120    return __r;
3121}
3122
3123template<class _Tp>
3124template<class _Alloc, class ..._Args>
3125shared_ptr<_Tp>
3126shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3127{
3128    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3129    typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3130    typedef __allocator_destructor<_A2> _D2;
3131    _A2 __a2(__a);
3132    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3133    ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3134    shared_ptr<_Tp> __r;
3135    __r.__ptr_ = __hold2.get()->get();
3136    __r.__cntrl_ = __hold2.release();
3137    __r.__enable_weak_this(__r.__ptr_);
3138    return __r;
3139}
3140
3141#else  // _LIBCPP_HAS_NO_VARIADICS
3142
3143template<class _Tp>
3144shared_ptr<_Tp>
3145shared_ptr<_Tp>::make_shared()
3146{
3147    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3148    typedef allocator<_CntrlBlk> _Alloc2;
3149    typedef __allocator_destructor<_Alloc2> _D2;
3150    _Alloc2 __alloc2;
3151    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3152    ::new(__hold2.get()) _CntrlBlk(__alloc2);
3153    shared_ptr<_Tp> __r;
3154    __r.__ptr_ = __hold2.get()->get();
3155    __r.__cntrl_ = __hold2.release();
3156    __r.__enable_weak_this(__r.__ptr_);
3157    return __r;
3158}
3159
3160template<class _Tp>
3161template<class _A0>
3162shared_ptr<_Tp>
3163shared_ptr<_Tp>::make_shared(_A0& __a0)
3164{
3165    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3166    typedef allocator<_CntrlBlk> _Alloc2;
3167    typedef __allocator_destructor<_Alloc2> _D2;
3168    _Alloc2 __alloc2;
3169    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3170    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3171    shared_ptr<_Tp> __r;
3172    __r.__ptr_ = __hold2.get()->get();
3173    __r.__cntrl_ = __hold2.release();
3174    __r.__enable_weak_this(__r.__ptr_);
3175    return __r;
3176}
3177
3178template<class _Tp>
3179template<class _A0, class _A1>
3180shared_ptr<_Tp>
3181shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3182{
3183    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3184    typedef allocator<_CntrlBlk> _Alloc2;
3185    typedef __allocator_destructor<_Alloc2> _D2;
3186    _Alloc2 __alloc2;
3187    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3188    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3189    shared_ptr<_Tp> __r;
3190    __r.__ptr_ = __hold2.get()->get();
3191    __r.__cntrl_ = __hold2.release();
3192    __r.__enable_weak_this(__r.__ptr_);
3193    return __r;
3194}
3195
3196template<class _Tp>
3197template<class _A0, class _A1, class _A2>
3198shared_ptr<_Tp>
3199shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3200{
3201    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3202    typedef allocator<_CntrlBlk> _Alloc2;
3203    typedef __allocator_destructor<_Alloc2> _D2;
3204    _Alloc2 __alloc2;
3205    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3206    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3207    shared_ptr<_Tp> __r;
3208    __r.__ptr_ = __hold2.get()->get();
3209    __r.__cntrl_ = __hold2.release();
3210    __r.__enable_weak_this(__r.__ptr_);
3211    return __r;
3212}
3213
3214template<class _Tp>
3215template<class _Alloc>
3216shared_ptr<_Tp>
3217shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3218{
3219    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3220    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3221    typedef __allocator_destructor<_Alloc2> _D2;
3222    _Alloc2 __alloc2(__a);
3223    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3224    ::new(__hold2.get()) _CntrlBlk(__a);
3225    shared_ptr<_Tp> __r;
3226    __r.__ptr_ = __hold2.get()->get();
3227    __r.__cntrl_ = __hold2.release();
3228    __r.__enable_weak_this(__r.__ptr_);
3229    return __r;
3230}
3231
3232template<class _Tp>
3233template<class _Alloc, class _A0>
3234shared_ptr<_Tp>
3235shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3236{
3237    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3238    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3239    typedef __allocator_destructor<_Alloc2> _D2;
3240    _Alloc2 __alloc2(__a);
3241    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3242    ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3243    shared_ptr<_Tp> __r;
3244    __r.__ptr_ = __hold2.get()->get();
3245    __r.__cntrl_ = __hold2.release();
3246    __r.__enable_weak_this(__r.__ptr_);
3247    return __r;
3248}
3249
3250template<class _Tp>
3251template<class _Alloc, class _A0, class _A1>
3252shared_ptr<_Tp>
3253shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3254{
3255    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3256    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3257    typedef __allocator_destructor<_Alloc2> _D2;
3258    _Alloc2 __alloc2(__a);
3259    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3260    ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3261    shared_ptr<_Tp> __r;
3262    __r.__ptr_ = __hold2.get()->get();
3263    __r.__cntrl_ = __hold2.release();
3264    __r.__enable_weak_this(__r.__ptr_);
3265    return __r;
3266}
3267
3268template<class _Tp>
3269template<class _Alloc, class _A0, class _A1, class _A2>
3270shared_ptr<_Tp>
3271shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3272{
3273    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3274    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3275    typedef __allocator_destructor<_Alloc2> _D2;
3276    _Alloc2 __alloc2(__a);
3277    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3278    ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3279    shared_ptr<_Tp> __r;
3280    __r.__ptr_ = __hold2.get()->get();
3281    __r.__cntrl_ = __hold2.release();
3282    __r.__enable_weak_this(__r.__ptr_);
3283    return __r;
3284}
3285
3286#endif  // _LIBCPP_HAS_NO_VARIADICS
3287
3288template<class _Tp>
3289shared_ptr<_Tp>::~shared_ptr()
3290{
3291    if (__cntrl_)
3292        __cntrl_->__release_shared();
3293}
3294
3295template<class _Tp>
3296inline _LIBCPP_INLINE_VISIBILITY
3297shared_ptr<_Tp>&
3298shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3299{
3300    shared_ptr(__r).swap(*this);
3301    return *this;
3302}
3303
3304template<class _Tp>
3305template<class _Yp>
3306inline _LIBCPP_INLINE_VISIBILITY
3307shared_ptr<_Tp>&
3308shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3309{
3310    shared_ptr(__r).swap(*this);
3311    return *this;
3312}
3313
3314#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3315
3316template<class _Tp>
3317inline _LIBCPP_INLINE_VISIBILITY
3318shared_ptr<_Tp>&
3319shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3320{
3321    shared_ptr(_STD::move(__r)).swap(*this);
3322    return *this;
3323}
3324
3325template<class _Tp>
3326template<class _Yp>
3327inline _LIBCPP_INLINE_VISIBILITY
3328shared_ptr<_Tp>&
3329shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3330{
3331    shared_ptr(_STD::move(__r)).swap(*this);
3332    return *this;
3333}
3334
3335template<class _Tp>
3336template<class _Yp>
3337inline _LIBCPP_INLINE_VISIBILITY
3338shared_ptr<_Tp>&
3339shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3340{
3341    shared_ptr(__r).swap(*this);
3342    return *this;
3343}
3344
3345template<class _Tp>
3346template <class _Yp, class _Dp>
3347inline _LIBCPP_INLINE_VISIBILITY
3348shared_ptr<_Tp>&
3349shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3350{
3351    shared_ptr(_STD::move(__r)).swap(*this);
3352    return *this;
3353}
3354
3355#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3356
3357template<class _Tp>
3358template<class _Yp>
3359inline _LIBCPP_INLINE_VISIBILITY
3360shared_ptr<_Tp>&
3361shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
3362{
3363    shared_ptr(__r).swap(*this);
3364    return *this;
3365}
3366
3367template<class _Tp>
3368template <class _Yp, class _Dp>
3369inline _LIBCPP_INLINE_VISIBILITY
3370shared_ptr<_Tp>&
3371shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3372{
3373    shared_ptr(_STD::move(__r)).swap(*this);
3374    return *this;
3375}
3376
3377#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3378
3379template<class _Tp>
3380inline _LIBCPP_INLINE_VISIBILITY
3381void
3382shared_ptr<_Tp>::swap(shared_ptr& __r)
3383{
3384    _STD::swap(__ptr_, __r.__ptr_);
3385    _STD::swap(__cntrl_, __r.__cntrl_);
3386}
3387
3388template<class _Tp>
3389inline _LIBCPP_INLINE_VISIBILITY
3390void
3391shared_ptr<_Tp>::reset()
3392{
3393    shared_ptr().swap(*this);
3394}
3395
3396template<class _Tp>
3397template<class _Yp>
3398inline _LIBCPP_INLINE_VISIBILITY
3399void
3400shared_ptr<_Tp>::reset(_Yp* __p)
3401{
3402    shared_ptr(__p).swap(*this);
3403}
3404
3405template<class _Tp>
3406template<class _Yp, class _Dp>
3407inline _LIBCPP_INLINE_VISIBILITY
3408void
3409shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3410{
3411    shared_ptr(__p, __d).swap(*this);
3412}
3413
3414template<class _Tp>
3415template<class _Yp, class _Dp, class _Alloc>
3416inline _LIBCPP_INLINE_VISIBILITY
3417void
3418shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3419{
3420    shared_ptr(__p, __d, __a).swap(*this);
3421}
3422
3423#ifndef _LIBCPP_HAS_NO_VARIADICS
3424
3425template<class _Tp, class ..._Args>
3426inline _LIBCPP_INLINE_VISIBILITY
3427shared_ptr<_Tp>
3428make_shared(_Args&& ...__args)
3429{
3430    return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3431}
3432
3433template<class _Tp, class _Alloc, class ..._Args>
3434inline _LIBCPP_INLINE_VISIBILITY
3435shared_ptr<_Tp>
3436allocate_shared(const _Alloc& __a, _Args&& ...__args)
3437{
3438    return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3439}
3440
3441#else  // _LIBCPP_HAS_NO_VARIADICS
3442
3443template<class _Tp>
3444inline _LIBCPP_INLINE_VISIBILITY
3445shared_ptr<_Tp>
3446make_shared()
3447{
3448    return shared_ptr<_Tp>::make_shared();
3449}
3450
3451template<class _Tp, class _A0>
3452inline _LIBCPP_INLINE_VISIBILITY
3453shared_ptr<_Tp>
3454make_shared(_A0& __a0)
3455{
3456    return shared_ptr<_Tp>::make_shared(__a0);
3457}
3458
3459template<class _Tp, class _A0, class _A1>
3460inline _LIBCPP_INLINE_VISIBILITY
3461shared_ptr<_Tp>
3462make_shared(_A0& __a0, _A1& __a1)
3463{
3464    return shared_ptr<_Tp>::make_shared(__a0, __a1);
3465}
3466
3467template<class _Tp, class _A0, class _A1, class _A2>
3468inline _LIBCPP_INLINE_VISIBILITY
3469shared_ptr<_Tp>
3470make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3471{
3472    return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3473}
3474
3475template<class _Tp, class _Alloc>
3476inline _LIBCPP_INLINE_VISIBILITY
3477shared_ptr<_Tp>
3478allocate_shared(const _Alloc& __a)
3479{
3480    return shared_ptr<_Tp>::allocate_shared(__a);
3481}
3482
3483template<class _Tp, class _Alloc, class _A0>
3484inline _LIBCPP_INLINE_VISIBILITY
3485shared_ptr<_Tp>
3486allocate_shared(const _Alloc& __a, _A0& __a0)
3487{
3488    return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3489}
3490
3491template<class _Tp, class _Alloc, class _A0, class _A1>
3492inline _LIBCPP_INLINE_VISIBILITY
3493shared_ptr<_Tp>
3494allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3495{
3496    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3497}
3498
3499template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3500inline _LIBCPP_INLINE_VISIBILITY
3501shared_ptr<_Tp>
3502allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3503{
3504    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3505}
3506
3507#endif  // _LIBCPP_HAS_NO_VARIADICS
3508
3509template<class _Tp, class _Up>
3510inline _LIBCPP_INLINE_VISIBILITY
3511bool
3512operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3513{
3514    return __x.get() == __y.get();
3515}
3516
3517template<class _Tp, class _Up>
3518inline _LIBCPP_INLINE_VISIBILITY
3519bool
3520operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3521{
3522    return !(__x == __y);
3523}
3524
3525template<class _Tp, class _Up>
3526inline _LIBCPP_INLINE_VISIBILITY
3527bool
3528operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3529{
3530    return __x.get() < __y.get();
3531}
3532
3533template<class _Tp>
3534inline _LIBCPP_INLINE_VISIBILITY
3535void
3536swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3537{
3538    __x.swap(__y);
3539}
3540
3541template<class _Tp, class _Up>
3542inline _LIBCPP_INLINE_VISIBILITY
3543shared_ptr<_Tp>
3544static_pointer_cast(const shared_ptr<_Up>& __r)
3545{
3546    return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3547}
3548
3549template<class _Tp, class _Up>
3550inline _LIBCPP_INLINE_VISIBILITY
3551shared_ptr<_Tp>
3552dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3553{
3554    _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3555    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3556}
3557
3558template<class _Tp, class _Up>
3559shared_ptr<_Tp>
3560const_pointer_cast(const shared_ptr<_Up>& __r)
3561{
3562    return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3563}
3564
3565#ifndef _LIBCPP_NO_RTTI
3566
3567template<class _Dp, class _Tp>
3568inline _LIBCPP_INLINE_VISIBILITY
3569_Dp*
3570get_deleter(const shared_ptr<_Tp>& __p)
3571{
3572    return __p.template __get_deleter<_Dp>();
3573}
3574
3575#endif  // _LIBCPP_NO_RTTI
3576
3577template<class _Tp>
3578class _LIBCPP_VISIBLE weak_ptr
3579{
3580public:
3581    typedef _Tp element_type;
3582private:
3583    element_type*        __ptr_;
3584    __shared_weak_count* __cntrl_;
3585
3586public:
3587    weak_ptr();
3588    template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3589                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3590    weak_ptr(weak_ptr const& __r);
3591    template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
3592                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3593
3594    ~weak_ptr();
3595
3596    weak_ptr& operator=(weak_ptr const& __r);
3597    template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r);
3598    template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r);
3599
3600    void swap(weak_ptr& __r);
3601    void reset();
3602
3603    _LIBCPP_INLINE_VISIBILITY
3604    long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
3605    _LIBCPP_INLINE_VISIBILITY
3606    bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3607    shared_ptr<_Tp> lock() const;
3608    template<class _Up>
3609        _LIBCPP_INLINE_VISIBILITY
3610        bool owner_before(const shared_ptr<_Up>& __r) const
3611        {return __cntrl_ < __r.__cntrl_;}
3612    template<class _Up>
3613        _LIBCPP_INLINE_VISIBILITY
3614        bool owner_before(const weak_ptr<_Up>& __r) const
3615        {return __cntrl_ < __r.__cntrl_;}
3616
3617    template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3618    template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3619};
3620
3621template<class _Tp>
3622inline _LIBCPP_INLINE_VISIBILITY
3623weak_ptr<_Tp>::weak_ptr()
3624    : __ptr_(0),
3625      __cntrl_(0)
3626{
3627}
3628
3629template<class _Tp>
3630inline _LIBCPP_INLINE_VISIBILITY
3631weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3632    : __ptr_(__r.__ptr_),
3633      __cntrl_(__r.__cntrl_)
3634{
3635    if (__cntrl_)
3636        __cntrl_->__add_weak();
3637}
3638
3639template<class _Tp>
3640template<class _Yp>
3641inline _LIBCPP_INLINE_VISIBILITY
3642weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3643                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3644    : __ptr_(__r.__ptr_),
3645      __cntrl_(__r.__cntrl_)
3646{
3647    if (__cntrl_)
3648        __cntrl_->__add_weak();
3649}
3650
3651template<class _Tp>
3652template<class _Yp>
3653inline _LIBCPP_INLINE_VISIBILITY
3654weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3655                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3656    : __ptr_(__r.__ptr_),
3657      __cntrl_(__r.__cntrl_)
3658{
3659    if (__cntrl_)
3660        __cntrl_->__add_weak();
3661}
3662
3663template<class _Tp>
3664weak_ptr<_Tp>::~weak_ptr()
3665{
3666    if (__cntrl_)
3667        __cntrl_->__release_weak();
3668}
3669
3670template<class _Tp>
3671inline _LIBCPP_INLINE_VISIBILITY
3672weak_ptr<_Tp>&
3673weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3674{
3675    weak_ptr(__r).swap(*this);
3676    return *this;
3677}
3678
3679template<class _Tp>
3680template<class _Yp>
3681inline _LIBCPP_INLINE_VISIBILITY
3682weak_ptr<_Tp>&
3683weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3684{
3685    weak_ptr(__r).swap(*this);
3686    return *this;
3687}
3688
3689template<class _Tp>
3690template<class _Yp>
3691inline _LIBCPP_INLINE_VISIBILITY
3692weak_ptr<_Tp>&
3693weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3694{
3695    weak_ptr(__r).swap(*this);
3696    return *this;
3697}
3698
3699template<class _Tp>
3700inline _LIBCPP_INLINE_VISIBILITY
3701void
3702weak_ptr<_Tp>::swap(weak_ptr& __r)
3703{
3704    _STD::swap(__ptr_, __r.__ptr_);
3705    _STD::swap(__cntrl_, __r.__cntrl_);
3706}
3707
3708template<class _Tp>
3709inline _LIBCPP_INLINE_VISIBILITY
3710void
3711swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3712{
3713    __x.swap(__y);
3714}
3715
3716template<class _Tp>
3717inline _LIBCPP_INLINE_VISIBILITY
3718void
3719weak_ptr<_Tp>::reset()
3720{
3721    weak_ptr().swap(*this);
3722}
3723
3724template<class _Tp>
3725template<class _Yp>
3726shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3727                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3728    : __ptr_(__r.__ptr_),
3729      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3730{
3731    if (__cntrl_ == 0)
3732#ifndef _LIBCPP_NO_EXCEPTIONS
3733        throw bad_weak_ptr();
3734#else
3735        assert(!"bad_weak_ptr");
3736#endif
3737}
3738
3739template<class _Tp>
3740shared_ptr<_Tp>
3741weak_ptr<_Tp>::lock() const
3742{
3743    shared_ptr<_Tp> __r;
3744    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3745    if (__r.__cntrl_)
3746        __r.__ptr_ = __ptr_;
3747    return __r;
3748}
3749
3750template <class _Tp> struct owner_less;
3751
3752template <class _Tp>
3753struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
3754    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
3755{
3756    typedef bool result_type;
3757    _LIBCPP_INLINE_VISIBILITY
3758    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3759        {return __x.owner_before(__y);}
3760    _LIBCPP_INLINE_VISIBILITY
3761    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
3762        {return __x.owner_before(__y);}
3763    _LIBCPP_INLINE_VISIBILITY
3764    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3765        {return __x.owner_before(__y);}
3766};
3767
3768template <class _Tp>
3769struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
3770    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3771{
3772    typedef bool result_type;
3773    _LIBCPP_INLINE_VISIBILITY
3774    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
3775        {return __x.owner_before(__y);}
3776    _LIBCPP_INLINE_VISIBILITY
3777    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
3778        {return __x.owner_before(__y);}
3779    _LIBCPP_INLINE_VISIBILITY
3780    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3781        {return __x.owner_before(__y);}
3782};
3783
3784template<class _Tp>
3785class _LIBCPP_VISIBLE enable_shared_from_this
3786{
3787    mutable weak_ptr<_Tp> __weak_this_;
3788protected:
3789    _LIBCPP_INLINE_VISIBILITY
3790    enable_shared_from_this() {}
3791    _LIBCPP_INLINE_VISIBILITY
3792    enable_shared_from_this(enable_shared_from_this const&) {}
3793    _LIBCPP_INLINE_VISIBILITY
3794    enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
3795    _LIBCPP_INLINE_VISIBILITY
3796    ~enable_shared_from_this() {}
3797public:
3798    _LIBCPP_INLINE_VISIBILITY
3799    shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
3800    _LIBCPP_INLINE_VISIBILITY
3801    shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3802
3803    template <class _Up> friend class shared_ptr;
3804};
3805
3806template <class _Tp>
3807struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
3808{
3809    typedef shared_ptr<_Tp>      argument_type;
3810    typedef size_t               result_type;
3811    _LIBCPP_INLINE_VISIBILITY
3812    result_type operator()(const argument_type& __ptr) const
3813    {
3814        return hash<_Tp*>()(__ptr.get());
3815    }
3816};
3817
3818//enum class
3819struct _LIBCPP_VISIBLE pointer_safety
3820{
3821    enum _
3822    {
3823        relaxed,
3824        preferred,
3825        strict
3826    };
3827
3828    _ __v_;
3829
3830    _LIBCPP_INLINE_VISIBILITY
3831    pointer_safety(_ __v) : __v_(__v) {}
3832    _LIBCPP_INLINE_VISIBILITY
3833    operator int() const {return __v_;}
3834};
3835
3836void declare_reachable(void* __p);
3837void declare_no_pointers(char* __p, size_t __n);
3838void undeclare_no_pointers(char* __p, size_t __n);
3839pointer_safety get_pointer_safety();
3840void* __undeclare_reachable(void*);
3841
3842template <class _Tp>
3843inline _LIBCPP_INLINE_VISIBILITY
3844_Tp*
3845undeclare_reachable(_Tp* __p)
3846{
3847    return static_cast<_Tp*>(__undeclare_reachable(__p));
3848}
3849
3850void* align(size_t, size_t, void*&, size_t&);
3851
3852_LIBCPP_END_NAMESPACE_STD
3853
3854#endif  // _LIBCPP_MEMORY
3855