blob: 49627147b06143384ff93fd43cd44d544c399b84 [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
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400453template<class T>
454shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
455template<class T, class D>
456shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
457
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458// shared_ptr comparisons:
459template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000460 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000461template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000462 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000463template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000464 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000465template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000466 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000467template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000468 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000469template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000470 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
471
472template <class T>
473 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
474template <class T>
475 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
476template <class T>
477 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
478template <class T>
479 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
480template <class T>
481 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
482template <class T>
483bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
484template <class T>
485 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
486template <class T>
487 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
488template <class T>
489 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
490template <class T>
491 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
492template <class T>
493 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
494template <class T>
495 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000498template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000499
500// shared_ptr casts:
501template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000502 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000503template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000504 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000505template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000506 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000507
508// shared_ptr I/O:
509template<class E, class T, class Y>
510 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
511
512// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000513template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000514
515template<class T, class... Args>
516 shared_ptr<T> make_shared(Args&&... args);
517template<class T, class A, class... Args>
518 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
519
520template<class T>
521class weak_ptr
522{
523public:
524 typedef T element_type;
525
526 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 constexpr weak_ptr() noexcept;
528 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
529 weak_ptr(weak_ptr const& r) noexcept;
530 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000531 weak_ptr(weak_ptr&& r) noexcept; // C++14
532 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000533
534 // destructor
535 ~weak_ptr();
536
537 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000538 weak_ptr& operator=(weak_ptr const& r) noexcept;
539 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
540 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000541 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
542 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543
544 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000545 void swap(weak_ptr& r) noexcept;
546 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000547
548 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000549 long use_count() const noexcept;
550 bool expired() const noexcept;
551 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000552 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
553 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000554};
555
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400556template<class T>
557weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
558
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000560template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000561
562// class owner_less:
563template<class T> struct owner_less;
564
565template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000566struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000567 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
568{
569 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000570 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
571 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
572 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000573};
574
575template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000576struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
578{
579 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000580 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
581 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
583};
584
585template <> // Added in C++14
586struct owner_less<void>
587{
588 template <class _Tp, class _Up>
589 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
590 template <class _Tp, class _Up>
591 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
592 template <class _Tp, class _Up>
593 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
594 template <class _Tp, class _Up>
595 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
596
597 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000598};
599
600template<class T>
601class enable_shared_from_this
602{
603protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000604 constexpr enable_shared_from_this() noexcept;
605 enable_shared_from_this(enable_shared_from_this const&) noexcept;
606 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000607 ~enable_shared_from_this();
608public:
609 shared_ptr<T> shared_from_this();
610 shared_ptr<T const> shared_from_this() const;
611};
612
613template<class T>
614 bool atomic_is_lock_free(const shared_ptr<T>* p);
615template<class T>
616 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
617template<class T>
618 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
619template<class T>
620 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
621template<class T>
622 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
623template<class T>
624 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
625template<class T>
626 shared_ptr<T>
627 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
628template<class T>
629 bool
630 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
631template<class T>
632 bool
633 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
634template<class T>
635 bool
636 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
637 shared_ptr<T> w, memory_order success,
638 memory_order failure);
639template<class T>
640 bool
641 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
642 shared_ptr<T> w, memory_order success,
643 memory_order failure);
644// Hash support
645template <class T> struct hash;
646template <class T, class D> struct hash<unique_ptr<T, D> >;
647template <class T> struct hash<shared_ptr<T> >;
648
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000649template <class T, class Alloc>
650 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
651
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000652// Pointer safety
653enum class pointer_safety { relaxed, preferred, strict };
654void declare_reachable(void *p);
655template <class T> T *undeclare_reachable(T *p);
656void declare_no_pointers(char *p, size_t n);
657void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000658pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000659
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
661
662} // std
663
664*/
665
666#include <__config>
667#include <type_traits>
668#include <typeinfo>
669#include <cstddef>
670#include <cstdint>
671#include <new>
672#include <utility>
673#include <limits>
674#include <iterator>
675#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000676#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000677#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000678#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000679#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000680#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000681# include <atomic>
682#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000683#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000684
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000685#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688
Eric Fiselierf4433a32017-05-31 22:07:49 +0000689_LIBCPP_PUSH_MACROS
690#include <__undef_macros>
691
692
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693_LIBCPP_BEGIN_NAMESPACE_STD
694
Eric Fiselier89659d12015-07-07 00:27:16 +0000695template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000696inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000697_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
698#if !defined(_LIBCPP_HAS_NO_THREADS) && \
699 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000700 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000701 return __atomic_load_n(__value, __ATOMIC_RELAXED);
702#else
703 return *__value;
704#endif
705}
706
Kuba Breckade9d6792016-09-04 09:55:12 +0000707template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000708inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000709_ValueType __libcpp_acquire_load(_ValueType const* __value) {
710#if !defined(_LIBCPP_HAS_NO_THREADS) && \
711 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000712 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000713 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
714#else
715 return *__value;
716#endif
717}
718
Marshall Clow78dbe462016-11-14 18:22:19 +0000719// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000720
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721template <class _Tp> class allocator;
722
Michael Park99f9e912020-03-04 11:27:14 -0500723#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724template <>
Michael Park99f9e912020-03-04 11:27:14 -0500725class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726{
727public:
728 typedef void* pointer;
729 typedef const void* const_pointer;
730 typedef void value_type;
731
732 template <class _Up> struct rebind {typedef allocator<_Up> other;};
733};
734
Howard Hinnant9f771052012-01-19 23:15:22 +0000735template <>
Michael Park99f9e912020-03-04 11:27:14 -0500736class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000737{
738public:
739 typedef const void* pointer;
740 typedef const void* const_pointer;
741 typedef const void value_type;
742
743 template <class _Up> struct rebind {typedef allocator<_Up> other;};
744};
Michael Park99f9e912020-03-04 11:27:14 -0500745#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000746
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747// pointer_traits
748
Marshall Clow0be70c32017-06-14 21:23:57 +0000749template <class _Tp, class = void>
750struct __has_element_type : false_type {};
751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000753struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000754 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755
756template <class _Ptr, bool = __has_element_type<_Ptr>::value>
757struct __pointer_traits_element_type;
758
759template <class _Ptr>
760struct __pointer_traits_element_type<_Ptr, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
765#ifndef _LIBCPP_HAS_NO_VARIADICS
766
767template <template <class, class...> class _Sp, class _Tp, class ..._Args>
768struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
769{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000770 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771};
772
773template <template <class, class...> class _Sp, class _Tp, class ..._Args>
774struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
775{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000776 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777};
778
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000779#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780
781template <template <class> class _Sp, class _Tp>
782struct __pointer_traits_element_type<_Sp<_Tp>, true>
783{
784 typedef typename _Sp<_Tp>::element_type type;
785};
786
787template <template <class> class _Sp, class _Tp>
788struct __pointer_traits_element_type<_Sp<_Tp>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class> class _Sp, class _Tp, class _A0>
794struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
795{
796 typedef typename _Sp<_Tp, _A0>::element_type type;
797};
798
799template <template <class, class> class _Sp, class _Tp, class _A0>
800struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
801{
802 typedef _Tp type;
803};
804
805template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
806struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
807{
808 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
809};
810
811template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
812struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
813{
814 typedef _Tp type;
815};
816
817template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
818 class _A1, class _A2>
819struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
820{
821 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
822};
823
824template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
825 class _A1, class _A2>
826struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
827{
828 typedef _Tp type;
829};
830
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000831#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832
Marshall Clow0be70c32017-06-14 21:23:57 +0000833template <class _Tp, class = void>
834struct __has_difference_type : false_type {};
835
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000837struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000838 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839
840template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
841struct __pointer_traits_difference_type
842{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000843 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844};
845
846template <class _Ptr>
847struct __pointer_traits_difference_type<_Ptr, true>
848{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000849 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850};
851
852template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500853struct __has_rebind
854{
855private:
856 struct __two {char __lx; char __lxx;};
857 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500858 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500859 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500860 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500861public:
862 static const bool value = sizeof(__test<_Tp>(0)) == 1;
863};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864
865template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
866struct __pointer_traits_rebind
867{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000868#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000869 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000871 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872#endif
873};
874
875#ifndef _LIBCPP_HAS_NO_VARIADICS
876
877template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
879{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000880#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000881 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000883 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884#endif
885};
886
887template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
889{
890 typedef _Sp<_Up, _Args...> type;
891};
892
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000893#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894
895template <template <class> class _Sp, class _Tp, class _Up>
896struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
897{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000898#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 typedef typename _Sp<_Tp>::template rebind<_Up> type;
900#else
901 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
902#endif
903};
904
905template <template <class> class _Sp, class _Tp, class _Up>
906struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
907{
908 typedef _Sp<_Up> type;
909};
910
911template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
913{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000914#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
916#else
917 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
918#endif
919};
920
921template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
923{
924 typedef _Sp<_Up, _A0> type;
925};
926
927template <template <class, class, class> class _Sp, class _Tp, class _A0,
928 class _A1, class _Up>
929struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
930{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
933#else
934 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
935#endif
936};
937
938template <template <class, class, class> class _Sp, class _Tp, class _A0,
939 class _A1, class _Up>
940struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
941{
942 typedef _Sp<_Up, _A0, _A1> type;
943};
944
945template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
946 class _A1, class _A2, class _Up>
947struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
948{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000949#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
951#else
952 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
953#endif
954};
955
956template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
957 class _A1, class _A2, class _Up>
958struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
959{
960 typedef _Sp<_Up, _A0, _A1, _A2> type;
961};
962
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000963#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
965template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000966struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967{
968 typedef _Ptr pointer;
969 typedef typename __pointer_traits_element_type<pointer>::type element_type;
970 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
971
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000972#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000973 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974#else
975 template <class _Up> struct rebind
976 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000977#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
984 __nat, element_type>::type& __r)
985 {return pointer::pointer_to(__r);}
986};
987
988template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000989struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990{
991 typedef _Tp* pointer;
992 typedef _Tp element_type;
993 typedef ptrdiff_t difference_type;
994
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000995#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 template <class _Up> using rebind = _Up*;
997#else
998 template <class _Up> struct rebind {typedef _Up* other;};
999#endif
1000
1001private:
1002 struct __nat {};
1003public:
Louis Dionne44e1ea82018-11-13 17:04:05 +00001004 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +00001006 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001007 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008};
1009
Eric Fiselierae3ab842015-08-23 02:56:05 +00001010template <class _From, class _To>
1011struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001012#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +00001013 typedef typename pointer_traits<_From>::template rebind<_To> type;
1014#else
1015 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
1016#endif
1017};
1018
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019// allocator_traits
1020
Marshall Clow0be70c32017-06-14 21:23:57 +00001021template <class _Tp, class = void>
1022struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023
1024template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001025struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001026 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028namespace __pointer_type_imp
1029{
1030
1031template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1032struct __pointer_type
1033{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001034 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035};
1036
1037template <class _Tp, class _Dp>
1038struct __pointer_type<_Tp, _Dp, false>
1039{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001040 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041};
1042
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001043} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044
1045template <class _Tp, class _Dp>
1046struct __pointer_type
1047{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001048 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 +00001049};
1050
Marshall Clow0be70c32017-06-14 21:23:57 +00001051template <class _Tp, class = void>
1052struct __has_const_pointer : false_type {};
1053
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001055struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001056 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057
1058template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1059struct __const_pointer
1060{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001061 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062};
1063
1064template <class _Tp, class _Ptr, class _Alloc>
1065struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1066{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001067#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001068 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069#else
1070 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1071#endif
1072};
1073
Marshall Clow0be70c32017-06-14 21:23:57 +00001074template <class _Tp, class = void>
1075struct __has_void_pointer : false_type {};
1076
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001078struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001079 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080
1081template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1082struct __void_pointer
1083{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001084 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085};
1086
1087template <class _Ptr, class _Alloc>
1088struct __void_pointer<_Ptr, _Alloc, false>
1089{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001090#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001091 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001093 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094#endif
1095};
1096
Marshall Clow0be70c32017-06-14 21:23:57 +00001097template <class _Tp, class = void>
1098struct __has_const_void_pointer : false_type {};
1099
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001101struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001102 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103
1104template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1105struct __const_void_pointer
1106{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001107 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108};
1109
1110template <class _Ptr, class _Alloc>
1111struct __const_void_pointer<_Ptr, _Alloc, false>
1112{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001113#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001114 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001116 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117#endif
1118};
1119
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001120
1121template <bool _UsePointerTraits> struct __to_address_helper;
1122
1123template <> struct __to_address_helper<true> {
1124 template <class _Pointer>
1125 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1126
1127 template <class _Pointer>
1128 _LIBCPP_CONSTEXPR
1129 static __return_type<_Pointer>
1130 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1131};
1132
1133template <class _Pointer, bool _Dummy = true>
1134using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1135
1136
Howard Hinnantc834c512011-11-29 18:15:50 +00001137template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001138inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001139_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001140__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001142 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 return __p;
1144}
1145
1146template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001147inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1148typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1149__to_address(const _Pointer& __p) _NOEXCEPT {
1150 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001151}
1152
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001153template <> struct __to_address_helper<false> {
1154 template <class _Pointer>
1155 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001156
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001157 template <class _Pointer>
1158 _LIBCPP_CONSTEXPR
1159 static __return_type<_Pointer>
1160 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1161};
1162
1163
1164#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001165template <class _Tp>
1166inline _LIBCPP_INLINE_VISIBILITY constexpr
1167_Tp*
1168to_address(_Tp* __p) _NOEXCEPT
1169{
1170 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1171 return __p;
1172}
1173
1174template <class _Pointer>
1175inline _LIBCPP_INLINE_VISIBILITY
1176auto
1177to_address(const _Pointer& __p) _NOEXCEPT
1178{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001179 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001180}
1181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182
Marshall Clow0be70c32017-06-14 21:23:57 +00001183template <class _Tp, class = void>
1184struct __has_size_type : false_type {};
1185
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001187struct __has_size_type<_Tp,
1188 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001190template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191struct __size_type
1192{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001193 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194};
1195
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001196template <class _Alloc, class _DiffType>
1197struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001199 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200};
1201
Marshall Clow0be70c32017-06-14 21:23:57 +00001202template <class _Tp, class = void>
1203struct __has_propagate_on_container_copy_assignment : false_type {};
1204
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001206struct __has_propagate_on_container_copy_assignment<_Tp,
1207 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1208 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209
1210template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1211struct __propagate_on_container_copy_assignment
1212{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001213 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214};
1215
1216template <class _Alloc>
1217struct __propagate_on_container_copy_assignment<_Alloc, true>
1218{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001219 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220};
1221
Marshall Clow0be70c32017-06-14 21:23:57 +00001222template <class _Tp, class = void>
1223struct __has_propagate_on_container_move_assignment : false_type {};
1224
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001226struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001227 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1228 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
1230template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1231struct __propagate_on_container_move_assignment
1232{
1233 typedef false_type type;
1234};
1235
1236template <class _Alloc>
1237struct __propagate_on_container_move_assignment<_Alloc, true>
1238{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001239 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240};
1241
Marshall Clow0be70c32017-06-14 21:23:57 +00001242template <class _Tp, class = void>
1243struct __has_propagate_on_container_swap : false_type {};
1244
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001246struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001247 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1248 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249
1250template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1251struct __propagate_on_container_swap
1252{
1253 typedef false_type type;
1254};
1255
1256template <class _Alloc>
1257struct __propagate_on_container_swap<_Alloc, true>
1258{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001259 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260};
1261
Marshall Clow0be70c32017-06-14 21:23:57 +00001262template <class _Tp, class = void>
1263struct __has_is_always_equal : false_type {};
1264
Marshall Clow0b587562015-06-02 16:34:03 +00001265template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001266struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001267 typename __void_t<typename _Tp::is_always_equal>::type>
1268 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001269
1270template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1271struct __is_always_equal
1272{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001273 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001274};
1275
1276template <class _Alloc>
1277struct __is_always_equal<_Alloc, true>
1278{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001279 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001280};
1281
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1283struct __has_rebind_other
1284{
1285private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001286 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001288 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001290 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291public:
1292 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1293};
1294
1295template <class _Tp, class _Up>
1296struct __has_rebind_other<_Tp, _Up, false>
1297{
1298 static const bool value = false;
1299};
1300
1301template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1302struct __allocator_traits_rebind
1303{
Michael Park99f9e912020-03-04 11:27:14 -05001304 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001305 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001306 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307};
1308
1309#ifndef _LIBCPP_HAS_NO_VARIADICS
1310
1311template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1312struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1313{
Michael Park99f9e912020-03-04 11:27:14 -05001314 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001315 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001316 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317};
1318
1319template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1320struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1321{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001322 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323};
1324
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001325#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
1327template <template <class> class _Alloc, class _Tp, class _Up>
1328struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1329{
1330 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1331};
1332
1333template <template <class> class _Alloc, class _Tp, class _Up>
1334struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1335{
1336 typedef _Alloc<_Up> type;
1337};
1338
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1340struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1341{
1342 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1343};
1344
1345template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1346struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1347{
1348 typedef _Alloc<_Up, _A0> type;
1349};
1350
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1352 class _A1, class _Up>
1353struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1354{
1355 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1356};
1357
1358template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1359 class _A1, class _Up>
1360struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1361{
1362 typedef _Alloc<_Up, _A0, _A1> type;
1363};
1364
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1366 class _A1, class _A2, class _Up>
1367struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1368{
1369 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1370};
1371
1372template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1373 class _A1, class _A2, class _Up>
1374struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1375{
1376 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1377};
1378
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001379#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001381#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382
Michael Park99f9e912020-03-04 11:27:14 -05001383_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1385auto
1386__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001387 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001388_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389
1390template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1391auto
1392__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1393 -> false_type;
1394
1395template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1396struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001397 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1398 declval<_SizeType>(),
1399 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400{
1401};
1402
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001403#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404
1405template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1406struct __has_allocate_hint
1407 : true_type
1408{
1409};
1410
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001411#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001413#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414
Michael Park99f9e912020-03-04 11:27:14 -05001415_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1416template <class _Alloc, class _Tp, class... _Args>
1417auto
1418__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1419 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1420_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421
1422template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001423auto
1424__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1425 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426
1427template <class _Alloc, class _Pointer, class ..._Args>
1428struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001429 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1430 declval<_Pointer>(),
1431 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432{
1433};
1434
Michael Park99f9e912020-03-04 11:27:14 -05001435_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436template <class _Alloc, class _Pointer>
1437auto
1438__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1439 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001440_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
1442template <class _Alloc, class _Pointer>
1443auto
1444__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1445 -> false_type;
1446
1447template <class _Alloc, class _Pointer>
1448struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001449 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1450 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451{
1452};
1453
Michael Park99f9e912020-03-04 11:27:14 -05001454_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455template <class _Alloc>
1456auto
1457__has_max_size_test(_Alloc&& __a)
1458 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001459_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460
1461template <class _Alloc>
1462auto
1463__has_max_size_test(const volatile _Alloc& __a)
1464 -> false_type;
1465
1466template <class _Alloc>
1467struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001468 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469{
1470};
1471
1472template <class _Alloc>
1473auto
1474__has_select_on_container_copy_construction_test(_Alloc&& __a)
1475 -> decltype(__a.select_on_container_copy_construction(), true_type());
1476
1477template <class _Alloc>
1478auto
1479__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1480 -> false_type;
1481
1482template <class _Alloc>
1483struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001484 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485{
1486};
1487
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001488#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001490template <class _Alloc, class _Pointer, class _Tp, class = void>
1491struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001493template <class _Alloc, class _Pointer, class _Tp>
1494struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1495 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1496>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001498template <class _Alloc, class _Pointer, class = void>
1499struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500
1501template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001502struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1503 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1504>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505
1506template <class _Alloc>
1507struct __has_max_size
1508 : true_type
1509{
1510};
1511
1512template <class _Alloc>
1513struct __has_select_on_container_copy_construction
1514 : false_type
1515{
1516};
1517
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001518#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001520template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1521struct __alloc_traits_difference_type
1522{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001523 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001524};
1525
1526template <class _Alloc, class _Ptr>
1527struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1528{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001529 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001530};
1531
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001532template <class _Tp>
1533struct __is_default_allocator : false_type {};
1534
1535template <class _Tp>
1536struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1537
Eric Fiselier909fe962019-09-13 16:09:33 +00001538
1539
1540template <class _Alloc,
1541 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1542 >
1543struct __is_cpp17_move_insertable;
1544template <class _Alloc>
1545struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1546template <class _Alloc>
1547struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1548
1549template <class _Alloc,
1550 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1551 >
1552struct __is_cpp17_copy_insertable;
1553template <class _Alloc>
1554struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1555template <class _Alloc>
1556struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1557 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1558 __is_cpp17_move_insertable<_Alloc>::value>
1559 {};
1560
1561
1562
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001564struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565{
1566 typedef _Alloc allocator_type;
1567 typedef typename allocator_type::value_type value_type;
1568
1569 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1570 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1571 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1572 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1573
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001574 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1575 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
1577 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1578 propagate_on_container_copy_assignment;
1579 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1580 propagate_on_container_move_assignment;
1581 typedef typename __propagate_on_container_swap<allocator_type>::type
1582 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001583 typedef typename __is_always_equal<allocator_type>::type
1584 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001586#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001588 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001589 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001590#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 template <class _Tp> struct rebind_alloc
1592 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1593 template <class _Tp> struct rebind_traits
1594 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001595#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596
Marshall Clow0e58cae2017-11-26 02:55:38 +00001597 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 static pointer allocate(allocator_type& __a, size_type __n)
1599 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001600 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001602 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1604
Howard Hinnant756c69b2010-09-22 16:48:34 +00001605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001606 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 {__a.deallocate(__p, __n);}
1608
1609#ifndef _LIBCPP_HAS_NO_VARIADICS
1610 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001613 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001614 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001615#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001617 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001618 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619 {
1620 ::new ((void*)__p) _Tp();
1621 }
1622 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001623 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001624 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001626 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1627 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628 }
1629 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001630 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001631 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632 const _A1& __a1)
1633 {
1634 ::new ((void*)__p) _Tp(__a0, __a1);
1635 }
1636 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001637 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001638 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 const _A1& __a1, const _A2& __a2)
1640 {
1641 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1642 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001643#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644
1645 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 static void destroy(allocator_type& __a, _Tp* __p)
1648 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1649
Howard Hinnant756c69b2010-09-22 16:48:34 +00001650 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001651 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1653
Howard Hinnant756c69b2010-09-22 16:48:34 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655 static allocator_type
1656 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001657 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 __has_select_on_container_copy_construction<const allocator_type>(),
1659 __a);}
1660
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001661 template <class _Ptr>
1662 _LIBCPP_INLINE_VISIBILITY
1663 static
1664 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001665 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001666 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001667 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1668 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001669 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001670 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001671#ifdef _LIBCPP_NO_EXCEPTIONS
1672 _VSTD::move(*__begin1)
1673#else
1674 _VSTD::move_if_noexcept(*__begin1)
1675#endif
1676 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 }
1678
1679 template <class _Tp>
1680 _LIBCPP_INLINE_VISIBILITY
1681 static
1682 typename enable_if
1683 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001684 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001685 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1686 is_trivially_move_constructible<_Tp>::value,
1687 void
1688 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001689 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001690 {
1691 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001692 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001693 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001694 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001695 __begin2 += _Np;
1696 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001697 }
1698
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001699 template <class _Iter, class _Ptr>
1700 _LIBCPP_INLINE_VISIBILITY
1701 static
1702 void
1703 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1704 {
1705 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001706 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001707 }
1708
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001709 template <class _SourceTp, class _DestTp,
1710 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1711 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001712 _LIBCPP_INLINE_VISIBILITY
1713 static
1714 typename enable_if
1715 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001716 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001717 is_same<_RawSourceTp, _RawDestTp>::value &&
1718 (__is_default_allocator<allocator_type>::value ||
1719 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001720 void
1721 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001722 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001723 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001724 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001725 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001726 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001727 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001728 __begin2 += _Np;
1729 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001730 }
1731
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001732 template <class _Ptr>
1733 _LIBCPP_INLINE_VISIBILITY
1734 static
1735 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001736 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001737 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001738 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1739 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001740 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001741 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001742 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001743#ifdef _LIBCPP_NO_EXCEPTIONS
1744 _VSTD::move(*--__end1)
1745#else
1746 _VSTD::move_if_noexcept(*--__end1)
1747#endif
1748 );
1749 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001750 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001751 }
1752
1753 template <class _Tp>
1754 _LIBCPP_INLINE_VISIBILITY
1755 static
1756 typename enable_if
1757 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001758 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001759 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1760 is_trivially_move_constructible<_Tp>::value,
1761 void
1762 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001763 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001764 {
1765 ptrdiff_t _Np = __end1 - __begin1;
1766 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001767 if (_Np > 0)
1768 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001769 }
1770
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771private:
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 Hinnantc51e1022010-05-11 19:42:16 +00001775 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001776 {
1777 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1778 return __a.allocate(__n, __hint);
1779 _LIBCPP_SUPPRESS_DEPRECATED_POP
1780 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001781 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001782 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001783 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 {return __a.allocate(__n);}
1785
1786#ifndef _LIBCPP_HAS_NO_VARIADICS
1787 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001790 {
1791 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1792 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1793 _LIBCPP_SUPPRESS_DEPRECATED_POP
1794 }
1795
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1799 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001800 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001802#else // _LIBCPP_HAS_NO_VARIADICS
1803 template <class _Tp, class _A0>
1804 _LIBCPP_INLINE_VISIBILITY
1805 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1806 const _A0& __a0)
1807 {__a.construct(__p, __a0);}
1808 template <class _Tp, class _A0>
1809 _LIBCPP_INLINE_VISIBILITY
1810 static void __construct(false_type, allocator_type&, _Tp* __p,
1811 const _A0& __a0)
1812 {
1813 ::new ((void*)__p) _Tp(__a0);
1814 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001815#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816
1817 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(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001820 {
1821 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1822 __a.destroy(__p);
1823 _LIBCPP_SUPPRESS_DEPRECATED_POP
1824 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 static void __destroy(false_type, allocator_type&, _Tp* __p)
1828 {
1829 __p->~_Tp();
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(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001834 {
1835 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1836 return __a.max_size();
1837 _LIBCPP_SUPPRESS_DEPRECATED_POP
1838 }
1839
Howard Hinnant756c69b2010-09-22 16:48:34 +00001840 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001841 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001842 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843
Howard Hinnant756c69b2010-09-22 16:48:34 +00001844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001846 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001850 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 {return __a;}
1852};
1853
Marshall Clow940e01c2015-04-07 05:21:38 +00001854template <class _Traits, class _Tp>
1855struct __rebind_alloc_helper
1856{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001857#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001858 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001859#else
1860 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1861#endif
1862};
1863
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864// allocator
1865
1866template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001867class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868{
1869public:
Michael Park99f9e912020-03-04 11:27:14 -05001870#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1871 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1872 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1873 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1874 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1875 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1876 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1877
1878 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1879#endif
1880
1881 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882
Howard Hinnant4931e092011-06-02 21:38:57 +00001883 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001884 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001885
Marshall Clowbc759762018-03-20 23:02:53 +00001886 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1887 allocator() _NOEXCEPT {}
1888
Louis Dionne481a2662018-09-23 18:35:00 +00001889 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001890 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1891 allocator(const allocator<_Up>&) _NOEXCEPT {}
1892
Michael Park99f9e912020-03-04 11:27:14 -05001893#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1894 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1895 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001896 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001897 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1898 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001899 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001900#endif
1901
1902 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001903 {
Michael Park99f9e912020-03-04 11:27:14 -05001904 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1905 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001906 __throw_length_error("allocator<T>::allocate(size_t n)"
1907 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001908 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001909 }
Michael Park99f9e912020-03-04 11:27:14 -05001910
1911#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1912 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1913 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1914#endif
1915
1916 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001917 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001918
1919#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1920 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001921 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001922
Howard Hinnant74279a52010-09-04 23:28:19 +00001923#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001925 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 void
1927 construct(_Up* __p, _Args&&... __args)
1928 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001929 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001931#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 _LIBCPP_INLINE_VISIBILITY
1933 void
1934 construct(pointer __p)
1935 {
1936 ::new((void*)__p) _Tp();
1937 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001938# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001939
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 template <class _A0>
1941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001942 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 construct(pointer __p, _A0& __a0)
1944 {
1945 ::new((void*)__p) _Tp(__a0);
1946 }
1947 template <class _A0>
1948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001949 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950 construct(pointer __p, const _A0& __a0)
1951 {
1952 ::new((void*)__p) _Tp(__a0);
1953 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001954# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 template <class _A0, class _A1>
1956 _LIBCPP_INLINE_VISIBILITY
1957 void
1958 construct(pointer __p, _A0& __a0, _A1& __a1)
1959 {
1960 ::new((void*)__p) _Tp(__a0, __a1);
1961 }
1962 template <class _A0, class _A1>
1963 _LIBCPP_INLINE_VISIBILITY
1964 void
1965 construct(pointer __p, const _A0& __a0, _A1& __a1)
1966 {
1967 ::new((void*)__p) _Tp(__a0, __a1);
1968 }
1969 template <class _A0, class _A1>
1970 _LIBCPP_INLINE_VISIBILITY
1971 void
1972 construct(pointer __p, _A0& __a0, const _A1& __a1)
1973 {
1974 ::new((void*)__p) _Tp(__a0, __a1);
1975 }
1976 template <class _A0, class _A1>
1977 _LIBCPP_INLINE_VISIBILITY
1978 void
1979 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1980 {
1981 ::new((void*)__p) _Tp(__a0, __a1);
1982 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001983#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001984 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986};
1987
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001988template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001989class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001990{
1991public:
Michael Park99f9e912020-03-04 11:27:14 -05001992#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1993 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1994 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1995 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1996 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1997 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1998 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1999
2000 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
2001#endif
2002
2003 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002004
2005 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00002006 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002007
Marshall Clowbc759762018-03-20 23:02:53 +00002008 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
2009 allocator() _NOEXCEPT {}
2010
2011 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00002012 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00002013 allocator(const allocator<_Up>&) _NOEXCEPT {}
2014
Michael Park99f9e912020-03-04 11:27:14 -05002015#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2016 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
2017 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002018 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05002019#endif
2020
2021 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00002022 {
Michael Park99f9e912020-03-04 11:27:14 -05002023 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
2024 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00002025 __throw_length_error("allocator<const T>::allocate(size_t n)"
2026 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05002027 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00002028 }
Michael Park99f9e912020-03-04 11:27:14 -05002029
2030#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2031 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
2032 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
2033#endif
2034
2035 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002036 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05002037
2038#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2039 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002040 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05002041
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002042#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2043 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05002044 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002045 void
2046 construct(_Up* __p, _Args&&... __args)
2047 {
2048 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
2049 }
2050#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2051 _LIBCPP_INLINE_VISIBILITY
2052 void
2053 construct(pointer __p)
2054 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002055 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002056 }
2057# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00002058
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002059 template <class _A0>
2060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002061 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002062 construct(pointer __p, _A0& __a0)
2063 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002064 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002065 }
2066 template <class _A0>
2067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002068 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002069 construct(pointer __p, const _A0& __a0)
2070 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002071 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002072 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002073# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2074 template <class _A0, class _A1>
2075 _LIBCPP_INLINE_VISIBILITY
2076 void
2077 construct(pointer __p, _A0& __a0, _A1& __a1)
2078 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002079 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002080 }
2081 template <class _A0, class _A1>
2082 _LIBCPP_INLINE_VISIBILITY
2083 void
2084 construct(pointer __p, const _A0& __a0, _A1& __a1)
2085 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002086 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002087 }
2088 template <class _A0, class _A1>
2089 _LIBCPP_INLINE_VISIBILITY
2090 void
2091 construct(pointer __p, _A0& __a0, const _A1& __a1)
2092 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002093 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002094 }
2095 template <class _A0, class _A1>
2096 _LIBCPP_INLINE_VISIBILITY
2097 void
2098 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2099 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002100 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002101 }
2102#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05002103 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2104#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002105};
2106
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107template <class _Tp, class _Up>
2108inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002109bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110
2111template <class _Tp, class _Up>
2112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002113bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114
2115template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002116class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117 : public iterator<output_iterator_tag,
2118 _Tp, // purposefully not C++03
2119 ptrdiff_t, // purposefully not C++03
2120 _Tp*, // purposefully not C++03
2121 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2122{
2123private:
2124 _OutputIterator __x_;
2125public:
2126 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2127 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2128 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002129 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002130#if _LIBCPP_STD_VER >= 14
2131 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002132 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002133#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2135 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2136 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002137#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002138 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002139#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140};
2141
2142template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002143_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002145get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146{
2147 pair<_Tp*, ptrdiff_t> __r(0, 0);
2148 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2149 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2150 / sizeof(_Tp);
2151 if (__n > __m)
2152 __n = __m;
2153 while (__n > 0)
2154 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002155#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002156 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002157 {
2158 std::align_val_t __al =
2159 std::align_val_t(std::alignment_of<_Tp>::value);
2160 __r.first = static_cast<_Tp*>(::operator new(
2161 __n * sizeof(_Tp), __al, nothrow));
2162 } else {
2163 __r.first = static_cast<_Tp*>(::operator new(
2164 __n * sizeof(_Tp), nothrow));
2165 }
2166#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002167 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002168 {
2169 // Since aligned operator new is unavailable, return an empty
2170 // buffer rather than one with invalid alignment.
2171 return __r;
2172 }
2173
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002175#endif
2176
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 if (__r.first)
2178 {
2179 __r.second = __n;
2180 break;
2181 }
2182 __n /= 2;
2183 }
2184 return __r;
2185}
2186
2187template <class _Tp>
2188inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002189void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2190{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002191 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002192}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193
Marshall Clowb22274f2017-01-24 22:22:33 +00002194#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002196struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197{
2198 _Tp* __ptr_;
2199};
2200
2201template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002202class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203{
2204private:
2205 _Tp* __ptr_;
2206public:
2207 typedef _Tp element_type;
2208
Dimitry Andric47269ce2020-03-13 19:36:26 +01002209 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
2210 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
2211 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002213 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002215 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002217 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002219 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
Dimitry Andric47269ce2020-03-13 19:36:26 +01002221 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002223 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
2224 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
2225 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 {
2227 _Tp* __t = __ptr_;
2228 __ptr_ = 0;
2229 return __t;
2230 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01002231 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 {
2233 if (__ptr_ != __p)
2234 delete __ptr_;
2235 __ptr_ = __p;
2236 }
2237
Dimitry Andric47269ce2020-03-13 19:36:26 +01002238 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
2239 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002241 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 {return auto_ptr<_Up>(release());}
2243};
2244
2245template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002246class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247{
2248public:
2249 typedef void element_type;
2250};
Marshall Clowb22274f2017-01-24 22:22:33 +00002251#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002253// Tag used to default initialize one or both of the pair's elements.
2254struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002255struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002256
Eric Fiselier9d355982017-04-12 23:45:53 +00002257template <class _Tp, int _Idx,
2258 bool _CanBeEmptyBase =
2259 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2260struct __compressed_pair_elem {
2261 typedef _Tp _ParamT;
2262 typedef _Tp& reference;
2263 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002266 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002267 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2268 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Eric Fiselier9d355982017-04-12 23:45:53 +00002270 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002271 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2272 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002273 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002274 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002275 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002276 : __value_(_VSTD::forward<_Up>(__u))
2277 {
2278 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002280
2281#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 template <class... _Args, size_t... _Indexes>
2283 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2284 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2285 __tuple_indices<_Indexes...>)
2286 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002289
Alex Lorenz76132112017-11-09 17:54:49 +00002290 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2291 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002292 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002295 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296};
2297
Eric Fiselier9d355982017-04-12 23:45:53 +00002298template <class _Tp, int _Idx>
2299struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2300 typedef _Tp _ParamT;
2301 typedef _Tp& reference;
2302 typedef const _Tp& const_reference;
2303 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002305 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2306 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2307 __compressed_pair_elem(__default_init_tag) {}
2308 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2309 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310
Eric Fiselier9d355982017-04-12 23:45:53 +00002311 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002312 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2313 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002314 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002315 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002316 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002317 : __value_type(_VSTD::forward<_Up>(__u))
2318 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002320#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002321 template <class... _Args, size_t... _Indexes>
2322 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2323 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2324 __tuple_indices<_Indexes...>)
2325 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002326#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327
Alex Lorenz76132112017-11-09 17:54:49 +00002328 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2329 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002330 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331};
2332
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002334class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2335 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002336 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2337 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002338
2339 // NOTE: This static assert should never fire because __compressed_pair
2340 // is *almost never* used in a scenario where it's possible for T1 == T2.
2341 // (The exception is std::function where it is possible that the function
2342 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002343 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002344 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2345 "The current implementation is NOT ABI-compatible with the previous "
2346 "implementation for this configuration");
2347
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002349 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002350 class = typename enable_if<
2351 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2352 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2353 >::type
2354 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002355 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002356 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002357
Eric Fiselier9d355982017-04-12 23:45:53 +00002358 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002359 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002360 __compressed_pair(_U1&& __t1, _U2&& __t2)
2361 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002363#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002364 template <class... _Args1, class... _Args2>
2365 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2366 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2367 tuple<_Args2...> __second_args)
2368 : _Base1(__pc, _VSTD::move(__first_args),
2369 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2370 _Base2(__pc, _VSTD::move(__second_args),
2371 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002372#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373
Eric Fiselier9d355982017-04-12 23:45:53 +00002374 _LIBCPP_INLINE_VISIBILITY
2375 typename _Base1::reference first() _NOEXCEPT {
2376 return static_cast<_Base1&>(*this).__get();
2377 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378
Eric Fiselier9d355982017-04-12 23:45:53 +00002379 _LIBCPP_INLINE_VISIBILITY
2380 typename _Base1::const_reference first() const _NOEXCEPT {
2381 return static_cast<_Base1 const&>(*this).__get();
2382 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383
Eric Fiselier9d355982017-04-12 23:45:53 +00002384 _LIBCPP_INLINE_VISIBILITY
2385 typename _Base2::reference second() _NOEXCEPT {
2386 return static_cast<_Base2&>(*this).__get();
2387 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388
Eric Fiselier9d355982017-04-12 23:45:53 +00002389 _LIBCPP_INLINE_VISIBILITY
2390 typename _Base2::const_reference second() const _NOEXCEPT {
2391 return static_cast<_Base2 const&>(*this).__get();
2392 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393
Eric Fiselier9d355982017-04-12 23:45:53 +00002394 _LIBCPP_INLINE_VISIBILITY
2395 void swap(__compressed_pair& __x)
2396 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2397 __is_nothrow_swappable<_T2>::value)
2398 {
2399 using std::swap;
2400 swap(first(), __x.first());
2401 swap(second(), __x.second());
2402 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403};
2404
2405template <class _T1, class _T2>
2406inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002407void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2408 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2409 __is_nothrow_swappable<_T2>::value) {
2410 __x.swap(__y);
2411}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002413// default_delete
2414
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002416struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002417 static_assert(!is_function<_Tp>::value,
2418 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002419#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002420 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002421#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002422 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002423#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002424 template <class _Up>
2425 _LIBCPP_INLINE_VISIBILITY
2426 default_delete(const default_delete<_Up>&,
2427 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2428 0) _NOEXCEPT {}
2429
2430 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2431 static_assert(sizeof(_Tp) > 0,
2432 "default_delete can not delete incomplete type");
2433 static_assert(!is_void<_Tp>::value,
2434 "default_delete can not delete incomplete type");
2435 delete __ptr;
2436 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437};
2438
2439template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002440struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2441private:
2442 template <class _Up>
2443 struct _EnableIfConvertible
2444 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2445
Howard Hinnant4500ca52011-12-18 21:19:44 +00002446public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002447#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002448 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002449#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002450 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002451#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002452
2453 template <class _Up>
2454 _LIBCPP_INLINE_VISIBILITY
2455 default_delete(const default_delete<_Up[]>&,
2456 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2457
2458 template <class _Up>
2459 _LIBCPP_INLINE_VISIBILITY
2460 typename _EnableIfConvertible<_Up>::type
2461 operator()(_Up* __ptr) const _NOEXCEPT {
2462 static_assert(sizeof(_Tp) > 0,
2463 "default_delete can not delete incomplete type");
2464 static_assert(!is_void<_Tp>::value,
2465 "default_delete can not delete void type");
2466 delete[] __ptr;
2467 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468};
2469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470template <class _Deleter>
2471struct __unique_ptr_deleter_sfinae {
2472 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2473 typedef const _Deleter& __lval_ref_type;
2474 typedef _Deleter&& __good_rval_ref_type;
2475 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476};
2477
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002478template <class _Deleter>
2479struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2480 typedef const _Deleter& __lval_ref_type;
2481 typedef const _Deleter&& __bad_rval_ref_type;
2482 typedef false_type __enable_rval_overload;
2483};
2484
2485template <class _Deleter>
2486struct __unique_ptr_deleter_sfinae<_Deleter&> {
2487 typedef _Deleter& __lval_ref_type;
2488 typedef _Deleter&& __bad_rval_ref_type;
2489 typedef false_type __enable_rval_overload;
2490};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002491
2492template <class _Tp, class _Dp = default_delete<_Tp> >
2493class _LIBCPP_TEMPLATE_VIS unique_ptr {
2494public:
2495 typedef _Tp element_type;
2496 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002497 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498
2499 static_assert(!is_rvalue_reference<deleter_type>::value,
2500 "the specified deleter type cannot be an rvalue reference");
2501
2502private:
2503 __compressed_pair<pointer, deleter_type> __ptr_;
2504
2505 struct __nat { int __for_bool_; };
2506
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002507 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002508
2509 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002510 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002511 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512
2513 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002514 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002515 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002516
2517 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002518 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002519 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002520
2521 template <bool _Dummy, class _Deleter = typename __dependent_type<
2522 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002523 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002524 typename enable_if<is_default_constructible<_Deleter>::value &&
2525 !is_pointer<_Deleter>::value>::type;
2526
2527 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002528 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002529 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2530
2531 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002532 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002533 is_convertible<typename _UPtr::pointer, pointer>::value &&
2534 !is_array<_Up>::value
2535 >::type;
2536
2537 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002538 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002539 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2540 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2541 >::type;
2542
2543 template <class _UDel>
2544 using _EnableIfDeleterAssignable = typename enable_if<
2545 is_assignable<_Dp&, _UDel&&>::value
2546 >::type;
2547
2548public:
2549 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002550 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002551 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002552 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002553
2554 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002555 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002556 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002557 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558
2559 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002560 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002561 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002562 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002563
2564 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002565 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002566 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002567 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002568 : __ptr_(__p, __d) {}
2569
2570 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002571 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002572 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002573 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002574 : __ptr_(__p, _VSTD::move(__d)) {
2575 static_assert(!is_reference<deleter_type>::value,
2576 "rvalue deleter bound to reference");
2577 }
2578
2579 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002580 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002581 _LIBCPP_INLINE_VISIBILITY
2582 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2583
2584 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002585 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002586 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2587 }
2588
2589 template <class _Up, class _Ep,
2590 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2591 class = _EnableIfDeleterConvertible<_Ep>
2592 >
2593 _LIBCPP_INLINE_VISIBILITY
2594 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2595 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2596
2597#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2598 template <class _Up>
2599 _LIBCPP_INLINE_VISIBILITY
2600 unique_ptr(auto_ptr<_Up>&& __p,
2601 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002602 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002604 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002605#endif
2606
2607 _LIBCPP_INLINE_VISIBILITY
2608 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2609 reset(__u.release());
2610 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2611 return *this;
2612 }
2613
2614 template <class _Up, class _Ep,
2615 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2616 class = _EnableIfDeleterAssignable<_Ep>
2617 >
2618 _LIBCPP_INLINE_VISIBILITY
2619 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2620 reset(__u.release());
2621 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2622 return *this;
2623 }
2624
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002625#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2626 template <class _Up>
2627 _LIBCPP_INLINE_VISIBILITY
2628 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2629 is_same<_Dp, default_delete<_Tp> >::value,
2630 unique_ptr&>::type
2631 operator=(auto_ptr<_Up> __p) {
2632 reset(__p.release());
2633 return *this;
2634 }
2635#endif
2636
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002637#ifdef _LIBCPP_CXX03_LANG
2638 unique_ptr(unique_ptr const&) = delete;
2639 unique_ptr& operator=(unique_ptr const&) = delete;
2640#endif
2641
2642
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002643 _LIBCPP_INLINE_VISIBILITY
2644 ~unique_ptr() { reset(); }
2645
2646 _LIBCPP_INLINE_VISIBILITY
2647 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2648 reset();
2649 return *this;
2650 }
2651
2652 _LIBCPP_INLINE_VISIBILITY
2653 typename add_lvalue_reference<_Tp>::type
2654 operator*() const {
2655 return *__ptr_.first();
2656 }
2657 _LIBCPP_INLINE_VISIBILITY
2658 pointer operator->() const _NOEXCEPT {
2659 return __ptr_.first();
2660 }
2661 _LIBCPP_INLINE_VISIBILITY
2662 pointer get() const _NOEXCEPT {
2663 return __ptr_.first();
2664 }
2665 _LIBCPP_INLINE_VISIBILITY
2666 deleter_type& get_deleter() _NOEXCEPT {
2667 return __ptr_.second();
2668 }
2669 _LIBCPP_INLINE_VISIBILITY
2670 const deleter_type& get_deleter() const _NOEXCEPT {
2671 return __ptr_.second();
2672 }
2673 _LIBCPP_INLINE_VISIBILITY
2674 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2675 return __ptr_.first() != nullptr;
2676 }
2677
2678 _LIBCPP_INLINE_VISIBILITY
2679 pointer release() _NOEXCEPT {
2680 pointer __t = __ptr_.first();
2681 __ptr_.first() = pointer();
2682 return __t;
2683 }
2684
2685 _LIBCPP_INLINE_VISIBILITY
2686 void reset(pointer __p = pointer()) _NOEXCEPT {
2687 pointer __tmp = __ptr_.first();
2688 __ptr_.first() = __p;
2689 if (__tmp)
2690 __ptr_.second()(__tmp);
2691 }
2692
2693 _LIBCPP_INLINE_VISIBILITY
2694 void swap(unique_ptr& __u) _NOEXCEPT {
2695 __ptr_.swap(__u.__ptr_);
2696 }
2697};
2698
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002699
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002701class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002703 typedef _Tp element_type;
2704 typedef _Dp deleter_type;
2705 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2706
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002708 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709
Eric Fiselier31127cd2017-04-16 02:14:31 +00002710 template <class _From>
2711 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712
Eric Fiselier31127cd2017-04-16 02:14:31 +00002713 template <class _FromElem>
2714 struct _CheckArrayPointerConversion<_FromElem*>
2715 : integral_constant<bool,
2716 is_same<_FromElem*, pointer>::value ||
2717 (is_same<pointer, element_type*>::value &&
2718 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2719 >
2720 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721
Eric Fiselier31127cd2017-04-16 02:14:31 +00002722 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002723
2724 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002725 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002726 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727
2728 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002729 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002730 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731
2732 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002733 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002734 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735
2736 template <bool _Dummy, class _Deleter = typename __dependent_type<
2737 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002738 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002739 typename enable_if<is_default_constructible<_Deleter>::value &&
2740 !is_pointer<_Deleter>::value>::type;
2741
2742 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002743 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2745
2746 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002747 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002748 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002749 >::type;
2750
2751 template <class _UPtr, class _Up,
2752 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002753 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 is_array<_Up>::value &&
2755 is_same<pointer, element_type*>::value &&
2756 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2757 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2758 >::type;
2759
2760 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002761 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002762 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2763 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2764 >::type;
2765
2766 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002767 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002768 is_assignable<_Dp&, _UDel&&>::value
2769 >::type;
2770
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002772 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002773 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002774 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002775 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002778 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002779 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002780 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 template <class _Pp, bool _Dummy = true,
2783 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002784 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002785 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002786 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002787 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002789 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002790 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2791 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002792 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002793 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002794 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002796 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002797 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002799 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002800 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002802 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002803 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2804 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002805 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002806 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002807 : __ptr_(__p, _VSTD::move(__d)) {
2808 static_assert(!is_reference<deleter_type>::value,
2809 "rvalue deleter bound to reference");
2810 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002812 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002813 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002814 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002815 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002816 : __ptr_(nullptr, _VSTD::move(__d)) {
2817 static_assert(!is_reference<deleter_type>::value,
2818 "rvalue deleter bound to reference");
2819 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002820
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002821 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002822 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2823 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 _LIBCPP_INLINE_VISIBILITY
2825 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002826
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002827 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002828 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002829 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2830 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002831
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002833 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002834 reset(__u.release());
2835 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2836 return *this;
2837 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002839 template <class _Up, class _Ep,
2840 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2841 class = _EnableIfDeleterConvertible<_Ep>
2842 >
2843 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002844 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002845 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002846 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002848 template <class _Up, class _Ep,
2849 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2850 class = _EnableIfDeleterAssignable<_Ep>
2851 >
2852 _LIBCPP_INLINE_VISIBILITY
2853 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002854 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002855 reset(__u.release());
2856 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2857 return *this;
2858 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002860#ifdef _LIBCPP_CXX03_LANG
2861 unique_ptr(unique_ptr const&) = delete;
2862 unique_ptr& operator=(unique_ptr const&) = delete;
2863#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002864
2865public:
2866 _LIBCPP_INLINE_VISIBILITY
2867 ~unique_ptr() { reset(); }
2868
2869 _LIBCPP_INLINE_VISIBILITY
2870 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2871 reset();
2872 return *this;
2873 }
2874
2875 _LIBCPP_INLINE_VISIBILITY
2876 typename add_lvalue_reference<_Tp>::type
2877 operator[](size_t __i) const {
2878 return __ptr_.first()[__i];
2879 }
2880 _LIBCPP_INLINE_VISIBILITY
2881 pointer get() const _NOEXCEPT {
2882 return __ptr_.first();
2883 }
2884
2885 _LIBCPP_INLINE_VISIBILITY
2886 deleter_type& get_deleter() _NOEXCEPT {
2887 return __ptr_.second();
2888 }
2889
2890 _LIBCPP_INLINE_VISIBILITY
2891 const deleter_type& get_deleter() const _NOEXCEPT {
2892 return __ptr_.second();
2893 }
2894 _LIBCPP_INLINE_VISIBILITY
2895 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2896 return __ptr_.first() != nullptr;
2897 }
2898
2899 _LIBCPP_INLINE_VISIBILITY
2900 pointer release() _NOEXCEPT {
2901 pointer __t = __ptr_.first();
2902 __ptr_.first() = pointer();
2903 return __t;
2904 }
2905
2906 template <class _Pp>
2907 _LIBCPP_INLINE_VISIBILITY
2908 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002909 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002910 >::type
2911 reset(_Pp __p) _NOEXCEPT {
2912 pointer __tmp = __ptr_.first();
2913 __ptr_.first() = __p;
2914 if (__tmp)
2915 __ptr_.second()(__tmp);
2916 }
2917
2918 _LIBCPP_INLINE_VISIBILITY
2919 void reset(nullptr_t = nullptr) _NOEXCEPT {
2920 pointer __tmp = __ptr_.first();
2921 __ptr_.first() = nullptr;
2922 if (__tmp)
2923 __ptr_.second()(__tmp);
2924 }
2925
2926 _LIBCPP_INLINE_VISIBILITY
2927 void swap(unique_ptr& __u) _NOEXCEPT {
2928 __ptr_.swap(__u.__ptr_);
2929 }
2930
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931};
2932
2933template <class _Tp, class _Dp>
2934inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002935typename enable_if<
2936 __is_swappable<_Dp>::value,
2937 void
2938>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002939swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940
2941template <class _T1, class _D1, class _T2, class _D2>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
2944operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2945
2946template <class _T1, class _D1, class _T2, class _D2>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
2949operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2950
2951template <class _T1, class _D1, class _T2, class _D2>
2952inline _LIBCPP_INLINE_VISIBILITY
2953bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002954operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2955{
2956 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2957 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002958 typedef typename common_type<_P1, _P2>::type _Vp;
2959 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002960}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961
2962template <class _T1, class _D1, class _T2, class _D2>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2966
2967template <class _T1, class _D1, class _T2, class _D2>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
2970operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2971
2972template <class _T1, class _D1, class _T2, class _D2>
2973inline _LIBCPP_INLINE_VISIBILITY
2974bool
2975operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2976
Howard Hinnantb17caf92012-02-21 21:02:58 +00002977template <class _T1, class _D1>
2978inline _LIBCPP_INLINE_VISIBILITY
2979bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002980operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _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==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002989{
2990 return !__x;
2991}
2992
2993template <class _T1, class _D1>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002996operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _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
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003004operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003005{
3006 return static_cast<bool>(__x);
3007}
3008
3009template <class _T1, class _D1>
3010inline _LIBCPP_INLINE_VISIBILITY
3011bool
3012operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3013{
3014 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3015 return less<_P1>()(__x.get(), nullptr);
3016}
3017
3018template <class _T1, class _D1>
3019inline _LIBCPP_INLINE_VISIBILITY
3020bool
3021operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3022{
3023 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3024 return less<_P1>()(nullptr, __x.get());
3025}
3026
3027template <class _T1, class _D1>
3028inline _LIBCPP_INLINE_VISIBILITY
3029bool
3030operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3031{
3032 return nullptr < __x;
3033}
3034
3035template <class _T1, class _D1>
3036inline _LIBCPP_INLINE_VISIBILITY
3037bool
3038operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3039{
3040 return __x < nullptr;
3041}
3042
3043template <class _T1, class _D1>
3044inline _LIBCPP_INLINE_VISIBILITY
3045bool
3046operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3047{
3048 return !(nullptr < __x);
3049}
3050
3051template <class _T1, class _D1>
3052inline _LIBCPP_INLINE_VISIBILITY
3053bool
3054operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3055{
3056 return !(__x < nullptr);
3057}
3058
3059template <class _T1, class _D1>
3060inline _LIBCPP_INLINE_VISIBILITY
3061bool
3062operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3063{
3064 return !(__x < nullptr);
3065}
3066
3067template <class _T1, class _D1>
3068inline _LIBCPP_INLINE_VISIBILITY
3069bool
3070operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3071{
3072 return !(nullptr < __x);
3073}
3074
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003075#if _LIBCPP_STD_VER > 11
3076
3077template<class _Tp>
3078struct __unique_if
3079{
3080 typedef unique_ptr<_Tp> __unique_single;
3081};
3082
3083template<class _Tp>
3084struct __unique_if<_Tp[]>
3085{
3086 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3087};
3088
3089template<class _Tp, size_t _Np>
3090struct __unique_if<_Tp[_Np]>
3091{
3092 typedef void __unique_array_known_bound;
3093};
3094
3095template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003096inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003097typename __unique_if<_Tp>::__unique_single
3098make_unique(_Args&&... __args)
3099{
3100 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3101}
3102
3103template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003104inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003105typename __unique_if<_Tp>::__unique_array_unknown_bound
3106make_unique(size_t __n)
3107{
3108 typedef typename remove_extent<_Tp>::type _Up;
3109 return unique_ptr<_Tp>(new _Up[__n]());
3110}
3111
3112template<class _Tp, class... _Args>
3113 typename __unique_if<_Tp>::__unique_array_known_bound
3114 make_unique(_Args&&...) = delete;
3115
3116#endif // _LIBCPP_STD_VER > 11
3117
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003118template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003119#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003120struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003121#else
3122struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003123 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003124#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003125{
3126 typedef unique_ptr<_Tp, _Dp> argument_type;
3127 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003128 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003129 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003130 {
3131 typedef typename argument_type::pointer pointer;
3132 return hash<pointer>()(__ptr.get());
3133 }
3134};
3135
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136struct __destruct_n
3137{
3138private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003139 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140
3141 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003142 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003143 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144
3145 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003146 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 {}
3148
Howard Hinnant719bda32011-05-28 14:41:13 +00003149 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003150 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 {}
3153
Howard Hinnant719bda32011-05-28 14:41:13 +00003154 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003155 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003156 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157 {}
3158public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003159 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003160 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161
3162 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003164 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165
3166 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003167 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003168 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169
3170 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003171 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003172 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173};
3174
3175template <class _Alloc>
3176class __allocator_destructor
3177{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003178 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003180 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3181 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182private:
3183 _Alloc& __alloc_;
3184 size_type __s_;
3185public:
3186 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003187 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003190 void operator()(pointer __p) _NOEXCEPT
3191 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192};
3193
3194template <class _InputIterator, class _ForwardIterator>
3195_ForwardIterator
3196uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3197{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003199#ifndef _LIBCPP_NO_EXCEPTIONS
3200 _ForwardIterator __s = __r;
3201 try
3202 {
3203#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003204 for (; __f != __l; ++__f, (void) ++__r)
3205 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003206#ifndef _LIBCPP_NO_EXCEPTIONS
3207 }
3208 catch (...)
3209 {
3210 for (; __s != __r; ++__s)
3211 __s->~value_type();
3212 throw;
3213 }
3214#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 return __r;
3216}
3217
3218template <class _InputIterator, class _Size, class _ForwardIterator>
3219_ForwardIterator
3220uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3221{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003223#ifndef _LIBCPP_NO_EXCEPTIONS
3224 _ForwardIterator __s = __r;
3225 try
3226 {
3227#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003228 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3229 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003230#ifndef _LIBCPP_NO_EXCEPTIONS
3231 }
3232 catch (...)
3233 {
3234 for (; __s != __r; ++__s)
3235 __s->~value_type();
3236 throw;
3237 }
3238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239 return __r;
3240}
3241
3242template <class _ForwardIterator, class _Tp>
3243void
3244uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3245{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003247#ifndef _LIBCPP_NO_EXCEPTIONS
3248 _ForwardIterator __s = __f;
3249 try
3250 {
3251#endif
3252 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003253 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003254#ifndef _LIBCPP_NO_EXCEPTIONS
3255 }
3256 catch (...)
3257 {
3258 for (; __s != __f; ++__s)
3259 __s->~value_type();
3260 throw;
3261 }
3262#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263}
3264
3265template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003266_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3268{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003270#ifndef _LIBCPP_NO_EXCEPTIONS
3271 _ForwardIterator __s = __f;
3272 try
3273 {
3274#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003275 for (; __n > 0; ++__f, (void) --__n)
3276 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003277#ifndef _LIBCPP_NO_EXCEPTIONS
3278 }
3279 catch (...)
3280 {
3281 for (; __s != __f; ++__s)
3282 __s->~value_type();
3283 throw;
3284 }
3285#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003286 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287}
3288
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003289#if _LIBCPP_STD_VER > 14
3290
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003291template <class _Tp>
3292inline _LIBCPP_INLINE_VISIBILITY
3293void destroy_at(_Tp* __loc) {
3294 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3295 __loc->~_Tp();
3296}
3297
3298template <class _ForwardIterator>
3299inline _LIBCPP_INLINE_VISIBILITY
3300void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3301 for (; __first != __last; ++__first)
3302 _VSTD::destroy_at(_VSTD::addressof(*__first));
3303}
3304
3305template <class _ForwardIterator, class _Size>
3306inline _LIBCPP_INLINE_VISIBILITY
3307_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3308 for (; __n > 0; (void)++__first, --__n)
3309 _VSTD::destroy_at(_VSTD::addressof(*__first));
3310 return __first;
3311}
3312
Eric Fiselier290c07c2016-10-11 21:13:44 +00003313template <class _ForwardIterator>
3314inline _LIBCPP_INLINE_VISIBILITY
3315void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3316 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3317 auto __idx = __first;
3318#ifndef _LIBCPP_NO_EXCEPTIONS
3319 try {
3320#endif
3321 for (; __idx != __last; ++__idx)
3322 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3323#ifndef _LIBCPP_NO_EXCEPTIONS
3324 } catch (...) {
3325 _VSTD::destroy(__first, __idx);
3326 throw;
3327 }
3328#endif
3329}
3330
3331template <class _ForwardIterator, class _Size>
3332inline _LIBCPP_INLINE_VISIBILITY
3333_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3334 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3335 auto __idx = __first;
3336#ifndef _LIBCPP_NO_EXCEPTIONS
3337 try {
3338#endif
3339 for (; __n > 0; (void)++__idx, --__n)
3340 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3341 return __idx;
3342#ifndef _LIBCPP_NO_EXCEPTIONS
3343 } catch (...) {
3344 _VSTD::destroy(__first, __idx);
3345 throw;
3346 }
3347#endif
3348}
3349
3350
3351template <class _ForwardIterator>
3352inline _LIBCPP_INLINE_VISIBILITY
3353void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3354 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3355 auto __idx = __first;
3356#ifndef _LIBCPP_NO_EXCEPTIONS
3357 try {
3358#endif
3359 for (; __idx != __last; ++__idx)
3360 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3361#ifndef _LIBCPP_NO_EXCEPTIONS
3362 } catch (...) {
3363 _VSTD::destroy(__first, __idx);
3364 throw;
3365 }
3366#endif
3367}
3368
3369template <class _ForwardIterator, class _Size>
3370inline _LIBCPP_INLINE_VISIBILITY
3371_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3372 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3373 auto __idx = __first;
3374#ifndef _LIBCPP_NO_EXCEPTIONS
3375 try {
3376#endif
3377 for (; __n > 0; (void)++__idx, --__n)
3378 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3379 return __idx;
3380#ifndef _LIBCPP_NO_EXCEPTIONS
3381 } catch (...) {
3382 _VSTD::destroy(__first, __idx);
3383 throw;
3384 }
3385#endif
3386}
3387
3388
3389template <class _InputIt, class _ForwardIt>
3390inline _LIBCPP_INLINE_VISIBILITY
3391_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3392 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3393 auto __idx = __first_res;
3394#ifndef _LIBCPP_NO_EXCEPTIONS
3395 try {
3396#endif
3397 for (; __first != __last; (void)++__idx, ++__first)
3398 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3399 return __idx;
3400#ifndef _LIBCPP_NO_EXCEPTIONS
3401 } catch (...) {
3402 _VSTD::destroy(__first_res, __idx);
3403 throw;
3404 }
3405#endif
3406}
3407
3408template <class _InputIt, class _Size, class _ForwardIt>
3409inline _LIBCPP_INLINE_VISIBILITY
3410pair<_InputIt, _ForwardIt>
3411uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3412 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3413 auto __idx = __first_res;
3414#ifndef _LIBCPP_NO_EXCEPTIONS
3415 try {
3416#endif
3417 for (; __n > 0; ++__idx, (void)++__first, --__n)
3418 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3419 return {__first, __idx};
3420#ifndef _LIBCPP_NO_EXCEPTIONS
3421 } catch (...) {
3422 _VSTD::destroy(__first_res, __idx);
3423 throw;
3424 }
3425#endif
3426}
3427
3428
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003429#endif // _LIBCPP_STD_VER > 14
3430
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003431// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3432// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003433// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003434#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3435 && defined(__ATOMIC_RELAXED) \
3436 && defined(__ATOMIC_ACQ_REL)
3437# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003438#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003439# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3440#endif
3441
3442template <class _Tp>
3443inline _LIBCPP_INLINE_VISIBILITY _Tp
3444__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3445{
3446#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3447 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3448#else
3449 return __t += 1;
3450#endif
3451}
3452
3453template <class _Tp>
3454inline _LIBCPP_INLINE_VISIBILITY _Tp
3455__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3456{
3457#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3458 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3459#else
3460 return __t -= 1;
3461#endif
3462}
3463
Howard Hinnant756c69b2010-09-22 16:48:34 +00003464class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465 : public std::exception
3466{
3467public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003468 bad_weak_ptr() _NOEXCEPT = default;
3469 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003470 virtual ~bad_weak_ptr() _NOEXCEPT;
3471 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472};
3473
Louis Dionne16fe2952018-07-11 23:14:33 +00003474_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003475void __throw_bad_weak_ptr()
3476{
3477#ifndef _LIBCPP_NO_EXCEPTIONS
3478 throw bad_weak_ptr();
3479#else
3480 _VSTD::abort();
3481#endif
3482}
3483
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003484template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003486class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487{
3488 __shared_count(const __shared_count&);
3489 __shared_count& operator=(const __shared_count&);
3490
3491protected:
3492 long __shared_owners_;
3493 virtual ~__shared_count();
3494private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003495 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496
3497public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003499 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500 : __shared_owners_(__refs) {}
3501
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003502#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003503 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003504 void __add_shared() _NOEXCEPT;
3505 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003506#else
3507 _LIBCPP_INLINE_VISIBILITY
3508 void __add_shared() _NOEXCEPT {
3509 __libcpp_atomic_refcount_increment(__shared_owners_);
3510 }
3511 _LIBCPP_INLINE_VISIBILITY
3512 bool __release_shared() _NOEXCEPT {
3513 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3514 __on_zero_shared();
3515 return true;
3516 }
3517 return false;
3518 }
3519#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003521 long use_count() const _NOEXCEPT {
3522 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3523 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524};
3525
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003526class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527 : private __shared_count
3528{
3529 long __shared_weak_owners_;
3530
3531public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003533 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534 : __shared_count(__refs),
3535 __shared_weak_owners_(__refs) {}
3536protected:
3537 virtual ~__shared_weak_count();
3538
3539public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003540#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003541 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003542 void __add_shared() _NOEXCEPT;
3543 void __add_weak() _NOEXCEPT;
3544 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003545#else
3546 _LIBCPP_INLINE_VISIBILITY
3547 void __add_shared() _NOEXCEPT {
3548 __shared_count::__add_shared();
3549 }
3550 _LIBCPP_INLINE_VISIBILITY
3551 void __add_weak() _NOEXCEPT {
3552 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3553 }
3554 _LIBCPP_INLINE_VISIBILITY
3555 void __release_shared() _NOEXCEPT {
3556 if (__shared_count::__release_shared())
3557 __release_weak();
3558 }
3559#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003560 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003562 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3563 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564
Howard Hinnant807d6332013-02-25 15:50:36 +00003565 // Define the function out only if we build static libc++ without RTTI.
3566 // Otherwise we may break clients who need to compile their projects with
3567 // -fno-rtti and yet link against a libc++.dylib compiled
3568 // without -fno-rtti.
3569#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003570 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003571#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003573 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574};
3575
3576template <class _Tp, class _Dp, class _Alloc>
3577class __shared_ptr_pointer
3578 : public __shared_weak_count
3579{
3580 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3581public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003584 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585
Howard Hinnant72f73582010-08-11 17:04:31 +00003586#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003587 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003588#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589
3590private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003591 virtual void __on_zero_shared() _NOEXCEPT;
3592 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593};
3594
Howard Hinnant72f73582010-08-11 17:04:31 +00003595#ifndef _LIBCPP_NO_RTTI
3596
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597template <class _Tp, class _Dp, class _Alloc>
3598const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003599__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003601 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602}
3603
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003604#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003605
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606template <class _Tp, class _Dp, class _Alloc>
3607void
Howard Hinnant719bda32011-05-28 14:41:13 +00003608__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609{
3610 __data_.first().second()(__data_.first().first());
3611 __data_.first().second().~_Dp();
3612}
3613
3614template <class _Tp, class _Dp, class _Alloc>
3615void
Howard Hinnant719bda32011-05-28 14:41:13 +00003616__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003618 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3619 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003620 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3621
Eric Fiselierf8898c82015-02-05 23:01:40 +00003622 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003624 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625}
3626
3627template <class _Tp, class _Alloc>
3628class __shared_ptr_emplace
3629 : public __shared_weak_count
3630{
3631 __compressed_pair<_Alloc, _Tp> __data_;
3632public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633
Howard Hinnant756c69b2010-09-22 16:48:34 +00003634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003636 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003638
3639#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003643 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3644 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645#else // _LIBCPP_HAS_NO_VARIADICS
3646
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3650 : __data_(__a, _Tp(__a0)) {}
3651
3652 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3655 : __data_(__a, _Tp(__a0, __a1)) {}
3656
3657 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3660 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3661
3662#endif // _LIBCPP_HAS_NO_VARIADICS
3663
3664private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003665 virtual void __on_zero_shared() _NOEXCEPT;
3666 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003668 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003669 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670};
3671
3672template <class _Tp, class _Alloc>
3673void
Howard Hinnant719bda32011-05-28 14:41:13 +00003674__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
3676 __data_.second().~_Tp();
3677}
3678
3679template <class _Tp, class _Alloc>
3680void
Howard Hinnant719bda32011-05-28 14:41:13 +00003681__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003683 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3684 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003685 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003686 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003688 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689}
3690
Erik Pilkington2a398762017-05-25 15:43:31 +00003691struct __shared_ptr_dummy_rebind_allocator_type;
3692template <>
3693class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3694{
3695public:
3696 template <class _Other>
3697 struct rebind
3698 {
3699 typedef allocator<_Other> other;
3700 };
3701};
3702
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003703template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704
zoecarverd73f42a2020-05-11 18:42:50 -07003705template<class _Tp, class _Up>
3706struct __compatible_with
3707#if _LIBCPP_STD_VER > 14
3708 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3709#else
3710 : is_convertible<_Tp*, _Up*> {};
3711#endif // _LIBCPP_STD_VER > 14
3712
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003714class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003716public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003717#if _LIBCPP_STD_VER > 14
3718 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003719 typedef remove_extent_t<_Tp> element_type;
3720#else
3721 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003722#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003723
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724private:
3725 element_type* __ptr_;
3726 __shared_weak_count* __cntrl_;
3727
3728 struct __nat {int __for_bool_;};
3729public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003731 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003733 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003734 template<class _Yp>
3735 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003736 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003737 template<class _Yp, class _Dp>
3738 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003739 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003740 template<class _Yp, class _Dp, class _Alloc>
3741 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003742 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3744 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003745 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003747 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003751 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003752 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003753#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003755 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003756 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003757 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003758 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003759#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003761 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003762#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003763#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003764 template<class _Yp>
3765 shared_ptr(auto_ptr<_Yp>&& __r,
3766 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003767#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003768 template<class _Yp>
3769 shared_ptr(auto_ptr<_Yp> __r,
3770 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003772#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003773#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003774 template <class _Yp, class _Dp>
3775 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3776 typename enable_if
3777 <
3778 !is_lvalue_reference<_Dp>::value &&
3779 !is_array<_Yp>::value &&
3780 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3781 __nat
3782 >::type = __nat());
3783 template <class _Yp, class _Dp>
3784 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3785 typename enable_if
3786 <
3787 is_lvalue_reference<_Dp>::value &&
3788 !is_array<_Yp>::value &&
3789 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3790 __nat
3791 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003792#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003793 template <class _Yp, class _Dp>
3794 shared_ptr(unique_ptr<_Yp, _Dp>,
3795 typename enable_if
3796 <
3797 !is_lvalue_reference<_Dp>::value &&
3798 !is_array<_Yp>::value &&
3799 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3800 __nat
3801 >::type = __nat());
3802 template <class _Yp, class _Dp>
3803 shared_ptr(unique_ptr<_Yp, _Dp>,
3804 typename enable_if
3805 <
3806 is_lvalue_reference<_Dp>::value &&
3807 !is_array<_Yp>::value &&
3808 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3809 __nat
3810 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003811#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812
3813 ~shared_ptr();
3814
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003816 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003817 template<class _Yp>
3818 typename enable_if
3819 <
zoecarverd73f42a2020-05-11 18:42:50 -07003820 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003821 shared_ptr&
3822 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003824 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003825#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003827 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003828 template<class _Yp>
3829 typename enable_if
3830 <
zoecarverd73f42a2020-05-11 18:42:50 -07003831 __compatible_with<_Yp, element_type>::value,
3832 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003833 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003835 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003836#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003837 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003839 typename enable_if
3840 <
3841 !is_array<_Yp>::value &&
3842 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003843 shared_ptr
3844 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003845 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003846#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003847#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003848#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003849 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003851 typename enable_if
3852 <
3853 !is_array<_Yp>::value &&
3854 is_convertible<_Yp*, element_type*>::value,
3855 shared_ptr&
3856 >::type
3857 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003859#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003860 template <class _Yp, class _Dp>
3861 typename enable_if
3862 <
3863 !is_array<_Yp>::value &&
3864 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3865 shared_ptr&
3866 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003867#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003869 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003870#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003872 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873#endif
3874
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003876 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003878 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003879 template<class _Yp>
3880 typename enable_if
3881 <
zoecarverd73f42a2020-05-11 18:42:50 -07003882 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003883 void
3884 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003886 reset(_Yp* __p);
3887 template<class _Yp, class _Dp>
3888 typename enable_if
3889 <
zoecarverd73f42a2020-05-11 18:42:50 -07003890 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003891 void
3892 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003894 reset(_Yp* __p, _Dp __d);
3895 template<class _Yp, class _Dp, class _Alloc>
3896 typename enable_if
3897 <
zoecarverd73f42a2020-05-11 18:42:50 -07003898 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003899 void
3900 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003902 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903
Howard Hinnant756c69b2010-09-22 16:48:34 +00003904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003905 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003907 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3908 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003909 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003910 element_type* operator->() const _NOEXCEPT
3911 {
3912 static_assert(!_VSTD::is_array<_Tp>::value,
3913 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3914 return __ptr_;
3915 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003917 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003919 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003921 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003922 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003923 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003924 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003926 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003928 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003930 _LIBCPP_INLINE_VISIBILITY
3931 bool
3932 __owner_equivalent(const shared_ptr& __p) const
3933 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003934
zoecarverd73f42a2020-05-11 18:42:50 -07003935#if _LIBCPP_STD_VER > 14
3936 typename add_lvalue_reference<element_type>::type
3937 _LIBCPP_INLINE_VISIBILITY
3938 operator[](ptrdiff_t __i) const
3939 {
3940 static_assert(_VSTD::is_array<_Tp>::value,
3941 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3942 return __ptr_[__i];
3943 }
3944#endif
3945
Howard Hinnant72f73582010-08-11 17:04:31 +00003946#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003949 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003950 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003951 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003952 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003953#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954
Zoe Carverd9040c72019-10-22 15:16:49 +00003955 template<class _Yp, class _CntrlBlk>
3956 static shared_ptr<_Tp>
3957 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3958 {
3959 shared_ptr<_Tp> __r;
3960 __r.__ptr_ = __p;
3961 __r.__cntrl_ = __cntrl;
3962 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3963 return __r;
3964 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003965
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003967 template <class _Yp, bool = is_function<_Yp>::value>
3968 struct __shared_ptr_default_allocator
3969 {
3970 typedef allocator<_Yp> type;
3971 };
3972
3973 template <class _Yp>
3974 struct __shared_ptr_default_allocator<_Yp, true>
3975 {
3976 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3977 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003979 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003980 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003981 typename enable_if<is_convertible<_OrigPtr*,
3982 const enable_shared_from_this<_Yp>*
3983 >::value,
3984 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003985 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3986 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003988 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003989 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003990 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003991 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3992 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003993 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994 }
3995
Erik Pilkington2a398762017-05-25 15:43:31 +00003996 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997
zoecarverd73f42a2020-05-11 18:42:50 -07003998 template <class, class _Yp>
3999 struct __shared_ptr_default_delete
4000 : default_delete<_Yp> {};
4001
4002 template <class _Yp, class _Un, size_t _Sz>
4003 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
4004 : default_delete<_Yp[]> {};
4005
4006 template <class _Yp, class _Un>
4007 struct __shared_ptr_default_delete<_Yp[], _Un>
4008 : default_delete<_Yp[]> {};
4009
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004010 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4011 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012};
4013
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004014#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4015template<class _Tp>
4016shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
4017template<class _Tp, class _Dp>
4018shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
4019#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004020
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004022inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004023_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004024shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025 : __ptr_(0),
4026 __cntrl_(0)
4027{
4028}
4029
4030template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004031inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004032_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004033shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034 : __ptr_(0),
4035 __cntrl_(0)
4036{
4037}
4038
4039template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004040template<class _Yp>
4041shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07004042 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043 : __ptr_(__p)
4044{
4045 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004046 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07004047 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
4048 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004050 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051}
4052
4053template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004054template<class _Yp, class _Dp>
4055shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07004056 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057 : __ptr_(__p)
4058{
4059#ifndef _LIBCPP_NO_EXCEPTIONS
4060 try
4061 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004062#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004063 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4064 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4065 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004066 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067#ifndef _LIBCPP_NO_EXCEPTIONS
4068 }
4069 catch (...)
4070 {
4071 __d(__p);
4072 throw;
4073 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004074#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075}
4076
4077template<class _Tp>
4078template<class _Dp>
4079shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4080 : __ptr_(0)
4081{
4082#ifndef _LIBCPP_NO_EXCEPTIONS
4083 try
4084 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004085#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004086 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4087 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4088 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089#ifndef _LIBCPP_NO_EXCEPTIONS
4090 }
4091 catch (...)
4092 {
4093 __d(__p);
4094 throw;
4095 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097}
4098
4099template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004100template<class _Yp, class _Dp, class _Alloc>
4101shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07004102 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103 : __ptr_(__p)
4104{
4105#ifndef _LIBCPP_NO_EXCEPTIONS
4106 try
4107 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004108#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004110 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111 typedef __allocator_destructor<_A2> _D2;
4112 _A2 __a2(__a);
4113 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004114 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4115 _CntrlBlk(__p, __d, __a);
4116 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004117 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118#ifndef _LIBCPP_NO_EXCEPTIONS
4119 }
4120 catch (...)
4121 {
4122 __d(__p);
4123 throw;
4124 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004125#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126}
4127
4128template<class _Tp>
4129template<class _Dp, class _Alloc>
4130shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4131 : __ptr_(0)
4132{
4133#ifndef _LIBCPP_NO_EXCEPTIONS
4134 try
4135 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004136#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004138 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139 typedef __allocator_destructor<_A2> _D2;
4140 _A2 __a2(__a);
4141 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004142 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4143 _CntrlBlk(__p, __d, __a);
4144 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145#ifndef _LIBCPP_NO_EXCEPTIONS
4146 }
4147 catch (...)
4148 {
4149 __d(__p);
4150 throw;
4151 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153}
4154
4155template<class _Tp>
4156template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004157inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004158shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159 : __ptr_(__p),
4160 __cntrl_(__r.__cntrl_)
4161{
4162 if (__cntrl_)
4163 __cntrl_->__add_shared();
4164}
4165
4166template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004167inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004168shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 : __ptr_(__r.__ptr_),
4170 __cntrl_(__r.__cntrl_)
4171{
4172 if (__cntrl_)
4173 __cntrl_->__add_shared();
4174}
4175
4176template<class _Tp>
4177template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004178inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07004180 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004181 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182 : __ptr_(__r.__ptr_),
4183 __cntrl_(__r.__cntrl_)
4184{
4185 if (__cntrl_)
4186 __cntrl_->__add_shared();
4187}
4188
Howard Hinnant74279a52010-09-04 23:28:19 +00004189#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190
4191template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004192inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004193shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194 : __ptr_(__r.__ptr_),
4195 __cntrl_(__r.__cntrl_)
4196{
4197 __r.__ptr_ = 0;
4198 __r.__cntrl_ = 0;
4199}
4200
4201template<class _Tp>
4202template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004203inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07004205 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004206 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207 : __ptr_(__r.__ptr_),
4208 __cntrl_(__r.__cntrl_)
4209{
4210 __r.__ptr_ = 0;
4211 __r.__cntrl_ = 0;
4212}
4213
Howard Hinnant74279a52010-09-04 23:28:19 +00004214#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215
Marshall Clowb22274f2017-01-24 22:22:33 +00004216#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004218template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004219#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004220shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004222shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004224 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225 : __ptr_(__r.get())
4226{
4227 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4228 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004229 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230 __r.release();
4231}
Marshall Clowb22274f2017-01-24 22:22:33 +00004232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233
4234template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004235template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004236#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4238#else
4239shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4240#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004241 typename enable_if
4242 <
4243 !is_lvalue_reference<_Dp>::value &&
4244 !is_array<_Yp>::value &&
4245 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4246 __nat
4247 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248 : __ptr_(__r.get())
4249{
Marshall Clow35cde742015-05-10 13:59:45 +00004250#if _LIBCPP_STD_VER > 11
4251 if (__ptr_ == nullptr)
4252 __cntrl_ = nullptr;
4253 else
4254#endif
4255 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004256 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4257 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4258 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004259 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004260 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261 __r.release();
4262}
4263
4264template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004265template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004266#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4268#else
4269shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4270#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004271 typename enable_if
4272 <
4273 is_lvalue_reference<_Dp>::value &&
4274 !is_array<_Yp>::value &&
4275 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4276 __nat
4277 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 : __ptr_(__r.get())
4279{
Marshall Clow35cde742015-05-10 13:59:45 +00004280#if _LIBCPP_STD_VER > 11
4281 if (__ptr_ == nullptr)
4282 __cntrl_ = nullptr;
4283 else
4284#endif
4285 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004286 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004287 typedef __shared_ptr_pointer<_Yp*,
4288 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004289 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004290 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004291 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004292 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293 __r.release();
4294}
4295
Zoe Carver6cd05c32019-08-19 15:47:16 +00004296template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297shared_ptr<_Tp>::~shared_ptr()
4298{
4299 if (__cntrl_)
4300 __cntrl_->__release_shared();
4301}
4302
4303template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004304inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004306shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307{
4308 shared_ptr(__r).swap(*this);
4309 return *this;
4310}
4311
4312template<class _Tp>
4313template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004315typename enable_if
4316<
zoecarverd73f42a2020-05-11 18:42:50 -07004317 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004318 shared_ptr<_Tp>&
4319>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004320shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321{
4322 shared_ptr(__r).swap(*this);
4323 return *this;
4324}
4325
Howard Hinnant74279a52010-09-04 23:28:19 +00004326#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327
4328template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004329inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004331shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004333 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334 return *this;
4335}
4336
4337template<class _Tp>
4338template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004339inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004340typename enable_if
4341<
zoecarverd73f42a2020-05-11 18:42:50 -07004342 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004343 shared_ptr<_Tp>&
4344>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4346{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004347 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 return *this;
4349}
4350
Marshall Clowb22274f2017-01-24 22:22:33 +00004351#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352template<class _Tp>
4353template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004354inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004355typename enable_if
4356<
4357 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004358 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004359 shared_ptr<_Tp>
4360>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __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}
Marshall Clowb22274f2017-01-24 22:22:33 +00004366#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367
4368template<class _Tp>
4369template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004370inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004371typename enable_if
4372<
4373 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004374 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004375 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004376 shared_ptr<_Tp>&
4377>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4379{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004380 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381 return *this;
4382}
4383
Howard Hinnant74279a52010-09-04 23:28:19 +00004384#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385
Marshall Clowb22274f2017-01-24 22:22:33 +00004386#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387template<class _Tp>
4388template<class _Yp>
4389inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004390typename enable_if
4391<
4392 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004393 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004394 shared_ptr<_Tp>&
4395>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004396shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397{
4398 shared_ptr(__r).swap(*this);
4399 return *this;
4400}
Marshall Clowb22274f2017-01-24 22:22:33 +00004401#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004402
4403template<class _Tp>
4404template <class _Yp, class _Dp>
4405inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004406typename enable_if
4407<
4408 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004409 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004410 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004411 shared_ptr<_Tp>&
4412>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4414{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004415 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416 return *this;
4417}
4418
Howard Hinnant74279a52010-09-04 23:28:19 +00004419#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004420
4421template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004422inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423void
Howard Hinnant719bda32011-05-28 14:41:13 +00004424shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004426 _VSTD::swap(__ptr_, __r.__ptr_);
4427 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428}
4429
4430template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004431inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432void
Howard Hinnant719bda32011-05-28 14:41:13 +00004433shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434{
4435 shared_ptr().swap(*this);
4436}
4437
4438template<class _Tp>
4439template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004440inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004441typename enable_if
4442<
zoecarverd73f42a2020-05-11 18:42:50 -07004443 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004444 void
4445>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446shared_ptr<_Tp>::reset(_Yp* __p)
4447{
4448 shared_ptr(__p).swap(*this);
4449}
4450
4451template<class _Tp>
4452template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004453inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004454typename enable_if
4455<
zoecarverd73f42a2020-05-11 18:42:50 -07004456 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004457 void
4458>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4460{
4461 shared_ptr(__p, __d).swap(*this);
4462}
4463
4464template<class _Tp>
4465template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004466inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004467typename enable_if
4468<
zoecarverd73f42a2020-05-11 18:42:50 -07004469 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004470 void
4471>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4473{
4474 shared_ptr(__p, __d, __a).swap(*this);
4475}
4476
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004477template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004479typename enable_if
4480<
4481 !is_array<_Tp>::value,
4482 shared_ptr<_Tp>
4483>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484make_shared(_Args&& ...__args)
4485{
Zoe Carverd9040c72019-10-22 15:16:49 +00004486 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4487 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4488 typedef allocator<_CntrlBlk> _A2;
4489 typedef __allocator_destructor<_A2> _D2;
4490
4491 _A2 __a2;
4492 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4493 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4494
4495 _Tp *__ptr = __hold2.get()->get();
4496 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497}
4498
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004499template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004501typename enable_if
4502<
4503 !is_array<_Tp>::value,
4504 shared_ptr<_Tp>
4505>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004506allocate_shared(const _Alloc& __a, _Args&& ...__args)
4507{
zoecarver505730a2020-02-25 16:50:57 -08004508 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4509
4510 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4511 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4512 typedef __allocator_destructor<_A2> _D2;
4513
4514 _A2 __a2(__a);
4515 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4516 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4517 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4518
4519 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4520 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521}
4522
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523template<class _Tp, class _Up>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004526operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004527{
4528 return __x.get() == __y.get();
4529}
4530
4531template<class _Tp, class _Up>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004534operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004535{
4536 return !(__x == __y);
4537}
4538
4539template<class _Tp, class _Up>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004542operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004543{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004544#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004545 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4546 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004547#else
4548 return less<>()(__x.get(), __y.get());
4549#endif
4550
Howard Hinnantb17caf92012-02-21 21:02:58 +00004551}
4552
4553template<class _Tp, class _Up>
4554inline _LIBCPP_INLINE_VISIBILITY
4555bool
4556operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4557{
4558 return __y < __x;
4559}
4560
4561template<class _Tp, class _Up>
4562inline _LIBCPP_INLINE_VISIBILITY
4563bool
4564operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4565{
4566 return !(__y < __x);
4567}
4568
4569template<class _Tp, class _Up>
4570inline _LIBCPP_INLINE_VISIBILITY
4571bool
4572operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4573{
4574 return !(__x < __y);
4575}
4576
4577template<class _Tp>
4578inline _LIBCPP_INLINE_VISIBILITY
4579bool
4580operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4581{
4582 return !__x;
4583}
4584
4585template<class _Tp>
4586inline _LIBCPP_INLINE_VISIBILITY
4587bool
4588operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4589{
4590 return !__x;
4591}
4592
4593template<class _Tp>
4594inline _LIBCPP_INLINE_VISIBILITY
4595bool
4596operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4597{
4598 return static_cast<bool>(__x);
4599}
4600
4601template<class _Tp>
4602inline _LIBCPP_INLINE_VISIBILITY
4603bool
4604operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4605{
4606 return static_cast<bool>(__x);
4607}
4608
4609template<class _Tp>
4610inline _LIBCPP_INLINE_VISIBILITY
4611bool
4612operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4613{
4614 return less<_Tp*>()(__x.get(), nullptr);
4615}
4616
4617template<class _Tp>
4618inline _LIBCPP_INLINE_VISIBILITY
4619bool
4620operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4621{
4622 return less<_Tp*>()(nullptr, __x.get());
4623}
4624
4625template<class _Tp>
4626inline _LIBCPP_INLINE_VISIBILITY
4627bool
4628operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4629{
4630 return nullptr < __x;
4631}
4632
4633template<class _Tp>
4634inline _LIBCPP_INLINE_VISIBILITY
4635bool
4636operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4637{
4638 return __x < nullptr;
4639}
4640
4641template<class _Tp>
4642inline _LIBCPP_INLINE_VISIBILITY
4643bool
4644operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4645{
4646 return !(nullptr < __x);
4647}
4648
4649template<class _Tp>
4650inline _LIBCPP_INLINE_VISIBILITY
4651bool
4652operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4653{
4654 return !(__x < nullptr);
4655}
4656
4657template<class _Tp>
4658inline _LIBCPP_INLINE_VISIBILITY
4659bool
4660operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4661{
4662 return !(__x < nullptr);
4663}
4664
4665template<class _Tp>
4666inline _LIBCPP_INLINE_VISIBILITY
4667bool
4668operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4669{
4670 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004671}
4672
4673template<class _Tp>
4674inline _LIBCPP_INLINE_VISIBILITY
4675void
Howard Hinnant719bda32011-05-28 14:41:13 +00004676swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004677{
4678 __x.swap(__y);
4679}
4680
4681template<class _Tp, class _Up>
4682inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004683shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004684static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004685{
zoecarverd73f42a2020-05-11 18:42:50 -07004686 return shared_ptr<_Tp>(__r,
4687 static_cast<
4688 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004689}
4690
4691template<class _Tp, class _Up>
4692inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004693shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004694dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004695{
zoecarverd73f42a2020-05-11 18:42:50 -07004696 typedef typename shared_ptr<_Tp>::element_type _ET;
4697 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004698 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4699}
4700
4701template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004702shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004703const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004704{
zoecarverd73f42a2020-05-11 18:42:50 -07004705 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004706 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004707}
4708
zoecarverd73f42a2020-05-11 18:42:50 -07004709template<class _Tp, class _Up>
4710shared_ptr<_Tp>
4711reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4712{
4713 return shared_ptr<_Tp>(__r,
4714 reinterpret_cast<
4715 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4716}
4717
Howard Hinnant72f73582010-08-11 17:04:31 +00004718#ifndef _LIBCPP_NO_RTTI
4719
Howard Hinnantc51e1022010-05-11 19:42:16 +00004720template<class _Dp, class _Tp>
4721inline _LIBCPP_INLINE_VISIBILITY
4722_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004723get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004724{
4725 return __p.template __get_deleter<_Dp>();
4726}
4727
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004728#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004729
Howard Hinnantc51e1022010-05-11 19:42:16 +00004730template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004731class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004732{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004733public:
4734 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004735private:
4736 element_type* __ptr_;
4737 __shared_weak_count* __cntrl_;
4738
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004739public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004741 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004742 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004743 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4744 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004746 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004747 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004748 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4749 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004750
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004753 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004754 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004755 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4756 _NOEXCEPT;
4757#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004758 ~weak_ptr();
4759
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004761 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004762 template<class _Yp>
4763 typename enable_if
4764 <
4765 is_convertible<_Yp*, element_type*>::value,
4766 weak_ptr&
4767 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004769 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4770
4771#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4772
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004774 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4775 template<class _Yp>
4776 typename enable_if
4777 <
4778 is_convertible<_Yp*, element_type*>::value,
4779 weak_ptr&
4780 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004782 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4783
4784#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4785
4786 template<class _Yp>
4787 typename enable_if
4788 <
4789 is_convertible<_Yp*, element_type*>::value,
4790 weak_ptr&
4791 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004793 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004794
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004796 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004798 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004799
Howard Hinnant756c69b2010-09-22 16:48:34 +00004800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004801 long use_count() const _NOEXCEPT
4802 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004804 bool expired() const _NOEXCEPT
4805 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4806 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004807 template<class _Up>
4808 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004809 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004810 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004811 template<class _Up>
4812 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004813 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004814 {return __cntrl_ < __r.__cntrl_;}
4815
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004816 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4817 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004818};
4819
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004820#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4821template<class _Tp>
4822weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4823#endif
4824
Howard Hinnantc51e1022010-05-11 19:42:16 +00004825template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004826inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004827_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004828weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004829 : __ptr_(0),
4830 __cntrl_(0)
4831{
4832}
4833
4834template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004835inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004836weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004837 : __ptr_(__r.__ptr_),
4838 __cntrl_(__r.__cntrl_)
4839{
4840 if (__cntrl_)
4841 __cntrl_->__add_weak();
4842}
4843
4844template<class _Tp>
4845template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004846inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004847weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004848 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004849 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004850 : __ptr_(__r.__ptr_),
4851 __cntrl_(__r.__cntrl_)
4852{
4853 if (__cntrl_)
4854 __cntrl_->__add_weak();
4855}
4856
4857template<class _Tp>
4858template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004859inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004860weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004861 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004862 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004863 : __ptr_(__r.__ptr_),
4864 __cntrl_(__r.__cntrl_)
4865{
4866 if (__cntrl_)
4867 __cntrl_->__add_weak();
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>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4875 : __ptr_(__r.__ptr_),
4876 __cntrl_(__r.__cntrl_)
4877{
4878 __r.__ptr_ = 0;
4879 __r.__cntrl_ = 0;
4880}
4881
4882template<class _Tp>
4883template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004884inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004885weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4886 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4887 _NOEXCEPT
4888 : __ptr_(__r.__ptr_),
4889 __cntrl_(__r.__cntrl_)
4890{
4891 __r.__ptr_ = 0;
4892 __r.__cntrl_ = 0;
4893}
4894
4895#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4896
Howard Hinnantc51e1022010-05-11 19:42:16 +00004897template<class _Tp>
4898weak_ptr<_Tp>::~weak_ptr()
4899{
4900 if (__cntrl_)
4901 __cntrl_->__release_weak();
4902}
4903
4904template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004905inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004906weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004907weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908{
4909 weak_ptr(__r).swap(*this);
4910 return *this;
4911}
4912
4913template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004914template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004915inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004916typename enable_if
4917<
4918 is_convertible<_Yp*, _Tp*>::value,
4919 weak_ptr<_Tp>&
4920>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004921weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922{
4923 weak_ptr(__r).swap(*this);
4924 return *this;
4925}
4926
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004927#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4928
4929template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004930inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004931weak_ptr<_Tp>&
4932weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4933{
4934 weak_ptr(_VSTD::move(__r)).swap(*this);
4935 return *this;
4936}
4937
Howard Hinnantc51e1022010-05-11 19:42:16 +00004938template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004939template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004940inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004941typename enable_if
4942<
4943 is_convertible<_Yp*, _Tp*>::value,
4944 weak_ptr<_Tp>&
4945>::type
4946weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4947{
4948 weak_ptr(_VSTD::move(__r)).swap(*this);
4949 return *this;
4950}
4951
4952#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4953
4954template<class _Tp>
4955template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004956inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004957typename enable_if
4958<
4959 is_convertible<_Yp*, _Tp*>::value,
4960 weak_ptr<_Tp>&
4961>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004962weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963{
4964 weak_ptr(__r).swap(*this);
4965 return *this;
4966}
4967
4968template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004969inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004970void
Howard Hinnant719bda32011-05-28 14:41:13 +00004971weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004972{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004973 _VSTD::swap(__ptr_, __r.__ptr_);
4974 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975}
4976
4977template<class _Tp>
4978inline _LIBCPP_INLINE_VISIBILITY
4979void
Howard Hinnant719bda32011-05-28 14:41:13 +00004980swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004981{
4982 __x.swap(__y);
4983}
4984
4985template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004986inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004987void
Howard Hinnant719bda32011-05-28 14:41:13 +00004988weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004989{
4990 weak_ptr().swap(*this);
4991}
4992
4993template<class _Tp>
4994template<class _Yp>
4995shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004996 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004997 : __ptr_(__r.__ptr_),
4998 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4999{
5000 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005001 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005002}
5003
5004template<class _Tp>
5005shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005006weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005007{
5008 shared_ptr<_Tp> __r;
5009 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5010 if (__r.__cntrl_)
5011 __r.__ptr_ = __ptr_;
5012 return __r;
5013}
5014
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005015#if _LIBCPP_STD_VER > 14
5016template <class _Tp = void> struct owner_less;
5017#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005018template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005019#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005020
5021template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005022struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005023 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005024{
5025 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005026 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005027 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005029 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005030 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005031 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005032 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005033 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005034 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005035};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005036
5037template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005038struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005039 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5040{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005041 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005042 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005043 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005044 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005045 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005046 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005047 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005048 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005049 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050 {return __x.owner_before(__y);}
5051};
5052
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005053#if _LIBCPP_STD_VER > 14
5054template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005055struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005056{
5057 template <class _Tp, class _Up>
5058 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005059 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005060 {return __x.owner_before(__y);}
5061 template <class _Tp, class _Up>
5062 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005063 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005064 {return __x.owner_before(__y);}
5065 template <class _Tp, class _Up>
5066 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005067 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005068 {return __x.owner_before(__y);}
5069 template <class _Tp, class _Up>
5070 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005071 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005072 {return __x.owner_before(__y);}
5073 typedef void is_transparent;
5074};
5075#endif
5076
Howard Hinnantc51e1022010-05-11 19:42:16 +00005077template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005078class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005079{
5080 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005081protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005082 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005083 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005085 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005087 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5088 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005090 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005091public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005093 shared_ptr<_Tp> shared_from_this()
5094 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005096 shared_ptr<_Tp const> shared_from_this() const
5097 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005098
Eric Fiselier84006862016-06-02 00:15:35 +00005099#if _LIBCPP_STD_VER > 14
5100 _LIBCPP_INLINE_VISIBILITY
5101 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5102 { return __weak_this_; }
5103
5104 _LIBCPP_INLINE_VISIBILITY
5105 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5106 { return __weak_this_; }
5107#endif // _LIBCPP_STD_VER > 14
5108
Howard Hinnantc51e1022010-05-11 19:42:16 +00005109 template <class _Up> friend class shared_ptr;
5110};
5111
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005112template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005113struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005114{
5115 typedef shared_ptr<_Tp> argument_type;
5116 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005117
Howard Hinnant756c69b2010-09-22 16:48:34 +00005118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005119 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005120 {
zoecarverd73f42a2020-05-11 18:42:50 -07005121 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005122 }
5123};
5124
Howard Hinnantc834c512011-11-29 18:15:50 +00005125template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005126inline _LIBCPP_INLINE_VISIBILITY
5127basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005128operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005129
Eric Fiselier9b492672016-06-18 02:12:53 +00005130
5131#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005132
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005133class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005134{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005135 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005136public:
5137 void lock() _NOEXCEPT;
5138 void unlock() _NOEXCEPT;
5139
5140private:
5141 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5142 __sp_mut(const __sp_mut&);
5143 __sp_mut& operator=(const __sp_mut&);
5144
Howard Hinnant8331b762013-03-06 23:30:19 +00005145 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005146};
5147
Mehdi Amini228053d2017-05-04 17:08:54 +00005148_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5149__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005150
5151template <class _Tp>
5152inline _LIBCPP_INLINE_VISIBILITY
5153bool
5154atomic_is_lock_free(const shared_ptr<_Tp>*)
5155{
5156 return false;
5157}
5158
5159template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005160_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005161shared_ptr<_Tp>
5162atomic_load(const shared_ptr<_Tp>* __p)
5163{
5164 __sp_mut& __m = __get_sp_mut(__p);
5165 __m.lock();
5166 shared_ptr<_Tp> __q = *__p;
5167 __m.unlock();
5168 return __q;
5169}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005170
Howard Hinnant9fa30202012-07-30 01:40:57 +00005171template <class _Tp>
5172inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005173_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005174shared_ptr<_Tp>
5175atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5176{
5177 return atomic_load(__p);
5178}
5179
5180template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005181_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005182void
5183atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5184{
5185 __sp_mut& __m = __get_sp_mut(__p);
5186 __m.lock();
5187 __p->swap(__r);
5188 __m.unlock();
5189}
5190
5191template <class _Tp>
5192inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005193_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005194void
5195atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5196{
5197 atomic_store(__p, __r);
5198}
5199
5200template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005201_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005202shared_ptr<_Tp>
5203atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5204{
5205 __sp_mut& __m = __get_sp_mut(__p);
5206 __m.lock();
5207 __p->swap(__r);
5208 __m.unlock();
5209 return __r;
5210}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005211
Howard Hinnant9fa30202012-07-30 01:40:57 +00005212template <class _Tp>
5213inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005214_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005215shared_ptr<_Tp>
5216atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5217{
5218 return atomic_exchange(__p, __r);
5219}
5220
5221template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005222_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005223bool
5224atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5225{
Marshall Clow4201ee82016-05-18 17:50:13 +00005226 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005227 __sp_mut& __m = __get_sp_mut(__p);
5228 __m.lock();
5229 if (__p->__owner_equivalent(*__v))
5230 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005231 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005232 *__p = __w;
5233 __m.unlock();
5234 return true;
5235 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005236 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005237 *__v = *__p;
5238 __m.unlock();
5239 return false;
5240}
5241
5242template <class _Tp>
5243inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005244_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005245bool
5246atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5247{
5248 return atomic_compare_exchange_strong(__p, __v, __w);
5249}
5250
5251template <class _Tp>
5252inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005253_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005254bool
5255atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5256 shared_ptr<_Tp> __w, memory_order, memory_order)
5257{
5258 return atomic_compare_exchange_strong(__p, __v, __w);
5259}
5260
5261template <class _Tp>
5262inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005263_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005264bool
5265atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5266 shared_ptr<_Tp> __w, memory_order, memory_order)
5267{
5268 return atomic_compare_exchange_weak(__p, __v, __w);
5269}
5270
Eric Fiselier9b492672016-06-18 02:12:53 +00005271#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005272
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005273//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005274#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5275# ifndef _LIBCPP_CXX03_LANG
5276enum class pointer_safety : unsigned char {
5277 relaxed,
5278 preferred,
5279 strict
5280};
5281# endif
5282#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005283struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005284{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005285 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005286 {
5287 relaxed,
5288 preferred,
5289 strict
5290 };
5291
Howard Hinnant49e145e2012-10-30 19:06:59 +00005292 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005293
Howard Hinnant756c69b2010-09-22 16:48:34 +00005294 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005295 pointer_safety() : __v_() {}
5296
5297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005298 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005300 operator int() const {return __v_;}
5301};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005302#endif
5303
5304#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005305 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005306_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5307#else
5308// This function is only offered in C++03 under ABI v1.
5309# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5310inline _LIBCPP_INLINE_VISIBILITY
5311pointer_safety get_pointer_safety() _NOEXCEPT {
5312 return pointer_safety::relaxed;
5313}
5314# endif
5315#endif
5316
Howard Hinnantc51e1022010-05-11 19:42:16 +00005317
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005318_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5319_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5320_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005321_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005322
5323template <class _Tp>
5324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005325_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005326undeclare_reachable(_Tp* __p)
5327{
5328 return static_cast<_Tp*>(__undeclare_reachable(__p));
5329}
5330
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005331_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005332
Marshall Clow8982dcd2015-07-13 20:04:56 +00005333// --- Helper for container swap --
5334template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005335inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005336void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5337#if _LIBCPP_STD_VER >= 14
5338 _NOEXCEPT
5339#else
5340 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5341#endif
5342{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005343 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005344 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5345}
5346
5347template <typename _Alloc>
5348_LIBCPP_INLINE_VISIBILITY
5349void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5350#if _LIBCPP_STD_VER >= 14
5351 _NOEXCEPT
5352#else
5353 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5354#endif
5355{
5356 using _VSTD::swap;
5357 swap(__a1, __a2);
5358}
5359
5360template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005361inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005362void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5363
Marshall Clowff91de82015-08-18 19:51:37 +00005364template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005365struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005366 _Traits::propagate_on_container_move_assignment::value
5367#if _LIBCPP_STD_VER > 14
5368 || _Traits::is_always_equal::value
5369#else
5370 && is_nothrow_move_assignable<_Alloc>::value
5371#endif
5372 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005373
Marshall Clowa591b9a2016-07-11 21:38:08 +00005374
5375#ifndef _LIBCPP_HAS_NO_VARIADICS
5376template <class _Tp, class _Alloc>
5377struct __temp_value {
5378 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005379
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005380 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005381 _Alloc &__a;
5382
5383 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5384 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005385
Marshall Clowa591b9a2016-07-11 21:38:08 +00005386 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005387 _LIBCPP_NO_CFI
5388 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5389 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5390 _VSTD::forward<_Args>(__args)...);
5391 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005392
Marshall Clowa591b9a2016-07-11 21:38:08 +00005393 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5394 };
5395#endif
5396
Marshall Clowe46031a2018-07-02 18:41:15 +00005397template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005398struct __is_allocator : false_type {};
5399
5400template<typename _Alloc>
5401struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005402 typename __void_t<typename _Alloc::value_type>::type,
5403 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5404 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005405 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005406
Eric Fiselier74ebee62019-06-08 01:31:19 +00005407// __builtin_new_allocator -- A non-templated helper for allocating and
5408// deallocating memory using __builtin_operator_new and
5409// __builtin_operator_delete. It should be used in preference to
5410// `std::allocator<T>` to avoid additional instantiations.
5411struct __builtin_new_allocator {
5412 struct __builtin_new_deleter {
5413 typedef void* pointer_type;
5414
5415 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5416 : __size_(__size), __align_(__align) {}
5417
5418 void operator()(void* p) const _NOEXCEPT {
5419 std::__libcpp_deallocate(p, __size_, __align_);
5420 }
5421
5422 private:
5423 size_t __size_;
5424 size_t __align_;
5425 };
5426
5427 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5428
5429 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5430 return __holder_t(std::__libcpp_allocate(__s, __align),
5431 __builtin_new_deleter(__s, __align));
5432 }
5433
5434 static void __deallocate_bytes(void* __p, size_t __s,
5435 size_t __align) _NOEXCEPT {
5436 std::__libcpp_deallocate(__p, __s, __align);
5437 }
5438
5439 template <class _Tp>
5440 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5441 static __holder_t __allocate_type(size_t __n) {
5442 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5443 }
5444
5445 template <class _Tp>
5446 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5447 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5448 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5449 }
5450};
5451
5452
Howard Hinnantc51e1022010-05-11 19:42:16 +00005453_LIBCPP_END_NAMESPACE_STD
5454
Eric Fiselierf4433a32017-05-31 22:07:49 +00005455_LIBCPP_POP_MACROS
5456
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005457#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005458# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005459#endif
5460
Howard Hinnantc51e1022010-05-11 19:42:16 +00005461#endif // _LIBCPP_MEMORY