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