blob: 6eb21848925b2716a33fb9b9fd6f8494dfc0e16d [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-05-28 14:41:13 +000037template <class T>
38struct 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
Howard Hinnantc51e1022010-05-11 19:42:16 +000049template <class Alloc>
50struct 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
Howard Hinnant8e4e6002010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 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;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
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
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
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
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
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
112template <class T>
113class allocator
114{
115public:
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
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 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;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 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);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class OutputIterator, class T>
148class 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{
155public:
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
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Marshall Clowb22274f2017-01-24 22:22:33 +0000211template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000214class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
216public:
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
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 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() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 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() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 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);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000407 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 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:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 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
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // 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;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000436 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<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:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 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;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 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;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000534 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000549 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000559 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
564template <> // Added in C++14
565struct 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577};
578
579template<class T>
580class enable_shared_from_this
581{
582protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000583 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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000586 ~enable_shared_from_this();
587public:
588 shared_ptr<T> shared_from_this();
589 shared_ptr<T const> shared_from_this() const;
590};
591
592template<class T>
593 bool atomic_is_lock_free(const shared_ptr<T>* p);
594template<class T>
595 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
596template<class T>
597 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
598template<class T>
599 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
600template<class T>
601 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
602template<class T>
603 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
604template<class T>
605 shared_ptr<T>
606 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
607template<class T>
608 bool
609 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
610template<class T>
611 bool
612 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<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);
618template<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
624template <class T> struct hash;
625template <class T, class D> struct hash<unique_ptr<T, D> >;
626template <class T> struct hash<shared_ptr<T> >;
627
628// Pointer safety
629enum class pointer_safety { relaxed, preferred, strict };
630void declare_reachable(void *p);
631template <class T> T *undeclare_reachable(T *p);
632void declare_no_pointers(char *p, size_t n);
633void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000634pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000635
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636void* 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>
Howard Hinnantdc095972011-07-18 15:51:59 +0000652#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000653#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000654#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000655#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000658# include <atomic>
659#endif
660
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000661#include <__undef_min_max>
662
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000663#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000665#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667_LIBCPP_BEGIN_NAMESPACE_STD
668
Eric Fiselier89659d12015-07-07 00:27:16 +0000669template <class _ValueType>
670inline _LIBCPP_ALWAYS_INLINE
671_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
672#if !defined(_LIBCPP_HAS_NO_THREADS) && \
673 defined(__ATOMIC_RELAXED) && \
674 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
675 return __atomic_load_n(__value, __ATOMIC_RELAXED);
676#else
677 return *__value;
678#endif
679}
680
Kuba Breckade9d6792016-09-04 09:55:12 +0000681template <class _ValueType>
682inline _LIBCPP_ALWAYS_INLINE
683_ValueType __libcpp_acquire_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_ACQUIRE) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
688#else
689 return *__value;
690#endif
691}
692
Marshall Clow78dbe462016-11-14 18:22:19 +0000693// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695template <class _Tp> class allocator;
696
697template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000698class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699{
700public:
701 typedef void* pointer;
702 typedef const void* const_pointer;
703 typedef void value_type;
704
705 template <class _Up> struct rebind {typedef allocator<_Up> other;};
706};
707
Howard Hinnant9f771052012-01-19 23:15:22 +0000708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000710{
711public:
712 typedef const void* pointer;
713 typedef const void* const_pointer;
714 typedef const void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719// pointer_traits
720
721template <class _Tp>
722struct __has_element_type
723{
724private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000725 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 template <class _Up> static __two __test(...);
727 template <class _Up> static char __test(typename _Up::element_type* = 0);
728public:
729 static const bool value = sizeof(__test<_Tp>(0)) == 1;
730};
731
732template <class _Ptr, bool = __has_element_type<_Ptr>::value>
733struct __pointer_traits_element_type;
734
735template <class _Ptr>
736struct __pointer_traits_element_type<_Ptr, true>
737{
738 typedef typename _Ptr::element_type type;
739};
740
741#ifndef _LIBCPP_HAS_NO_VARIADICS
742
743template <template <class, class...> class _Sp, class _Tp, class ..._Args>
744struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
745{
746 typedef typename _Sp<_Tp, _Args...>::element_type type;
747};
748
749template <template <class, class...> class _Sp, class _Tp, class ..._Args>
750struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
751{
752 typedef _Tp type;
753};
754
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000755#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
757template <template <class> class _Sp, class _Tp>
758struct __pointer_traits_element_type<_Sp<_Tp>, true>
759{
760 typedef typename _Sp<_Tp>::element_type type;
761};
762
763template <template <class> class _Sp, class _Tp>
764struct __pointer_traits_element_type<_Sp<_Tp>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class> class _Sp, class _Tp, class _A0>
770struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
771{
772 typedef typename _Sp<_Tp, _A0>::element_type type;
773};
774
775template <template <class, class> class _Sp, class _Tp, class _A0>
776struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
777{
778 typedef _Tp type;
779};
780
781template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
782struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
783{
784 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
785};
786
787template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
788struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
794 class _A1, class _A2>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
796{
797 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
803{
804 typedef _Tp type;
805};
806
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
809template <class _Tp>
810struct __has_difference_type
811{
812private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000813 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 template <class _Up> static __two __test(...);
815 template <class _Up> static char __test(typename _Up::difference_type* = 0);
816public:
817 static const bool value = sizeof(__test<_Tp>(0)) == 1;
818};
819
820template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
821struct __pointer_traits_difference_type
822{
823 typedef ptrdiff_t type;
824};
825
826template <class _Ptr>
827struct __pointer_traits_difference_type<_Ptr, true>
828{
829 typedef typename _Ptr::difference_type type;
830};
831
832template <class _Tp, class _Up>
833struct __has_rebind
834{
835private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000836 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 template <class _Xp> static __two __test(...);
838 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
839public:
840 static const bool value = sizeof(__test<_Tp>(0)) == 1;
841};
842
843template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
844struct __pointer_traits_rebind
845{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 typedef typename _Tp::template rebind<_Up> type;
848#else
849 typedef typename _Tp::template rebind<_Up>::other type;
850#endif
851};
852
853#ifndef _LIBCPP_HAS_NO_VARIADICS
854
855template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
867{
868 typedef _Sp<_Up, _Args...> type;
869};
870
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000871#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
873template <template <class> class _Sp, class _Tp, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
875{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 typedef typename _Sp<_Tp>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class> class _Sp, class _Tp, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
885{
886 typedef _Sp<_Up> type;
887};
888
889template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
891{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000892#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
894#else
895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
896#endif
897};
898
899template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
900struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
901{
902 typedef _Sp<_Up, _A0> type;
903};
904
905template <template <class, class, class> class _Sp, class _Tp, class _A0,
906 class _A1, class _Up>
907struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
908{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000909#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
911#else
912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
913#endif
914};
915
916template <template <class, class, class> class _Sp, class _Tp, class _A0,
917 class _A1, class _Up>
918struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
919{
920 typedef _Sp<_Up, _A0, _A1> type;
921};
922
923template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
924 class _A1, class _A2, class _Up>
925struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
926{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000927#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
929#else
930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
931#endif
932};
933
934template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
935 class _A1, class _A2, class _Up>
936struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
937{
938 typedef _Sp<_Up, _A0, _A1, _A2> type;
939};
940
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000941#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000944struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
946 typedef _Ptr pointer;
947 typedef typename __pointer_traits_element_type<pointer>::type element_type;
948 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
949
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000951 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952#else
953 template <class _Up> struct rebind
954 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957private:
958 struct __nat {};
959public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 static pointer pointer_to(typename conditional<is_void<element_type>::value,
962 __nat, element_type>::type& __r)
963 {return pointer::pointer_to(__r);}
964};
965
966template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
969 typedef _Tp* pointer;
970 typedef _Tp element_type;
971 typedef ptrdiff_t difference_type;
972
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000973#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 template <class _Up> using rebind = _Up*;
975#else
976 template <class _Up> struct rebind {typedef _Up* other;};
977#endif
978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000984 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000985 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988template <class _From, class _To>
989struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991 typedef typename pointer_traits<_From>::template rebind<_To> type;
992#else
993 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
994#endif
995};
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// allocator_traits
998
999namespace __has_pointer_type_imp
1000{
Howard Hinnant6e260172013-09-21 01:45:05 +00001001 template <class _Up> static __two __test(...);
1002 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003}
1004
1005template <class _Tp>
1006struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +00001007 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
1009};
1010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
1034template <class _Tp>
1035struct __has_const_pointer
1036{
1037private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001038 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template <class _Up> static __two __test(...);
1040 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1041public:
1042 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1043};
1044
1045template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1046struct __const_pointer
1047{
1048 typedef typename _Alloc::const_pointer type;
1049};
1050
1051template <class _Tp, class _Ptr, class _Alloc>
1052struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1053{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001054#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1056#else
1057 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1058#endif
1059};
1060
1061template <class _Tp>
1062struct __has_void_pointer
1063{
1064private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001065 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1073struct __void_pointer
1074{
1075 typedef typename _Alloc::void_pointer type;
1076};
1077
1078template <class _Ptr, class _Alloc>
1079struct __void_pointer<_Ptr, _Alloc, false>
1080{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1083#else
1084 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1085#endif
1086};
1087
1088template <class _Tp>
1089struct __has_const_void_pointer
1090{
1091private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001092 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 template <class _Up> static __two __test(...);
1094 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1095public:
1096 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1097};
1098
1099template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1100struct __const_void_pointer
1101{
1102 typedef typename _Alloc::const_void_pointer type;
1103};
1104
1105template <class _Ptr, class _Alloc>
1106struct __const_void_pointer<_Ptr, _Alloc, false>
1107{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001108#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1110#else
1111 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1112#endif
1113};
1114
Howard Hinnantc834c512011-11-29 18:15:50 +00001115template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001117_Tp*
1118__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 return __p;
1121}
1122
1123template <class _Pointer>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001126__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001128 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129}
1130
1131template <class _Tp>
1132struct __has_size_type
1133{
1134private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001135 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 template <class _Up> static __two __test(...);
1137 template <class _Up> static char __test(typename _Up::size_type* = 0);
1138public:
1139 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1140};
1141
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001142template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143struct __size_type
1144{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001145 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146};
1147
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001148template <class _Alloc, class _DiffType>
1149struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 typedef typename _Alloc::size_type type;
1152};
1153
1154template <class _Tp>
1155struct __has_propagate_on_container_copy_assignment
1156{
1157private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001158 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 template <class _Up> static __two __test(...);
1160 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1161public:
1162 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1163};
1164
1165template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1166struct __propagate_on_container_copy_assignment
1167{
1168 typedef false_type type;
1169};
1170
1171template <class _Alloc>
1172struct __propagate_on_container_copy_assignment<_Alloc, true>
1173{
1174 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1175};
1176
1177template <class _Tp>
1178struct __has_propagate_on_container_move_assignment
1179{
1180private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001181 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 template <class _Up> static __two __test(...);
1183 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1184public:
1185 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1186};
1187
1188template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1189struct __propagate_on_container_move_assignment
1190{
1191 typedef false_type type;
1192};
1193
1194template <class _Alloc>
1195struct __propagate_on_container_move_assignment<_Alloc, true>
1196{
1197 typedef typename _Alloc::propagate_on_container_move_assignment type;
1198};
1199
1200template <class _Tp>
1201struct __has_propagate_on_container_swap
1202{
1203private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001204 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 template <class _Up> static __two __test(...);
1206 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1207public:
1208 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1209};
1210
1211template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1212struct __propagate_on_container_swap
1213{
1214 typedef false_type type;
1215};
1216
1217template <class _Alloc>
1218struct __propagate_on_container_swap<_Alloc, true>
1219{
1220 typedef typename _Alloc::propagate_on_container_swap type;
1221};
1222
Marshall Clow0b587562015-06-02 16:34:03 +00001223template <class _Tp>
1224struct __has_is_always_equal
1225{
1226private:
1227 struct __two {char __lx; char __lxx;};
1228 template <class _Up> static __two __test(...);
1229 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1230public:
1231 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1232};
1233
1234template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1235struct __is_always_equal
1236{
1237 typedef typename _VSTD::is_empty<_Alloc>::type type;
1238};
1239
1240template <class _Alloc>
1241struct __is_always_equal<_Alloc, true>
1242{
1243 typedef typename _Alloc::is_always_equal type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1247struct __has_rebind_other
1248{
1249private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001250 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 template <class _Xp> static __two __test(...);
1252 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1253public:
1254 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1255};
1256
1257template <class _Tp, class _Up>
1258struct __has_rebind_other<_Tp, _Up, false>
1259{
1260 static const bool value = false;
1261};
1262
1263template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1264struct __allocator_traits_rebind
1265{
1266 typedef typename _Tp::template rebind<_Up>::other type;
1267};
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1279{
1280 typedef _Alloc<_Up, _Args...> type;
1281};
1282
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
1285template <template <class> class _Alloc, class _Tp, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1293{
1294 typedef _Alloc<_Up> type;
1295};
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1299{
1300 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1301};
1302
1303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1> type;
1321};
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _A2, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1326{
1327 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1328};
1329
1330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1333{
1334 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1335};
1336
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001337#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001339#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1342auto
1343__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1344 -> decltype(__a.allocate(__sz, __p), true_type());
1345
1346template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1347auto
1348__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1349 -> false_type;
1350
1351template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1352struct __has_allocate_hint
1353 : integral_constant<bool,
1354 is_same<
1355 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1356 declval<_SizeType>(),
1357 declval<_ConstVoidPtr>())),
1358 true_type>::value>
1359{
1360};
1361
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001362#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1365struct __has_allocate_hint
1366 : true_type
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001375decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1376 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 true_type())
1378__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1379
1380template <class _Alloc, class _Pointer, class ..._Args>
1381false_type
1382__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1383
1384template <class _Alloc, class _Pointer, class ..._Args>
1385struct __has_construct
1386 : integral_constant<bool,
1387 is_same<
1388 decltype(__has_construct_test(declval<_Alloc>(),
1389 declval<_Pointer>(),
1390 declval<_Args>()...)),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc, class _Pointer>
1396auto
1397__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1398 -> decltype(__a.destroy(__p), true_type());
1399
1400template <class _Alloc, class _Pointer>
1401auto
1402__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1403 -> false_type;
1404
1405template <class _Alloc, class _Pointer>
1406struct __has_destroy
1407 : integral_constant<bool,
1408 is_same<
1409 decltype(__has_destroy_test(declval<_Alloc>(),
1410 declval<_Pointer>())),
1411 true_type>::value>
1412{
1413};
1414
1415template <class _Alloc>
1416auto
1417__has_max_size_test(_Alloc&& __a)
1418 -> decltype(__a.max_size(), true_type());
1419
1420template <class _Alloc>
1421auto
1422__has_max_size_test(const volatile _Alloc& __a)
1423 -> false_type;
1424
1425template <class _Alloc>
1426struct __has_max_size
1427 : integral_constant<bool,
1428 is_same<
1429 decltype(__has_max_size_test(declval<_Alloc&>())),
1430 true_type>::value>
1431{
1432};
1433
1434template <class _Alloc>
1435auto
1436__has_select_on_container_copy_construction_test(_Alloc&& __a)
1437 -> decltype(__a.select_on_container_copy_construction(), true_type());
1438
1439template <class _Alloc>
1440auto
1441__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1442 -> false_type;
1443
1444template <class _Alloc>
1445struct __has_select_on_container_copy_construction
1446 : integral_constant<bool,
1447 is_same<
1448 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1449 true_type>::value>
1450{
1451};
1452
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001453#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455#ifndef _LIBCPP_HAS_NO_VARIADICS
1456
1457template <class _Alloc, class _Pointer, class ..._Args>
1458struct __has_construct
1459 : false_type
1460{
1461};
1462
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463#else // _LIBCPP_HAS_NO_VARIADICS
1464
1465template <class _Alloc, class _Pointer, class _Args>
1466struct __has_construct
1467 : false_type
1468{
1469};
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
1474struct __has_destroy
1475 : false_type
1476{
1477};
1478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1544 {return allocate(__a, __n, __hint,
1545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp(__a0);
1569 }
1570 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001572 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 const _A1& __a1)
1574 {
1575 ::new ((void*)__p) _Tp(__a0, __a1);
1576 }
1577 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1, const _A2& __a2)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1583 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void destroy(allocator_type& __a, _Tp* __p)
1589 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001592 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(const allocator_type& __a)
1598 {return select_on_container_copy_construction(
1599 __has_select_on_container_copy_construction<const allocator_type>(),
1600 __a);}
1601
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001602 template <class _Ptr>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 void
1606 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1607 {
1608 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1609 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1610 }
1611
1612 template <class _Tp>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 typename enable_if
1616 <
1617 (is_same<allocator_type, allocator<_Tp> >::value
1618 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1619 is_trivially_move_constructible<_Tp>::value,
1620 void
1621 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001622 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 {
1624 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001625 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001626 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 __begin2 += _Np;
1629 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 }
1631
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001632 template <class _Iter, class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
1636 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1637 {
1638 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1639 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1640 }
1641
1642 template <class _Tp>
1643 _LIBCPP_INLINE_VISIBILITY
1644 static
1645 typename enable_if
1646 <
1647 (is_same<allocator_type, allocator<_Tp> >::value
1648 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1649 is_trivially_move_constructible<_Tp>::value,
1650 void
1651 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001652 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001653 {
1654 typedef typename remove_const<_Tp>::type _Vp;
1655 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001656 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001657 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 __begin2 += _Np;
1660 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001661 }
1662
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 template <class _Ptr>
1664 _LIBCPP_INLINE_VISIBILITY
1665 static
1666 void
1667 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1668 {
1669 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 {
1671 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1672 --__end2;
1673 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 }
1675
1676 template <class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 static
1679 typename enable_if
1680 <
1681 (is_same<allocator_type, allocator<_Tp> >::value
1682 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1683 is_trivially_move_constructible<_Tp>::value,
1684 void
1685 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001686 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 {
1688 ptrdiff_t _Np = __end1 - __begin1;
1689 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001690 if (_Np > 0)
1691 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001692 }
1693
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694private:
1695
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static pointer allocate(allocator_type& __a, size_type __n,
1698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001702 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {return __a.allocate(__n);}
1704
1705#ifndef _LIBCPP_HAS_NO_VARIADICS
1706 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001714 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001716#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
1718 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1721 {__a.destroy(__p);}
1722 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 static void __destroy(false_type, allocator_type&, _Tp* __p)
1725 {
1726 __p->~_Tp();
1727 }
1728
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static size_type __max_size(true_type, const allocator_type& __a)
1731 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001734 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 static allocator_type
1738 select_on_container_copy_construction(true_type, const allocator_type& __a)
1739 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static allocator_type
1742 select_on_container_copy_construction(false_type, const allocator_type& __a)
1743 {return __a;}
1744};
1745
Marshall Clow940e01c2015-04-07 05:21:38 +00001746template <class _Traits, class _Tp>
1747struct __rebind_alloc_helper
1748{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001749#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001750 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001751#else
1752 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1753#endif
1754};
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756// allocator
1757
1758template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001759class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761public:
1762 typedef size_t size_type;
1763 typedef ptrdiff_t difference_type;
1764 typedef _Tp* pointer;
1765 typedef const _Tp* const_pointer;
1766 typedef _Tp& reference;
1767 typedef const _Tp& const_reference;
1768 typedef _Tp value_type;
1769
Howard Hinnant4931e092011-06-02 21:38:57 +00001770 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001771 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1774
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1776 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1777 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001779 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001782 {
1783 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1787 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001788 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001789 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1791 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001792#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 template <class _Up, class... _Args>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(_Up* __p, _Args&&... __args)
1797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001798 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p)
1804 {
1805 ::new((void*)__p) _Tp();
1806 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001807# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0>
1810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001811 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 construct(pointer __p, _A0& __a0)
1813 {
1814 ::new((void*)__p) _Tp(__a0);
1815 }
1816 template <class _A0>
1817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001818 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 construct(pointer __p, const _A0& __a0)
1820 {
1821 ::new((void*)__p) _Tp(__a0);
1822 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001823# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, _A1& __a1)
1828 {
1829 ::new((void*)__p) _Tp(__a0, __a1);
1830 }
1831 template <class _A0, class _A1>
1832 _LIBCPP_INLINE_VISIBILITY
1833 void
1834 construct(pointer __p, const _A0& __a0, _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
1838 template <class _A0, class _A1>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(pointer __p, _A0& __a0, const _A1& __a1)
1842 {
1843 ::new((void*)__p) _Tp(__a0, __a1);
1844 }
1845 template <class _A0, class _A1>
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1849 {
1850 ::new((void*)__p) _Tp(__a0, __a1);
1851 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1854};
1855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858{
1859public:
1860 typedef size_t size_type;
1861 typedef ptrdiff_t difference_type;
1862 typedef const _Tp* pointer;
1863 typedef const _Tp* const_pointer;
1864 typedef const _Tp& reference;
1865 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001866 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867
1868 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001869 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870
1871 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1872
1873 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1874 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1875 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1876 {return _VSTD::addressof(__x);}
1877 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001878 {
1879 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001880 __throw_length_error("allocator<const T>::allocate(size_t n)"
1881 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001883 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001885 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1887 {return size_type(~0) / sizeof(_Tp);}
1888#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1889 template <class _Up, class... _Args>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(_Up* __p, _Args&&... __args)
1893 {
1894 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1895 }
1896#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p)
1900 {
1901 ::new((void*)__p) _Tp();
1902 }
1903# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001904
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905 template <class _A0>
1906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001907 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 construct(pointer __p, _A0& __a0)
1909 {
1910 ::new((void*)__p) _Tp(__a0);
1911 }
1912 template <class _A0>
1913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001914 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 construct(pointer __p, const _A0& __a0)
1916 {
1917 ::new((void*)__p) _Tp(__a0);
1918 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, _A1& __a1)
1924 {
1925 ::new((void*)__p) _Tp(__a0, __a1);
1926 }
1927 template <class _A0, class _A1>
1928 _LIBCPP_INLINE_VISIBILITY
1929 void
1930 construct(pointer __p, const _A0& __a0, _A1& __a1)
1931 {
1932 ::new((void*)__p) _Tp(__a0, __a1);
1933 }
1934 template <class _A0, class _A1>
1935 _LIBCPP_INLINE_VISIBILITY
1936 void
1937 construct(pointer __p, _A0& __a0, const _A1& __a1)
1938 {
1939 ::new((void*)__p) _Tp(__a0, __a1);
1940 }
1941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1945 {
1946 ::new((void*)__p) _Tp(__a0, __a1);
1947 }
1948#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1949 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1950};
1951
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952template <class _Tp, class _Up>
1953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001954bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001961class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 : public iterator<output_iterator_tag,
1963 _Tp, // purposefully not C++03
1964 ptrdiff_t, // purposefully not C++03
1965 _Tp*, // purposefully not C++03
1966 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1967{
1968private:
1969 _OutputIterator __x_;
1970public:
1971 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1972 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1974 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001975#if _LIBCPP_STD_VER >= 14
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1977 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1978#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1981 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001982#if _LIBCPP_STD_VER >= 14
1983 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985};
1986
1987template <class _Tp>
1988pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001989get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
1991 pair<_Tp*, ptrdiff_t> __r(0, 0);
1992 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1993 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1994 / sizeof(_Tp);
1995 if (__n > __m)
1996 __n = __m;
1997 while (__n > 0)
1998 {
1999 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2000 if (__r.first)
2001 {
2002 __r.second = __n;
2003 break;
2004 }
2005 __n /= 2;
2006 }
2007 return __r;
2008}
2009
2010template <class _Tp>
2011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002012void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Marshall Clowb22274f2017-01-24 22:22:33 +00002014#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _Tp>
2016struct auto_ptr_ref
2017{
2018 _Tp* __ptr_;
2019};
2020
2021template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002022class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024private:
2025 _Tp* __ptr_;
2026public:
2027 typedef _Tp element_type;
2028
2029 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2030 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2031 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2032 : __ptr_(__p.release()) {}
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2034 {reset(__p.release()); return *this;}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2038 {reset(__p.__ptr_); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2040
2041 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2042 {return *__ptr_;}
2043 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2046 {
2047 _Tp* __t = __ptr_;
2048 __ptr_ = 0;
2049 return __t;
2050 }
2051 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2052 {
2053 if (__ptr_ != __p)
2054 delete __ptr_;
2055 __ptr_ = __p;
2056 }
2057
2058 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2059 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2060 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2062 {return auto_ptr<_Up>(release());}
2063};
2064
2065template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002066class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068public:
2069 typedef void element_type;
2070};
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2074 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002075 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002076 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002077 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002078 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002079 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080struct __libcpp_compressed_pair_switch;
2081
2082template <class _T1, class _T2, bool IsSame>
2083struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2084
2085template <class _T1, class _T2, bool IsSame>
2086struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2087
2088template <class _T1, class _T2, bool IsSame>
2089struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2090
2091template <class _T1, class _T2>
2092struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2093
2094template <class _T1, class _T2>
2095struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2096
2097template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2098class __libcpp_compressed_pair_imp;
2099
2100template <class _T1, class _T2>
2101class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2102{
2103private:
2104 _T1 __first_;
2105 _T2 __second_;
2106public:
2107 typedef _T1 _T1_param;
2108 typedef _T2 _T2_param;
2109
2110 typedef typename remove_reference<_T1>::type& _T1_reference;
2111 typedef typename remove_reference<_T2>::type& _T2_reference;
2112
Eric Fiselier593652b2017-04-12 22:43:49 +00002113 typedef typename remove_reference<typename add_const<_T1>::type>::type& _T1_const_reference;
2114 typedef typename remove_reference<typename add_const<_T2>::type>::type& _T2_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115
Marshall Clowac92bfe2015-07-16 03:05:06 +00002116 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002117 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002118 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002119 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002120 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002122 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123
Howard Hinnant83b1c052011-12-19 17:58:44 +00002124#ifndef _LIBCPP_HAS_NO_VARIADICS
2125
2126 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2127 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002128 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002129 tuple<_Args1...> __first_args,
2130 tuple<_Args2...> __second_args,
2131 __tuple_indices<_I1...>,
2132 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002133 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2134 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002135 {}
2136
2137#endif // _LIBCPP_HAS_NO_VARIADICS
2138
Howard Hinnant719bda32011-05-28 14:41:13 +00002139 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2140 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141
Howard Hinnant719bda32011-05-28 14:41:13 +00002142 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2143 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144
2145 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002146 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002147 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002149 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 swap(__first_, __x.__first_);
2151 swap(__second_, __x.__second_);
2152 }
2153};
2154
2155template <class _T1, class _T2>
2156class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2157 : private _T1
2158{
2159private:
2160 _T2 __second_;
2161public:
2162 typedef _T1 _T1_param;
2163 typedef _T2 _T2_param;
2164
2165 typedef _T1& _T1_reference;
2166 typedef typename remove_reference<_T2>::type& _T2_reference;
2167
2168 typedef const _T1& _T1_const_reference;
Eric Fiselier593652b2017-04-12 22:43:49 +00002169 typedef typename remove_reference<typename add_const<_T2>::type>::type&
2170 _T2_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171
Marshall Clowac92bfe2015-07-16 03:05:06 +00002172 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002173 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002174 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002175 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002176 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002178 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179
Howard Hinnant83b1c052011-12-19 17:58:44 +00002180#ifndef _LIBCPP_HAS_NO_VARIADICS
2181
2182 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2183 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002184 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002185 tuple<_Args1...> __first_args,
2186 tuple<_Args2...> __second_args,
2187 __tuple_indices<_I1...>,
2188 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002189 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2190 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002191 {}
2192
2193#endif // _LIBCPP_HAS_NO_VARIADICS
2194
Howard Hinnant719bda32011-05-28 14:41:13 +00002195 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2196 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197
Howard Hinnant719bda32011-05-28 14:41:13 +00002198 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2199 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200
2201 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002202 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002203 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002205 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 swap(__second_, __x.__second_);
2207 }
2208};
2209
2210template <class _T1, class _T2>
2211class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2212 : private _T2
2213{
2214private:
2215 _T1 __first_;
2216public:
2217 typedef _T1 _T1_param;
2218 typedef _T2 _T2_param;
2219
2220 typedef typename remove_reference<_T1>::type& _T1_reference;
2221 typedef _T2& _T2_reference;
2222
Eric Fiselier593652b2017-04-12 22:43:49 +00002223 typedef typename remove_reference<typename add_const<_T1>::type>::type&
2224 _T1_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 typedef const _T2& _T2_const_reference;
2226
Marshall Clowac92bfe2015-07-16 03:05:06 +00002227 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002229 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002231 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002233 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2234 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002235 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236
Howard Hinnant83b1c052011-12-19 17:58:44 +00002237#ifndef _LIBCPP_HAS_NO_VARIADICS
2238
2239 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2240 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002241 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002242 tuple<_Args1...> __first_args,
2243 tuple<_Args2...> __second_args,
2244 __tuple_indices<_I1...>,
2245 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002246 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2247 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002248
2249 {}
2250
2251#endif // _LIBCPP_HAS_NO_VARIADICS
2252
Howard Hinnant719bda32011-05-28 14:41:13 +00002253 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2254 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
Howard Hinnant719bda32011-05-28 14:41:13 +00002256 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2257 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258
2259 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002260 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002261 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002263 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 swap(__first_, __x.__first_);
2265 }
2266};
2267
2268template <class _T1, class _T2>
2269class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2270 : private _T1,
2271 private _T2
2272{
2273public:
2274 typedef _T1 _T1_param;
2275 typedef _T2 _T2_param;
2276
2277 typedef _T1& _T1_reference;
2278 typedef _T2& _T2_reference;
2279
2280 typedef const _T1& _T1_const_reference;
2281 typedef const _T2& _T2_const_reference;
2282
2283 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2284 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002285 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002287 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002289 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290
Howard Hinnant83b1c052011-12-19 17:58:44 +00002291#ifndef _LIBCPP_HAS_NO_VARIADICS
2292
2293 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2294 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002295 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002296 tuple<_Args1...> __first_args,
2297 tuple<_Args2...> __second_args,
2298 __tuple_indices<_I1...>,
2299 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002300 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2301 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002302 {}
2303
2304#endif // _LIBCPP_HAS_NO_VARIADICS
2305
Howard Hinnant719bda32011-05-28 14:41:13 +00002306 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2307 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308
Howard Hinnant719bda32011-05-28 14:41:13 +00002309 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2310 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Howard Hinnant28b24882011-12-01 20:21:04 +00002312 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002313 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002314 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 {
2316 }
2317};
2318
2319template <class _T1, class _T2>
2320class __compressed_pair
2321 : private __libcpp_compressed_pair_imp<_T1, _T2>
2322{
2323 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2324public:
2325 typedef typename base::_T1_param _T1_param;
2326 typedef typename base::_T2_param _T2_param;
2327
2328 typedef typename base::_T1_reference _T1_reference;
2329 typedef typename base::_T2_reference _T2_reference;
2330
2331 typedef typename base::_T1_const_reference _T1_const_reference;
2332 typedef typename base::_T2_const_reference _T2_const_reference;
2333
2334 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002335 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002336 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002337 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002338 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002340 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341
Howard Hinnant83b1c052011-12-19 17:58:44 +00002342#ifndef _LIBCPP_HAS_NO_VARIADICS
2343
2344 template <class... _Args1, class... _Args2>
2345 _LIBCPP_INLINE_VISIBILITY
2346 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2347 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002348 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002349 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2350 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2351 {}
2352
2353#endif // _LIBCPP_HAS_NO_VARIADICS
2354
Howard Hinnant719bda32011-05-28 14:41:13 +00002355 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2356 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357
Howard Hinnant719bda32011-05-28 14:41:13 +00002358 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2359 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360
Howard Hinnant719bda32011-05-28 14:41:13 +00002361 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2362 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002363 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002364 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365};
2366
2367template <class _T1, class _T2>
2368inline _LIBCPP_INLINE_VISIBILITY
2369void
2370swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002371 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002372 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 {__x.swap(__y);}
2374
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002375// __same_or_less_cv_qualified
2376
2377template <class _Ptr1, class _Ptr2,
2378 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2379 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2380 >::value
2381 >
2382struct __same_or_less_cv_qualified_imp
2383 : is_convertible<_Ptr1, _Ptr2> {};
2384
2385template <class _Ptr1, class _Ptr2>
2386struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2387 : false_type {};
2388
Marshall Clowdf296162014-04-26 05:19:48 +00002389template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2390 is_same<_Ptr1, _Ptr2>::value ||
2391 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002392struct __same_or_less_cv_qualified
2393 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2394
2395template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002396struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002397 : false_type {};
2398
2399// default_delete
2400
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002402struct _LIBCPP_TEMPLATE_VIS default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403{
Eric Fiselier07b2b552016-11-18 06:42:17 +00002404#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002405 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2406#else
2407 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2408#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409 template <class _Up>
2410 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002411 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2412 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413 {
2414 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002415 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 delete __ptr;
2417 }
2418};
2419
2420template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002421struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002423public:
Eric Fiselier07b2b552016-11-18 06:42:17 +00002424#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002425 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2426#else
2427 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2428#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002429 template <class _Up>
2430 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002431 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002432 template <class _Up>
2433 _LIBCPP_INLINE_VISIBILITY
2434 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002435 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 {
2437 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002438 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439 delete [] __ptr;
2440 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441};
2442
2443template <class _Tp, class _Dp = default_delete<_Tp> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002444class _LIBCPP_TEMPLATE_VIS unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445{
2446public:
2447 typedef _Tp element_type;
2448 typedef _Dp deleter_type;
2449 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2450private:
2451 __compressed_pair<pointer, deleter_type> __ptr_;
2452
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002453#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 unique_ptr(unique_ptr&);
2455 template <class _Up, class _Ep>
2456 unique_ptr(unique_ptr<_Up, _Ep>&);
2457 unique_ptr& operator=(unique_ptr&);
2458 template <class _Up, class _Ep>
2459 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002460#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461
2462 struct __nat {int __for_bool_;};
2463
Eric Fiselier593652b2017-04-12 22:43:49 +00002464 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2465 typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
2466 _Dp_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002468 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 : __ptr_(pointer())
2470 {
2471 static_assert(!is_pointer<deleter_type>::value,
2472 "unique_ptr constructed with null function pointer deleter");
2473 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002474 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 : __ptr_(pointer())
2476 {
2477 static_assert(!is_pointer<deleter_type>::value,
2478 "unique_ptr constructed with null function pointer deleter");
2479 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002480 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002481 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 {
2483 static_assert(!is_pointer<deleter_type>::value,
2484 "unique_ptr constructed with null function pointer deleter");
2485 }
2486
Howard Hinnant74279a52010-09-04 23:28:19 +00002487#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2489 is_reference<deleter_type>::value,
2490 deleter_type,
2491 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002492 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493 : __ptr_(__p, __d) {}
2494
2495 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002496 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002497 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 {
2499 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2500 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002501 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002502 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503 template <class _Up, class _Ep>
2504 _LIBCPP_INLINE_VISIBILITY
2505 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2506 typename enable_if
2507 <
2508 !is_array<_Up>::value &&
2509 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2510 is_convertible<_Ep, deleter_type>::value &&
2511 (
2512 !is_reference<deleter_type>::value ||
2513 is_same<deleter_type, _Ep>::value
2514 ),
2515 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002516 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002517 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518
Marshall Clowb22274f2017-01-24 22:22:33 +00002519#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520 template <class _Up>
2521 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2522 typename enable_if<
2523 is_convertible<_Up*, _Tp*>::value &&
2524 is_same<_Dp, default_delete<_Tp> >::value,
2525 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002526 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 : __ptr_(__p.release())
2528 {
2529 }
Marshall Clowb22274f2017-01-24 22:22:33 +00002530#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531
Howard Hinnant719bda32011-05-28 14:41:13 +00002532 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 {
2534 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002535 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536 return *this;
2537 }
2538
2539 template <class _Up, class _Ep>
2540 _LIBCPP_INLINE_VISIBILITY
2541 typename enable_if
2542 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002543 !is_array<_Up>::value &&
2544 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2545 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546 unique_ptr&
2547 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002548 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549 {
2550 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002551 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 return *this;
2553 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002554#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555
2556 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2557 {
2558 return __rv<unique_ptr>(*this);
2559 }
2560
2561 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002562 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563
2564 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002565 _LIBCPP_INLINE_VISIBILITY
2566 typename enable_if<
2567 !is_array<_Up>::value &&
2568 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2569 is_assignable<deleter_type&, _Ep&>::value,
2570 unique_ptr&
2571 >::type
2572 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 {
2574 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002575 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 return *this;
2577 }
2578
2579 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002580 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581
Marshall Clowb22274f2017-01-24 22:22:33 +00002582#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583 template <class _Up>
2584 _LIBCPP_INLINE_VISIBILITY
2585 typename enable_if<
2586 is_convertible<_Up*, _Tp*>::value &&
2587 is_same<_Dp, default_delete<_Tp> >::value,
2588 unique_ptr&
2589 >::type
2590 operator=(auto_ptr<_Up> __p)
2591 {reset(__p.release()); return *this;}
Marshall Clowb22274f2017-01-24 22:22:33 +00002592#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00002593#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2595
Howard Hinnant719bda32011-05-28 14:41:13 +00002596 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 {
2598 reset();
2599 return *this;
2600 }
2601
2602 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2603 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002604 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2605 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2606 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2607 {return __ptr_.second();}
2608 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2609 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002610 _LIBCPP_INLINE_VISIBILITY
2611 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2612 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613
Howard Hinnant719bda32011-05-28 14:41:13 +00002614 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 {
2616 pointer __t = __ptr_.first();
2617 __ptr_.first() = pointer();
2618 return __t;
2619 }
2620
Howard Hinnant719bda32011-05-28 14:41:13 +00002621 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 {
2623 pointer __tmp = __ptr_.first();
2624 __ptr_.first() = __p;
2625 if (__tmp)
2626 __ptr_.second()(__tmp);
2627 }
2628
Howard Hinnant719bda32011-05-28 14:41:13 +00002629 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2630 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631};
2632
2633template <class _Tp, class _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002634class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635{
2636public:
2637 typedef _Tp element_type;
2638 typedef _Dp deleter_type;
2639 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2640private:
2641 __compressed_pair<pointer, deleter_type> __ptr_;
2642
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002643#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644 unique_ptr(unique_ptr&);
2645 template <class _Up>
2646 unique_ptr(unique_ptr<_Up>&);
2647 unique_ptr& operator=(unique_ptr&);
2648 template <class _Up>
2649 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002650#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651
2652 struct __nat {int __for_bool_;};
2653
Eric Fiselier593652b2017-04-12 22:43:49 +00002654 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2655 typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
2656 _Dp_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002658 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 : __ptr_(pointer())
2660 {
2661 static_assert(!is_pointer<deleter_type>::value,
2662 "unique_ptr constructed with null function pointer deleter");
2663 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002664 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665 : __ptr_(pointer())
2666 {
2667 static_assert(!is_pointer<deleter_type>::value,
2668 "unique_ptr constructed with null function pointer deleter");
2669 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002670#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002671 template <class _Pp>
2672 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2673 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 : __ptr_(__p)
2675 {
2676 static_assert(!is_pointer<deleter_type>::value,
2677 "unique_ptr constructed with null function pointer deleter");
2678 }
2679
Logan Chiend435f8b2014-01-31 09:30:46 +00002680 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002681 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 is_reference<deleter_type>::value,
2683 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002684 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2685 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002686 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 : __ptr_(__p, __d) {}
2688
2689 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2690 is_reference<deleter_type>::value,
2691 deleter_type,
2692 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002693 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 : __ptr_(pointer(), __d) {}
2695
Logan Chiend435f8b2014-01-31 09:30:46 +00002696 template <class _Pp>
2697 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2698 typename remove_reference<deleter_type>::type&& __d,
2699 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002700 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002701 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 {
2703 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2704 }
2705
2706 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002707 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002708 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709 {
2710 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2711 }
2712
Howard Hinnant719bda32011-05-28 14:41:13 +00002713 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002714 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715
Howard Hinnant719bda32011-05-28 14:41:13 +00002716 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 {
2718 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002719 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 return *this;
2721 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002722
2723 template <class _Up, class _Ep>
2724 _LIBCPP_INLINE_VISIBILITY
2725 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2726 typename enable_if
2727 <
2728 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002729 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002730 && is_convertible<_Ep, deleter_type>::value &&
2731 (
2732 !is_reference<deleter_type>::value ||
2733 is_same<deleter_type, _Ep>::value
2734 ),
2735 __nat
2736 >::type = __nat()
2737 ) _NOEXCEPT
2738 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2739
2740
2741 template <class _Up, class _Ep>
2742 _LIBCPP_INLINE_VISIBILITY
2743 typename enable_if
2744 <
2745 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002746 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2747 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002748 unique_ptr&
2749 >::type
2750 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2751 {
2752 reset(__u.release());
2753 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2754 return *this;
2755 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002756#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757
2758 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2759 : __ptr_(__p)
2760 {
2761 static_assert(!is_pointer<deleter_type>::value,
2762 "unique_ptr constructed with null function pointer deleter");
2763 }
2764
2765 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002766 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767
2768 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002769 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770
2771 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2772 {
2773 return __rv<unique_ptr>(*this);
2774 }
2775
2776 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002777 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778
2779 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2780 {
2781 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002782 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 return *this;
2784 }
2785
Howard Hinnant74279a52010-09-04 23:28:19 +00002786#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2788
Howard Hinnant719bda32011-05-28 14:41:13 +00002789 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 {
2791 reset();
2792 return *this;
2793 }
2794
2795 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2796 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002797 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2798 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2799 {return __ptr_.second();}
2800 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2801 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002802 _LIBCPP_INLINE_VISIBILITY
2803 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2804 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805
Howard Hinnant719bda32011-05-28 14:41:13 +00002806 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 {
2808 pointer __t = __ptr_.first();
2809 __ptr_.first() = pointer();
2810 return __t;
2811 }
2812
Logan Chiend435f8b2014-01-31 09:30:46 +00002813 template <class _Pp>
2814 _LIBCPP_INLINE_VISIBILITY
2815 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2816 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 {
2818 pointer __tmp = __ptr_.first();
2819 __ptr_.first() = __p;
2820 if (__tmp)
2821 __ptr_.second()(__tmp);
2822 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002823 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 {
2825 pointer __tmp = __ptr_.first();
2826 __ptr_.first() = nullptr;
2827 if (__tmp)
2828 __ptr_.second()(__tmp);
2829 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830
2831 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2832private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002833
Howard Hinnant74279a52010-09-04 23:28:19 +00002834#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 template <class _Up>
2836 explicit unique_ptr(_Up);
2837 template <class _Up>
2838 unique_ptr(_Up __u,
2839 typename conditional<
2840 is_reference<deleter_type>::value,
2841 deleter_type,
2842 typename add_lvalue_reference<const deleter_type>::type>::type,
2843 typename enable_if
2844 <
2845 is_convertible<_Up, pointer>::value,
2846 __nat
2847 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002848#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849};
2850
2851template <class _Tp, class _Dp>
2852inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002853typename enable_if<
2854 __is_swappable<_Dp>::value,
2855 void
2856>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002857swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858
2859template <class _T1, class _D1, class _T2, class _D2>
2860inline _LIBCPP_INLINE_VISIBILITY
2861bool
2862operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2863
2864template <class _T1, class _D1, class _T2, class _D2>
2865inline _LIBCPP_INLINE_VISIBILITY
2866bool
2867operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2868
2869template <class _T1, class _D1, class _T2, class _D2>
2870inline _LIBCPP_INLINE_VISIBILITY
2871bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002872operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2873{
2874 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2875 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002876 typedef typename common_type<_P1, _P2>::type _Vp;
2877 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002878}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879
2880template <class _T1, class _D1, class _T2, class _D2>
2881inline _LIBCPP_INLINE_VISIBILITY
2882bool
2883operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2884
2885template <class _T1, class _D1, class _T2, class _D2>
2886inline _LIBCPP_INLINE_VISIBILITY
2887bool
2888operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2889
2890template <class _T1, class _D1, class _T2, class _D2>
2891inline _LIBCPP_INLINE_VISIBILITY
2892bool
2893operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2894
Howard Hinnantb17caf92012-02-21 21:02:58 +00002895template <class _T1, class _D1>
2896inline _LIBCPP_INLINE_VISIBILITY
2897bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002898operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002899{
2900 return !__x;
2901}
2902
2903template <class _T1, class _D1>
2904inline _LIBCPP_INLINE_VISIBILITY
2905bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002906operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002907{
2908 return !__x;
2909}
2910
2911template <class _T1, class _D1>
2912inline _LIBCPP_INLINE_VISIBILITY
2913bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002914operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002915{
2916 return static_cast<bool>(__x);
2917}
2918
2919template <class _T1, class _D1>
2920inline _LIBCPP_INLINE_VISIBILITY
2921bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002922operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002923{
2924 return static_cast<bool>(__x);
2925}
2926
2927template <class _T1, class _D1>
2928inline _LIBCPP_INLINE_VISIBILITY
2929bool
2930operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2931{
2932 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2933 return less<_P1>()(__x.get(), nullptr);
2934}
2935
2936template <class _T1, class _D1>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
2939operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2940{
2941 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2942 return less<_P1>()(nullptr, __x.get());
2943}
2944
2945template <class _T1, class _D1>
2946inline _LIBCPP_INLINE_VISIBILITY
2947bool
2948operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2949{
2950 return nullptr < __x;
2951}
2952
2953template <class _T1, class _D1>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
2956operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2957{
2958 return __x < nullptr;
2959}
2960
2961template <class _T1, class _D1>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
2964operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2965{
2966 return !(nullptr < __x);
2967}
2968
2969template <class _T1, class _D1>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
2972operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2973{
2974 return !(__x < nullptr);
2975}
2976
2977template <class _T1, class _D1>
2978inline _LIBCPP_INLINE_VISIBILITY
2979bool
2980operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2981{
2982 return !(__x < nullptr);
2983}
2984
2985template <class _T1, class _D1>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
2988operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2989{
2990 return !(nullptr < __x);
2991}
2992
Howard Hinnant9e028f12012-05-01 15:37:54 +00002993#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2994
2995template <class _Tp, class _Dp>
2996inline _LIBCPP_INLINE_VISIBILITY
2997unique_ptr<_Tp, _Dp>
2998move(unique_ptr<_Tp, _Dp>& __t)
2999{
3000 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3001}
3002
3003#endif
3004
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003005#if _LIBCPP_STD_VER > 11
3006
3007template<class _Tp>
3008struct __unique_if
3009{
3010 typedef unique_ptr<_Tp> __unique_single;
3011};
3012
3013template<class _Tp>
3014struct __unique_if<_Tp[]>
3015{
3016 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3017};
3018
3019template<class _Tp, size_t _Np>
3020struct __unique_if<_Tp[_Np]>
3021{
3022 typedef void __unique_array_known_bound;
3023};
3024
3025template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003026inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003027typename __unique_if<_Tp>::__unique_single
3028make_unique(_Args&&... __args)
3029{
3030 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3031}
3032
3033template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003034inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003035typename __unique_if<_Tp>::__unique_array_unknown_bound
3036make_unique(size_t __n)
3037{
3038 typedef typename remove_extent<_Tp>::type _Up;
3039 return unique_ptr<_Tp>(new _Up[__n]());
3040}
3041
3042template<class _Tp, class... _Args>
3043 typename __unique_if<_Tp>::__unique_array_known_bound
3044 make_unique(_Args&&...) = delete;
3045
3046#endif // _LIBCPP_STD_VER > 11
3047
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003048template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003049#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003050struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003051#else
3052struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3053 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3054#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003055{
3056 typedef unique_ptr<_Tp, _Dp> argument_type;
3057 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003058 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003059 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003060 {
3061 typedef typename argument_type::pointer pointer;
3062 return hash<pointer>()(__ptr.get());
3063 }
3064};
3065
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066struct __destruct_n
3067{
3068private:
3069 size_t size;
3070
3071 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003072 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3074
3075 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003076 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077 {}
3078
Howard Hinnant719bda32011-05-28 14:41:13 +00003079 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003081 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082 {}
3083
Howard Hinnant719bda32011-05-28 14:41:13 +00003084 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003086 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 {}
3088public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003089 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3090 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091
3092 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003093 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003094 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095
3096 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003097 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003098 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099
3100 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003101 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003102 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103};
3104
3105template <class _Alloc>
3106class __allocator_destructor
3107{
3108 typedef allocator_traits<_Alloc> __alloc_traits;
3109public:
3110 typedef typename __alloc_traits::pointer pointer;
3111 typedef typename __alloc_traits::size_type size_type;
3112private:
3113 _Alloc& __alloc_;
3114 size_type __s_;
3115public:
3116 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003117 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003120 void operator()(pointer __p) _NOEXCEPT
3121 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122};
3123
3124template <class _InputIterator, class _ForwardIterator>
3125_ForwardIterator
3126uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3127{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003129#ifndef _LIBCPP_NO_EXCEPTIONS
3130 _ForwardIterator __s = __r;
3131 try
3132 {
3133#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003134 for (; __f != __l; ++__f, (void) ++__r)
3135 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003136#ifndef _LIBCPP_NO_EXCEPTIONS
3137 }
3138 catch (...)
3139 {
3140 for (; __s != __r; ++__s)
3141 __s->~value_type();
3142 throw;
3143 }
3144#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145 return __r;
3146}
3147
3148template <class _InputIterator, class _Size, class _ForwardIterator>
3149_ForwardIterator
3150uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3151{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003153#ifndef _LIBCPP_NO_EXCEPTIONS
3154 _ForwardIterator __s = __r;
3155 try
3156 {
3157#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003158 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3159 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003160#ifndef _LIBCPP_NO_EXCEPTIONS
3161 }
3162 catch (...)
3163 {
3164 for (; __s != __r; ++__s)
3165 __s->~value_type();
3166 throw;
3167 }
3168#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169 return __r;
3170}
3171
3172template <class _ForwardIterator, class _Tp>
3173void
3174uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3175{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003177#ifndef _LIBCPP_NO_EXCEPTIONS
3178 _ForwardIterator __s = __f;
3179 try
3180 {
3181#endif
3182 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003183 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003184#ifndef _LIBCPP_NO_EXCEPTIONS
3185 }
3186 catch (...)
3187 {
3188 for (; __s != __f; ++__s)
3189 __s->~value_type();
3190 throw;
3191 }
3192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193}
3194
3195template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003196_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3198{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003200#ifndef _LIBCPP_NO_EXCEPTIONS
3201 _ForwardIterator __s = __f;
3202 try
3203 {
3204#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003205 for (; __n > 0; ++__f, (void) --__n)
3206 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003207#ifndef _LIBCPP_NO_EXCEPTIONS
3208 }
3209 catch (...)
3210 {
3211 for (; __s != __f; ++__s)
3212 __s->~value_type();
3213 throw;
3214 }
3215#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003216 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217}
3218
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003219#if _LIBCPP_STD_VER > 14
3220
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003221template <class _Tp>
3222inline _LIBCPP_INLINE_VISIBILITY
3223void destroy_at(_Tp* __loc) {
3224 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3225 __loc->~_Tp();
3226}
3227
3228template <class _ForwardIterator>
3229inline _LIBCPP_INLINE_VISIBILITY
3230void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3231 for (; __first != __last; ++__first)
3232 _VSTD::destroy_at(_VSTD::addressof(*__first));
3233}
3234
3235template <class _ForwardIterator, class _Size>
3236inline _LIBCPP_INLINE_VISIBILITY
3237_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3238 for (; __n > 0; (void)++__first, --__n)
3239 _VSTD::destroy_at(_VSTD::addressof(*__first));
3240 return __first;
3241}
3242
Eric Fiselier290c07c2016-10-11 21:13:44 +00003243template <class _ForwardIterator>
3244inline _LIBCPP_INLINE_VISIBILITY
3245void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3246 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3247 auto __idx = __first;
3248#ifndef _LIBCPP_NO_EXCEPTIONS
3249 try {
3250#endif
3251 for (; __idx != __last; ++__idx)
3252 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3253#ifndef _LIBCPP_NO_EXCEPTIONS
3254 } catch (...) {
3255 _VSTD::destroy(__first, __idx);
3256 throw;
3257 }
3258#endif
3259}
3260
3261template <class _ForwardIterator, class _Size>
3262inline _LIBCPP_INLINE_VISIBILITY
3263_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3264 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3265 auto __idx = __first;
3266#ifndef _LIBCPP_NO_EXCEPTIONS
3267 try {
3268#endif
3269 for (; __n > 0; (void)++__idx, --__n)
3270 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3271 return __idx;
3272#ifndef _LIBCPP_NO_EXCEPTIONS
3273 } catch (...) {
3274 _VSTD::destroy(__first, __idx);
3275 throw;
3276 }
3277#endif
3278}
3279
3280
3281template <class _ForwardIterator>
3282inline _LIBCPP_INLINE_VISIBILITY
3283void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3284 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3285 auto __idx = __first;
3286#ifndef _LIBCPP_NO_EXCEPTIONS
3287 try {
3288#endif
3289 for (; __idx != __last; ++__idx)
3290 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3291#ifndef _LIBCPP_NO_EXCEPTIONS
3292 } catch (...) {
3293 _VSTD::destroy(__first, __idx);
3294 throw;
3295 }
3296#endif
3297}
3298
3299template <class _ForwardIterator, class _Size>
3300inline _LIBCPP_INLINE_VISIBILITY
3301_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3302 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3303 auto __idx = __first;
3304#ifndef _LIBCPP_NO_EXCEPTIONS
3305 try {
3306#endif
3307 for (; __n > 0; (void)++__idx, --__n)
3308 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3309 return __idx;
3310#ifndef _LIBCPP_NO_EXCEPTIONS
3311 } catch (...) {
3312 _VSTD::destroy(__first, __idx);
3313 throw;
3314 }
3315#endif
3316}
3317
3318
3319template <class _InputIt, class _ForwardIt>
3320inline _LIBCPP_INLINE_VISIBILITY
3321_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3322 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3323 auto __idx = __first_res;
3324#ifndef _LIBCPP_NO_EXCEPTIONS
3325 try {
3326#endif
3327 for (; __first != __last; (void)++__idx, ++__first)
3328 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3329 return __idx;
3330#ifndef _LIBCPP_NO_EXCEPTIONS
3331 } catch (...) {
3332 _VSTD::destroy(__first_res, __idx);
3333 throw;
3334 }
3335#endif
3336}
3337
3338template <class _InputIt, class _Size, class _ForwardIt>
3339inline _LIBCPP_INLINE_VISIBILITY
3340pair<_InputIt, _ForwardIt>
3341uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3342 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3343 auto __idx = __first_res;
3344#ifndef _LIBCPP_NO_EXCEPTIONS
3345 try {
3346#endif
3347 for (; __n > 0; ++__idx, (void)++__first, --__n)
3348 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3349 return {__first, __idx};
3350#ifndef _LIBCPP_NO_EXCEPTIONS
3351 } catch (...) {
3352 _VSTD::destroy(__first_res, __idx);
3353 throw;
3354 }
3355#endif
3356}
3357
3358
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003359#endif // _LIBCPP_STD_VER > 14
3360
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003361// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3362// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003363// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003364#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3365 && defined(__ATOMIC_RELAXED) \
3366 && defined(__ATOMIC_ACQ_REL)
3367# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3368#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3369# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3370#endif
3371
3372template <class _Tp>
3373inline _LIBCPP_INLINE_VISIBILITY _Tp
3374__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3375{
3376#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3377 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3378#else
3379 return __t += 1;
3380#endif
3381}
3382
3383template <class _Tp>
3384inline _LIBCPP_INLINE_VISIBILITY _Tp
3385__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3386{
3387#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3388 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3389#else
3390 return __t -= 1;
3391#endif
3392}
3393
Howard Hinnant756c69b2010-09-22 16:48:34 +00003394class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395 : public std::exception
3396{
3397public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003398 virtual ~bad_weak_ptr() _NOEXCEPT;
3399 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400};
3401
Marshall Clow8fea1612016-08-25 15:09:01 +00003402_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3403void __throw_bad_weak_ptr()
3404{
3405#ifndef _LIBCPP_NO_EXCEPTIONS
3406 throw bad_weak_ptr();
3407#else
3408 _VSTD::abort();
3409#endif
3410}
3411
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003412template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003414class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415{
3416 __shared_count(const __shared_count&);
3417 __shared_count& operator=(const __shared_count&);
3418
3419protected:
3420 long __shared_owners_;
3421 virtual ~__shared_count();
3422private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003423 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424
3425public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003427 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428 : __shared_owners_(__refs) {}
3429
Eric Fiselier98848572017-01-17 03:16:26 +00003430#if defined(_LIBCPP_BUILDING_MEMORY) && \
3431 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003432 void __add_shared() _NOEXCEPT;
3433 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003434#else
3435 _LIBCPP_INLINE_VISIBILITY
3436 void __add_shared() _NOEXCEPT {
3437 __libcpp_atomic_refcount_increment(__shared_owners_);
3438 }
3439 _LIBCPP_INLINE_VISIBILITY
3440 bool __release_shared() _NOEXCEPT {
3441 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3442 __on_zero_shared();
3443 return true;
3444 }
3445 return false;
3446 }
3447#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003448 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003449 long use_count() const _NOEXCEPT {
3450 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3451 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452};
3453
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003454class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455 : private __shared_count
3456{
3457 long __shared_weak_owners_;
3458
3459public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003461 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462 : __shared_count(__refs),
3463 __shared_weak_owners_(__refs) {}
3464protected:
3465 virtual ~__shared_weak_count();
3466
3467public:
Eric Fiselier98848572017-01-17 03:16:26 +00003468#if defined(_LIBCPP_BUILDING_MEMORY) && \
3469 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003470 void __add_shared() _NOEXCEPT;
3471 void __add_weak() _NOEXCEPT;
3472 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003473#else
3474 _LIBCPP_INLINE_VISIBILITY
3475 void __add_shared() _NOEXCEPT {
3476 __shared_count::__add_shared();
3477 }
3478 _LIBCPP_INLINE_VISIBILITY
3479 void __add_weak() _NOEXCEPT {
3480 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3481 }
3482 _LIBCPP_INLINE_VISIBILITY
3483 void __release_shared() _NOEXCEPT {
3484 if (__shared_count::__release_shared())
3485 __release_weak();
3486 }
3487#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003488 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003490 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3491 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492
Howard Hinnant807d6332013-02-25 15:50:36 +00003493 // Define the function out only if we build static libc++ without RTTI.
3494 // Otherwise we may break clients who need to compile their projects with
3495 // -fno-rtti and yet link against a libc++.dylib compiled
3496 // without -fno-rtti.
3497#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003498 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003499#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003501 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003502};
3503
3504template <class _Tp, class _Dp, class _Alloc>
3505class __shared_ptr_pointer
3506 : public __shared_weak_count
3507{
3508 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3509public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003512 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513
Howard Hinnant72f73582010-08-11 17:04:31 +00003514#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003515 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003516#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517
3518private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003519 virtual void __on_zero_shared() _NOEXCEPT;
3520 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521};
3522
Howard Hinnant72f73582010-08-11 17:04:31 +00003523#ifndef _LIBCPP_NO_RTTI
3524
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525template <class _Tp, class _Dp, class _Alloc>
3526const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003527__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003529 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003532#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003533
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534template <class _Tp, class _Dp, class _Alloc>
3535void
Howard Hinnant719bda32011-05-28 14:41:13 +00003536__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537{
3538 __data_.first().second()(__data_.first().first());
3539 __data_.first().second().~_Dp();
3540}
3541
3542template <class _Tp, class _Dp, class _Alloc>
3543void
Howard Hinnant719bda32011-05-28 14:41:13 +00003544__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003546 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3547 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003548 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3549
Eric Fiselierf8898c82015-02-05 23:01:40 +00003550 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003552 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553}
3554
3555template <class _Tp, class _Alloc>
3556class __shared_ptr_emplace
3557 : public __shared_weak_count
3558{
3559 __compressed_pair<_Alloc, _Tp> __data_;
3560public:
3561#ifndef _LIBCPP_HAS_NO_VARIADICS
3562
Howard Hinnant756c69b2010-09-22 16:48:34 +00003563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003565 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566
3567 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003570 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3571 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572
3573#else // _LIBCPP_HAS_NO_VARIADICS
3574
Howard Hinnant756c69b2010-09-22 16:48:34 +00003575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576 __shared_ptr_emplace(_Alloc __a)
3577 : __data_(__a) {}
3578
3579 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3582 : __data_(__a, _Tp(__a0)) {}
3583
3584 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3587 : __data_(__a, _Tp(__a0, __a1)) {}
3588
3589 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3592 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3593
3594#endif // _LIBCPP_HAS_NO_VARIADICS
3595
3596private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003597 virtual void __on_zero_shared() _NOEXCEPT;
3598 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003601 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602};
3603
3604template <class _Tp, class _Alloc>
3605void
Howard Hinnant719bda32011-05-28 14:41:13 +00003606__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607{
3608 __data_.second().~_Tp();
3609}
3610
3611template <class _Tp, class _Alloc>
3612void
Howard Hinnant719bda32011-05-28 14:41:13 +00003613__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003615 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3616 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003617 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003618 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003620 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621}
3622
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003623template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624
3625template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003626class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003628public:
3629 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003630
Eric Fiselierae5b6672016-06-27 01:02:43 +00003631#if _LIBCPP_STD_VER > 14
3632 typedef weak_ptr<_Tp> weak_type;
3633#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634private:
3635 element_type* __ptr_;
3636 __shared_weak_count* __cntrl_;
3637
3638 struct __nat {int __for_bool_;};
3639public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003641 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003643 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003644 template<class _Yp>
3645 explicit shared_ptr(_Yp* __p,
3646 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3647 template<class _Yp, class _Dp>
3648 shared_ptr(_Yp* __p, _Dp __d,
3649 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3650 template<class _Yp, class _Dp, class _Alloc>
3651 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3652 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3654 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003655 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003657 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003661 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003662 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003663#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003665 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003666 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003667 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003668 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003669#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003671 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003672#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003673#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003674 template<class _Yp>
3675 shared_ptr(auto_ptr<_Yp>&& __r,
3676 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003677#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003678 template<class _Yp>
3679 shared_ptr(auto_ptr<_Yp> __r,
3680 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003682#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003683#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003684 template <class _Yp, class _Dp>
3685 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3686 typename enable_if
3687 <
3688 !is_lvalue_reference<_Dp>::value &&
3689 !is_array<_Yp>::value &&
3690 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3691 __nat
3692 >::type = __nat());
3693 template <class _Yp, class _Dp>
3694 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3695 typename enable_if
3696 <
3697 is_lvalue_reference<_Dp>::value &&
3698 !is_array<_Yp>::value &&
3699 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3700 __nat
3701 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003702#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003703 template <class _Yp, class _Dp>
3704 shared_ptr(unique_ptr<_Yp, _Dp>,
3705 typename enable_if
3706 <
3707 !is_lvalue_reference<_Dp>::value &&
3708 !is_array<_Yp>::value &&
3709 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3710 __nat
3711 >::type = __nat());
3712 template <class _Yp, class _Dp>
3713 shared_ptr(unique_ptr<_Yp, _Dp>,
3714 typename enable_if
3715 <
3716 is_lvalue_reference<_Dp>::value &&
3717 !is_array<_Yp>::value &&
3718 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3719 __nat
3720 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003721#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722
3723 ~shared_ptr();
3724
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003726 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003727 template<class _Yp>
3728 typename enable_if
3729 <
3730 is_convertible<_Yp*, element_type*>::value,
3731 shared_ptr&
3732 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003734 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003735#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003737 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003738 template<class _Yp>
3739 typename enable_if
3740 <
3741 is_convertible<_Yp*, element_type*>::value,
3742 shared_ptr<_Tp>&
3743 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003745 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003746#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003747 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003749 typename enable_if
3750 <
3751 !is_array<_Yp>::value &&
3752 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003753 shared_ptr
3754 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003755 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003756#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003757#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003758#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003759 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003761 typename enable_if
3762 <
3763 !is_array<_Yp>::value &&
3764 is_convertible<_Yp*, element_type*>::value,
3765 shared_ptr&
3766 >::type
3767 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003769#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003770 template <class _Yp, class _Dp>
3771 typename enable_if
3772 <
3773 !is_array<_Yp>::value &&
3774 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3775 shared_ptr&
3776 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003777#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003779 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003780#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003782 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783#endif
3784
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003786 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003788 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003789 template<class _Yp>
3790 typename enable_if
3791 <
3792 is_convertible<_Yp*, element_type*>::value,
3793 void
3794 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003796 reset(_Yp* __p);
3797 template<class _Yp, class _Dp>
3798 typename enable_if
3799 <
3800 is_convertible<_Yp*, element_type*>::value,
3801 void
3802 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 reset(_Yp* __p, _Dp __d);
3805 template<class _Yp, class _Dp, class _Alloc>
3806 typename enable_if
3807 <
3808 is_convertible<_Yp*, element_type*>::value,
3809 void
3810 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003812 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813
Howard Hinnant756c69b2010-09-22 16:48:34 +00003814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003815 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003817 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3818 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003820 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003822 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003824 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003826 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003827 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003828 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003829 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003831 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003832 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003833 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003835 _LIBCPP_INLINE_VISIBILITY
3836 bool
3837 __owner_equivalent(const shared_ptr& __p) const
3838 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839
Howard Hinnant72f73582010-08-11 17:04:31 +00003840#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003843 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003845#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846
3847#ifndef _LIBCPP_HAS_NO_VARIADICS
3848
3849 template<class ..._Args>
3850 static
3851 shared_ptr<_Tp>
3852 make_shared(_Args&& ...__args);
3853
3854 template<class _Alloc, class ..._Args>
3855 static
3856 shared_ptr<_Tp>
3857 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3858
3859#else // _LIBCPP_HAS_NO_VARIADICS
3860
3861 static shared_ptr<_Tp> make_shared();
3862
3863 template<class _A0>
3864 static shared_ptr<_Tp> make_shared(_A0&);
3865
3866 template<class _A0, class _A1>
3867 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3868
3869 template<class _A0, class _A1, class _A2>
3870 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3871
3872 template<class _Alloc>
3873 static shared_ptr<_Tp>
3874 allocate_shared(const _Alloc& __a);
3875
3876 template<class _Alloc, class _A0>
3877 static shared_ptr<_Tp>
3878 allocate_shared(const _Alloc& __a, _A0& __a0);
3879
3880 template<class _Alloc, class _A0, class _A1>
3881 static shared_ptr<_Tp>
3882 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3883
3884 template<class _Alloc, class _A0, class _A1, class _A2>
3885 static shared_ptr<_Tp>
3886 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3887
3888#endif // _LIBCPP_HAS_NO_VARIADICS
3889
3890private:
3891
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003892 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003895 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3896 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003898 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003899 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003900 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003901 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3902 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003903 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003904 }
3905
Howard Hinnant756c69b2010-09-22 16:48:34 +00003906 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003907 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003909 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3910 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911};
3912
3913template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003914inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003915_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003916shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 : __ptr_(0),
3918 __cntrl_(0)
3919{
3920}
3921
3922template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003923inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003924_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003925shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 : __ptr_(0),
3927 __cntrl_(0)
3928{
3929}
3930
3931template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003932template<class _Yp>
3933shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3934 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 : __ptr_(__p)
3936{
3937 unique_ptr<_Yp> __hold(__p);
3938 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3939 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3940 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003941 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942}
3943
3944template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003945template<class _Yp, class _Dp>
3946shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3947 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948 : __ptr_(__p)
3949{
3950#ifndef _LIBCPP_NO_EXCEPTIONS
3951 try
3952 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003953#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3955 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003956 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957#ifndef _LIBCPP_NO_EXCEPTIONS
3958 }
3959 catch (...)
3960 {
3961 __d(__p);
3962 throw;
3963 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003964#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965}
3966
3967template<class _Tp>
3968template<class _Dp>
3969shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3970 : __ptr_(0)
3971{
3972#ifndef _LIBCPP_NO_EXCEPTIONS
3973 try
3974 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003975#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3977 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3978#ifndef _LIBCPP_NO_EXCEPTIONS
3979 }
3980 catch (...)
3981 {
3982 __d(__p);
3983 throw;
3984 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003985#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986}
3987
3988template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003989template<class _Yp, class _Dp, class _Alloc>
3990shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3991 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992 : __ptr_(__p)
3993{
3994#ifndef _LIBCPP_NO_EXCEPTIONS
3995 try
3996 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003997#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003999 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000 typedef __allocator_destructor<_A2> _D2;
4001 _A2 __a2(__a);
4002 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004003 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4004 _CntrlBlk(__p, __d, __a);
4005 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004006 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007#ifndef _LIBCPP_NO_EXCEPTIONS
4008 }
4009 catch (...)
4010 {
4011 __d(__p);
4012 throw;
4013 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004014#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015}
4016
4017template<class _Tp>
4018template<class _Dp, class _Alloc>
4019shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4020 : __ptr_(0)
4021{
4022#ifndef _LIBCPP_NO_EXCEPTIONS
4023 try
4024 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004025#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004027 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028 typedef __allocator_destructor<_A2> _D2;
4029 _A2 __a2(__a);
4030 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004031 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4032 _CntrlBlk(__p, __d, __a);
4033 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034#ifndef _LIBCPP_NO_EXCEPTIONS
4035 }
4036 catch (...)
4037 {
4038 __d(__p);
4039 throw;
4040 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004041#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042}
4043
4044template<class _Tp>
4045template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004046inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004047shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048 : __ptr_(__p),
4049 __cntrl_(__r.__cntrl_)
4050{
4051 if (__cntrl_)
4052 __cntrl_->__add_shared();
4053}
4054
4055template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004056inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004057shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058 : __ptr_(__r.__ptr_),
4059 __cntrl_(__r.__cntrl_)
4060{
4061 if (__cntrl_)
4062 __cntrl_->__add_shared();
4063}
4064
4065template<class _Tp>
4066template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004067inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004069 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004070 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 : __ptr_(__r.__ptr_),
4072 __cntrl_(__r.__cntrl_)
4073{
4074 if (__cntrl_)
4075 __cntrl_->__add_shared();
4076}
4077
Howard Hinnant74279a52010-09-04 23:28:19 +00004078#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079
4080template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004081inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004082shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 : __ptr_(__r.__ptr_),
4084 __cntrl_(__r.__cntrl_)
4085{
4086 __r.__ptr_ = 0;
4087 __r.__cntrl_ = 0;
4088}
4089
4090template<class _Tp>
4091template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004092inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004094 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004095 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096 : __ptr_(__r.__ptr_),
4097 __cntrl_(__r.__cntrl_)
4098{
4099 __r.__ptr_ = 0;
4100 __r.__cntrl_ = 0;
4101}
4102
Howard Hinnant74279a52010-09-04 23:28:19 +00004103#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104
Marshall Clowb22274f2017-01-24 22:22:33 +00004105#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004107template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004108#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004109shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004111shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004113 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114 : __ptr_(__r.get())
4115{
4116 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4117 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004118 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119 __r.release();
4120}
Marshall Clowb22274f2017-01-24 22:22:33 +00004121#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122
4123template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004124template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004125#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4127#else
4128shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4129#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004130 typename enable_if
4131 <
4132 !is_lvalue_reference<_Dp>::value &&
4133 !is_array<_Yp>::value &&
4134 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4135 __nat
4136 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137 : __ptr_(__r.get())
4138{
Marshall Clow35cde742015-05-10 13:59:45 +00004139#if _LIBCPP_STD_VER > 11
4140 if (__ptr_ == nullptr)
4141 __cntrl_ = nullptr;
4142 else
4143#endif
4144 {
4145 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4146 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004147 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004148 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149 __r.release();
4150}
4151
4152template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004153template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004154#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4156#else
4157shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4158#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004159 typename enable_if
4160 <
4161 is_lvalue_reference<_Dp>::value &&
4162 !is_array<_Yp>::value &&
4163 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4164 __nat
4165 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166 : __ptr_(__r.get())
4167{
Marshall Clow35cde742015-05-10 13:59:45 +00004168#if _LIBCPP_STD_VER > 11
4169 if (__ptr_ == nullptr)
4170 __cntrl_ = nullptr;
4171 else
4172#endif
4173 {
4174 typedef __shared_ptr_pointer<_Yp*,
4175 reference_wrapper<typename remove_reference<_Dp>::type>,
4176 allocator<_Yp> > _CntrlBlk;
4177 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004178 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004179 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180 __r.release();
4181}
4182
4183#ifndef _LIBCPP_HAS_NO_VARIADICS
4184
4185template<class _Tp>
4186template<class ..._Args>
4187shared_ptr<_Tp>
4188shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4189{
4190 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4191 typedef allocator<_CntrlBlk> _A2;
4192 typedef __allocator_destructor<_A2> _D2;
4193 _A2 __a2;
4194 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004195 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196 shared_ptr<_Tp> __r;
4197 __r.__ptr_ = __hold2.get()->get();
4198 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004199 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200 return __r;
4201}
4202
4203template<class _Tp>
4204template<class _Alloc, class ..._Args>
4205shared_ptr<_Tp>
4206shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4207{
4208 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004209 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210 typedef __allocator_destructor<_A2> _D2;
4211 _A2 __a2(__a);
4212 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004213 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4214 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215 shared_ptr<_Tp> __r;
4216 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004217 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004218 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219 return __r;
4220}
4221
4222#else // _LIBCPP_HAS_NO_VARIADICS
4223
4224template<class _Tp>
4225shared_ptr<_Tp>
4226shared_ptr<_Tp>::make_shared()
4227{
4228 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4229 typedef allocator<_CntrlBlk> _Alloc2;
4230 typedef __allocator_destructor<_Alloc2> _D2;
4231 _Alloc2 __alloc2;
4232 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4233 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4234 shared_ptr<_Tp> __r;
4235 __r.__ptr_ = __hold2.get()->get();
4236 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004237 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238 return __r;
4239}
4240
4241template<class _Tp>
4242template<class _A0>
4243shared_ptr<_Tp>
4244shared_ptr<_Tp>::make_shared(_A0& __a0)
4245{
4246 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4247 typedef allocator<_CntrlBlk> _Alloc2;
4248 typedef __allocator_destructor<_Alloc2> _D2;
4249 _Alloc2 __alloc2;
4250 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4251 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4252 shared_ptr<_Tp> __r;
4253 __r.__ptr_ = __hold2.get()->get();
4254 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004255 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256 return __r;
4257}
4258
4259template<class _Tp>
4260template<class _A0, class _A1>
4261shared_ptr<_Tp>
4262shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4263{
4264 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4265 typedef allocator<_CntrlBlk> _Alloc2;
4266 typedef __allocator_destructor<_Alloc2> _D2;
4267 _Alloc2 __alloc2;
4268 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4269 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4270 shared_ptr<_Tp> __r;
4271 __r.__ptr_ = __hold2.get()->get();
4272 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004273 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274 return __r;
4275}
4276
4277template<class _Tp>
4278template<class _A0, class _A1, class _A2>
4279shared_ptr<_Tp>
4280shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4281{
4282 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4283 typedef allocator<_CntrlBlk> _Alloc2;
4284 typedef __allocator_destructor<_Alloc2> _D2;
4285 _Alloc2 __alloc2;
4286 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4287 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4288 shared_ptr<_Tp> __r;
4289 __r.__ptr_ = __hold2.get()->get();
4290 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004291 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 return __r;
4293}
4294
4295template<class _Tp>
4296template<class _Alloc>
4297shared_ptr<_Tp>
4298shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4299{
4300 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004301 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302 typedef __allocator_destructor<_Alloc2> _D2;
4303 _Alloc2 __alloc2(__a);
4304 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004305 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4306 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 shared_ptr<_Tp> __r;
4308 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004309 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004310 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311 return __r;
4312}
4313
4314template<class _Tp>
4315template<class _Alloc, class _A0>
4316shared_ptr<_Tp>
4317shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4318{
4319 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004320 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321 typedef __allocator_destructor<_Alloc2> _D2;
4322 _Alloc2 __alloc2(__a);
4323 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004324 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4325 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326 shared_ptr<_Tp> __r;
4327 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004328 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004329 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330 return __r;
4331}
4332
4333template<class _Tp>
4334template<class _Alloc, class _A0, class _A1>
4335shared_ptr<_Tp>
4336shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4337{
4338 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004339 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340 typedef __allocator_destructor<_Alloc2> _D2;
4341 _Alloc2 __alloc2(__a);
4342 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004343 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4344 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345 shared_ptr<_Tp> __r;
4346 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004347 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004348 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349 return __r;
4350}
4351
4352template<class _Tp>
4353template<class _Alloc, class _A0, class _A1, class _A2>
4354shared_ptr<_Tp>
4355shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4356{
4357 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004358 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359 typedef __allocator_destructor<_Alloc2> _D2;
4360 _Alloc2 __alloc2(__a);
4361 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004362 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4363 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364 shared_ptr<_Tp> __r;
4365 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004366 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004367 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368 return __r;
4369}
4370
4371#endif // _LIBCPP_HAS_NO_VARIADICS
4372
4373template<class _Tp>
4374shared_ptr<_Tp>::~shared_ptr()
4375{
4376 if (__cntrl_)
4377 __cntrl_->__release_shared();
4378}
4379
4380template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004381inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004383shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384{
4385 shared_ptr(__r).swap(*this);
4386 return *this;
4387}
4388
4389template<class _Tp>
4390template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004391inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004392typename enable_if
4393<
Marshall Clow7e384b72017-01-10 16:59:33 +00004394 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004395 shared_ptr<_Tp>&
4396>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004397shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398{
4399 shared_ptr(__r).swap(*this);
4400 return *this;
4401}
4402
Howard Hinnant74279a52010-09-04 23:28:19 +00004403#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404
4405template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004406inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004408shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004409{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004410 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411 return *this;
4412}
4413
4414template<class _Tp>
4415template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004416inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004417typename enable_if
4418<
Marshall Clow7e384b72017-01-10 16:59:33 +00004419 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004420 shared_ptr<_Tp>&
4421>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4423{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004424 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425 return *this;
4426}
4427
Marshall Clowb22274f2017-01-24 22:22:33 +00004428#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429template<class _Tp>
4430template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004431inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004432typename enable_if
4433<
4434 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004435 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004436 shared_ptr<_Tp>
4437>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4439{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004440 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441 return *this;
4442}
Marshall Clowb22274f2017-01-24 22:22:33 +00004443#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444
4445template<class _Tp>
4446template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004447inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004448typename enable_if
4449<
4450 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004451 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4452 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004453 shared_ptr<_Tp>&
4454>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4456{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004457 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458 return *this;
4459}
4460
Howard Hinnant74279a52010-09-04 23:28:19 +00004461#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462
Marshall Clowb22274f2017-01-24 22:22:33 +00004463#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004464template<class _Tp>
4465template<class _Yp>
4466inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004467typename enable_if
4468<
4469 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004470 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004471 shared_ptr<_Tp>&
4472>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004473shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474{
4475 shared_ptr(__r).swap(*this);
4476 return *this;
4477}
Marshall Clowb22274f2017-01-24 22:22:33 +00004478#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479
4480template<class _Tp>
4481template <class _Yp, class _Dp>
4482inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004483typename enable_if
4484<
4485 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004486 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4487 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004488 shared_ptr<_Tp>&
4489>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4491{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004492 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004493 return *this;
4494}
4495
Howard Hinnant74279a52010-09-04 23:28:19 +00004496#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497
4498template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004499inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500void
Howard Hinnant719bda32011-05-28 14:41:13 +00004501shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004503 _VSTD::swap(__ptr_, __r.__ptr_);
4504 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004505}
4506
4507template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004508inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509void
Howard Hinnant719bda32011-05-28 14:41:13 +00004510shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511{
4512 shared_ptr().swap(*this);
4513}
4514
4515template<class _Tp>
4516template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004517inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004518typename enable_if
4519<
Marshall Clow7e384b72017-01-10 16:59:33 +00004520 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004521 void
4522>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523shared_ptr<_Tp>::reset(_Yp* __p)
4524{
4525 shared_ptr(__p).swap(*this);
4526}
4527
4528template<class _Tp>
4529template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004530inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004531typename enable_if
4532<
Marshall Clow7e384b72017-01-10 16:59:33 +00004533 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004534 void
4535>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4537{
4538 shared_ptr(__p, __d).swap(*this);
4539}
4540
4541template<class _Tp>
4542template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004543inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004544typename enable_if
4545<
Marshall Clow7e384b72017-01-10 16:59:33 +00004546 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004547 void
4548>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4550{
4551 shared_ptr(__p, __d, __a).swap(*this);
4552}
4553
4554#ifndef _LIBCPP_HAS_NO_VARIADICS
4555
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004556template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004558typename enable_if
4559<
4560 !is_array<_Tp>::value,
4561 shared_ptr<_Tp>
4562>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004563make_shared(_Args&& ...__args)
4564{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004565 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566}
4567
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004568template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004569inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004570typename enable_if
4571<
4572 !is_array<_Tp>::value,
4573 shared_ptr<_Tp>
4574>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004575allocate_shared(const _Alloc& __a, _Args&& ...__args)
4576{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004577 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578}
4579
4580#else // _LIBCPP_HAS_NO_VARIADICS
4581
4582template<class _Tp>
4583inline _LIBCPP_INLINE_VISIBILITY
4584shared_ptr<_Tp>
4585make_shared()
4586{
4587 return shared_ptr<_Tp>::make_shared();
4588}
4589
4590template<class _Tp, class _A0>
4591inline _LIBCPP_INLINE_VISIBILITY
4592shared_ptr<_Tp>
4593make_shared(_A0& __a0)
4594{
4595 return shared_ptr<_Tp>::make_shared(__a0);
4596}
4597
4598template<class _Tp, class _A0, class _A1>
4599inline _LIBCPP_INLINE_VISIBILITY
4600shared_ptr<_Tp>
4601make_shared(_A0& __a0, _A1& __a1)
4602{
4603 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4604}
4605
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004606template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004607inline _LIBCPP_INLINE_VISIBILITY
4608shared_ptr<_Tp>
4609make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4610{
4611 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4612}
4613
4614template<class _Tp, class _Alloc>
4615inline _LIBCPP_INLINE_VISIBILITY
4616shared_ptr<_Tp>
4617allocate_shared(const _Alloc& __a)
4618{
4619 return shared_ptr<_Tp>::allocate_shared(__a);
4620}
4621
4622template<class _Tp, class _Alloc, class _A0>
4623inline _LIBCPP_INLINE_VISIBILITY
4624shared_ptr<_Tp>
4625allocate_shared(const _Alloc& __a, _A0& __a0)
4626{
4627 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4628}
4629
4630template<class _Tp, class _Alloc, class _A0, class _A1>
4631inline _LIBCPP_INLINE_VISIBILITY
4632shared_ptr<_Tp>
4633allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4634{
4635 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4636}
4637
4638template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4639inline _LIBCPP_INLINE_VISIBILITY
4640shared_ptr<_Tp>
4641allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4642{
4643 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4644}
4645
4646#endif // _LIBCPP_HAS_NO_VARIADICS
4647
4648template<class _Tp, class _Up>
4649inline _LIBCPP_INLINE_VISIBILITY
4650bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004651operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004652{
4653 return __x.get() == __y.get();
4654}
4655
4656template<class _Tp, class _Up>
4657inline _LIBCPP_INLINE_VISIBILITY
4658bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004659operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004660{
4661 return !(__x == __y);
4662}
4663
4664template<class _Tp, class _Up>
4665inline _LIBCPP_INLINE_VISIBILITY
4666bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004667operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004668{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004669 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4670 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004671}
4672
4673template<class _Tp, class _Up>
4674inline _LIBCPP_INLINE_VISIBILITY
4675bool
4676operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4677{
4678 return __y < __x;
4679}
4680
4681template<class _Tp, class _Up>
4682inline _LIBCPP_INLINE_VISIBILITY
4683bool
4684operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4685{
4686 return !(__y < __x);
4687}
4688
4689template<class _Tp, class _Up>
4690inline _LIBCPP_INLINE_VISIBILITY
4691bool
4692operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4693{
4694 return !(__x < __y);
4695}
4696
4697template<class _Tp>
4698inline _LIBCPP_INLINE_VISIBILITY
4699bool
4700operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4701{
4702 return !__x;
4703}
4704
4705template<class _Tp>
4706inline _LIBCPP_INLINE_VISIBILITY
4707bool
4708operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4709{
4710 return !__x;
4711}
4712
4713template<class _Tp>
4714inline _LIBCPP_INLINE_VISIBILITY
4715bool
4716operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4717{
4718 return static_cast<bool>(__x);
4719}
4720
4721template<class _Tp>
4722inline _LIBCPP_INLINE_VISIBILITY
4723bool
4724operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4725{
4726 return static_cast<bool>(__x);
4727}
4728
4729template<class _Tp>
4730inline _LIBCPP_INLINE_VISIBILITY
4731bool
4732operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4733{
4734 return less<_Tp*>()(__x.get(), nullptr);
4735}
4736
4737template<class _Tp>
4738inline _LIBCPP_INLINE_VISIBILITY
4739bool
4740operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4741{
4742 return less<_Tp*>()(nullptr, __x.get());
4743}
4744
4745template<class _Tp>
4746inline _LIBCPP_INLINE_VISIBILITY
4747bool
4748operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4749{
4750 return nullptr < __x;
4751}
4752
4753template<class _Tp>
4754inline _LIBCPP_INLINE_VISIBILITY
4755bool
4756operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4757{
4758 return __x < nullptr;
4759}
4760
4761template<class _Tp>
4762inline _LIBCPP_INLINE_VISIBILITY
4763bool
4764operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4765{
4766 return !(nullptr < __x);
4767}
4768
4769template<class _Tp>
4770inline _LIBCPP_INLINE_VISIBILITY
4771bool
4772operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4773{
4774 return !(__x < nullptr);
4775}
4776
4777template<class _Tp>
4778inline _LIBCPP_INLINE_VISIBILITY
4779bool
4780operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4781{
4782 return !(__x < nullptr);
4783}
4784
4785template<class _Tp>
4786inline _LIBCPP_INLINE_VISIBILITY
4787bool
4788operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4789{
4790 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004791}
4792
4793template<class _Tp>
4794inline _LIBCPP_INLINE_VISIBILITY
4795void
Howard Hinnant719bda32011-05-28 14:41:13 +00004796swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004797{
4798 __x.swap(__y);
4799}
4800
4801template<class _Tp, class _Up>
4802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004803typename enable_if
4804<
4805 !is_array<_Tp>::value && !is_array<_Up>::value,
4806 shared_ptr<_Tp>
4807>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004808static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004809{
4810 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4811}
4812
4813template<class _Tp, class _Up>
4814inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004815typename enable_if
4816<
4817 !is_array<_Tp>::value && !is_array<_Up>::value,
4818 shared_ptr<_Tp>
4819>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004820dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004821{
4822 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4823 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4824}
4825
4826template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004827typename enable_if
4828<
4829 is_array<_Tp>::value == is_array<_Up>::value,
4830 shared_ptr<_Tp>
4831>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004832const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004833{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004834 typedef typename remove_extent<_Tp>::type _RTp;
4835 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004836}
4837
Howard Hinnant72f73582010-08-11 17:04:31 +00004838#ifndef _LIBCPP_NO_RTTI
4839
Howard Hinnantc51e1022010-05-11 19:42:16 +00004840template<class _Dp, class _Tp>
4841inline _LIBCPP_INLINE_VISIBILITY
4842_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004843get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004844{
4845 return __p.template __get_deleter<_Dp>();
4846}
4847
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004848#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004849
Howard Hinnantc51e1022010-05-11 19:42:16 +00004850template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004851class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004852{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004853public:
4854 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004855private:
4856 element_type* __ptr_;
4857 __shared_weak_count* __cntrl_;
4858
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004859public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004861 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004862 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004863 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4864 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004866 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004867 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004868 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4869 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004870
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004871#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004873 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004874 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004875 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4876 _NOEXCEPT;
4877#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004878 ~weak_ptr();
4879
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004881 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004882 template<class _Yp>
4883 typename enable_if
4884 <
4885 is_convertible<_Yp*, element_type*>::value,
4886 weak_ptr&
4887 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004889 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4890
4891#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4892
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004894 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4895 template<class _Yp>
4896 typename enable_if
4897 <
4898 is_convertible<_Yp*, element_type*>::value,
4899 weak_ptr&
4900 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004902 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4903
4904#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4905
4906 template<class _Yp>
4907 typename enable_if
4908 <
4909 is_convertible<_Yp*, element_type*>::value,
4910 weak_ptr&
4911 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004913 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004914
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004916 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004918 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004919
Howard Hinnant756c69b2010-09-22 16:48:34 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004921 long use_count() const _NOEXCEPT
4922 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004924 bool expired() const _NOEXCEPT
4925 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4926 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004927 template<class _Up>
4928 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004929 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004930 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004931 template<class _Up>
4932 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004933 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004934 {return __cntrl_ < __r.__cntrl_;}
4935
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004936 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4937 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004938};
4939
4940template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004941inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004942_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004943weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004944 : __ptr_(0),
4945 __cntrl_(0)
4946{
4947}
4948
4949template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004950inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004951weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004952 : __ptr_(__r.__ptr_),
4953 __cntrl_(__r.__cntrl_)
4954{
4955 if (__cntrl_)
4956 __cntrl_->__add_weak();
4957}
4958
4959template<class _Tp>
4960template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004962weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004963 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004964 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004965 : __ptr_(__r.__ptr_),
4966 __cntrl_(__r.__cntrl_)
4967{
4968 if (__cntrl_)
4969 __cntrl_->__add_weak();
4970}
4971
4972template<class _Tp>
4973template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004974inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004976 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004977 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004978 : __ptr_(__r.__ptr_),
4979 __cntrl_(__r.__cntrl_)
4980{
4981 if (__cntrl_)
4982 __cntrl_->__add_weak();
4983}
4984
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004985#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4986
4987template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004988inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004989weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4990 : __ptr_(__r.__ptr_),
4991 __cntrl_(__r.__cntrl_)
4992{
4993 __r.__ptr_ = 0;
4994 __r.__cntrl_ = 0;
4995}
4996
4997template<class _Tp>
4998template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004999inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005000weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5001 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5002 _NOEXCEPT
5003 : __ptr_(__r.__ptr_),
5004 __cntrl_(__r.__cntrl_)
5005{
5006 __r.__ptr_ = 0;
5007 __r.__cntrl_ = 0;
5008}
5009
5010#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5011
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012template<class _Tp>
5013weak_ptr<_Tp>::~weak_ptr()
5014{
5015 if (__cntrl_)
5016 __cntrl_->__release_weak();
5017}
5018
5019template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005020inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005021weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005022weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005023{
5024 weak_ptr(__r).swap(*this);
5025 return *this;
5026}
5027
5028template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005029template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005030inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005031typename enable_if
5032<
5033 is_convertible<_Yp*, _Tp*>::value,
5034 weak_ptr<_Tp>&
5035>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005036weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005037{
5038 weak_ptr(__r).swap(*this);
5039 return *this;
5040}
5041
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005042#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5043
5044template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005045inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005046weak_ptr<_Tp>&
5047weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5048{
5049 weak_ptr(_VSTD::move(__r)).swap(*this);
5050 return *this;
5051}
5052
Howard Hinnantc51e1022010-05-11 19:42:16 +00005053template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005054template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005055inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005056typename enable_if
5057<
5058 is_convertible<_Yp*, _Tp*>::value,
5059 weak_ptr<_Tp>&
5060>::type
5061weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5062{
5063 weak_ptr(_VSTD::move(__r)).swap(*this);
5064 return *this;
5065}
5066
5067#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5068
5069template<class _Tp>
5070template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005071inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005072typename enable_if
5073<
5074 is_convertible<_Yp*, _Tp*>::value,
5075 weak_ptr<_Tp>&
5076>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005077weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005078{
5079 weak_ptr(__r).swap(*this);
5080 return *this;
5081}
5082
5083template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005084inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005085void
Howard Hinnant719bda32011-05-28 14:41:13 +00005086weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005087{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005088 _VSTD::swap(__ptr_, __r.__ptr_);
5089 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005090}
5091
5092template<class _Tp>
5093inline _LIBCPP_INLINE_VISIBILITY
5094void
Howard Hinnant719bda32011-05-28 14:41:13 +00005095swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005096{
5097 __x.swap(__y);
5098}
5099
5100template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005101inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005102void
Howard Hinnant719bda32011-05-28 14:41:13 +00005103weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005104{
5105 weak_ptr().swap(*this);
5106}
5107
5108template<class _Tp>
5109template<class _Yp>
5110shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005111 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005112 : __ptr_(__r.__ptr_),
5113 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5114{
5115 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005116 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005117}
5118
5119template<class _Tp>
5120shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005121weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005122{
5123 shared_ptr<_Tp> __r;
5124 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5125 if (__r.__cntrl_)
5126 __r.__ptr_ = __ptr_;
5127 return __r;
5128}
5129
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005130#if _LIBCPP_STD_VER > 14
5131template <class _Tp = void> struct owner_less;
5132#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005133template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005134#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135
5136template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005137struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005138 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005139{
5140 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005141 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005142 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005143 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005144 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005145 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005146 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005147 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005148 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005149 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005150};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005151
5152template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005153struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005154 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5155{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005156 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005157 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005158 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005159 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005160 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005161 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005162 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005163 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005164 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005165 {return __x.owner_before(__y);}
5166};
5167
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005168#if _LIBCPP_STD_VER > 14
5169template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005170struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005171{
5172 template <class _Tp, class _Up>
5173 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005174 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005175 {return __x.owner_before(__y);}
5176 template <class _Tp, class _Up>
5177 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005178 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005179 {return __x.owner_before(__y);}
5180 template <class _Tp, class _Up>
5181 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005182 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005183 {return __x.owner_before(__y);}
5184 template <class _Tp, class _Up>
5185 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005186 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005187 {return __x.owner_before(__y);}
5188 typedef void is_transparent;
5189};
5190#endif
5191
Howard Hinnantc51e1022010-05-11 19:42:16 +00005192template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005193class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005194{
5195 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005196protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005198 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005200 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005202 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5203 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005205 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005206public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005208 shared_ptr<_Tp> shared_from_this()
5209 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005211 shared_ptr<_Tp const> shared_from_this() const
5212 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005213
Eric Fiselier84006862016-06-02 00:15:35 +00005214#if _LIBCPP_STD_VER > 14
5215 _LIBCPP_INLINE_VISIBILITY
5216 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5217 { return __weak_this_; }
5218
5219 _LIBCPP_INLINE_VISIBILITY
5220 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5221 { return __weak_this_; }
5222#endif // _LIBCPP_STD_VER > 14
5223
Howard Hinnantc51e1022010-05-11 19:42:16 +00005224 template <class _Up> friend class shared_ptr;
5225};
5226
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005227template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005228struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005229{
5230 typedef shared_ptr<_Tp> argument_type;
5231 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005232
Howard Hinnant756c69b2010-09-22 16:48:34 +00005233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005234 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005235 {
5236 return hash<_Tp*>()(__ptr.get());
5237 }
5238};
5239
Howard Hinnantc834c512011-11-29 18:15:50 +00005240template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005241inline _LIBCPP_INLINE_VISIBILITY
5242basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005243operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005244
Eric Fiselier9b492672016-06-18 02:12:53 +00005245
5246#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005247
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005248class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005249{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005250 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005251public:
5252 void lock() _NOEXCEPT;
5253 void unlock() _NOEXCEPT;
5254
5255private:
5256 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5257 __sp_mut(const __sp_mut&);
5258 __sp_mut& operator=(const __sp_mut&);
5259
Howard Hinnant8331b762013-03-06 23:30:19 +00005260 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005261};
5262
Howard Hinnant8331b762013-03-06 23:30:19 +00005263_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005264
5265template <class _Tp>
5266inline _LIBCPP_INLINE_VISIBILITY
5267bool
5268atomic_is_lock_free(const shared_ptr<_Tp>*)
5269{
5270 return false;
5271}
5272
5273template <class _Tp>
5274shared_ptr<_Tp>
5275atomic_load(const shared_ptr<_Tp>* __p)
5276{
5277 __sp_mut& __m = __get_sp_mut(__p);
5278 __m.lock();
5279 shared_ptr<_Tp> __q = *__p;
5280 __m.unlock();
5281 return __q;
5282}
5283
5284template <class _Tp>
5285inline _LIBCPP_INLINE_VISIBILITY
5286shared_ptr<_Tp>
5287atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5288{
5289 return atomic_load(__p);
5290}
5291
5292template <class _Tp>
5293void
5294atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5295{
5296 __sp_mut& __m = __get_sp_mut(__p);
5297 __m.lock();
5298 __p->swap(__r);
5299 __m.unlock();
5300}
5301
5302template <class _Tp>
5303inline _LIBCPP_INLINE_VISIBILITY
5304void
5305atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5306{
5307 atomic_store(__p, __r);
5308}
5309
5310template <class _Tp>
5311shared_ptr<_Tp>
5312atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5313{
5314 __sp_mut& __m = __get_sp_mut(__p);
5315 __m.lock();
5316 __p->swap(__r);
5317 __m.unlock();
5318 return __r;
5319}
5320
5321template <class _Tp>
5322inline _LIBCPP_INLINE_VISIBILITY
5323shared_ptr<_Tp>
5324atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5325{
5326 return atomic_exchange(__p, __r);
5327}
5328
5329template <class _Tp>
5330bool
5331atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5332{
Marshall Clow4201ee82016-05-18 17:50:13 +00005333 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005334 __sp_mut& __m = __get_sp_mut(__p);
5335 __m.lock();
5336 if (__p->__owner_equivalent(*__v))
5337 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005338 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005339 *__p = __w;
5340 __m.unlock();
5341 return true;
5342 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005343 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005344 *__v = *__p;
5345 __m.unlock();
5346 return false;
5347}
5348
5349template <class _Tp>
5350inline _LIBCPP_INLINE_VISIBILITY
5351bool
5352atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5353{
5354 return atomic_compare_exchange_strong(__p, __v, __w);
5355}
5356
5357template <class _Tp>
5358inline _LIBCPP_INLINE_VISIBILITY
5359bool
5360atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5361 shared_ptr<_Tp> __w, memory_order, memory_order)
5362{
5363 return atomic_compare_exchange_strong(__p, __v, __w);
5364}
5365
5366template <class _Tp>
5367inline _LIBCPP_INLINE_VISIBILITY
5368bool
5369atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5370 shared_ptr<_Tp> __w, memory_order, memory_order)
5371{
5372 return atomic_compare_exchange_weak(__p, __v, __w);
5373}
5374
Eric Fiselier9b492672016-06-18 02:12:53 +00005375#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005376
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005377//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005378#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5379# ifndef _LIBCPP_CXX03_LANG
5380enum class pointer_safety : unsigned char {
5381 relaxed,
5382 preferred,
5383 strict
5384};
5385# endif
5386#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005387struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005388{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005389 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005390 {
5391 relaxed,
5392 preferred,
5393 strict
5394 };
5395
Howard Hinnant49e145e2012-10-30 19:06:59 +00005396 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005397
Howard Hinnant756c69b2010-09-22 16:48:34 +00005398 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005399 pointer_safety() : __v_() {}
5400
5401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005402 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005404 operator int() const {return __v_;}
5405};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005406#endif
5407
5408#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5409 defined(_LIBCPP_BUILDING_MEMORY)
5410_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5411#else
5412// This function is only offered in C++03 under ABI v1.
5413# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5414inline _LIBCPP_INLINE_VISIBILITY
5415pointer_safety get_pointer_safety() _NOEXCEPT {
5416 return pointer_safety::relaxed;
5417}
5418# endif
5419#endif
5420
Howard Hinnantc51e1022010-05-11 19:42:16 +00005421
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005422_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5423_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5424_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005425_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005426
5427template <class _Tp>
5428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005429_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005430undeclare_reachable(_Tp* __p)
5431{
5432 return static_cast<_Tp*>(__undeclare_reachable(__p));
5433}
5434
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005435_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005436
Marshall Clow8982dcd2015-07-13 20:04:56 +00005437// --- Helper for container swap --
5438template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005439inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005440void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5441#if _LIBCPP_STD_VER >= 14
5442 _NOEXCEPT
5443#else
5444 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5445#endif
5446{
5447 __swap_allocator(__a1, __a2,
5448 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5449}
5450
5451template <typename _Alloc>
5452_LIBCPP_INLINE_VISIBILITY
5453void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5454#if _LIBCPP_STD_VER >= 14
5455 _NOEXCEPT
5456#else
5457 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5458#endif
5459{
5460 using _VSTD::swap;
5461 swap(__a1, __a2);
5462}
5463
5464template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005465inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005466void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5467
Marshall Clowff91de82015-08-18 19:51:37 +00005468template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005469struct __noexcept_move_assign_container : public integral_constant<bool,
5470 _Traits::propagate_on_container_move_assignment::value
5471#if _LIBCPP_STD_VER > 14
5472 || _Traits::is_always_equal::value
5473#else
5474 && is_nothrow_move_assignable<_Alloc>::value
5475#endif
5476 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005477
Marshall Clowa591b9a2016-07-11 21:38:08 +00005478
5479#ifndef _LIBCPP_HAS_NO_VARIADICS
5480template <class _Tp, class _Alloc>
5481struct __temp_value {
5482 typedef allocator_traits<_Alloc> _Traits;
5483
5484 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5485 _Alloc &__a;
5486
5487 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5488 _Tp & get() { return *__addr(); }
5489
5490 template<class... _Args>
5491 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5492 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5493
5494 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5495 };
5496#endif
5497
Howard Hinnantc51e1022010-05-11 19:42:16 +00005498_LIBCPP_END_NAMESPACE_STD
5499
5500#endif // _LIBCPP_MEMORY