memory revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
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
2466struct __destruct_n
2467{
2468private:
2469    size_t size;
2470
2471    template <class _Tp>
2472    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type)
2473        {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2474
2475    template <class _Tp>
2476    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type)
2477        {}
2478
2479    _LIBCPP_INLINE_VISIBILITY void __incr(false_type)
2480        {++size;}
2481    _LIBCPP_INLINE_VISIBILITY void __incr(true_type)
2482        {}
2483
2484    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type)
2485        {size = __s;}
2486    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type)
2487        {}
2488public:
2489    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) : size(__s) {}
2490
2491    template <class _Tp>
2492    _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*)
2493        {__incr(integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2494
2495    template <class _Tp>
2496    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*)
2497        {__set(__s, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2498
2499    template <class _Tp>
2500    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p)
2501        {__process(__p, integral_constant<bool, has_trivial_destructor<_Tp>::value>());}
2502};
2503
2504template <class _Alloc>
2505class __allocator_destructor
2506{
2507    typedef allocator_traits<_Alloc> __alloc_traits;
2508public:
2509    typedef typename __alloc_traits::pointer pointer;
2510    typedef typename __alloc_traits::size_type size_type;
2511private:
2512    _Alloc& __alloc_;
2513    size_type __s_;
2514public:
2515    _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
2516        : __alloc_(__a), __s_(__s) {}
2517    void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
2518};
2519
2520template <class _InputIterator, class _ForwardIterator>
2521_ForwardIterator
2522uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2523{
2524    __destruct_n __d(0);
2525    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2526    unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2527    for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2528        ::new(&*__r) value_type(*__f);
2529    __h.release();
2530    return __r;
2531}
2532
2533template <class _InputIterator, class _Size, class _ForwardIterator>
2534_ForwardIterator
2535uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2536{
2537    __destruct_n __d(0);
2538    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2539    unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2540    for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2541        ::new(&*__r) value_type(*__f);
2542    __h.release();
2543    return __r;
2544}
2545
2546template <class _ForwardIterator, class _Tp>
2547void
2548uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2549{
2550    __destruct_n __d(0);
2551    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2552    unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2553    for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2554        ::new(&*__f) value_type(__x);
2555    __h.release();
2556}
2557
2558template <class _ForwardIterator, class _Size, class _Tp>
2559void
2560uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2561{
2562    __destruct_n __d(0);
2563    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2564    unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2565    for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2566        ::new(&*__f) value_type(__x);
2567    __h.release();
2568}
2569
2570class bad_weak_ptr
2571    : public std::exception
2572{
2573public:
2574    virtual ~bad_weak_ptr() throw();
2575    virtual const char* what() const throw();
2576};
2577
2578template<class _Tp> class weak_ptr;
2579
2580class __shared_count
2581{
2582    __shared_count(const __shared_count&);
2583    __shared_count& operator=(const __shared_count&);
2584
2585protected:
2586    long __shared_owners_;
2587    virtual ~__shared_count();
2588private:
2589    virtual void __on_zero_shared() = 0;
2590
2591public:
2592    explicit __shared_count(long __refs = 0)
2593        : __shared_owners_(__refs) {}
2594
2595    void __add_shared();
2596    void __release_shared();
2597    long use_count() const {return __shared_owners_ + 1;}
2598};
2599
2600class __shared_weak_count
2601    : private __shared_count
2602{
2603    long __shared_weak_owners_;
2604
2605public:
2606    explicit __shared_weak_count(long __refs = 0)
2607        : __shared_count(__refs),
2608          __shared_weak_owners_(__refs) {}
2609protected:
2610    virtual ~__shared_weak_count();
2611
2612public:
2613    void __add_shared();
2614    void __add_weak();
2615    void __release_shared();
2616    void __release_weak();
2617    long use_count() const {return __shared_count::use_count();}
2618    __shared_weak_count* lock();
2619
2620    virtual const void* __get_deleter(const type_info&) const;
2621private:
2622    virtual void __on_zero_shared_weak() = 0;
2623};
2624
2625template <class _Tp, class _Dp, class _Alloc>
2626class __shared_ptr_pointer
2627    : public __shared_weak_count
2628{
2629    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2630public:
2631    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2632        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2633
2634    virtual const void* __get_deleter(const type_info&) const;
2635
2636private:
2637    virtual void __on_zero_shared();
2638    virtual void __on_zero_shared_weak();
2639};
2640
2641template <class _Tp, class _Dp, class _Alloc>
2642const void*
2643__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const
2644{
2645    return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2646}
2647
2648template <class _Tp, class _Dp, class _Alloc>
2649void
2650__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared()
2651{
2652    __data_.first().second()(__data_.first().first());
2653    __data_.first().second().~_Dp();
2654}
2655
2656template <class _Tp, class _Dp, class _Alloc>
2657void
2658__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak()
2659{
2660    typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2661    __data_.second().~_Alloc();
2662    __a.deallocate(this, 1);
2663}
2664
2665template <class _Tp, class _Alloc>
2666class __shared_ptr_emplace
2667    : public __shared_weak_count
2668{
2669    __compressed_pair<_Alloc, _Tp> __data_;
2670public:
2671#ifndef _LIBCPP_HAS_NO_VARIADICS
2672
2673    __shared_ptr_emplace(_Alloc __a)
2674        :  __data_(_STD::move(__a)) {}
2675
2676    template <class ..._Args>
2677        __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2678            :  __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2679
2680#else  // _LIBCPP_HAS_NO_VARIADICS
2681
2682    __shared_ptr_emplace(_Alloc __a)
2683        :  __data_(__a) {}
2684
2685    template <class _A0>
2686        __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2687            :  __data_(__a, _Tp(__a0)) {}
2688
2689    template <class _A0, class _A1>
2690        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2691            :  __data_(__a, _Tp(__a0, __a1)) {}
2692
2693    template <class _A0, class _A1, class _A2>
2694        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2695            :  __data_(__a, _Tp(__a0, __a1, __a2)) {}
2696
2697#endif  // _LIBCPP_HAS_NO_VARIADICS
2698
2699private:
2700    virtual void __on_zero_shared();
2701    virtual void __on_zero_shared_weak();
2702public:
2703    _Tp* get() {return &__data_.second();}
2704};
2705
2706template <class _Tp, class _Alloc>
2707void
2708__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared()
2709{
2710    __data_.second().~_Tp();
2711}
2712
2713template <class _Tp, class _Alloc>
2714void
2715__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak()
2716{
2717    typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2718    __data_.first().~_Alloc();
2719    __a.deallocate(this, 1);
2720}
2721
2722template<class _Tp> class enable_shared_from_this;
2723
2724template<class _Tp>
2725class shared_ptr
2726{
2727public: 
2728    typedef _Tp element_type; 
2729private:
2730    element_type*      __ptr_;
2731    __shared_weak_count* __cntrl_;
2732
2733    struct __nat {int __for_bool_;};
2734public:
2735    shared_ptr();
2736    shared_ptr(nullptr_t);
2737    template<class _Yp> explicit shared_ptr(_Yp* __p);
2738    template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d); 
2739    template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a); 
2740    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2741    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
2742    template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p); 
2743    shared_ptr(const shared_ptr& __r);
2744    template<class _Yp>
2745        shared_ptr(const shared_ptr<_Yp>& __r,
2746                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
2747#ifdef _LIBCPP_MOVE    
2748    shared_ptr(shared_ptr&& __r);
2749    template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
2750                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat()); 
2751#endif
2752    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
2753                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat()); 
2754#ifdef _LIBCPP_MOVE    
2755    template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r); 
2756#else
2757    template<class _Yp> shared_ptr(auto_ptr<_Yp>& __r); 
2758#endif
2759#ifdef _LIBCPP_MOVE    
2760private:
2761    template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete; 
2762public:
2763    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2764       typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2765    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2766       typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2767#else
2768    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2769       typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2770    template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2771       typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2772#endif
2773
2774    ~shared_ptr();
2775
2776    shared_ptr& operator=(const shared_ptr& __r); 
2777    template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r); 
2778#ifdef _LIBCPP_MOVE    
2779    shared_ptr& operator=(shared_ptr&& __r); 
2780    template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r); 
2781    template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r); 
2782#else
2783    template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>& __r); 
2784#endif
2785#ifdef _LIBCPP_MOVE    
2786private:
2787    template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete; 
2788public:
2789    template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r); 
2790#else
2791    template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r); 
2792#endif
2793
2794    void swap(shared_ptr& __r);
2795    void reset();
2796    template<class _Yp> void reset(_Yp* __p);
2797    template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2798    template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2799
2800    element_type* get() const {return __ptr_;}
2801    typename add_lvalue_reference<element_type>::type operator*() const {return *__ptr_;}
2802    element_type* operator->() const {return __ptr_;}
2803    long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
2804    bool unique() const {return use_count() == 1;}
2805    bool empty() const {return __cntrl_ == 0;}
2806    /*explicit*/ operator bool() const {return get() != 0;}
2807    template <class _U> bool owner_before(shared_ptr<_U> const& __p) const
2808        {return __cntrl_ < __p.__cntrl_;}
2809    template <class _U> bool owner_before(weak_ptr<_U> const& __p) const
2810        {return __cntrl_ < __p.__cntrl_;}
2811
2812    template <class _Dp>
2813        _Dp* __get_deleter() const
2814            {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
2815
2816#ifndef _LIBCPP_HAS_NO_VARIADICS
2817
2818    template<class ..._Args>
2819        static
2820        shared_ptr<_Tp>
2821        make_shared(_Args&& ...__args);
2822
2823    template<class _Alloc, class ..._Args>
2824        static
2825        shared_ptr<_Tp>
2826        allocate_shared(const _Alloc& __a, _Args&& ...__args);
2827
2828#else  // _LIBCPP_HAS_NO_VARIADICS
2829
2830    static shared_ptr<_Tp> make_shared();
2831
2832    template<class _A0>
2833        static shared_ptr<_Tp> make_shared(_A0&);
2834
2835    template<class _A0, class _A1>
2836        static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2837
2838    template<class _A0, class _A1, class _A2>
2839        static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2840
2841    template<class _Alloc>
2842        static shared_ptr<_Tp>
2843        allocate_shared(const _Alloc& __a);
2844
2845    template<class _Alloc, class _A0>
2846        static shared_ptr<_Tp>
2847        allocate_shared(const _Alloc& __a, _A0& __a0);
2848
2849    template<class _Alloc, class _A0, class _A1>
2850        static shared_ptr<_Tp>
2851        allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2852
2853    template<class _Alloc, class _A0, class _A1, class _A2>
2854        static shared_ptr<_Tp>
2855        allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2856
2857#endif  // _LIBCPP_HAS_NO_VARIADICS
2858
2859private:
2860
2861    template <class _Yp>
2862        void
2863        __enable_weak_this(const enable_shared_from_this<_Yp>* __e)
2864        {
2865            if (__e)
2866                __e->__weak_this_ = *this;
2867        }
2868
2869    void __enable_weak_this(const void*) {}
2870
2871    template <class _Up> friend class shared_ptr;
2872    template <class _Up> friend class weak_ptr;
2873};
2874
2875template<class _Tp>
2876inline _LIBCPP_INLINE_VISIBILITY
2877shared_ptr<_Tp>::shared_ptr()
2878    : __ptr_(0),
2879      __cntrl_(0)
2880{
2881}
2882
2883template<class _Tp>
2884inline _LIBCPP_INLINE_VISIBILITY
2885shared_ptr<_Tp>::shared_ptr(nullptr_t)
2886    : __ptr_(0),
2887      __cntrl_(0)
2888{
2889}
2890
2891template<class _Tp>
2892template<class _Yp>
2893shared_ptr<_Tp>::shared_ptr(_Yp* __p)
2894    : __ptr_(__p)
2895{
2896    unique_ptr<_Yp> __hold(__p);
2897    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
2898    __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
2899    __hold.release();
2900    __enable_weak_this(__p);
2901}
2902
2903template<class _Tp>
2904template<class _Yp, class _Dp>
2905shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
2906    : __ptr_(__p)
2907{
2908#ifndef _LIBCPP_NO_EXCEPTIONS
2909    try
2910    {
2911#endif
2912        typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
2913        __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
2914        __enable_weak_this(__p);
2915#ifndef _LIBCPP_NO_EXCEPTIONS
2916    }
2917    catch (...)
2918    {
2919        __d(__p);
2920        throw;
2921    }
2922#endif
2923}
2924
2925template<class _Tp>
2926template<class _Dp>
2927shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
2928    : __ptr_(0)
2929{
2930#ifndef _LIBCPP_NO_EXCEPTIONS
2931    try
2932    {
2933#endif
2934        typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
2935        __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
2936#ifndef _LIBCPP_NO_EXCEPTIONS
2937    }
2938    catch (...)
2939    {
2940        __d(__p);
2941        throw;
2942    }
2943#endif
2944}
2945
2946template<class _Tp>
2947template<class _Yp, class _Dp, class _Alloc>
2948shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
2949    : __ptr_(__p)
2950{
2951#ifndef _LIBCPP_NO_EXCEPTIONS
2952    try
2953    {
2954#endif
2955        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
2956        typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2957        typedef __allocator_destructor<_A2> _D2;
2958        _A2 __a2(__a);
2959        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2960        ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2961        __cntrl_ = __hold2.release();
2962        __enable_weak_this(__p);
2963#ifndef _LIBCPP_NO_EXCEPTIONS
2964    }
2965    catch (...)
2966    {
2967        __d(__p);
2968        throw;
2969    }
2970#endif
2971}
2972
2973template<class _Tp>
2974template<class _Dp, class _Alloc>
2975shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
2976    : __ptr_(0)
2977{
2978#ifndef _LIBCPP_NO_EXCEPTIONS
2979    try
2980    {
2981#endif
2982        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
2983        typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
2984        typedef __allocator_destructor<_A2> _D2;
2985        _A2 __a2(__a);
2986        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
2987        ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
2988        __cntrl_ = __hold2.release();
2989#ifndef _LIBCPP_NO_EXCEPTIONS
2990    }
2991    catch (...)
2992    {
2993        __d(__p);
2994        throw;
2995    }
2996#endif
2997}
2998
2999template<class _Tp>
3000template<class _Yp>
3001inline _LIBCPP_INLINE_VISIBILITY
3002shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p)
3003    : __ptr_(__p),
3004      __cntrl_(__r.__cntrl_)
3005{
3006    if (__cntrl_)
3007        __cntrl_->__add_shared();
3008}
3009
3010template<class _Tp>
3011inline _LIBCPP_INLINE_VISIBILITY
3012shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r)
3013    : __ptr_(__r.__ptr_),
3014      __cntrl_(__r.__cntrl_)
3015{
3016    if (__cntrl_)
3017        __cntrl_->__add_shared();
3018}
3019
3020template<class _Tp>
3021template<class _Yp>
3022inline _LIBCPP_INLINE_VISIBILITY
3023shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3024                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3025    : __ptr_(__r.__ptr_),
3026      __cntrl_(__r.__cntrl_)
3027{
3028    if (__cntrl_)
3029        __cntrl_->__add_shared();
3030}
3031
3032#ifdef _LIBCPP_MOVE
3033
3034template<class _Tp>
3035inline _LIBCPP_INLINE_VISIBILITY
3036shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r)
3037    : __ptr_(__r.__ptr_),
3038      __cntrl_(__r.__cntrl_)
3039{
3040    __r.__ptr_ = 0;
3041    __r.__cntrl_ = 0;
3042}
3043
3044template<class _Tp>
3045template<class _Yp>
3046inline _LIBCPP_INLINE_VISIBILITY
3047shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3048                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3049    : __ptr_(__r.__ptr_),
3050      __cntrl_(__r.__cntrl_)
3051{
3052    __r.__ptr_ = 0;
3053    __r.__cntrl_ = 0;
3054}
3055
3056#endif
3057
3058template<class _Tp>
3059template<class _Yp>
3060#ifdef _LIBCPP_MOVE    
3061shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3062#else
3063shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>& __r)
3064#endif
3065    : __ptr_(__r.get())
3066{
3067    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3068    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3069    __enable_weak_this(__r.get());
3070    __r.release();
3071}
3072
3073template<class _Tp>
3074template <class _Yp, class _Dp>
3075#ifdef _LIBCPP_MOVE    
3076shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3077#else
3078shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3079#endif
3080           typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3081    : __ptr_(__r.get())
3082{
3083    typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3084    __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3085    __enable_weak_this(__r.get());
3086    __r.release();
3087}
3088
3089template<class _Tp>
3090template <class _Yp, class _Dp>
3091#ifdef _LIBCPP_MOVE    
3092shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3093#else
3094shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3095#endif
3096           typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3097    : __ptr_(__r.get())
3098{
3099    typedef __shared_ptr_pointer<_Yp*,
3100                                 reference_wrapper<typename remove_reference<_Dp>::type>,
3101                                 allocator<_Yp> > _CntrlBlk;
3102    __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3103    __enable_weak_this(__r.get());
3104    __r.release();
3105}
3106
3107#ifndef _LIBCPP_HAS_NO_VARIADICS
3108
3109template<class _Tp>
3110template<class ..._Args>
3111shared_ptr<_Tp>
3112shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3113{
3114    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3115    typedef allocator<_CntrlBlk> _A2;
3116    typedef __allocator_destructor<_A2> _D2;
3117    _A2 __a2;
3118    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3119    ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3120    shared_ptr<_Tp> __r;
3121    __r.__ptr_ = __hold2.get()->get();
3122    __r.__cntrl_ = __hold2.release();
3123    __r.__enable_weak_this(__r.__ptr_);
3124    return __r;
3125}
3126
3127template<class _Tp>
3128template<class _Alloc, class ..._Args>
3129shared_ptr<_Tp>
3130shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3131{
3132    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3133    typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3134    typedef __allocator_destructor<_A2> _D2;
3135    _A2 __a2(__a);
3136    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3137    ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3138    shared_ptr<_Tp> __r;
3139    __r.__ptr_ = __hold2.get()->get();
3140    __r.__cntrl_ = __hold2.release();
3141    __r.__enable_weak_this(__r.__ptr_);
3142    return __r;
3143}
3144
3145#else  // _LIBCPP_HAS_NO_VARIADICS
3146
3147template<class _Tp>
3148shared_ptr<_Tp>
3149shared_ptr<_Tp>::make_shared()
3150{
3151    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3152    typedef allocator<_CntrlBlk> _Alloc2;
3153    typedef __allocator_destructor<_Alloc2> _D2;
3154    _Alloc2 __alloc2;
3155    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3156    ::new(__hold2.get()) _CntrlBlk(__alloc2);
3157    shared_ptr<_Tp> __r;
3158    __r.__ptr_ = __hold2.get()->get();
3159    __r.__cntrl_ = __hold2.release();
3160    __r.__enable_weak_this(__r.__ptr_);
3161    return __r;
3162}
3163
3164template<class _Tp>
3165template<class _A0>
3166shared_ptr<_Tp>
3167shared_ptr<_Tp>::make_shared(_A0& __a0)
3168{
3169    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3170    typedef allocator<_CntrlBlk> _Alloc2;
3171    typedef __allocator_destructor<_Alloc2> _D2;
3172    _Alloc2 __alloc2;
3173    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3174    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3175    shared_ptr<_Tp> __r;
3176    __r.__ptr_ = __hold2.get()->get();
3177    __r.__cntrl_ = __hold2.release();
3178    __r.__enable_weak_this(__r.__ptr_);
3179    return __r;
3180}
3181
3182template<class _Tp>
3183template<class _A0, class _A1>
3184shared_ptr<_Tp>
3185shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3186{
3187    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3188    typedef allocator<_CntrlBlk> _Alloc2;
3189    typedef __allocator_destructor<_Alloc2> _D2;
3190    _Alloc2 __alloc2;
3191    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3192    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3193    shared_ptr<_Tp> __r;
3194    __r.__ptr_ = __hold2.get()->get();
3195    __r.__cntrl_ = __hold2.release();
3196    __r.__enable_weak_this(__r.__ptr_);
3197    return __r;
3198}
3199
3200template<class _Tp>
3201template<class _A0, class _A1, class _A2>
3202shared_ptr<_Tp>
3203shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3204{
3205    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3206    typedef allocator<_CntrlBlk> _Alloc2;
3207    typedef __allocator_destructor<_Alloc2> _D2;
3208    _Alloc2 __alloc2;
3209    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3210    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3211    shared_ptr<_Tp> __r;
3212    __r.__ptr_ = __hold2.get()->get();
3213    __r.__cntrl_ = __hold2.release();
3214    __r.__enable_weak_this(__r.__ptr_);
3215    return __r;
3216}
3217
3218template<class _Tp>
3219template<class _Alloc>
3220shared_ptr<_Tp>
3221shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3222{
3223    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3224    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3225    typedef __allocator_destructor<_Alloc2> _D2;
3226    _Alloc2 __alloc2(__a);
3227    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3228    ::new(__hold2.get()) _CntrlBlk(__a);
3229    shared_ptr<_Tp> __r;
3230    __r.__ptr_ = __hold2.get()->get();
3231    __r.__cntrl_ = __hold2.release();
3232    __r.__enable_weak_this(__r.__ptr_);
3233    return __r;
3234}
3235
3236template<class _Tp>
3237template<class _Alloc, class _A0>
3238shared_ptr<_Tp>
3239shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3240{
3241    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3242    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3243    typedef __allocator_destructor<_Alloc2> _D2;
3244    _Alloc2 __alloc2(__a);
3245    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3246    ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3247    shared_ptr<_Tp> __r;
3248    __r.__ptr_ = __hold2.get()->get();
3249    __r.__cntrl_ = __hold2.release();
3250    __r.__enable_weak_this(__r.__ptr_);
3251    return __r;
3252}
3253
3254template<class _Tp>
3255template<class _Alloc, class _A0, class _A1>
3256shared_ptr<_Tp>
3257shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3258{
3259    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3260    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3261    typedef __allocator_destructor<_Alloc2> _D2;
3262    _Alloc2 __alloc2(__a);
3263    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3264    ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3265    shared_ptr<_Tp> __r;
3266    __r.__ptr_ = __hold2.get()->get();
3267    __r.__cntrl_ = __hold2.release();
3268    __r.__enable_weak_this(__r.__ptr_);
3269    return __r;
3270}
3271
3272template<class _Tp>
3273template<class _Alloc, class _A0, class _A1, class _A2>
3274shared_ptr<_Tp>
3275shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3276{
3277    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3278    typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3279    typedef __allocator_destructor<_Alloc2> _D2;
3280    _Alloc2 __alloc2(__a);
3281    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3282    ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3283    shared_ptr<_Tp> __r;
3284    __r.__ptr_ = __hold2.get()->get();
3285    __r.__cntrl_ = __hold2.release();
3286    __r.__enable_weak_this(__r.__ptr_);
3287    return __r;
3288}
3289
3290#endif  // _LIBCPP_HAS_NO_VARIADICS
3291
3292template<class _Tp>
3293shared_ptr<_Tp>::~shared_ptr()
3294{
3295    if (__cntrl_)
3296        __cntrl_->__release_shared();
3297}
3298
3299template<class _Tp>
3300inline _LIBCPP_INLINE_VISIBILITY
3301shared_ptr<_Tp>&
3302shared_ptr<_Tp>::operator=(const shared_ptr& __r)
3303{
3304    shared_ptr(__r).swap(*this);
3305    return *this;
3306}
3307
3308template<class _Tp>
3309template<class _Yp>
3310inline _LIBCPP_INLINE_VISIBILITY
3311shared_ptr<_Tp>&
3312shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r)
3313{
3314    shared_ptr(__r).swap(*this);
3315    return *this;
3316}
3317
3318#ifdef _LIBCPP_MOVE    
3319
3320template<class _Tp>
3321inline _LIBCPP_INLINE_VISIBILITY
3322shared_ptr<_Tp>&
3323shared_ptr<_Tp>::operator=(shared_ptr&& __r)
3324{
3325    shared_ptr(_STD::move(__r)).swap(*this);
3326    return *this;
3327}
3328
3329template<class _Tp>
3330template<class _Yp>
3331inline _LIBCPP_INLINE_VISIBILITY
3332shared_ptr<_Tp>&
3333shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3334{
3335    shared_ptr(_STD::move(__r)).swap(*this);
3336    return *this;
3337}
3338
3339template<class _Tp>
3340template<class _Yp>
3341inline _LIBCPP_INLINE_VISIBILITY
3342shared_ptr<_Tp>&
3343shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3344{
3345    shared_ptr(__r).swap(*this);
3346    return *this;
3347}
3348
3349template<class _Tp>
3350template <class _Yp, class _Dp>
3351inline _LIBCPP_INLINE_VISIBILITY
3352shared_ptr<_Tp>&
3353shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3354{
3355    shared_ptr(_STD::move(__r)).swap(*this);
3356    return *this;
3357}
3358
3359#else
3360
3361template<class _Tp>
3362template<class _Yp>
3363inline _LIBCPP_INLINE_VISIBILITY
3364shared_ptr<_Tp>&
3365shared_ptr<_Tp>::operator=(auto_ptr<_Yp>& __r)
3366{
3367    shared_ptr(__r).swap(*this);
3368    return *this;
3369}
3370
3371template<class _Tp>
3372template <class _Yp, class _Dp>
3373inline _LIBCPP_INLINE_VISIBILITY
3374shared_ptr<_Tp>&
3375shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3376{
3377    shared_ptr(_STD::move(__r)).swap(*this);
3378    return *this;
3379}
3380
3381#endif
3382
3383template<class _Tp>
3384inline _LIBCPP_INLINE_VISIBILITY
3385void
3386shared_ptr<_Tp>::swap(shared_ptr& __r)
3387{
3388    _STD::swap(__ptr_, __r.__ptr_);
3389    _STD::swap(__cntrl_, __r.__cntrl_);
3390}
3391
3392template<class _Tp>
3393inline _LIBCPP_INLINE_VISIBILITY
3394void
3395shared_ptr<_Tp>::reset()
3396{
3397    shared_ptr().swap(*this);
3398}
3399
3400template<class _Tp>
3401template<class _Yp>
3402inline _LIBCPP_INLINE_VISIBILITY
3403void
3404shared_ptr<_Tp>::reset(_Yp* __p)
3405{
3406    shared_ptr(__p).swap(*this);
3407}
3408
3409template<class _Tp>
3410template<class _Yp, class _Dp>
3411inline _LIBCPP_INLINE_VISIBILITY
3412void
3413shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3414{
3415    shared_ptr(__p, __d).swap(*this);
3416}
3417
3418template<class _Tp>
3419template<class _Yp, class _Dp, class _Alloc>
3420inline _LIBCPP_INLINE_VISIBILITY
3421void
3422shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3423{
3424    shared_ptr(__p, __d, __a).swap(*this);
3425}
3426
3427#ifndef _LIBCPP_HAS_NO_VARIADICS
3428
3429template<class _Tp, class ..._Args> 
3430inline _LIBCPP_INLINE_VISIBILITY
3431shared_ptr<_Tp>
3432make_shared(_Args&& ...__args)
3433{
3434    return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3435}
3436
3437template<class _Tp, class _Alloc, class ..._Args> 
3438inline _LIBCPP_INLINE_VISIBILITY
3439shared_ptr<_Tp>
3440allocate_shared(const _Alloc& __a, _Args&& ...__args)
3441{
3442    return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3443}
3444
3445#else  // _LIBCPP_HAS_NO_VARIADICS
3446
3447template<class _Tp>
3448inline _LIBCPP_INLINE_VISIBILITY
3449shared_ptr<_Tp>
3450make_shared()
3451{
3452    return shared_ptr<_Tp>::make_shared();
3453}
3454
3455template<class _Tp, class _A0>
3456inline _LIBCPP_INLINE_VISIBILITY
3457shared_ptr<_Tp>
3458make_shared(_A0& __a0)
3459{
3460    return shared_ptr<_Tp>::make_shared(__a0);
3461}
3462
3463template<class _Tp, class _A0, class _A1>
3464inline _LIBCPP_INLINE_VISIBILITY
3465shared_ptr<_Tp>
3466make_shared(_A0& __a0, _A1& __a1)
3467{
3468    return shared_ptr<_Tp>::make_shared(__a0, __a1);
3469}
3470
3471template<class _Tp, class _A0, class _A1, class _A2> 
3472inline _LIBCPP_INLINE_VISIBILITY
3473shared_ptr<_Tp>
3474make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3475{
3476    return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3477}
3478
3479template<class _Tp, class _Alloc>
3480inline _LIBCPP_INLINE_VISIBILITY
3481shared_ptr<_Tp>
3482allocate_shared(const _Alloc& __a)
3483{
3484    return shared_ptr<_Tp>::allocate_shared(__a);
3485}
3486
3487template<class _Tp, class _Alloc, class _A0>
3488inline _LIBCPP_INLINE_VISIBILITY
3489shared_ptr<_Tp>
3490allocate_shared(const _Alloc& __a, _A0& __a0)
3491{
3492    return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3493}
3494
3495template<class _Tp, class _Alloc, class _A0, class _A1>
3496inline _LIBCPP_INLINE_VISIBILITY
3497shared_ptr<_Tp>
3498allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3499{
3500    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3501}
3502
3503template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3504inline _LIBCPP_INLINE_VISIBILITY
3505shared_ptr<_Tp>
3506allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3507{
3508    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3509}
3510
3511#endif  // _LIBCPP_HAS_NO_VARIADICS
3512
3513template<class _Tp, class _Up>
3514inline _LIBCPP_INLINE_VISIBILITY
3515bool
3516operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3517{
3518    return __x.get() == __y.get();
3519}
3520
3521template<class _Tp, class _Up>
3522inline _LIBCPP_INLINE_VISIBILITY
3523bool
3524operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3525{
3526    return !(__x == __y);
3527}
3528
3529template<class _Tp, class _Up>
3530inline _LIBCPP_INLINE_VISIBILITY
3531bool
3532operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y)
3533{
3534    return __x.get() < __y.get();
3535}
3536
3537template<class _Tp>
3538inline _LIBCPP_INLINE_VISIBILITY
3539void
3540swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y)
3541{
3542    __x.swap(__y);
3543}
3544
3545template<class _Tp, class _Up>
3546inline _LIBCPP_INLINE_VISIBILITY
3547shared_ptr<_Tp>
3548static_pointer_cast(const shared_ptr<_Up>& __r)
3549{
3550    return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3551}
3552
3553template<class _Tp, class _Up>
3554inline _LIBCPP_INLINE_VISIBILITY
3555shared_ptr<_Tp>
3556dynamic_pointer_cast(const shared_ptr<_Up>& __r)
3557{
3558    _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3559    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3560}
3561
3562template<class _Tp, class _Up>
3563shared_ptr<_Tp>
3564const_pointer_cast(const shared_ptr<_Up>& __r)
3565{
3566    return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3567}
3568
3569template<class _Dp, class _Tp>
3570inline _LIBCPP_INLINE_VISIBILITY
3571_Dp*
3572get_deleter(const shared_ptr<_Tp>& __p)
3573{
3574    return __p.template __get_deleter<_Dp>();
3575}
3576
3577template<class _Tp>
3578class weak_ptr
3579{
3580public: 
3581    typedef _Tp element_type; 
3582private:
3583    element_type*        __ptr_;
3584    __shared_weak_count* __cntrl_;
3585
3586public: 
3587    weak_ptr(); 
3588    template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
3589                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat());
3590    weak_ptr(weak_ptr const& __r); 
3591    template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
3592                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat()); 
3593    
3594    ~weak_ptr(); 
3595    
3596    weak_ptr& operator=(weak_ptr const& __r); 
3597    template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r); 
3598    template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r); 
3599    
3600    void swap(weak_ptr& __r); 
3601    void reset(); 
3602    
3603    long use_count() const {return __cntrl_ ? __cntrl_->use_count() : 0;}
3604    bool expired() const {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3605    shared_ptr<_Tp> lock() const; 
3606    template<class _Up> bool owner_before(const shared_ptr<_Up>& __r) const 
3607        {return __cntrl_ < __r.__cntrl_;}
3608    template<class _Up> bool owner_before(const weak_ptr<_Up>& __r) const
3609        {return __cntrl_ < __r.__cntrl_;}
3610
3611    template <class _Up> friend class weak_ptr;
3612    template <class _Up> friend class shared_ptr;
3613};
3614
3615template<class _Tp>
3616inline _LIBCPP_INLINE_VISIBILITY
3617weak_ptr<_Tp>::weak_ptr()
3618    : __ptr_(0),
3619      __cntrl_(0)
3620{
3621}
3622
3623template<class _Tp>
3624inline _LIBCPP_INLINE_VISIBILITY
3625weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r)
3626    : __ptr_(__r.__ptr_),
3627      __cntrl_(__r.__cntrl_)
3628{
3629    if (__cntrl_)
3630        __cntrl_->__add_weak();
3631}
3632
3633template<class _Tp>
3634template<class _Yp>
3635inline _LIBCPP_INLINE_VISIBILITY
3636weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
3637                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3638    : __ptr_(__r.__ptr_),
3639      __cntrl_(__r.__cntrl_)
3640{
3641    if (__cntrl_)
3642        __cntrl_->__add_weak();
3643}
3644
3645template<class _Tp>
3646template<class _Yp>
3647inline _LIBCPP_INLINE_VISIBILITY
3648weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
3649                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3650    : __ptr_(__r.__ptr_),
3651      __cntrl_(__r.__cntrl_)
3652{
3653    if (__cntrl_)
3654        __cntrl_->__add_weak();
3655}
3656
3657template<class _Tp>
3658weak_ptr<_Tp>::~weak_ptr()
3659{
3660    if (__cntrl_)
3661        __cntrl_->__release_weak();
3662}
3663
3664template<class _Tp>
3665inline _LIBCPP_INLINE_VISIBILITY
3666weak_ptr<_Tp>&
3667weak_ptr<_Tp>::operator=(weak_ptr const& __r)
3668{
3669    weak_ptr(__r).swap(*this);
3670    return *this;
3671}
3672
3673template<class _Tp>
3674template<class _Yp> 
3675inline _LIBCPP_INLINE_VISIBILITY
3676weak_ptr<_Tp>&
3677weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r)
3678{
3679    weak_ptr(__r).swap(*this);
3680    return *this;
3681}
3682
3683template<class _Tp>
3684template<class _Yp> 
3685inline _LIBCPP_INLINE_VISIBILITY
3686weak_ptr<_Tp>&
3687weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r)
3688{
3689    weak_ptr(__r).swap(*this);
3690    return *this;
3691}
3692
3693template<class _Tp>
3694inline _LIBCPP_INLINE_VISIBILITY
3695void
3696weak_ptr<_Tp>::swap(weak_ptr& __r)
3697{
3698    _STD::swap(__ptr_, __r.__ptr_);
3699    _STD::swap(__cntrl_, __r.__cntrl_);
3700}
3701
3702template<class _Tp>
3703inline _LIBCPP_INLINE_VISIBILITY
3704void
3705swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y)
3706{
3707    __x.swap(__y);
3708}
3709
3710template<class _Tp>
3711inline _LIBCPP_INLINE_VISIBILITY
3712void
3713weak_ptr<_Tp>::reset()
3714{
3715    weak_ptr().swap(*this);
3716}
3717
3718template<class _Tp>
3719template<class _Yp>
3720shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3721                            typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3722    : __ptr_(__r.__ptr_),
3723      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3724{
3725    if (__cntrl_ == 0)
3726#ifndef _LIBCPP_NO_EXCEPTIONS
3727        throw bad_weak_ptr();
3728#else
3729        assert(!"bad_weak_ptr");
3730#endif
3731}
3732
3733template<class _Tp>
3734shared_ptr<_Tp>
3735weak_ptr<_Tp>::lock() const
3736{
3737    shared_ptr<_Tp> __r;
3738    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3739    if (__r.__cntrl_)
3740        __r.__ptr_ = __ptr_;
3741    return __r;
3742}
3743
3744template <class _Tp> struct owner_less; 
3745
3746template <class _Tp>
3747struct owner_less<shared_ptr<_Tp> > 
3748    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
3749{ 
3750    typedef bool result_type; 
3751    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3752        {return __x.owner_before(__y);}
3753    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
3754        {return __x.owner_before(__y);}
3755    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3756        {return __x.owner_before(__y);}
3757}; 
3758
3759template <class _Tp>
3760struct owner_less<weak_ptr<_Tp> > 
3761    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3762{
3763    typedef bool result_type; 
3764    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
3765        {return __x.owner_before(__y);}
3766    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const
3767        {return __x.owner_before(__y);}
3768    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3769        {return __x.owner_before(__y);}
3770};
3771
3772template<class _Tp>
3773class enable_shared_from_this
3774{
3775    mutable weak_ptr<_Tp> __weak_this_;
3776protected: 
3777    enable_shared_from_this() {}
3778    enable_shared_from_this(enable_shared_from_this const&) {}
3779    enable_shared_from_this& operator=(enable_shared_from_this const&) {return *this;}
3780    ~enable_shared_from_this() {}
3781public: 
3782    shared_ptr<_Tp> shared_from_this() {return shared_ptr<_Tp>(__weak_this_);}
3783    shared_ptr<_Tp const> shared_from_this() const {return shared_ptr<const _Tp>(__weak_this_);}
3784
3785    template <class _Up> friend class shared_ptr;
3786};
3787
3788//enum class 
3789struct pointer_safety
3790{
3791    enum _
3792    {
3793        relaxed,
3794        preferred,
3795        strict
3796    };
3797
3798    _ __v_;
3799
3800    pointer_safety(_ __v) : __v_(__v) {}
3801    operator int() const {return __v_;}
3802};
3803
3804void declare_reachable(void* __p);
3805void declare_no_pointers(char* __p, size_t __n);
3806void undeclare_no_pointers(char* __p, size_t __n);
3807pointer_safety get_pointer_safety();
3808void* __undeclare_reachable(void*);
3809
3810template <class _Tp>
3811inline _LIBCPP_INLINE_VISIBILITY
3812_Tp* 
3813undeclare_reachable(_Tp* __p)
3814{
3815    return static_cast<_Tp*>(__undeclare_reachable(__p));
3816}
3817
3818void* align(size_t, size_t, void*&, size_t&);
3819
3820_LIBCPP_END_NAMESPACE_STD
3821
3822#endif  // _LIBCPP_MEMORY
3823
3824// hh 060228 Created
3825