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