blob: 729b8b5c874ecfdc1e04561d25c5755084d8be10 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
50
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Marshall Clow0e58cae2017-11-26 02:55:38 +000086 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant719bda32011-05-28 14:41:13 +000089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args);
93
94 template <class T>
95 static void destroy(allocator_type& a, T* p);
96
Marshall Clow4f834b52013-08-27 20:22:15 +000097 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 static allocator_type
100 select_on_container_copy_construction(const allocator_type& a);
101};
102
103template <>
Michael Park99f9e912020-03-04 11:27:14 -0500104class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
106public:
107 typedef void* pointer;
108 typedef const void* const_pointer;
109 typedef void value_type;
110
111 template <class _Up> struct rebind {typedef allocator<_Up> other;};
112};
113
114template <class T>
115class allocator
116{
117public:
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef size_t size_type; // deprecated in C++17, removed in C++20
119 typedef ptrdiff_t difference_type; // deprecated in C++17, removed in C++20
120 typedef T* pointer; // deprecated in C++17, removed in C++20
121 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<T>::type
123 reference; // deprecated in C++17, removed in C++20
124 typedef typename add_lvalue_reference<const T>::type
125 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Michael Park99f9e912020-03-04 11:27:14 -0500127 typedef T value_type;
128
129 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
130
131 typedef true_type propagate_on_container_move_assignment;
132 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Marshall Clowbc759762018-03-20 23:02:53 +0000134 constexpr allocator() noexcept; // constexpr in C++20
135 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
136 template <class U>
137 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000138 ~allocator();
Michael Park99f9e912020-03-04 11:27:14 -0500139 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
140 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
141 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
142 T* allocate(size_t n);
143 void deallocate(T* p, size_t n) noexcept;
144 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000147 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500148 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149};
150
151template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000152bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
154template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000155bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
157template <class OutputIterator, class T>
158class raw_storage_iterator
159 : public iterator<output_iterator_tag,
160 T, // purposefully not C++03
161 ptrdiff_t, // purposefully not C++03
162 T*, // purposefully not C++03
163 raw_storage_iterator&> // purposefully not C++03
164{
165public:
166 explicit raw_storage_iterator(OutputIterator x);
167 raw_storage_iterator& operator*();
168 raw_storage_iterator& operator=(const T& element);
169 raw_storage_iterator& operator++();
170 raw_storage_iterator operator++(int);
171};
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
174template <class T> void return_temporary_buffer(T* p) noexcept;
175
176template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000177template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179template <class InputIterator, class ForwardIterator>
180ForwardIterator
181uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
182
Howard Hinnant719bda32011-05-28 14:41:13 +0000183template <class InputIterator, class Size, class ForwardIterator>
184ForwardIterator
185uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
186
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187template <class ForwardIterator, class T>
188void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
189
190template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000191ForwardIterator
192uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000194template <class T>
195void destroy_at(T* location);
196
197template <class ForwardIterator>
198 void destroy(ForwardIterator first, ForwardIterator last);
199
200template <class ForwardIterator, class Size>
201 ForwardIterator destroy_n(ForwardIterator first, Size n);
202
203template <class InputIterator, class ForwardIterator>
204 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
205
206template <class InputIterator, class Size, class ForwardIterator>
207 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
208
209template <class ForwardIterator>
210 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
214
215template <class ForwardIterator>
216 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
217
218template <class ForwardIterator, class Size>
219 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
220
Louis Dionne481a2662018-09-23 18:35:00 +0000221template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222
223template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000224class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225{
226public:
227 typedef X element_type;
228
229 explicit auto_ptr(X* p =0) throw();
230 auto_ptr(auto_ptr&) throw();
231 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
232 auto_ptr& operator=(auto_ptr&) throw();
233 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
234 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
235 ~auto_ptr() throw();
236
237 typename add_lvalue_reference<X>::type operator*() const throw();
238 X* operator->() const throw();
239 X* get() const throw();
240 X* release() throw();
241 void reset(X* p =0) throw();
242
243 auto_ptr(auto_ptr_ref<X>) throw();
244 template<class Y> operator auto_ptr_ref<Y>() throw();
245 template<class Y> operator auto_ptr<Y>() throw();
246};
247
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248template <class T>
249struct default_delete
250{
Howard Hinnant719bda32011-05-28 14:41:13 +0000251 constexpr default_delete() noexcept = default;
252 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000253
Howard Hinnant719bda32011-05-28 14:41:13 +0000254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255};
256
257template <class T>
258struct default_delete<T[]>
259{
Howard Hinnant719bda32011-05-28 14:41:13 +0000260 constexpr default_delete() noexcept = default;
261 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000262 template <class U> void operator()(U*) const = delete;
263};
264
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000265template <class T, class D = default_delete<T>>
266class unique_ptr
267{
268public:
269 typedef see below pointer;
270 typedef T element_type;
271 typedef D deleter_type;
272
273 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 constexpr unique_ptr() noexcept;
275 explicit unique_ptr(pointer p) noexcept;
276 unique_ptr(pointer p, see below d1) noexcept;
277 unique_ptr(pointer p, see below d2) noexcept;
278 unique_ptr(unique_ptr&& u) noexcept;
279 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000280 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000281 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000283 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284
285 // destructor
286 ~unique_ptr();
287
288 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
291 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000292
293 // observers
294 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer operator->() const noexcept;
296 pointer get() const noexcept;
297 deleter_type& get_deleter() noexcept;
298 const deleter_type& get_deleter() const noexcept;
299 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000300
301 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000302 pointer release() noexcept;
303 void reset(pointer p = pointer()) noexcept;
304 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
308class unique_ptr<T[], D>
309{
310public:
311 typedef implementation-defined pointer;
312 typedef T element_type;
313 typedef D deleter_type;
314
315 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000316 constexpr unique_ptr() noexcept;
317 explicit unique_ptr(pointer p) noexcept;
318 unique_ptr(pointer p, see below d) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(unique_ptr&& u) noexcept;
321 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000324 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000325
326 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000327 unique_ptr& operator=(unique_ptr&& u) noexcept;
328 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // observers
331 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 pointer get() const noexcept;
333 deleter_type& get_deleter() noexcept;
334 const deleter_type& get_deleter() const noexcept;
335 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336
337 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000338 pointer release() noexcept;
339 void reset(pointer p = pointer()) noexcept;
340 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000341 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000342 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000343};
344
345template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000346 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000347
348template <class T1, class D1, class T2, class D2>
349 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354template <class T1, class D1, class T2, class D2>
355 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
356template <class T1, class D1, class T2, class D2>
357 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
358template <class T1, class D1, class T2, class D2>
359 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
360
Howard Hinnant719bda32011-05-28 14:41:13 +0000361template <class T, class D>
362 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
363template <class T, class D>
364 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
365template <class T, class D>
366 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
367template <class T, class D>
368 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
369
370template <class T, class D>
371 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
372template <class T, class D>
373 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
374template <class T, class D>
375 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
376template <class T, class D>
377 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
378template <class T, class D>
379 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
380template <class T, class D>
381 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
382template <class T, class D>
383 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
384template <class T, class D>
385 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387class bad_weak_ptr
388 : public std::exception
389{
Howard Hinnant719bda32011-05-28 14:41:13 +0000390 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000391};
392
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000393template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
394template<class T> unique_ptr<T> make_unique(size_t n); // C++14
395template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
396
Marshall Clowe52a3242017-11-27 15:51:36 +0000397template<class E, class T, class Y, class D>
398 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
399
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000400template<class T>
401class shared_ptr
402{
403public:
404 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000405 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406
407 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000408 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(Y* p);
410 template<class Y, class D> shared_ptr(Y* p, D d);
411 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
412 template <class D> shared_ptr(nullptr_t p, D d);
413 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
415 shared_ptr(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
417 shared_ptr(shared_ptr&& r) noexcept;
418 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000419 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000420 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
422 shared_ptr(nullptr_t) : shared_ptr() { }
423
424 // destructor:
425 ~shared_ptr();
426
427 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000428 shared_ptr& operator=(const shared_ptr& r) noexcept;
429 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
430 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000432 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000433 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
434
435 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000436 void swap(shared_ptr& r) noexcept;
437 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438 template<class Y> void reset(Y* p);
439 template<class Y, class D> void reset(Y* p, D d);
440 template<class Y, class D, class A> void reset(Y* p, D d, A a);
441
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 // observers:
443 T* get() const noexcept;
444 T& operator*() const noexcept;
445 T* operator->() const noexcept;
446 long use_count() const noexcept;
447 bool unique() const noexcept;
448 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000449 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
450 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451};
452
453// shared_ptr comparisons:
454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000457 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000459 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
466
467template <class T>
468 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
469template <class T>
470 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
471template <class T>
472 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
473template <class T>
474 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
475template <class T>
476 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
477template <class T>
478bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
479template <class T>
480 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
481template <class T>
482 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
483template <class T>
484 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
485template <class T>
486 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
487template <class T>
488 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
489template <class T>
490 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491
492// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000493template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000494
495// shared_ptr casts:
496template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000497 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000498template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000499 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000501 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503// shared_ptr I/O:
504template<class E, class T, class Y>
505 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
506
507// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000508template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000509
510template<class T, class... Args>
511 shared_ptr<T> make_shared(Args&&... args);
512template<class T, class A, class... Args>
513 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
514
515template<class T>
516class weak_ptr
517{
518public:
519 typedef T element_type;
520
521 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000522 constexpr weak_ptr() noexcept;
523 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
524 weak_ptr(weak_ptr const& r) noexcept;
525 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000526 weak_ptr(weak_ptr&& r) noexcept; // C++14
527 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // destructor
530 ~weak_ptr();
531
532 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 weak_ptr& operator=(weak_ptr const& r) noexcept;
534 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
535 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000536 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
537 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000538
539 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000540 void swap(weak_ptr& r) noexcept;
541 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542
543 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000544 long use_count() const noexcept;
545 bool expired() const noexcept;
546 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000547 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
548 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000549};
550
551// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000552template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000553
554// class owner_less:
555template<class T> struct owner_less;
556
557template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000558struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
560{
561 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000562 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
563 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
564 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000565};
566
567template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000568struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000569 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
570{
571 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000572 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
574 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
575};
576
577template <> // Added in C++14
578struct owner_less<void>
579{
580 template <class _Tp, class _Up>
581 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
582 template <class _Tp, class _Up>
583 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
584 template <class _Tp, class _Up>
585 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
586 template <class _Tp, class _Up>
587 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
588
589 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000590};
591
592template<class T>
593class enable_shared_from_this
594{
595protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000596 constexpr enable_shared_from_this() noexcept;
597 enable_shared_from_this(enable_shared_from_this const&) noexcept;
598 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599 ~enable_shared_from_this();
600public:
601 shared_ptr<T> shared_from_this();
602 shared_ptr<T const> shared_from_this() const;
603};
604
605template<class T>
606 bool atomic_is_lock_free(const shared_ptr<T>* p);
607template<class T>
608 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
609template<class T>
610 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
611template<class T>
612 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
613template<class T>
614 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
615template<class T>
616 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
617template<class T>
618 shared_ptr<T>
619 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
620template<class T>
621 bool
622 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
623template<class T>
624 bool
625 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
626template<class T>
627 bool
628 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
629 shared_ptr<T> w, memory_order success,
630 memory_order failure);
631template<class T>
632 bool
633 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
634 shared_ptr<T> w, memory_order success,
635 memory_order failure);
636// Hash support
637template <class T> struct hash;
638template <class T, class D> struct hash<unique_ptr<T, D> >;
639template <class T> struct hash<shared_ptr<T> >;
640
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000641template <class T, class Alloc>
642 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
643
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000644// Pointer safety
645enum class pointer_safety { relaxed, preferred, strict };
646void declare_reachable(void *p);
647template <class T> T *undeclare_reachable(T *p);
648void declare_no_pointers(char *p, size_t n);
649void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000650pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000651
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
653
654} // std
655
656*/
657
658#include <__config>
659#include <type_traits>
660#include <typeinfo>
661#include <cstddef>
662#include <cstdint>
663#include <new>
664#include <utility>
665#include <limits>
666#include <iterator>
667#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000668#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000669#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000670#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000671#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000672#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000673# include <atomic>
674#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000675#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000676
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000677#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680
Eric Fiselierf4433a32017-05-31 22:07:49 +0000681_LIBCPP_PUSH_MACROS
682#include <__undef_macros>
683
684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685_LIBCPP_BEGIN_NAMESPACE_STD
686
Eric Fiselier89659d12015-07-07 00:27:16 +0000687template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000688inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000689_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
690#if !defined(_LIBCPP_HAS_NO_THREADS) && \
691 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000692 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000693 return __atomic_load_n(__value, __ATOMIC_RELAXED);
694#else
695 return *__value;
696#endif
697}
698
Kuba Breckade9d6792016-09-04 09:55:12 +0000699template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000700inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000701_ValueType __libcpp_acquire_load(_ValueType const* __value) {
702#if !defined(_LIBCPP_HAS_NO_THREADS) && \
703 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000704 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000705 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
706#else
707 return *__value;
708#endif
709}
710
Marshall Clow78dbe462016-11-14 18:22:19 +0000711// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713template <class _Tp> class allocator;
714
Michael Park99f9e912020-03-04 11:27:14 -0500715#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716template <>
Michael Park99f9e912020-03-04 11:27:14 -0500717class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718{
719public:
720 typedef void* pointer;
721 typedef const void* const_pointer;
722 typedef void value_type;
723
724 template <class _Up> struct rebind {typedef allocator<_Up> other;};
725};
726
Howard Hinnant9f771052012-01-19 23:15:22 +0000727template <>
Michael Park99f9e912020-03-04 11:27:14 -0500728class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000729{
730public:
731 typedef const void* pointer;
732 typedef const void* const_pointer;
733 typedef const void value_type;
734
735 template <class _Up> struct rebind {typedef allocator<_Up> other;};
736};
Michael Park99f9e912020-03-04 11:27:14 -0500737#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000738
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739// pointer_traits
740
Marshall Clow0be70c32017-06-14 21:23:57 +0000741template <class _Tp, class = void>
742struct __has_element_type : false_type {};
743
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000745struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000746 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
748template <class _Ptr, bool = __has_element_type<_Ptr>::value>
749struct __pointer_traits_element_type;
750
751template <class _Ptr>
752struct __pointer_traits_element_type<_Ptr, true>
753{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000754 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755};
756
757#ifndef _LIBCPP_HAS_NO_VARIADICS
758
759template <template <class, class...> class _Sp, class _Tp, class ..._Args>
760struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
765template <template <class, class...> class _Sp, class _Tp, class ..._Args>
766struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
767{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000768 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000771#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
773template <template <class> class _Sp, class _Tp>
774struct __pointer_traits_element_type<_Sp<_Tp>, true>
775{
776 typedef typename _Sp<_Tp>::element_type type;
777};
778
779template <template <class> class _Sp, class _Tp>
780struct __pointer_traits_element_type<_Sp<_Tp>, false>
781{
782 typedef _Tp type;
783};
784
785template <template <class, class> class _Sp, class _Tp, class _A0>
786struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
787{
788 typedef typename _Sp<_Tp, _A0>::element_type type;
789};
790
791template <template <class, class> class _Sp, class _Tp, class _A0>
792struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
793{
794 typedef _Tp type;
795};
796
797template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
798struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
799{
800 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
801};
802
803template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
805{
806 typedef _Tp type;
807};
808
809template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
810 class _A1, class _A2>
811struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
812{
813 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
814};
815
816template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
817 class _A1, class _A2>
818struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
819{
820 typedef _Tp type;
821};
822
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000823#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
Marshall Clow0be70c32017-06-14 21:23:57 +0000825template <class _Tp, class = void>
826struct __has_difference_type : false_type {};
827
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000829struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000830 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831
832template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
833struct __pointer_traits_difference_type
834{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000835 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836};
837
838template <class _Ptr>
839struct __pointer_traits_difference_type<_Ptr, true>
840{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000841 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842};
843
844template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500845struct __has_rebind
846{
847private:
848 struct __two {char __lx; char __lxx;};
849 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500850 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500851 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500852 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500853public:
854 static const bool value = sizeof(__test<_Tp>(0)) == 1;
855};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856
857template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
858struct __pointer_traits_rebind
859{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000860#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000861 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000863 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864#endif
865};
866
867#ifndef _LIBCPP_HAS_NO_VARIADICS
868
869template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
870struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
871{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000872#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000873 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000875 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876#endif
877};
878
879template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
880struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
881{
882 typedef _Sp<_Up, _Args...> type;
883};
884
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000885#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886
887template <template <class> class _Sp, class _Tp, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
889{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000890#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 typedef typename _Sp<_Tp>::template rebind<_Up> type;
892#else
893 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
894#endif
895};
896
897template <template <class> class _Sp, class _Tp, class _Up>
898struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
899{
900 typedef _Sp<_Up> type;
901};
902
903template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
905{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000906#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
908#else
909 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
910#endif
911};
912
913template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
914struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
915{
916 typedef _Sp<_Up, _A0> type;
917};
918
919template <template <class, class, class> class _Sp, class _Tp, class _A0,
920 class _A1, class _Up>
921struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
922{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000923#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
925#else
926 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
927#endif
928};
929
930template <template <class, class, class> class _Sp, class _Tp, class _A0,
931 class _A1, class _Up>
932struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
933{
934 typedef _Sp<_Up, _A0, _A1> type;
935};
936
937template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
938 class _A1, class _A2, class _Up>
939struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
940{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000941#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
943#else
944 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
945#endif
946};
947
948template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
949 class _A1, class _A2, class _Up>
950struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
951{
952 typedef _Sp<_Up, _A0, _A1, _A2> type;
953};
954
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000955#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000958struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959{
960 typedef _Ptr pointer;
961 typedef typename __pointer_traits_element_type<pointer>::type element_type;
962 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
963
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000964#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000965 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966#else
967 template <class _Up> struct rebind
968 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000969#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
971private:
972 struct __nat {};
973public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 static pointer pointer_to(typename conditional<is_void<element_type>::value,
976 __nat, element_type>::type& __r)
977 {return pointer::pointer_to(__r);}
978};
979
980template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000981struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982{
983 typedef _Tp* pointer;
984 typedef _Tp element_type;
985 typedef ptrdiff_t difference_type;
986
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000987#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 template <class _Up> using rebind = _Up*;
989#else
990 template <class _Up> struct rebind {typedef _Up* other;};
991#endif
992
993private:
994 struct __nat {};
995public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000996 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000998 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000999 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000};
1001
Eric Fiselierae3ab842015-08-23 02:56:05 +00001002template <class _From, class _To>
1003struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001004#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +00001005 typedef typename pointer_traits<_From>::template rebind<_To> type;
1006#else
1007 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
1008#endif
1009};
1010
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011// allocator_traits
1012
Marshall Clow0be70c32017-06-14 21:23:57 +00001013template <class _Tp, class = void>
1014struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015
1016template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001017struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001018 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
1020namespace __pointer_type_imp
1021{
1022
1023template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1024struct __pointer_type
1025{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001026 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027};
1028
1029template <class _Tp, class _Dp>
1030struct __pointer_type<_Tp, _Dp, false>
1031{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001032 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033};
1034
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001035} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036
1037template <class _Tp, class _Dp>
1038struct __pointer_type
1039{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001040 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041};
1042
Marshall Clow0be70c32017-06-14 21:23:57 +00001043template <class _Tp, class = void>
1044struct __has_const_pointer : false_type {};
1045
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001047struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001048 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049
1050template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1051struct __const_pointer
1052{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001053 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054};
1055
1056template <class _Tp, class _Ptr, class _Alloc>
1057struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1058{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001059#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001060 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061#else
1062 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1063#endif
1064};
1065
Marshall Clow0be70c32017-06-14 21:23:57 +00001066template <class _Tp, class = void>
1067struct __has_void_pointer : false_type {};
1068
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001070struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001071 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072
1073template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1074struct __void_pointer
1075{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001076 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077};
1078
1079template <class _Ptr, class _Alloc>
1080struct __void_pointer<_Ptr, _Alloc, false>
1081{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001082#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001083 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001085 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086#endif
1087};
1088
Marshall Clow0be70c32017-06-14 21:23:57 +00001089template <class _Tp, class = void>
1090struct __has_const_void_pointer : false_type {};
1091
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001093struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001094 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095
1096template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1097struct __const_void_pointer
1098{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001099 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100};
1101
1102template <class _Ptr, class _Alloc>
1103struct __const_void_pointer<_Ptr, _Alloc, false>
1104{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001105#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001106 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001108 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109#endif
1110};
1111
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001112
1113template <bool _UsePointerTraits> struct __to_address_helper;
1114
1115template <> struct __to_address_helper<true> {
1116 template <class _Pointer>
1117 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1118
1119 template <class _Pointer>
1120 _LIBCPP_CONSTEXPR
1121 static __return_type<_Pointer>
1122 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1123};
1124
1125template <class _Pointer, bool _Dummy = true>
1126using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1127
1128
Howard Hinnantc834c512011-11-29 18:15:50 +00001129template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001130inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001131_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001132__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001134 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 return __p;
1136}
1137
1138template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001139inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1140typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1141__to_address(const _Pointer& __p) _NOEXCEPT {
1142 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001143}
1144
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001145template <> struct __to_address_helper<false> {
1146 template <class _Pointer>
1147 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001148
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001149 template <class _Pointer>
1150 _LIBCPP_CONSTEXPR
1151 static __return_type<_Pointer>
1152 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1153};
1154
1155
1156#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001157template <class _Tp>
1158inline _LIBCPP_INLINE_VISIBILITY constexpr
1159_Tp*
1160to_address(_Tp* __p) _NOEXCEPT
1161{
1162 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1163 return __p;
1164}
1165
1166template <class _Pointer>
1167inline _LIBCPP_INLINE_VISIBILITY
1168auto
1169to_address(const _Pointer& __p) _NOEXCEPT
1170{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001171 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001172}
1173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174
Marshall Clow0be70c32017-06-14 21:23:57 +00001175template <class _Tp, class = void>
1176struct __has_size_type : false_type {};
1177
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001179struct __has_size_type<_Tp,
1180 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001182template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183struct __size_type
1184{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001185 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186};
1187
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001188template <class _Alloc, class _DiffType>
1189struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001191 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192};
1193
Marshall Clow0be70c32017-06-14 21:23:57 +00001194template <class _Tp, class = void>
1195struct __has_propagate_on_container_copy_assignment : false_type {};
1196
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001198struct __has_propagate_on_container_copy_assignment<_Tp,
1199 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1200 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201
1202template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1203struct __propagate_on_container_copy_assignment
1204{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001205 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206};
1207
1208template <class _Alloc>
1209struct __propagate_on_container_copy_assignment<_Alloc, true>
1210{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001211 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212};
1213
Marshall Clow0be70c32017-06-14 21:23:57 +00001214template <class _Tp, class = void>
1215struct __has_propagate_on_container_move_assignment : false_type {};
1216
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001218struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001219 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1220 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
1222template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1223struct __propagate_on_container_move_assignment
1224{
1225 typedef false_type type;
1226};
1227
1228template <class _Alloc>
1229struct __propagate_on_container_move_assignment<_Alloc, true>
1230{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001231 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232};
1233
Marshall Clow0be70c32017-06-14 21:23:57 +00001234template <class _Tp, class = void>
1235struct __has_propagate_on_container_swap : false_type {};
1236
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001238struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001239 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1240 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
1242template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1243struct __propagate_on_container_swap
1244{
1245 typedef false_type type;
1246};
1247
1248template <class _Alloc>
1249struct __propagate_on_container_swap<_Alloc, true>
1250{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001251 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252};
1253
Marshall Clow0be70c32017-06-14 21:23:57 +00001254template <class _Tp, class = void>
1255struct __has_is_always_equal : false_type {};
1256
Marshall Clow0b587562015-06-02 16:34:03 +00001257template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001258struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001259 typename __void_t<typename _Tp::is_always_equal>::type>
1260 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001261
1262template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1263struct __is_always_equal
1264{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001265 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001266};
1267
1268template <class _Alloc>
1269struct __is_always_equal<_Alloc, true>
1270{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001271 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001272};
1273
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1275struct __has_rebind_other
1276{
1277private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001278 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001280 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001282 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283public:
1284 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1285};
1286
1287template <class _Tp, class _Up>
1288struct __has_rebind_other<_Tp, _Up, false>
1289{
1290 static const bool value = false;
1291};
1292
1293template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1294struct __allocator_traits_rebind
1295{
Michael Park99f9e912020-03-04 11:27:14 -05001296 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001297 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001298 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299};
1300
1301#ifndef _LIBCPP_HAS_NO_VARIADICS
1302
1303template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1305{
Michael Park99f9e912020-03-04 11:27:14 -05001306 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001307 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001308 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309};
1310
1311template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1312struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1313{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001314 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315};
1316
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001317#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318
1319template <template <class> class _Alloc, class _Tp, class _Up>
1320struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1321{
1322 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1323};
1324
1325template <template <class> class _Alloc, class _Tp, class _Up>
1326struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1327{
1328 typedef _Alloc<_Up> type;
1329};
1330
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1333{
1334 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1335};
1336
1337template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1338struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1339{
1340 typedef _Alloc<_Up, _A0> type;
1341};
1342
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1344 class _A1, class _Up>
1345struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1346{
1347 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1348};
1349
1350template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1351 class _A1, class _Up>
1352struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1353{
1354 typedef _Alloc<_Up, _A0, _A1> type;
1355};
1356
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1358 class _A1, class _A2, class _Up>
1359struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1360{
1361 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1362};
1363
1364template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1365 class _A1, class _A2, class _Up>
1366struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1367{
1368 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1369};
1370
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001371#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001373#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374
Michael Park99f9e912020-03-04 11:27:14 -05001375_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1377auto
1378__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001379 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001380_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381
1382template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1383auto
1384__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1385 -> false_type;
1386
1387template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1388struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001389 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1390 declval<_SizeType>(),
1391 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392{
1393};
1394
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001395#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
1397template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1398struct __has_allocate_hint
1399 : true_type
1400{
1401};
1402
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001403#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001405#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406
Michael Park99f9e912020-03-04 11:27:14 -05001407_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1408template <class _Alloc, class _Tp, class... _Args>
1409auto
1410__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1411 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1412_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413
1414template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001415auto
1416__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1417 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418
1419template <class _Alloc, class _Pointer, class ..._Args>
1420struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001421 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1422 declval<_Pointer>(),
1423 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424{
1425};
1426
Michael Park99f9e912020-03-04 11:27:14 -05001427_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428template <class _Alloc, class _Pointer>
1429auto
1430__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1431 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001432_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433
1434template <class _Alloc, class _Pointer>
1435auto
1436__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1437 -> false_type;
1438
1439template <class _Alloc, class _Pointer>
1440struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001441 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1442 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443{
1444};
1445
Michael Park99f9e912020-03-04 11:27:14 -05001446_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447template <class _Alloc>
1448auto
1449__has_max_size_test(_Alloc&& __a)
1450 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001451_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452
1453template <class _Alloc>
1454auto
1455__has_max_size_test(const volatile _Alloc& __a)
1456 -> false_type;
1457
1458template <class _Alloc>
1459struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001460 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461{
1462};
1463
1464template <class _Alloc>
1465auto
1466__has_select_on_container_copy_construction_test(_Alloc&& __a)
1467 -> decltype(__a.select_on_container_copy_construction(), true_type());
1468
1469template <class _Alloc>
1470auto
1471__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1472 -> false_type;
1473
1474template <class _Alloc>
1475struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001476 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477{
1478};
1479
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001480#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001482template <class _Alloc, class _Pointer, class _Tp, class = void>
1483struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001485template <class _Alloc, class _Pointer, class _Tp>
1486struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1487 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1488>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001490template <class _Alloc, class _Pointer, class = void>
1491struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
1493template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001494struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1495 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1496>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497
1498template <class _Alloc>
1499struct __has_max_size
1500 : true_type
1501{
1502};
1503
1504template <class _Alloc>
1505struct __has_select_on_container_copy_construction
1506 : false_type
1507{
1508};
1509
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001510#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001512template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1513struct __alloc_traits_difference_type
1514{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001515 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516};
1517
1518template <class _Alloc, class _Ptr>
1519struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1520{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001521 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001522};
1523
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001524template <class _Tp>
1525struct __is_default_allocator : false_type {};
1526
1527template <class _Tp>
1528struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1529
Eric Fiselier909fe962019-09-13 16:09:33 +00001530
1531
1532template <class _Alloc,
1533 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1534 >
1535struct __is_cpp17_move_insertable;
1536template <class _Alloc>
1537struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1538template <class _Alloc>
1539struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1540
1541template <class _Alloc,
1542 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1543 >
1544struct __is_cpp17_copy_insertable;
1545template <class _Alloc>
1546struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1547template <class _Alloc>
1548struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1549 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1550 __is_cpp17_move_insertable<_Alloc>::value>
1551 {};
1552
1553
1554
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001556struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557{
1558 typedef _Alloc allocator_type;
1559 typedef typename allocator_type::value_type value_type;
1560
1561 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1562 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1563 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1564 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1565
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001566 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1567 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568
1569 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1570 propagate_on_container_copy_assignment;
1571 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1572 propagate_on_container_move_assignment;
1573 typedef typename __propagate_on_container_swap<allocator_type>::type
1574 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001575 typedef typename __is_always_equal<allocator_type>::type
1576 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001578#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001580 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001581 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001582#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 template <class _Tp> struct rebind_alloc
1584 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1585 template <class _Tp> struct rebind_traits
1586 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001587#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588
Marshall Clow0e58cae2017-11-26 02:55:38 +00001589 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 static pointer allocate(allocator_type& __a, size_type __n)
1591 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001592 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001594 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1596
Howard Hinnant756c69b2010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001598 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 {__a.deallocate(__p, __n);}
1600
1601#ifndef _LIBCPP_HAS_NO_VARIADICS
1602 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001605 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001606 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001607#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001609 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001610 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 {
1612 ::new ((void*)__p) _Tp();
1613 }
1614 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001615 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001616 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001618 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1619 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 }
1621 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001622 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001623 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 const _A1& __a1)
1625 {
1626 ::new ((void*)__p) _Tp(__a0, __a1);
1627 }
1628 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001629 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001630 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631 const _A1& __a1, const _A2& __a2)
1632 {
1633 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1634 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001635#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636
1637 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 static void destroy(allocator_type& __a, _Tp* __p)
1640 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1641
Howard Hinnant756c69b2010-09-22 16:48:34 +00001642 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001643 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1645
Howard Hinnant756c69b2010-09-22 16:48:34 +00001646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 static allocator_type
1648 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001649 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 __has_select_on_container_copy_construction<const allocator_type>(),
1651 __a);}
1652
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001653 template <class _Ptr>
1654 _LIBCPP_INLINE_VISIBILITY
1655 static
1656 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001657 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001658 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001659 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1660 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001661 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001662 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001663#ifdef _LIBCPP_NO_EXCEPTIONS
1664 _VSTD::move(*__begin1)
1665#else
1666 _VSTD::move_if_noexcept(*__begin1)
1667#endif
1668 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001669 }
1670
1671 template <class _Tp>
1672 _LIBCPP_INLINE_VISIBILITY
1673 static
1674 typename enable_if
1675 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001676 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1678 is_trivially_move_constructible<_Tp>::value,
1679 void
1680 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001681 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001682 {
1683 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001684 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001685 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001686 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001687 __begin2 += _Np;
1688 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001689 }
1690
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001691 template <class _Iter, class _Ptr>
1692 _LIBCPP_INLINE_VISIBILITY
1693 static
1694 void
1695 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1696 {
1697 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001698 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001699 }
1700
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001701 template <class _SourceTp, class _DestTp,
1702 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1703 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001704 _LIBCPP_INLINE_VISIBILITY
1705 static
1706 typename enable_if
1707 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001708 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001709 is_same<_RawSourceTp, _RawDestTp>::value &&
1710 (__is_default_allocator<allocator_type>::value ||
1711 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001712 void
1713 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001714 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001715 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001716 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001717 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001718 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001719 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001720 __begin2 += _Np;
1721 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001722 }
1723
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001724 template <class _Ptr>
1725 _LIBCPP_INLINE_VISIBILITY
1726 static
1727 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001728 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001729 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001730 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1731 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001732 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001733 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001734 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001735#ifdef _LIBCPP_NO_EXCEPTIONS
1736 _VSTD::move(*--__end1)
1737#else
1738 _VSTD::move_if_noexcept(*--__end1)
1739#endif
1740 );
1741 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001742 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001743 }
1744
1745 template <class _Tp>
1746 _LIBCPP_INLINE_VISIBILITY
1747 static
1748 typename enable_if
1749 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001750 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001751 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1752 is_trivially_move_constructible<_Tp>::value,
1753 void
1754 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001755 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001756 {
1757 ptrdiff_t _Np = __end1 - __begin1;
1758 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001759 if (_Np > 0)
1760 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001761 }
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763private:
1764
Howard Hinnant756c69b2010-09-22 16:48:34 +00001765 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001766 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001768 {
1769 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1770 return __a.allocate(__n, __hint);
1771 _LIBCPP_SUPPRESS_DEPRECATED_POP
1772 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001773 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001774 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001775 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 {return __a.allocate(__n);}
1777
1778#ifndef _LIBCPP_HAS_NO_VARIADICS
1779 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001782 {
1783 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1784 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1785 _LIBCPP_SUPPRESS_DEPRECATED_POP
1786 }
1787
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1791 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001792 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001794#else // _LIBCPP_HAS_NO_VARIADICS
1795 template <class _Tp, class _A0>
1796 _LIBCPP_INLINE_VISIBILITY
1797 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1798 const _A0& __a0)
1799 {__a.construct(__p, __a0);}
1800 template <class _Tp, class _A0>
1801 _LIBCPP_INLINE_VISIBILITY
1802 static void __construct(false_type, allocator_type&, _Tp* __p,
1803 const _A0& __a0)
1804 {
1805 ::new ((void*)__p) _Tp(__a0);
1806 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808
1809 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001812 {
1813 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1814 __a.destroy(__p);
1815 _LIBCPP_SUPPRESS_DEPRECATED_POP
1816 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 static void __destroy(false_type, allocator_type&, _Tp* __p)
1820 {
1821 __p->~_Tp();
1822 }
1823
Howard Hinnant756c69b2010-09-22 16:48:34 +00001824 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001825 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001826 {
1827 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1828 return __a.max_size();
1829 _LIBCPP_SUPPRESS_DEPRECATED_POP
1830 }
1831
Howard Hinnant756c69b2010-09-22 16:48:34 +00001832 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001833 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001834 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835
Howard Hinnant756c69b2010-09-22 16:48:34 +00001836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001838 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001842 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 {return __a;}
1844};
1845
Marshall Clow940e01c2015-04-07 05:21:38 +00001846template <class _Traits, class _Tp>
1847struct __rebind_alloc_helper
1848{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001849#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001850 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001851#else
1852 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1853#endif
1854};
1855
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856// allocator
1857
1858template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001859class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860{
1861public:
Michael Park99f9e912020-03-04 11:27:14 -05001862#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1863 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1864 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1865 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1866 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1867 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1868 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1869
1870 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1871#endif
1872
1873 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874
Howard Hinnant4931e092011-06-02 21:38:57 +00001875 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001876 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001877
Marshall Clowbc759762018-03-20 23:02:53 +00001878 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1879 allocator() _NOEXCEPT {}
1880
Louis Dionne481a2662018-09-23 18:35:00 +00001881 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001882 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1883 allocator(const allocator<_Up>&) _NOEXCEPT {}
1884
Michael Park99f9e912020-03-04 11:27:14 -05001885#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1886 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1887 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001888 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001889 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1890 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001891 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001892#endif
1893
1894 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001895 {
Michael Park99f9e912020-03-04 11:27:14 -05001896 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1897 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001898 __throw_length_error("allocator<T>::allocate(size_t n)"
1899 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001900 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001901 }
Michael Park99f9e912020-03-04 11:27:14 -05001902
1903#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1904 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1905 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1906#endif
1907
1908 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001909 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001910
1911#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1912 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001913 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001914
Howard Hinnant74279a52010-09-04 23:28:19 +00001915#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001917 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918 void
1919 construct(_Up* __p, _Args&&... __args)
1920 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001921 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001923#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 _LIBCPP_INLINE_VISIBILITY
1925 void
1926 construct(pointer __p)
1927 {
1928 ::new((void*)__p) _Tp();
1929 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001930# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001931
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 template <class _A0>
1933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001934 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 construct(pointer __p, _A0& __a0)
1936 {
1937 ::new((void*)__p) _Tp(__a0);
1938 }
1939 template <class _A0>
1940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001941 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942 construct(pointer __p, const _A0& __a0)
1943 {
1944 ::new((void*)__p) _Tp(__a0);
1945 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001946# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 template <class _A0, class _A1>
1948 _LIBCPP_INLINE_VISIBILITY
1949 void
1950 construct(pointer __p, _A0& __a0, _A1& __a1)
1951 {
1952 ::new((void*)__p) _Tp(__a0, __a1);
1953 }
1954 template <class _A0, class _A1>
1955 _LIBCPP_INLINE_VISIBILITY
1956 void
1957 construct(pointer __p, const _A0& __a0, _A1& __a1)
1958 {
1959 ::new((void*)__p) _Tp(__a0, __a1);
1960 }
1961 template <class _A0, class _A1>
1962 _LIBCPP_INLINE_VISIBILITY
1963 void
1964 construct(pointer __p, _A0& __a0, const _A1& __a1)
1965 {
1966 ::new((void*)__p) _Tp(__a0, __a1);
1967 }
1968 template <class _A0, class _A1>
1969 _LIBCPP_INLINE_VISIBILITY
1970 void
1971 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1972 {
1973 ::new((void*)__p) _Tp(__a0, __a1);
1974 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001975#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001976 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1977#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978};
1979
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001980template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001981class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001982{
1983public:
Michael Park99f9e912020-03-04 11:27:14 -05001984#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1985 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1986 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1987 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1988 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1989 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1990 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1991
1992 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1993#endif
1994
1995 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001996
1997 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001998 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001999
Marshall Clowbc759762018-03-20 23:02:53 +00002000 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2001 allocator() _NOEXCEPT {}
2002
2003 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00002004 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00002005 allocator(const allocator<_Up>&) _NOEXCEPT {}
2006
Michael Park99f9e912020-03-04 11:27:14 -05002007#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2008 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
2009 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002010 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05002011#endif
2012
2013 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00002014 {
Michael Park99f9e912020-03-04 11:27:14 -05002015 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
2016 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00002017 __throw_length_error("allocator<const T>::allocate(size_t n)"
2018 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05002019 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00002020 }
Michael Park99f9e912020-03-04 11:27:14 -05002021
2022#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2023 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
2024 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
2025#endif
2026
2027 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002028 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05002029
2030#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2031 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002032 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05002033
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002034#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2035 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05002036 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002037 void
2038 construct(_Up* __p, _Args&&... __args)
2039 {
2040 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
2041 }
2042#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2043 _LIBCPP_INLINE_VISIBILITY
2044 void
2045 construct(pointer __p)
2046 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002047 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002048 }
2049# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00002050
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002051 template <class _A0>
2052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002053 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002054 construct(pointer __p, _A0& __a0)
2055 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002056 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002057 }
2058 template <class _A0>
2059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002060 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002061 construct(pointer __p, const _A0& __a0)
2062 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002063 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002064 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002065# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2066 template <class _A0, class _A1>
2067 _LIBCPP_INLINE_VISIBILITY
2068 void
2069 construct(pointer __p, _A0& __a0, _A1& __a1)
2070 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002071 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002072 }
2073 template <class _A0, class _A1>
2074 _LIBCPP_INLINE_VISIBILITY
2075 void
2076 construct(pointer __p, const _A0& __a0, _A1& __a1)
2077 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002078 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002079 }
2080 template <class _A0, class _A1>
2081 _LIBCPP_INLINE_VISIBILITY
2082 void
2083 construct(pointer __p, _A0& __a0, const _A1& __a1)
2084 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002085 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002086 }
2087 template <class _A0, class _A1>
2088 _LIBCPP_INLINE_VISIBILITY
2089 void
2090 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2091 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002092 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002093 }
2094#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05002095 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2096#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002097};
2098
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099template <class _Tp, class _Up>
2100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002101bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102
2103template <class _Tp, class _Up>
2104inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002105bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
2107template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002108class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109 : public iterator<output_iterator_tag,
2110 _Tp, // purposefully not C++03
2111 ptrdiff_t, // purposefully not C++03
2112 _Tp*, // purposefully not C++03
2113 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2114{
2115private:
2116 _OutputIterator __x_;
2117public:
2118 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2119 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2120 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002121 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002122#if _LIBCPP_STD_VER >= 14
2123 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002124 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2127 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2128 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002129#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002130 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002131#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132};
2133
2134template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002135_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002137get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138{
2139 pair<_Tp*, ptrdiff_t> __r(0, 0);
2140 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2141 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2142 / sizeof(_Tp);
2143 if (__n > __m)
2144 __n = __m;
2145 while (__n > 0)
2146 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002147#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002148 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002149 {
2150 std::align_val_t __al =
2151 std::align_val_t(std::alignment_of<_Tp>::value);
2152 __r.first = static_cast<_Tp*>(::operator new(
2153 __n * sizeof(_Tp), __al, nothrow));
2154 } else {
2155 __r.first = static_cast<_Tp*>(::operator new(
2156 __n * sizeof(_Tp), nothrow));
2157 }
2158#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002159 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002160 {
2161 // Since aligned operator new is unavailable, return an empty
2162 // buffer rather than one with invalid alignment.
2163 return __r;
2164 }
2165
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002167#endif
2168
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169 if (__r.first)
2170 {
2171 __r.second = __n;
2172 break;
2173 }
2174 __n /= 2;
2175 }
2176 return __r;
2177}
2178
2179template <class _Tp>
2180inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002181void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2182{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002183 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002184}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185
Marshall Clowb22274f2017-01-24 22:22:33 +00002186#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002188struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189{
2190 _Tp* __ptr_;
2191};
2192
2193template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002194class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195{
2196private:
2197 _Tp* __ptr_;
2198public:
2199 typedef _Tp element_type;
2200
Dimitry Andric47269ce2020-03-13 19:36:26 +01002201 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
2202 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
2203 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002205 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002207 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002209 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002211 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Dimitry Andric47269ce2020-03-13 19:36:26 +01002213 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002215 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
2216 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
2217 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 {
2219 _Tp* __t = __ptr_;
2220 __ptr_ = 0;
2221 return __t;
2222 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01002223 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 {
2225 if (__ptr_ != __p)
2226 delete __ptr_;
2227 __ptr_ = __p;
2228 }
2229
Dimitry Andric47269ce2020-03-13 19:36:26 +01002230 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
2231 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002233 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 {return auto_ptr<_Up>(release());}
2235};
2236
2237template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002238class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239{
2240public:
2241 typedef void element_type;
2242};
Marshall Clowb22274f2017-01-24 22:22:33 +00002243#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002245// Tag used to default initialize one or both of the pair's elements.
2246struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002247struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002248
Eric Fiselier9d355982017-04-12 23:45:53 +00002249template <class _Tp, int _Idx,
2250 bool _CanBeEmptyBase =
2251 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2252struct __compressed_pair_elem {
2253 typedef _Tp _ParamT;
2254 typedef _Tp& reference;
2255 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002258 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2260 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261
Eric Fiselier9d355982017-04-12 23:45:53 +00002262 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002263 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2264 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002266 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002267 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002268 : __value_(_VSTD::forward<_Up>(__u))
2269 {
2270 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002272
2273#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002274 template <class... _Args, size_t... _Indexes>
2275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2276 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2277 __tuple_indices<_Indexes...>)
2278 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002281
Alex Lorenz76132112017-11-09 17:54:49 +00002282 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2283 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002284 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002287 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288};
2289
Eric Fiselier9d355982017-04-12 23:45:53 +00002290template <class _Tp, int _Idx>
2291struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2292 typedef _Tp _ParamT;
2293 typedef _Tp& reference;
2294 typedef const _Tp& const_reference;
2295 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002297 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2298 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2299 __compressed_pair_elem(__default_init_tag) {}
2300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2301 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302
Eric Fiselier9d355982017-04-12 23:45:53 +00002303 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002304 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2305 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002306 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002307 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002308 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002309 : __value_type(_VSTD::forward<_Up>(__u))
2310 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002312#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002313 template <class... _Args, size_t... _Indexes>
2314 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2315 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2316 __tuple_indices<_Indexes...>)
2317 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002318#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319
Alex Lorenz76132112017-11-09 17:54:49 +00002320 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2321 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002322 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323};
2324
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002326class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2327 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002328 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2329 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002330
2331 // NOTE: This static assert should never fire because __compressed_pair
2332 // is *almost never* used in a scenario where it's possible for T1 == T2.
2333 // (The exception is std::function where it is possible that the function
2334 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002335 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002336 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2337 "The current implementation is NOT ABI-compatible with the previous "
2338 "implementation for this configuration");
2339
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002341 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002342 class = typename enable_if<
2343 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2344 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2345 >::type
2346 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002347 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002348 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349
Eric Fiselier9d355982017-04-12 23:45:53 +00002350 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002352 __compressed_pair(_U1&& __t1, _U2&& __t2)
2353 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002355#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002356 template <class... _Args1, class... _Args2>
2357 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2358 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2359 tuple<_Args2...> __second_args)
2360 : _Base1(__pc, _VSTD::move(__first_args),
2361 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2362 _Base2(__pc, _VSTD::move(__second_args),
2363 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002364#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365
Eric Fiselier9d355982017-04-12 23:45:53 +00002366 _LIBCPP_INLINE_VISIBILITY
2367 typename _Base1::reference first() _NOEXCEPT {
2368 return static_cast<_Base1&>(*this).__get();
2369 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370
Eric Fiselier9d355982017-04-12 23:45:53 +00002371 _LIBCPP_INLINE_VISIBILITY
2372 typename _Base1::const_reference first() const _NOEXCEPT {
2373 return static_cast<_Base1 const&>(*this).__get();
2374 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375
Eric Fiselier9d355982017-04-12 23:45:53 +00002376 _LIBCPP_INLINE_VISIBILITY
2377 typename _Base2::reference second() _NOEXCEPT {
2378 return static_cast<_Base2&>(*this).__get();
2379 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380
Eric Fiselier9d355982017-04-12 23:45:53 +00002381 _LIBCPP_INLINE_VISIBILITY
2382 typename _Base2::const_reference second() const _NOEXCEPT {
2383 return static_cast<_Base2 const&>(*this).__get();
2384 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Eric Fiselier9d355982017-04-12 23:45:53 +00002386 _LIBCPP_INLINE_VISIBILITY
2387 void swap(__compressed_pair& __x)
2388 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2389 __is_nothrow_swappable<_T2>::value)
2390 {
2391 using std::swap;
2392 swap(first(), __x.first());
2393 swap(second(), __x.second());
2394 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395};
2396
2397template <class _T1, class _T2>
2398inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002399void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2400 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2401 __is_nothrow_swappable<_T2>::value) {
2402 __x.swap(__y);
2403}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002405// default_delete
2406
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002408struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002409 static_assert(!is_function<_Tp>::value,
2410 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002411#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002412 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002413#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002414 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002415#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002416 template <class _Up>
2417 _LIBCPP_INLINE_VISIBILITY
2418 default_delete(const default_delete<_Up>&,
2419 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2420 0) _NOEXCEPT {}
2421
2422 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2423 static_assert(sizeof(_Tp) > 0,
2424 "default_delete can not delete incomplete type");
2425 static_assert(!is_void<_Tp>::value,
2426 "default_delete can not delete incomplete type");
2427 delete __ptr;
2428 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429};
2430
2431template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002432struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2433private:
2434 template <class _Up>
2435 struct _EnableIfConvertible
2436 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2437
Howard Hinnant4500ca52011-12-18 21:19:44 +00002438public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002439#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002440 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002441#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002442 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002443#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002444
2445 template <class _Up>
2446 _LIBCPP_INLINE_VISIBILITY
2447 default_delete(const default_delete<_Up[]>&,
2448 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2449
2450 template <class _Up>
2451 _LIBCPP_INLINE_VISIBILITY
2452 typename _EnableIfConvertible<_Up>::type
2453 operator()(_Up* __ptr) const _NOEXCEPT {
2454 static_assert(sizeof(_Tp) > 0,
2455 "default_delete can not delete incomplete type");
2456 static_assert(!is_void<_Tp>::value,
2457 "default_delete can not delete void type");
2458 delete[] __ptr;
2459 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460};
2461
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002462template <class _Deleter>
2463struct __unique_ptr_deleter_sfinae {
2464 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2465 typedef const _Deleter& __lval_ref_type;
2466 typedef _Deleter&& __good_rval_ref_type;
2467 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468};
2469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470template <class _Deleter>
2471struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2472 typedef const _Deleter& __lval_ref_type;
2473 typedef const _Deleter&& __bad_rval_ref_type;
2474 typedef false_type __enable_rval_overload;
2475};
2476
2477template <class _Deleter>
2478struct __unique_ptr_deleter_sfinae<_Deleter&> {
2479 typedef _Deleter& __lval_ref_type;
2480 typedef _Deleter&& __bad_rval_ref_type;
2481 typedef false_type __enable_rval_overload;
2482};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002483
2484template <class _Tp, class _Dp = default_delete<_Tp> >
2485class _LIBCPP_TEMPLATE_VIS unique_ptr {
2486public:
2487 typedef _Tp element_type;
2488 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002489 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002490
2491 static_assert(!is_rvalue_reference<deleter_type>::value,
2492 "the specified deleter type cannot be an rvalue reference");
2493
2494private:
2495 __compressed_pair<pointer, deleter_type> __ptr_;
2496
2497 struct __nat { int __for_bool_; };
2498
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002499 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500
2501 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002502 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002503 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002504
2505 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002506 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002507 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002508
2509 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002510 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002511 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512
2513 template <bool _Dummy, class _Deleter = typename __dependent_type<
2514 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002515 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002516 typename enable_if<is_default_constructible<_Deleter>::value &&
2517 !is_pointer<_Deleter>::value>::type;
2518
2519 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002520 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002521 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2522
2523 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002524 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525 is_convertible<typename _UPtr::pointer, pointer>::value &&
2526 !is_array<_Up>::value
2527 >::type;
2528
2529 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002530 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002531 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2532 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2533 >::type;
2534
2535 template <class _UDel>
2536 using _EnableIfDeleterAssignable = typename enable_if<
2537 is_assignable<_Dp&, _UDel&&>::value
2538 >::type;
2539
2540public:
2541 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002542 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002543 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002544 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002545
2546 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002547 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002549 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002550
2551 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002552 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002553 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002554 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002555
2556 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002557 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002559 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002560 : __ptr_(__p, __d) {}
2561
2562 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002563 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002564 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002565 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002566 : __ptr_(__p, _VSTD::move(__d)) {
2567 static_assert(!is_reference<deleter_type>::value,
2568 "rvalue deleter bound to reference");
2569 }
2570
2571 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002572 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002573 _LIBCPP_INLINE_VISIBILITY
2574 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2575
2576 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002577 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002578 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2579 }
2580
2581 template <class _Up, class _Ep,
2582 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2583 class = _EnableIfDeleterConvertible<_Ep>
2584 >
2585 _LIBCPP_INLINE_VISIBILITY
2586 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2587 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2588
2589#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2590 template <class _Up>
2591 _LIBCPP_INLINE_VISIBILITY
2592 unique_ptr(auto_ptr<_Up>&& __p,
2593 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002594 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002595 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002596 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002597#endif
2598
2599 _LIBCPP_INLINE_VISIBILITY
2600 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2601 reset(__u.release());
2602 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2603 return *this;
2604 }
2605
2606 template <class _Up, class _Ep,
2607 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2608 class = _EnableIfDeleterAssignable<_Ep>
2609 >
2610 _LIBCPP_INLINE_VISIBILITY
2611 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2612 reset(__u.release());
2613 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2614 return *this;
2615 }
2616
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002617#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2618 template <class _Up>
2619 _LIBCPP_INLINE_VISIBILITY
2620 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2621 is_same<_Dp, default_delete<_Tp> >::value,
2622 unique_ptr&>::type
2623 operator=(auto_ptr<_Up> __p) {
2624 reset(__p.release());
2625 return *this;
2626 }
2627#endif
2628
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002629#ifdef _LIBCPP_CXX03_LANG
2630 unique_ptr(unique_ptr const&) = delete;
2631 unique_ptr& operator=(unique_ptr const&) = delete;
2632#endif
2633
2634
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002635 _LIBCPP_INLINE_VISIBILITY
2636 ~unique_ptr() { reset(); }
2637
2638 _LIBCPP_INLINE_VISIBILITY
2639 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2640 reset();
2641 return *this;
2642 }
2643
2644 _LIBCPP_INLINE_VISIBILITY
2645 typename add_lvalue_reference<_Tp>::type
2646 operator*() const {
2647 return *__ptr_.first();
2648 }
2649 _LIBCPP_INLINE_VISIBILITY
2650 pointer operator->() const _NOEXCEPT {
2651 return __ptr_.first();
2652 }
2653 _LIBCPP_INLINE_VISIBILITY
2654 pointer get() const _NOEXCEPT {
2655 return __ptr_.first();
2656 }
2657 _LIBCPP_INLINE_VISIBILITY
2658 deleter_type& get_deleter() _NOEXCEPT {
2659 return __ptr_.second();
2660 }
2661 _LIBCPP_INLINE_VISIBILITY
2662 const deleter_type& get_deleter() const _NOEXCEPT {
2663 return __ptr_.second();
2664 }
2665 _LIBCPP_INLINE_VISIBILITY
2666 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2667 return __ptr_.first() != nullptr;
2668 }
2669
2670 _LIBCPP_INLINE_VISIBILITY
2671 pointer release() _NOEXCEPT {
2672 pointer __t = __ptr_.first();
2673 __ptr_.first() = pointer();
2674 return __t;
2675 }
2676
2677 _LIBCPP_INLINE_VISIBILITY
2678 void reset(pointer __p = pointer()) _NOEXCEPT {
2679 pointer __tmp = __ptr_.first();
2680 __ptr_.first() = __p;
2681 if (__tmp)
2682 __ptr_.second()(__tmp);
2683 }
2684
2685 _LIBCPP_INLINE_VISIBILITY
2686 void swap(unique_ptr& __u) _NOEXCEPT {
2687 __ptr_.swap(__u.__ptr_);
2688 }
2689};
2690
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002691
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002693class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002695 typedef _Tp element_type;
2696 typedef _Dp deleter_type;
2697 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2698
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002700 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701
Eric Fiselier31127cd2017-04-16 02:14:31 +00002702 template <class _From>
2703 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiselier31127cd2017-04-16 02:14:31 +00002705 template <class _FromElem>
2706 struct _CheckArrayPointerConversion<_FromElem*>
2707 : integral_constant<bool,
2708 is_same<_FromElem*, pointer>::value ||
2709 (is_same<pointer, element_type*>::value &&
2710 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2711 >
2712 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713
Eric Fiselier31127cd2017-04-16 02:14:31 +00002714 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715
2716 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002717 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002718 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002719
2720 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002721 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002722 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723
2724 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002725 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002726 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727
2728 template <bool _Dummy, class _Deleter = typename __dependent_type<
2729 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002730 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731 typename enable_if<is_default_constructible<_Deleter>::value &&
2732 !is_pointer<_Deleter>::value>::type;
2733
2734 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002735 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002736 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2737
2738 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002739 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002740 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002741 >::type;
2742
2743 template <class _UPtr, class _Up,
2744 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002745 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002746 is_array<_Up>::value &&
2747 is_same<pointer, element_type*>::value &&
2748 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2749 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2750 >::type;
2751
2752 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002753 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2755 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2756 >::type;
2757
2758 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002759 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002760 is_assignable<_Dp&, _UDel&&>::value
2761 >::type;
2762
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002764 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002765 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002766 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002767 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002769 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002770 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002771 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002772 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002774 template <class _Pp, bool _Dummy = true,
2775 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002776 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002778 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002779 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002781 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002782 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2783 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002784 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002785 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002786 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002788 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002789 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002790 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002791 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002792 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002794 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002795 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2796 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002797 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002798 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002799 : __ptr_(__p, _VSTD::move(__d)) {
2800 static_assert(!is_reference<deleter_type>::value,
2801 "rvalue deleter bound to reference");
2802 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002804 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002805 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002806 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002807 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002808 : __ptr_(nullptr, _VSTD::move(__d)) {
2809 static_assert(!is_reference<deleter_type>::value,
2810 "rvalue deleter bound to reference");
2811 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002812
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002813 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002814 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2815 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002816 _LIBCPP_INLINE_VISIBILITY
2817 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002818
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002819 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002820 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002821 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2822 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002823
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002825 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002826 reset(__u.release());
2827 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2828 return *this;
2829 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002831 template <class _Up, class _Ep,
2832 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2833 class = _EnableIfDeleterConvertible<_Ep>
2834 >
2835 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002836 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002837 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002838 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002840 template <class _Up, class _Ep,
2841 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2842 class = _EnableIfDeleterAssignable<_Ep>
2843 >
2844 _LIBCPP_INLINE_VISIBILITY
2845 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002846 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002847 reset(__u.release());
2848 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2849 return *this;
2850 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002852#ifdef _LIBCPP_CXX03_LANG
2853 unique_ptr(unique_ptr const&) = delete;
2854 unique_ptr& operator=(unique_ptr const&) = delete;
2855#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002856
2857public:
2858 _LIBCPP_INLINE_VISIBILITY
2859 ~unique_ptr() { reset(); }
2860
2861 _LIBCPP_INLINE_VISIBILITY
2862 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2863 reset();
2864 return *this;
2865 }
2866
2867 _LIBCPP_INLINE_VISIBILITY
2868 typename add_lvalue_reference<_Tp>::type
2869 operator[](size_t __i) const {
2870 return __ptr_.first()[__i];
2871 }
2872 _LIBCPP_INLINE_VISIBILITY
2873 pointer get() const _NOEXCEPT {
2874 return __ptr_.first();
2875 }
2876
2877 _LIBCPP_INLINE_VISIBILITY
2878 deleter_type& get_deleter() _NOEXCEPT {
2879 return __ptr_.second();
2880 }
2881
2882 _LIBCPP_INLINE_VISIBILITY
2883 const deleter_type& get_deleter() const _NOEXCEPT {
2884 return __ptr_.second();
2885 }
2886 _LIBCPP_INLINE_VISIBILITY
2887 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2888 return __ptr_.first() != nullptr;
2889 }
2890
2891 _LIBCPP_INLINE_VISIBILITY
2892 pointer release() _NOEXCEPT {
2893 pointer __t = __ptr_.first();
2894 __ptr_.first() = pointer();
2895 return __t;
2896 }
2897
2898 template <class _Pp>
2899 _LIBCPP_INLINE_VISIBILITY
2900 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002901 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002902 >::type
2903 reset(_Pp __p) _NOEXCEPT {
2904 pointer __tmp = __ptr_.first();
2905 __ptr_.first() = __p;
2906 if (__tmp)
2907 __ptr_.second()(__tmp);
2908 }
2909
2910 _LIBCPP_INLINE_VISIBILITY
2911 void reset(nullptr_t = nullptr) _NOEXCEPT {
2912 pointer __tmp = __ptr_.first();
2913 __ptr_.first() = nullptr;
2914 if (__tmp)
2915 __ptr_.second()(__tmp);
2916 }
2917
2918 _LIBCPP_INLINE_VISIBILITY
2919 void swap(unique_ptr& __u) _NOEXCEPT {
2920 __ptr_.swap(__u.__ptr_);
2921 }
2922
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923};
2924
2925template <class _Tp, class _Dp>
2926inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002927typename enable_if<
2928 __is_swappable<_Dp>::value,
2929 void
2930>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002931swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932
2933template <class _T1, class _D1, class _T2, class _D2>
2934inline _LIBCPP_INLINE_VISIBILITY
2935bool
2936operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2937
2938template <class _T1, class _D1, class _T2, class _D2>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
2941operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2942
2943template <class _T1, class _D1, class _T2, class _D2>
2944inline _LIBCPP_INLINE_VISIBILITY
2945bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002946operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2947{
2948 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2949 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002950 typedef typename common_type<_P1, _P2>::type _Vp;
2951 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002952}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953
2954template <class _T1, class _D1, class _T2, class _D2>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2958
2959template <class _T1, class _D1, class _T2, class _D2>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
2962operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2963
2964template <class _T1, class _D1, class _T2, class _D2>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
2967operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2968
Howard Hinnantb17caf92012-02-21 21:02:58 +00002969template <class _T1, class _D1>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002972operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002973{
2974 return !__x;
2975}
2976
2977template <class _T1, class _D1>
2978inline _LIBCPP_INLINE_VISIBILITY
2979bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002980operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002981{
2982 return !__x;
2983}
2984
2985template <class _T1, class _D1>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002988operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002989{
2990 return static_cast<bool>(__x);
2991}
2992
2993template <class _T1, class _D1>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002996operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002997{
2998 return static_cast<bool>(__x);
2999}
3000
3001template <class _T1, class _D1>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
3004operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3005{
3006 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3007 return less<_P1>()(__x.get(), nullptr);
3008}
3009
3010template <class _T1, class _D1>
3011inline _LIBCPP_INLINE_VISIBILITY
3012bool
3013operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3014{
3015 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3016 return less<_P1>()(nullptr, __x.get());
3017}
3018
3019template <class _T1, class _D1>
3020inline _LIBCPP_INLINE_VISIBILITY
3021bool
3022operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3023{
3024 return nullptr < __x;
3025}
3026
3027template <class _T1, class _D1>
3028inline _LIBCPP_INLINE_VISIBILITY
3029bool
3030operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3031{
3032 return __x < nullptr;
3033}
3034
3035template <class _T1, class _D1>
3036inline _LIBCPP_INLINE_VISIBILITY
3037bool
3038operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3039{
3040 return !(nullptr < __x);
3041}
3042
3043template <class _T1, class _D1>
3044inline _LIBCPP_INLINE_VISIBILITY
3045bool
3046operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3047{
3048 return !(__x < nullptr);
3049}
3050
3051template <class _T1, class _D1>
3052inline _LIBCPP_INLINE_VISIBILITY
3053bool
3054operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3055{
3056 return !(__x < nullptr);
3057}
3058
3059template <class _T1, class _D1>
3060inline _LIBCPP_INLINE_VISIBILITY
3061bool
3062operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3063{
3064 return !(nullptr < __x);
3065}
3066
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003067#if _LIBCPP_STD_VER > 11
3068
3069template<class _Tp>
3070struct __unique_if
3071{
3072 typedef unique_ptr<_Tp> __unique_single;
3073};
3074
3075template<class _Tp>
3076struct __unique_if<_Tp[]>
3077{
3078 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3079};
3080
3081template<class _Tp, size_t _Np>
3082struct __unique_if<_Tp[_Np]>
3083{
3084 typedef void __unique_array_known_bound;
3085};
3086
3087template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003088inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003089typename __unique_if<_Tp>::__unique_single
3090make_unique(_Args&&... __args)
3091{
3092 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3093}
3094
3095template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003096inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003097typename __unique_if<_Tp>::__unique_array_unknown_bound
3098make_unique(size_t __n)
3099{
3100 typedef typename remove_extent<_Tp>::type _Up;
3101 return unique_ptr<_Tp>(new _Up[__n]());
3102}
3103
3104template<class _Tp, class... _Args>
3105 typename __unique_if<_Tp>::__unique_array_known_bound
3106 make_unique(_Args&&...) = delete;
3107
3108#endif // _LIBCPP_STD_VER > 11
3109
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003110template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003111#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003112struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003113#else
3114struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003115 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003116#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003117{
3118 typedef unique_ptr<_Tp, _Dp> argument_type;
3119 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003120 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003121 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003122 {
3123 typedef typename argument_type::pointer pointer;
3124 return hash<pointer>()(__ptr.get());
3125 }
3126};
3127
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128struct __destruct_n
3129{
3130private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003131 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132
3133 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003135 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136
3137 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003138 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 {}
3140
Howard Hinnant719bda32011-05-28 14:41:13 +00003141 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003142 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003143 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 {}
3145
Howard Hinnant719bda32011-05-28 14:41:13 +00003146 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003147 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003148 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 {}
3150public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003152 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153
3154 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003155 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003156 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157
3158 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003159 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003160 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161
3162 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003164 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165};
3166
3167template <class _Alloc>
3168class __allocator_destructor
3169{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003170 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003172 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3173 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174private:
3175 _Alloc& __alloc_;
3176 size_type __s_;
3177public:
3178 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003179 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003182 void operator()(pointer __p) _NOEXCEPT
3183 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184};
3185
3186template <class _InputIterator, class _ForwardIterator>
3187_ForwardIterator
3188uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3189{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003191#ifndef _LIBCPP_NO_EXCEPTIONS
3192 _ForwardIterator __s = __r;
3193 try
3194 {
3195#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003196 for (; __f != __l; ++__f, (void) ++__r)
3197 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003198#ifndef _LIBCPP_NO_EXCEPTIONS
3199 }
3200 catch (...)
3201 {
3202 for (; __s != __r; ++__s)
3203 __s->~value_type();
3204 throw;
3205 }
3206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 return __r;
3208}
3209
3210template <class _InputIterator, class _Size, class _ForwardIterator>
3211_ForwardIterator
3212uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3213{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003215#ifndef _LIBCPP_NO_EXCEPTIONS
3216 _ForwardIterator __s = __r;
3217 try
3218 {
3219#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003220 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3221 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003222#ifndef _LIBCPP_NO_EXCEPTIONS
3223 }
3224 catch (...)
3225 {
3226 for (; __s != __r; ++__s)
3227 __s->~value_type();
3228 throw;
3229 }
3230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231 return __r;
3232}
3233
3234template <class _ForwardIterator, class _Tp>
3235void
3236uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3237{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003239#ifndef _LIBCPP_NO_EXCEPTIONS
3240 _ForwardIterator __s = __f;
3241 try
3242 {
3243#endif
3244 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003245 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003246#ifndef _LIBCPP_NO_EXCEPTIONS
3247 }
3248 catch (...)
3249 {
3250 for (; __s != __f; ++__s)
3251 __s->~value_type();
3252 throw;
3253 }
3254#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255}
3256
3257template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003258_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3260{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003262#ifndef _LIBCPP_NO_EXCEPTIONS
3263 _ForwardIterator __s = __f;
3264 try
3265 {
3266#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003267 for (; __n > 0; ++__f, (void) --__n)
3268 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003269#ifndef _LIBCPP_NO_EXCEPTIONS
3270 }
3271 catch (...)
3272 {
3273 for (; __s != __f; ++__s)
3274 __s->~value_type();
3275 throw;
3276 }
3277#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003278 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279}
3280
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003281#if _LIBCPP_STD_VER > 14
3282
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003283template <class _Tp>
3284inline _LIBCPP_INLINE_VISIBILITY
3285void destroy_at(_Tp* __loc) {
3286 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3287 __loc->~_Tp();
3288}
3289
3290template <class _ForwardIterator>
3291inline _LIBCPP_INLINE_VISIBILITY
3292void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3293 for (; __first != __last; ++__first)
3294 _VSTD::destroy_at(_VSTD::addressof(*__first));
3295}
3296
3297template <class _ForwardIterator, class _Size>
3298inline _LIBCPP_INLINE_VISIBILITY
3299_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3300 for (; __n > 0; (void)++__first, --__n)
3301 _VSTD::destroy_at(_VSTD::addressof(*__first));
3302 return __first;
3303}
3304
Eric Fiselier290c07c2016-10-11 21:13:44 +00003305template <class _ForwardIterator>
3306inline _LIBCPP_INLINE_VISIBILITY
3307void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3308 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3309 auto __idx = __first;
3310#ifndef _LIBCPP_NO_EXCEPTIONS
3311 try {
3312#endif
3313 for (; __idx != __last; ++__idx)
3314 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3315#ifndef _LIBCPP_NO_EXCEPTIONS
3316 } catch (...) {
3317 _VSTD::destroy(__first, __idx);
3318 throw;
3319 }
3320#endif
3321}
3322
3323template <class _ForwardIterator, class _Size>
3324inline _LIBCPP_INLINE_VISIBILITY
3325_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3326 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3327 auto __idx = __first;
3328#ifndef _LIBCPP_NO_EXCEPTIONS
3329 try {
3330#endif
3331 for (; __n > 0; (void)++__idx, --__n)
3332 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3333 return __idx;
3334#ifndef _LIBCPP_NO_EXCEPTIONS
3335 } catch (...) {
3336 _VSTD::destroy(__first, __idx);
3337 throw;
3338 }
3339#endif
3340}
3341
3342
3343template <class _ForwardIterator>
3344inline _LIBCPP_INLINE_VISIBILITY
3345void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3346 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3347 auto __idx = __first;
3348#ifndef _LIBCPP_NO_EXCEPTIONS
3349 try {
3350#endif
3351 for (; __idx != __last; ++__idx)
3352 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3353#ifndef _LIBCPP_NO_EXCEPTIONS
3354 } catch (...) {
3355 _VSTD::destroy(__first, __idx);
3356 throw;
3357 }
3358#endif
3359}
3360
3361template <class _ForwardIterator, class _Size>
3362inline _LIBCPP_INLINE_VISIBILITY
3363_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3364 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3365 auto __idx = __first;
3366#ifndef _LIBCPP_NO_EXCEPTIONS
3367 try {
3368#endif
3369 for (; __n > 0; (void)++__idx, --__n)
3370 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3371 return __idx;
3372#ifndef _LIBCPP_NO_EXCEPTIONS
3373 } catch (...) {
3374 _VSTD::destroy(__first, __idx);
3375 throw;
3376 }
3377#endif
3378}
3379
3380
3381template <class _InputIt, class _ForwardIt>
3382inline _LIBCPP_INLINE_VISIBILITY
3383_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3384 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3385 auto __idx = __first_res;
3386#ifndef _LIBCPP_NO_EXCEPTIONS
3387 try {
3388#endif
3389 for (; __first != __last; (void)++__idx, ++__first)
3390 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3391 return __idx;
3392#ifndef _LIBCPP_NO_EXCEPTIONS
3393 } catch (...) {
3394 _VSTD::destroy(__first_res, __idx);
3395 throw;
3396 }
3397#endif
3398}
3399
3400template <class _InputIt, class _Size, class _ForwardIt>
3401inline _LIBCPP_INLINE_VISIBILITY
3402pair<_InputIt, _ForwardIt>
3403uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3404 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3405 auto __idx = __first_res;
3406#ifndef _LIBCPP_NO_EXCEPTIONS
3407 try {
3408#endif
3409 for (; __n > 0; ++__idx, (void)++__first, --__n)
3410 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3411 return {__first, __idx};
3412#ifndef _LIBCPP_NO_EXCEPTIONS
3413 } catch (...) {
3414 _VSTD::destroy(__first_res, __idx);
3415 throw;
3416 }
3417#endif
3418}
3419
3420
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003421#endif // _LIBCPP_STD_VER > 14
3422
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003423// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3424// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003425// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003426#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3427 && defined(__ATOMIC_RELAXED) \
3428 && defined(__ATOMIC_ACQ_REL)
3429# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003430#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003431# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3432#endif
3433
3434template <class _Tp>
3435inline _LIBCPP_INLINE_VISIBILITY _Tp
3436__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3437{
3438#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3439 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3440#else
3441 return __t += 1;
3442#endif
3443}
3444
3445template <class _Tp>
3446inline _LIBCPP_INLINE_VISIBILITY _Tp
3447__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3448{
3449#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3450 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3451#else
3452 return __t -= 1;
3453#endif
3454}
3455
Howard Hinnant756c69b2010-09-22 16:48:34 +00003456class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457 : public std::exception
3458{
3459public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003460 bad_weak_ptr() _NOEXCEPT = default;
3461 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003462 virtual ~bad_weak_ptr() _NOEXCEPT;
3463 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464};
3465
Louis Dionne16fe2952018-07-11 23:14:33 +00003466_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003467void __throw_bad_weak_ptr()
3468{
3469#ifndef _LIBCPP_NO_EXCEPTIONS
3470 throw bad_weak_ptr();
3471#else
3472 _VSTD::abort();
3473#endif
3474}
3475
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003476template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003478class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479{
3480 __shared_count(const __shared_count&);
3481 __shared_count& operator=(const __shared_count&);
3482
3483protected:
3484 long __shared_owners_;
3485 virtual ~__shared_count();
3486private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003487 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488
3489public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003491 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492 : __shared_owners_(__refs) {}
3493
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003494#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003495 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003496 void __add_shared() _NOEXCEPT;
3497 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003498#else
3499 _LIBCPP_INLINE_VISIBILITY
3500 void __add_shared() _NOEXCEPT {
3501 __libcpp_atomic_refcount_increment(__shared_owners_);
3502 }
3503 _LIBCPP_INLINE_VISIBILITY
3504 bool __release_shared() _NOEXCEPT {
3505 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3506 __on_zero_shared();
3507 return true;
3508 }
3509 return false;
3510 }
3511#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003512 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003513 long use_count() const _NOEXCEPT {
3514 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3515 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516};
3517
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003518class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519 : private __shared_count
3520{
3521 long __shared_weak_owners_;
3522
3523public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003525 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526 : __shared_count(__refs),
3527 __shared_weak_owners_(__refs) {}
3528protected:
3529 virtual ~__shared_weak_count();
3530
3531public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003532#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003533 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003534 void __add_shared() _NOEXCEPT;
3535 void __add_weak() _NOEXCEPT;
3536 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003537#else
3538 _LIBCPP_INLINE_VISIBILITY
3539 void __add_shared() _NOEXCEPT {
3540 __shared_count::__add_shared();
3541 }
3542 _LIBCPP_INLINE_VISIBILITY
3543 void __add_weak() _NOEXCEPT {
3544 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3545 }
3546 _LIBCPP_INLINE_VISIBILITY
3547 void __release_shared() _NOEXCEPT {
3548 if (__shared_count::__release_shared())
3549 __release_weak();
3550 }
3551#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003552 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003554 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3555 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556
Howard Hinnant807d6332013-02-25 15:50:36 +00003557 // Define the function out only if we build static libc++ without RTTI.
3558 // Otherwise we may break clients who need to compile their projects with
3559 // -fno-rtti and yet link against a libc++.dylib compiled
3560 // without -fno-rtti.
3561#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003562 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003563#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003565 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566};
3567
3568template <class _Tp, class _Dp, class _Alloc>
3569class __shared_ptr_pointer
3570 : public __shared_weak_count
3571{
3572 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3573public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003576 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577
Howard Hinnant72f73582010-08-11 17:04:31 +00003578#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003579 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003580#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581
3582private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003583 virtual void __on_zero_shared() _NOEXCEPT;
3584 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585};
3586
Howard Hinnant72f73582010-08-11 17:04:31 +00003587#ifndef _LIBCPP_NO_RTTI
3588
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589template <class _Tp, class _Dp, class _Alloc>
3590const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003591__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003593 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594}
3595
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003596#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003597
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598template <class _Tp, class _Dp, class _Alloc>
3599void
Howard Hinnant719bda32011-05-28 14:41:13 +00003600__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601{
3602 __data_.first().second()(__data_.first().first());
3603 __data_.first().second().~_Dp();
3604}
3605
3606template <class _Tp, class _Dp, class _Alloc>
3607void
Howard Hinnant719bda32011-05-28 14:41:13 +00003608__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003610 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3611 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003612 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3613
Eric Fiselierf8898c82015-02-05 23:01:40 +00003614 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003616 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617}
3618
3619template <class _Tp, class _Alloc>
3620class __shared_ptr_emplace
3621 : public __shared_weak_count
3622{
3623 __compressed_pair<_Alloc, _Tp> __data_;
3624public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625
Howard Hinnant756c69b2010-09-22 16:48:34 +00003626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003628 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003630
3631#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003635 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3636 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637#else // _LIBCPP_HAS_NO_VARIADICS
3638
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3642 : __data_(__a, _Tp(__a0)) {}
3643
3644 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3647 : __data_(__a, _Tp(__a0, __a1)) {}
3648
3649 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3652 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3653
3654#endif // _LIBCPP_HAS_NO_VARIADICS
3655
3656private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003657 virtual void __on_zero_shared() _NOEXCEPT;
3658 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003660 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003661 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662};
3663
3664template <class _Tp, class _Alloc>
3665void
Howard Hinnant719bda32011-05-28 14:41:13 +00003666__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667{
3668 __data_.second().~_Tp();
3669}
3670
3671template <class _Tp, class _Alloc>
3672void
Howard Hinnant719bda32011-05-28 14:41:13 +00003673__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003675 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3676 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003677 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003678 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003680 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681}
3682
Erik Pilkington2a398762017-05-25 15:43:31 +00003683struct __shared_ptr_dummy_rebind_allocator_type;
3684template <>
3685class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3686{
3687public:
3688 template <class _Other>
3689 struct rebind
3690 {
3691 typedef allocator<_Other> other;
3692 };
3693};
3694
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003695template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696
3697template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003698class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003700public:
3701 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003702
Eric Fiselierae5b6672016-06-27 01:02:43 +00003703#if _LIBCPP_STD_VER > 14
3704 typedef weak_ptr<_Tp> weak_type;
3705#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706private:
3707 element_type* __ptr_;
3708 __shared_weak_count* __cntrl_;
3709
3710 struct __nat {int __for_bool_;};
3711public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003713 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003715 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003716 template<class _Yp>
3717 explicit shared_ptr(_Yp* __p,
3718 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3719 template<class _Yp, class _Dp>
3720 shared_ptr(_Yp* __p, _Dp __d,
3721 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3722 template<class _Yp, class _Dp, class _Alloc>
3723 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3724 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3726 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003727 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003729 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003733 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003734 _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(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003738 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003739 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003740 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003741#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003743 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003744#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003745#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003746 template<class _Yp>
3747 shared_ptr(auto_ptr<_Yp>&& __r,
3748 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003749#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003750 template<class _Yp>
3751 shared_ptr(auto_ptr<_Yp> __r,
3752 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003754#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003755#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003756 template <class _Yp, class _Dp>
3757 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3758 typename enable_if
3759 <
3760 !is_lvalue_reference<_Dp>::value &&
3761 !is_array<_Yp>::value &&
3762 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3763 __nat
3764 >::type = __nat());
3765 template <class _Yp, class _Dp>
3766 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3767 typename enable_if
3768 <
3769 is_lvalue_reference<_Dp>::value &&
3770 !is_array<_Yp>::value &&
3771 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3772 __nat
3773 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003774#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003775 template <class _Yp, class _Dp>
3776 shared_ptr(unique_ptr<_Yp, _Dp>,
3777 typename enable_if
3778 <
3779 !is_lvalue_reference<_Dp>::value &&
3780 !is_array<_Yp>::value &&
3781 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3782 __nat
3783 >::type = __nat());
3784 template <class _Yp, class _Dp>
3785 shared_ptr(unique_ptr<_Yp, _Dp>,
3786 typename enable_if
3787 <
3788 is_lvalue_reference<_Dp>::value &&
3789 !is_array<_Yp>::value &&
3790 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3791 __nat
3792 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003793#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794
3795 ~shared_ptr();
3796
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003798 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003799 template<class _Yp>
3800 typename enable_if
3801 <
3802 is_convertible<_Yp*, element_type*>::value,
3803 shared_ptr&
3804 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003806 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003807#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003809 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003810 template<class _Yp>
3811 typename enable_if
3812 <
3813 is_convertible<_Yp*, element_type*>::value,
3814 shared_ptr<_Tp>&
3815 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003817 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003818#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003819 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003821 typename enable_if
3822 <
3823 !is_array<_Yp>::value &&
3824 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003825 shared_ptr
3826 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003827 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003828#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003829#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003830#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003831 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003833 typename enable_if
3834 <
3835 !is_array<_Yp>::value &&
3836 is_convertible<_Yp*, element_type*>::value,
3837 shared_ptr&
3838 >::type
3839 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003841#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003842 template <class _Yp, class _Dp>
3843 typename enable_if
3844 <
3845 !is_array<_Yp>::value &&
3846 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3847 shared_ptr&
3848 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003849#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003851 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003852#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003854 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855#endif
3856
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003858 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003860 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003861 template<class _Yp>
3862 typename enable_if
3863 <
3864 is_convertible<_Yp*, element_type*>::value,
3865 void
3866 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003868 reset(_Yp* __p);
3869 template<class _Yp, class _Dp>
3870 typename enable_if
3871 <
3872 is_convertible<_Yp*, element_type*>::value,
3873 void
3874 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003876 reset(_Yp* __p, _Dp __d);
3877 template<class _Yp, class _Dp, class _Alloc>
3878 typename enable_if
3879 <
3880 is_convertible<_Yp*, element_type*>::value,
3881 void
3882 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003884 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885
Howard Hinnant756c69b2010-09-22 16:48:34 +00003886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003887 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003889 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3890 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003894 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003896 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003898 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003899 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003900 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003901 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003903 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003904 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003905 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003907 _LIBCPP_INLINE_VISIBILITY
3908 bool
3909 __owner_equivalent(const shared_ptr& __p) const
3910 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911
Howard Hinnant72f73582010-08-11 17:04:31 +00003912#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003915 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003916 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003917 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003918 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003919#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920
Zoe Carverd9040c72019-10-22 15:16:49 +00003921 template<class _Yp, class _CntrlBlk>
3922 static shared_ptr<_Tp>
3923 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3924 {
3925 shared_ptr<_Tp> __r;
3926 __r.__ptr_ = __p;
3927 __r.__cntrl_ = __cntrl;
3928 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3929 return __r;
3930 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003931
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003933 template <class _Yp, bool = is_function<_Yp>::value>
3934 struct __shared_ptr_default_allocator
3935 {
3936 typedef allocator<_Yp> type;
3937 };
3938
3939 template <class _Yp>
3940 struct __shared_ptr_default_allocator<_Yp, true>
3941 {
3942 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3943 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003945 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003946 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003947 typename enable_if<is_convertible<_OrigPtr*,
3948 const enable_shared_from_this<_Yp>*
3949 >::value,
3950 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003951 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3952 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003954 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003955 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003956 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003957 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3958 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003959 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960 }
3961
Erik Pilkington2a398762017-05-25 15:43:31 +00003962 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003964 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3965 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966};
3967
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003968
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003970inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003971_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003972shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 : __ptr_(0),
3974 __cntrl_(0)
3975{
3976}
3977
3978template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003979inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003980_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003981shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982 : __ptr_(0),
3983 __cntrl_(0)
3984{
3985}
3986
3987template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003988template<class _Yp>
3989shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3990 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991 : __ptr_(__p)
3992{
3993 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003994 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3995 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3996 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003998 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999}
4000
4001template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004002template<class _Yp, class _Dp>
4003shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4004 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005 : __ptr_(__p)
4006{
4007#ifndef _LIBCPP_NO_EXCEPTIONS
4008 try
4009 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004010#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004011 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4012 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4013 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004014 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015#ifndef _LIBCPP_NO_EXCEPTIONS
4016 }
4017 catch (...)
4018 {
4019 __d(__p);
4020 throw;
4021 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004022#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023}
4024
4025template<class _Tp>
4026template<class _Dp>
4027shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4028 : __ptr_(0)
4029{
4030#ifndef _LIBCPP_NO_EXCEPTIONS
4031 try
4032 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004033#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004034 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4035 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4036 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037#ifndef _LIBCPP_NO_EXCEPTIONS
4038 }
4039 catch (...)
4040 {
4041 __d(__p);
4042 throw;
4043 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004044#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045}
4046
4047template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004048template<class _Yp, class _Dp, class _Alloc>
4049shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4050 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051 : __ptr_(__p)
4052{
4053#ifndef _LIBCPP_NO_EXCEPTIONS
4054 try
4055 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004056#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004058 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059 typedef __allocator_destructor<_A2> _D2;
4060 _A2 __a2(__a);
4061 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004062 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4063 _CntrlBlk(__p, __d, __a);
4064 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004065 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066#ifndef _LIBCPP_NO_EXCEPTIONS
4067 }
4068 catch (...)
4069 {
4070 __d(__p);
4071 throw;
4072 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004073#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074}
4075
4076template<class _Tp>
4077template<class _Dp, class _Alloc>
4078shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4079 : __ptr_(0)
4080{
4081#ifndef _LIBCPP_NO_EXCEPTIONS
4082 try
4083 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004084#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004086 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087 typedef __allocator_destructor<_A2> _D2;
4088 _A2 __a2(__a);
4089 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004090 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4091 _CntrlBlk(__p, __d, __a);
4092 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093#ifndef _LIBCPP_NO_EXCEPTIONS
4094 }
4095 catch (...)
4096 {
4097 __d(__p);
4098 throw;
4099 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004100#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101}
4102
4103template<class _Tp>
4104template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004105inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004106shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107 : __ptr_(__p),
4108 __cntrl_(__r.__cntrl_)
4109{
4110 if (__cntrl_)
4111 __cntrl_->__add_shared();
4112}
4113
4114template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004115inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004116shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117 : __ptr_(__r.__ptr_),
4118 __cntrl_(__r.__cntrl_)
4119{
4120 if (__cntrl_)
4121 __cntrl_->__add_shared();
4122}
4123
4124template<class _Tp>
4125template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004126inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004128 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004129 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 : __ptr_(__r.__ptr_),
4131 __cntrl_(__r.__cntrl_)
4132{
4133 if (__cntrl_)
4134 __cntrl_->__add_shared();
4135}
4136
Howard Hinnant74279a52010-09-04 23:28:19 +00004137#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138
4139template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004140inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004141shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142 : __ptr_(__r.__ptr_),
4143 __cntrl_(__r.__cntrl_)
4144{
4145 __r.__ptr_ = 0;
4146 __r.__cntrl_ = 0;
4147}
4148
4149template<class _Tp>
4150template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004151inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004153 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004154 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155 : __ptr_(__r.__ptr_),
4156 __cntrl_(__r.__cntrl_)
4157{
4158 __r.__ptr_ = 0;
4159 __r.__cntrl_ = 0;
4160}
4161
Howard Hinnant74279a52010-09-04 23:28:19 +00004162#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163
Marshall Clowb22274f2017-01-24 22:22:33 +00004164#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004166template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004167#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004168shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004170shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004172 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173 : __ptr_(__r.get())
4174{
4175 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4176 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004177 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178 __r.release();
4179}
Marshall Clowb22274f2017-01-24 22:22:33 +00004180#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181
4182template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004183template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004184#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4186#else
4187shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4188#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004189 typename enable_if
4190 <
4191 !is_lvalue_reference<_Dp>::value &&
4192 !is_array<_Yp>::value &&
4193 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4194 __nat
4195 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196 : __ptr_(__r.get())
4197{
Marshall Clow35cde742015-05-10 13:59:45 +00004198#if _LIBCPP_STD_VER > 11
4199 if (__ptr_ == nullptr)
4200 __cntrl_ = nullptr;
4201 else
4202#endif
4203 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004204 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4205 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4206 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004207 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004208 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209 __r.release();
4210}
4211
4212template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004213template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004214#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4216#else
4217shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4218#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004219 typename enable_if
4220 <
4221 is_lvalue_reference<_Dp>::value &&
4222 !is_array<_Yp>::value &&
4223 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4224 __nat
4225 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226 : __ptr_(__r.get())
4227{
Marshall Clow35cde742015-05-10 13:59:45 +00004228#if _LIBCPP_STD_VER > 11
4229 if (__ptr_ == nullptr)
4230 __cntrl_ = nullptr;
4231 else
4232#endif
4233 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004234 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004235 typedef __shared_ptr_pointer<_Yp*,
4236 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004237 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004238 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004239 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004240 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241 __r.release();
4242}
4243
Zoe Carver6cd05c32019-08-19 15:47:16 +00004244template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245shared_ptr<_Tp>::~shared_ptr()
4246{
4247 if (__cntrl_)
4248 __cntrl_->__release_shared();
4249}
4250
4251template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004252inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004254shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255{
4256 shared_ptr(__r).swap(*this);
4257 return *this;
4258}
4259
4260template<class _Tp>
4261template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004262inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004263typename enable_if
4264<
Marshall Clow7e384b72017-01-10 16:59:33 +00004265 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004266 shared_ptr<_Tp>&
4267>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004268shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269{
4270 shared_ptr(__r).swap(*this);
4271 return *this;
4272}
4273
Howard Hinnant74279a52010-09-04 23:28:19 +00004274#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275
4276template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004277inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004279shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004281 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282 return *this;
4283}
4284
4285template<class _Tp>
4286template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004287inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004288typename enable_if
4289<
Marshall Clow7e384b72017-01-10 16:59:33 +00004290 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004291 shared_ptr<_Tp>&
4292>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4294{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004295 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296 return *this;
4297}
4298
Marshall Clowb22274f2017-01-24 22:22:33 +00004299#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004300template<class _Tp>
4301template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004302inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004303typename enable_if
4304<
4305 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004306 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004307 shared_ptr<_Tp>
4308>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4310{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004311 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312 return *this;
4313}
Marshall Clowb22274f2017-01-24 22:22:33 +00004314#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315
4316template<class _Tp>
4317template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004318inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004319typename enable_if
4320<
4321 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004322 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004323 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004324 shared_ptr<_Tp>&
4325>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4327{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004328 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329 return *this;
4330}
4331
Howard Hinnant74279a52010-09-04 23:28:19 +00004332#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333
Marshall Clowb22274f2017-01-24 22:22:33 +00004334#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335template<class _Tp>
4336template<class _Yp>
4337inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004338typename enable_if
4339<
4340 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004341 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004342 shared_ptr<_Tp>&
4343>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004344shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345{
4346 shared_ptr(__r).swap(*this);
4347 return *this;
4348}
Marshall Clowb22274f2017-01-24 22:22:33 +00004349#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350
4351template<class _Tp>
4352template <class _Yp, class _Dp>
4353inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004354typename enable_if
4355<
4356 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004357 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004358 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004359 shared_ptr<_Tp>&
4360>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4362{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004363 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364 return *this;
4365}
4366
Howard Hinnant74279a52010-09-04 23:28:19 +00004367#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368
4369template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004370inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371void
Howard Hinnant719bda32011-05-28 14:41:13 +00004372shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004374 _VSTD::swap(__ptr_, __r.__ptr_);
4375 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376}
4377
4378template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004379inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380void
Howard Hinnant719bda32011-05-28 14:41:13 +00004381shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382{
4383 shared_ptr().swap(*this);
4384}
4385
4386template<class _Tp>
4387template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004388inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004389typename enable_if
4390<
Marshall Clow7e384b72017-01-10 16:59:33 +00004391 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004392 void
4393>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394shared_ptr<_Tp>::reset(_Yp* __p)
4395{
4396 shared_ptr(__p).swap(*this);
4397}
4398
4399template<class _Tp>
4400template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004401inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004402typename enable_if
4403<
Marshall Clow7e384b72017-01-10 16:59:33 +00004404 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004405 void
4406>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4408{
4409 shared_ptr(__p, __d).swap(*this);
4410}
4411
4412template<class _Tp>
4413template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004414inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004415typename enable_if
4416<
Marshall Clow7e384b72017-01-10 16:59:33 +00004417 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004418 void
4419>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004420shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4421{
4422 shared_ptr(__p, __d, __a).swap(*this);
4423}
4424
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004425template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004427typename enable_if
4428<
4429 !is_array<_Tp>::value,
4430 shared_ptr<_Tp>
4431>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432make_shared(_Args&& ...__args)
4433{
Zoe Carverd9040c72019-10-22 15:16:49 +00004434 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4435 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4436 typedef allocator<_CntrlBlk> _A2;
4437 typedef __allocator_destructor<_A2> _D2;
4438
4439 _A2 __a2;
4440 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4441 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4442
4443 _Tp *__ptr = __hold2.get()->get();
4444 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445}
4446
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004447template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004449typename enable_if
4450<
4451 !is_array<_Tp>::value,
4452 shared_ptr<_Tp>
4453>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004454allocate_shared(const _Alloc& __a, _Args&& ...__args)
4455{
zoecarver505730a2020-02-25 16:50:57 -08004456 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4457
4458 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4459 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4460 typedef __allocator_destructor<_A2> _D2;
4461
4462 _A2 __a2(__a);
4463 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4464 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4465 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4466
4467 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4468 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469}
4470
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471template<class _Tp, class _Up>
4472inline _LIBCPP_INLINE_VISIBILITY
4473bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004474operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475{
4476 return __x.get() == __y.get();
4477}
4478
4479template<class _Tp, class _Up>
4480inline _LIBCPP_INLINE_VISIBILITY
4481bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004482operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483{
4484 return !(__x == __y);
4485}
4486
4487template<class _Tp, class _Up>
4488inline _LIBCPP_INLINE_VISIBILITY
4489bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004490operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004492#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004493 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4494 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004495#else
4496 return less<>()(__x.get(), __y.get());
4497#endif
4498
Howard Hinnantb17caf92012-02-21 21:02:58 +00004499}
4500
4501template<class _Tp, class _Up>
4502inline _LIBCPP_INLINE_VISIBILITY
4503bool
4504operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4505{
4506 return __y < __x;
4507}
4508
4509template<class _Tp, class _Up>
4510inline _LIBCPP_INLINE_VISIBILITY
4511bool
4512operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4513{
4514 return !(__y < __x);
4515}
4516
4517template<class _Tp, class _Up>
4518inline _LIBCPP_INLINE_VISIBILITY
4519bool
4520operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4521{
4522 return !(__x < __y);
4523}
4524
4525template<class _Tp>
4526inline _LIBCPP_INLINE_VISIBILITY
4527bool
4528operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4529{
4530 return !__x;
4531}
4532
4533template<class _Tp>
4534inline _LIBCPP_INLINE_VISIBILITY
4535bool
4536operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4537{
4538 return !__x;
4539}
4540
4541template<class _Tp>
4542inline _LIBCPP_INLINE_VISIBILITY
4543bool
4544operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4545{
4546 return static_cast<bool>(__x);
4547}
4548
4549template<class _Tp>
4550inline _LIBCPP_INLINE_VISIBILITY
4551bool
4552operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4553{
4554 return static_cast<bool>(__x);
4555}
4556
4557template<class _Tp>
4558inline _LIBCPP_INLINE_VISIBILITY
4559bool
4560operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4561{
4562 return less<_Tp*>()(__x.get(), nullptr);
4563}
4564
4565template<class _Tp>
4566inline _LIBCPP_INLINE_VISIBILITY
4567bool
4568operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4569{
4570 return less<_Tp*>()(nullptr, __x.get());
4571}
4572
4573template<class _Tp>
4574inline _LIBCPP_INLINE_VISIBILITY
4575bool
4576operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4577{
4578 return nullptr < __x;
4579}
4580
4581template<class _Tp>
4582inline _LIBCPP_INLINE_VISIBILITY
4583bool
4584operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4585{
4586 return __x < nullptr;
4587}
4588
4589template<class _Tp>
4590inline _LIBCPP_INLINE_VISIBILITY
4591bool
4592operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4593{
4594 return !(nullptr < __x);
4595}
4596
4597template<class _Tp>
4598inline _LIBCPP_INLINE_VISIBILITY
4599bool
4600operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4601{
4602 return !(__x < nullptr);
4603}
4604
4605template<class _Tp>
4606inline _LIBCPP_INLINE_VISIBILITY
4607bool
4608operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4609{
4610 return !(__x < nullptr);
4611}
4612
4613template<class _Tp>
4614inline _LIBCPP_INLINE_VISIBILITY
4615bool
4616operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4617{
4618 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004619}
4620
4621template<class _Tp>
4622inline _LIBCPP_INLINE_VISIBILITY
4623void
Howard Hinnant719bda32011-05-28 14:41:13 +00004624swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004625{
4626 __x.swap(__y);
4627}
4628
4629template<class _Tp, class _Up>
4630inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004631typename enable_if
4632<
4633 !is_array<_Tp>::value && !is_array<_Up>::value,
4634 shared_ptr<_Tp>
4635>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004636static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637{
4638 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4639}
4640
4641template<class _Tp, class _Up>
4642inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004643typename enable_if
4644<
4645 !is_array<_Tp>::value && !is_array<_Up>::value,
4646 shared_ptr<_Tp>
4647>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004648dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649{
4650 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4651 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4652}
4653
4654template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004655typename enable_if
4656<
4657 is_array<_Tp>::value == is_array<_Up>::value,
4658 shared_ptr<_Tp>
4659>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004660const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004661{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004662 typedef typename remove_extent<_Tp>::type _RTp;
4663 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664}
4665
Howard Hinnant72f73582010-08-11 17:04:31 +00004666#ifndef _LIBCPP_NO_RTTI
4667
Howard Hinnantc51e1022010-05-11 19:42:16 +00004668template<class _Dp, class _Tp>
4669inline _LIBCPP_INLINE_VISIBILITY
4670_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004671get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004672{
4673 return __p.template __get_deleter<_Dp>();
4674}
4675
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004676#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004677
Howard Hinnantc51e1022010-05-11 19:42:16 +00004678template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004679class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004680{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004681public:
4682 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004683private:
4684 element_type* __ptr_;
4685 __shared_weak_count* __cntrl_;
4686
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004687public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004689 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004690 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004691 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4692 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004694 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004695 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004696 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4697 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004698
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004699#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004701 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004702 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004703 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4704 _NOEXCEPT;
4705#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004706 ~weak_ptr();
4707
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004709 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004710 template<class _Yp>
4711 typename enable_if
4712 <
4713 is_convertible<_Yp*, element_type*>::value,
4714 weak_ptr&
4715 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004717 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4718
4719#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4720
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004722 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4723 template<class _Yp>
4724 typename enable_if
4725 <
4726 is_convertible<_Yp*, element_type*>::value,
4727 weak_ptr&
4728 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004730 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4731
4732#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4733
4734 template<class _Yp>
4735 typename enable_if
4736 <
4737 is_convertible<_Yp*, element_type*>::value,
4738 weak_ptr&
4739 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004741 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004742
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004744 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004746 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004747
Howard Hinnant756c69b2010-09-22 16:48:34 +00004748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004749 long use_count() const _NOEXCEPT
4750 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004752 bool expired() const _NOEXCEPT
4753 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4754 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004755 template<class _Up>
4756 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004757 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004758 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004759 template<class _Up>
4760 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004761 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004762 {return __cntrl_ < __r.__cntrl_;}
4763
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004764 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4765 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004766};
4767
4768template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004769inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004770_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004771weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004772 : __ptr_(0),
4773 __cntrl_(0)
4774{
4775}
4776
4777template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004778inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004779weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004780 : __ptr_(__r.__ptr_),
4781 __cntrl_(__r.__cntrl_)
4782{
4783 if (__cntrl_)
4784 __cntrl_->__add_weak();
4785}
4786
4787template<class _Tp>
4788template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004789inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004790weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004791 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004792 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004793 : __ptr_(__r.__ptr_),
4794 __cntrl_(__r.__cntrl_)
4795{
4796 if (__cntrl_)
4797 __cntrl_->__add_weak();
4798}
4799
4800template<class _Tp>
4801template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004802inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004803weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004804 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004805 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004806 : __ptr_(__r.__ptr_),
4807 __cntrl_(__r.__cntrl_)
4808{
4809 if (__cntrl_)
4810 __cntrl_->__add_weak();
4811}
4812
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004813#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4814
4815template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004816inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004817weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4818 : __ptr_(__r.__ptr_),
4819 __cntrl_(__r.__cntrl_)
4820{
4821 __r.__ptr_ = 0;
4822 __r.__cntrl_ = 0;
4823}
4824
4825template<class _Tp>
4826template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004827inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004828weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4829 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4830 _NOEXCEPT
4831 : __ptr_(__r.__ptr_),
4832 __cntrl_(__r.__cntrl_)
4833{
4834 __r.__ptr_ = 0;
4835 __r.__cntrl_ = 0;
4836}
4837
4838#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4839
Howard Hinnantc51e1022010-05-11 19:42:16 +00004840template<class _Tp>
4841weak_ptr<_Tp>::~weak_ptr()
4842{
4843 if (__cntrl_)
4844 __cntrl_->__release_weak();
4845}
4846
4847template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004848inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004849weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004850weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004851{
4852 weak_ptr(__r).swap(*this);
4853 return *this;
4854}
4855
4856template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004857template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004858inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004859typename enable_if
4860<
4861 is_convertible<_Yp*, _Tp*>::value,
4862 weak_ptr<_Tp>&
4863>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004864weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004865{
4866 weak_ptr(__r).swap(*this);
4867 return *this;
4868}
4869
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004870#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4871
4872template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004873inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004874weak_ptr<_Tp>&
4875weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4876{
4877 weak_ptr(_VSTD::move(__r)).swap(*this);
4878 return *this;
4879}
4880
Howard Hinnantc51e1022010-05-11 19:42:16 +00004881template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004882template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004883inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004884typename enable_if
4885<
4886 is_convertible<_Yp*, _Tp*>::value,
4887 weak_ptr<_Tp>&
4888>::type
4889weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4890{
4891 weak_ptr(_VSTD::move(__r)).swap(*this);
4892 return *this;
4893}
4894
4895#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4896
4897template<class _Tp>
4898template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004899inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004900typename enable_if
4901<
4902 is_convertible<_Yp*, _Tp*>::value,
4903 weak_ptr<_Tp>&
4904>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004905weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004906{
4907 weak_ptr(__r).swap(*this);
4908 return *this;
4909}
4910
4911template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004912inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004913void
Howard Hinnant719bda32011-05-28 14:41:13 +00004914weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004915{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004916 _VSTD::swap(__ptr_, __r.__ptr_);
4917 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004918}
4919
4920template<class _Tp>
4921inline _LIBCPP_INLINE_VISIBILITY
4922void
Howard Hinnant719bda32011-05-28 14:41:13 +00004923swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924{
4925 __x.swap(__y);
4926}
4927
4928template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004929inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004930void
Howard Hinnant719bda32011-05-28 14:41:13 +00004931weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004932{
4933 weak_ptr().swap(*this);
4934}
4935
4936template<class _Tp>
4937template<class _Yp>
4938shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004939 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004940 : __ptr_(__r.__ptr_),
4941 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4942{
4943 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004944 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004945}
4946
4947template<class _Tp>
4948shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004949weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004950{
4951 shared_ptr<_Tp> __r;
4952 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4953 if (__r.__cntrl_)
4954 __r.__ptr_ = __ptr_;
4955 return __r;
4956}
4957
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004958#if _LIBCPP_STD_VER > 14
4959template <class _Tp = void> struct owner_less;
4960#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004961template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004962#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963
4964template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004965struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004966 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004967{
4968 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004969 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004970 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004971 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004972 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004973 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004974 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004975 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004976 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004978};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004979
4980template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004981struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4983{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004984 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004985 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004986 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004987 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004988 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004989 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004990 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004991 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004992 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993 {return __x.owner_before(__y);}
4994};
4995
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004996#if _LIBCPP_STD_VER > 14
4997template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004998struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004999{
5000 template <class _Tp, class _Up>
5001 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005002 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005003 {return __x.owner_before(__y);}
5004 template <class _Tp, class _Up>
5005 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005006 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005007 {return __x.owner_before(__y);}
5008 template <class _Tp, class _Up>
5009 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005010 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005011 {return __x.owner_before(__y);}
5012 template <class _Tp, class _Up>
5013 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005014 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005015 {return __x.owner_before(__y);}
5016 typedef void is_transparent;
5017};
5018#endif
5019
Howard Hinnantc51e1022010-05-11 19:42:16 +00005020template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005021class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005022{
5023 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005024protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005025 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005026 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005028 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005030 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5031 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005033 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005034public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005036 shared_ptr<_Tp> shared_from_this()
5037 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005039 shared_ptr<_Tp const> shared_from_this() const
5040 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005041
Eric Fiselier84006862016-06-02 00:15:35 +00005042#if _LIBCPP_STD_VER > 14
5043 _LIBCPP_INLINE_VISIBILITY
5044 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5045 { return __weak_this_; }
5046
5047 _LIBCPP_INLINE_VISIBILITY
5048 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5049 { return __weak_this_; }
5050#endif // _LIBCPP_STD_VER > 14
5051
Howard Hinnantc51e1022010-05-11 19:42:16 +00005052 template <class _Up> friend class shared_ptr;
5053};
5054
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005055template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005056struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005057{
5058 typedef shared_ptr<_Tp> argument_type;
5059 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005060
Howard Hinnant756c69b2010-09-22 16:48:34 +00005061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005062 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005063 {
5064 return hash<_Tp*>()(__ptr.get());
5065 }
5066};
5067
Howard Hinnantc834c512011-11-29 18:15:50 +00005068template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005069inline _LIBCPP_INLINE_VISIBILITY
5070basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005071operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005072
Eric Fiselier9b492672016-06-18 02:12:53 +00005073
5074#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005075
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005076class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005077{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005078 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005079public:
5080 void lock() _NOEXCEPT;
5081 void unlock() _NOEXCEPT;
5082
5083private:
5084 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5085 __sp_mut(const __sp_mut&);
5086 __sp_mut& operator=(const __sp_mut&);
5087
Howard Hinnant8331b762013-03-06 23:30:19 +00005088 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005089};
5090
Mehdi Amini228053d2017-05-04 17:08:54 +00005091_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5092__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005093
5094template <class _Tp>
5095inline _LIBCPP_INLINE_VISIBILITY
5096bool
5097atomic_is_lock_free(const shared_ptr<_Tp>*)
5098{
5099 return false;
5100}
5101
5102template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005103_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005104shared_ptr<_Tp>
5105atomic_load(const shared_ptr<_Tp>* __p)
5106{
5107 __sp_mut& __m = __get_sp_mut(__p);
5108 __m.lock();
5109 shared_ptr<_Tp> __q = *__p;
5110 __m.unlock();
5111 return __q;
5112}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005113
Howard Hinnant9fa30202012-07-30 01:40:57 +00005114template <class _Tp>
5115inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005116_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005117shared_ptr<_Tp>
5118atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5119{
5120 return atomic_load(__p);
5121}
5122
5123template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005124_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005125void
5126atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5127{
5128 __sp_mut& __m = __get_sp_mut(__p);
5129 __m.lock();
5130 __p->swap(__r);
5131 __m.unlock();
5132}
5133
5134template <class _Tp>
5135inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005136_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005137void
5138atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5139{
5140 atomic_store(__p, __r);
5141}
5142
5143template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005144_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005145shared_ptr<_Tp>
5146atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5147{
5148 __sp_mut& __m = __get_sp_mut(__p);
5149 __m.lock();
5150 __p->swap(__r);
5151 __m.unlock();
5152 return __r;
5153}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005154
Howard Hinnant9fa30202012-07-30 01:40:57 +00005155template <class _Tp>
5156inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005157_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005158shared_ptr<_Tp>
5159atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5160{
5161 return atomic_exchange(__p, __r);
5162}
5163
5164template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005165_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005166bool
5167atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5168{
Marshall Clow4201ee82016-05-18 17:50:13 +00005169 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005170 __sp_mut& __m = __get_sp_mut(__p);
5171 __m.lock();
5172 if (__p->__owner_equivalent(*__v))
5173 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005174 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005175 *__p = __w;
5176 __m.unlock();
5177 return true;
5178 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005179 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005180 *__v = *__p;
5181 __m.unlock();
5182 return false;
5183}
5184
5185template <class _Tp>
5186inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005187_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005188bool
5189atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5190{
5191 return atomic_compare_exchange_strong(__p, __v, __w);
5192}
5193
5194template <class _Tp>
5195inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005196_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005197bool
5198atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5199 shared_ptr<_Tp> __w, memory_order, memory_order)
5200{
5201 return atomic_compare_exchange_strong(__p, __v, __w);
5202}
5203
5204template <class _Tp>
5205inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005206_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005207bool
5208atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5209 shared_ptr<_Tp> __w, memory_order, memory_order)
5210{
5211 return atomic_compare_exchange_weak(__p, __v, __w);
5212}
5213
Eric Fiselier9b492672016-06-18 02:12:53 +00005214#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005215
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005216//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005217#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5218# ifndef _LIBCPP_CXX03_LANG
5219enum class pointer_safety : unsigned char {
5220 relaxed,
5221 preferred,
5222 strict
5223};
5224# endif
5225#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005226struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005228 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005229 {
5230 relaxed,
5231 preferred,
5232 strict
5233 };
5234
Howard Hinnant49e145e2012-10-30 19:06:59 +00005235 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005236
Howard Hinnant756c69b2010-09-22 16:48:34 +00005237 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005238 pointer_safety() : __v_() {}
5239
5240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005241 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005243 operator int() const {return __v_;}
5244};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005245#endif
5246
5247#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005248 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005249_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5250#else
5251// This function is only offered in C++03 under ABI v1.
5252# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5253inline _LIBCPP_INLINE_VISIBILITY
5254pointer_safety get_pointer_safety() _NOEXCEPT {
5255 return pointer_safety::relaxed;
5256}
5257# endif
5258#endif
5259
Howard Hinnantc51e1022010-05-11 19:42:16 +00005260
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005261_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5262_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5263_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005264_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005265
5266template <class _Tp>
5267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005268_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005269undeclare_reachable(_Tp* __p)
5270{
5271 return static_cast<_Tp*>(__undeclare_reachable(__p));
5272}
5273
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005274_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005275
Marshall Clow8982dcd2015-07-13 20:04:56 +00005276// --- Helper for container swap --
5277template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005278inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005279void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5280#if _LIBCPP_STD_VER >= 14
5281 _NOEXCEPT
5282#else
5283 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5284#endif
5285{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005286 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005287 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5288}
5289
5290template <typename _Alloc>
5291_LIBCPP_INLINE_VISIBILITY
5292void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5293#if _LIBCPP_STD_VER >= 14
5294 _NOEXCEPT
5295#else
5296 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5297#endif
5298{
5299 using _VSTD::swap;
5300 swap(__a1, __a2);
5301}
5302
5303template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005304inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005305void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5306
Marshall Clowff91de82015-08-18 19:51:37 +00005307template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005308struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005309 _Traits::propagate_on_container_move_assignment::value
5310#if _LIBCPP_STD_VER > 14
5311 || _Traits::is_always_equal::value
5312#else
5313 && is_nothrow_move_assignable<_Alloc>::value
5314#endif
5315 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005316
Marshall Clowa591b9a2016-07-11 21:38:08 +00005317
5318#ifndef _LIBCPP_HAS_NO_VARIADICS
5319template <class _Tp, class _Alloc>
5320struct __temp_value {
5321 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005322
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005323 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005324 _Alloc &__a;
5325
5326 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5327 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005328
Marshall Clowa591b9a2016-07-11 21:38:08 +00005329 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005330 _LIBCPP_NO_CFI
5331 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5332 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5333 _VSTD::forward<_Args>(__args)...);
5334 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005335
Marshall Clowa591b9a2016-07-11 21:38:08 +00005336 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5337 };
5338#endif
5339
Marshall Clowe46031a2018-07-02 18:41:15 +00005340template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005341struct __is_allocator : false_type {};
5342
5343template<typename _Alloc>
5344struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005345 typename __void_t<typename _Alloc::value_type>::type,
5346 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5347 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005348 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005349
Eric Fiselier74ebee62019-06-08 01:31:19 +00005350// __builtin_new_allocator -- A non-templated helper for allocating and
5351// deallocating memory using __builtin_operator_new and
5352// __builtin_operator_delete. It should be used in preference to
5353// `std::allocator<T>` to avoid additional instantiations.
5354struct __builtin_new_allocator {
5355 struct __builtin_new_deleter {
5356 typedef void* pointer_type;
5357
5358 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5359 : __size_(__size), __align_(__align) {}
5360
5361 void operator()(void* p) const _NOEXCEPT {
5362 std::__libcpp_deallocate(p, __size_, __align_);
5363 }
5364
5365 private:
5366 size_t __size_;
5367 size_t __align_;
5368 };
5369
5370 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5371
5372 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5373 return __holder_t(std::__libcpp_allocate(__s, __align),
5374 __builtin_new_deleter(__s, __align));
5375 }
5376
5377 static void __deallocate_bytes(void* __p, size_t __s,
5378 size_t __align) _NOEXCEPT {
5379 std::__libcpp_deallocate(__p, __s, __align);
5380 }
5381
5382 template <class _Tp>
5383 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5384 static __holder_t __allocate_type(size_t __n) {
5385 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5386 }
5387
5388 template <class _Tp>
5389 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5390 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5391 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5392 }
5393};
5394
5395
Howard Hinnantc51e1022010-05-11 19:42:16 +00005396_LIBCPP_END_NAMESPACE_STD
5397
Eric Fiselierf4433a32017-05-31 22:07:49 +00005398_LIBCPP_POP_MACROS
5399
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005400#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005401# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005402#endif
5403
Howard Hinnantc51e1022010-05-11 19:42:16 +00005404#endif // _LIBCPP_MEMORY