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