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