blob: 54aeab6ce82d307db0cf2c12769ac35f62d5454f [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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1311{
Michael Park99f9e912020-03-04 11:27:14 -05001312 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001313 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001314 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315};
1316
1317template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1319{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001320 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321};
1322
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001323#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
Michael Park99f9e912020-03-04 11:27:14 -05001325_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1327auto
1328__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001329 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001330_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
1332template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1333auto
1334__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1335 -> false_type;
1336
1337template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1338struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001339 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1340 declval<_SizeType>(),
1341 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342{
1343};
1344
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001345#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
1347template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1348struct __has_allocate_hint
1349 : true_type
1350{
1351};
1352
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001353#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354
zoecarver20590dd2020-05-23 14:03:04 -07001355template <class _Alloc, class ..._Args,
1356 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1357static true_type __test_has_construct(int);
1358template <class _Alloc, class...>
1359static false_type __test_has_construct(...);
1360
1361template <class _Alloc, class ..._Args>
1362struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1363
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001364#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365
Michael Park99f9e912020-03-04 11:27:14 -05001366_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367template <class _Alloc, class _Pointer>
1368auto
1369__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1370 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001371_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
1373template <class _Alloc, class _Pointer>
1374auto
1375__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1376 -> false_type;
1377
1378template <class _Alloc, class _Pointer>
1379struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001380 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1381 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382{
1383};
1384
Michael Park99f9e912020-03-04 11:27:14 -05001385_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386template <class _Alloc>
1387auto
1388__has_max_size_test(_Alloc&& __a)
1389 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001390_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391
1392template <class _Alloc>
1393auto
1394__has_max_size_test(const volatile _Alloc& __a)
1395 -> false_type;
1396
1397template <class _Alloc>
1398struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001399 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400{
1401};
1402
1403template <class _Alloc>
1404auto
1405__has_select_on_container_copy_construction_test(_Alloc&& __a)
1406 -> decltype(__a.select_on_container_copy_construction(), true_type());
1407
1408template <class _Alloc>
1409auto
1410__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1411 -> false_type;
1412
1413template <class _Alloc>
1414struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001415 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416{
1417};
1418
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001419#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001421template <class _Alloc, class _Pointer, class = void>
1422struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423
1424template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001425struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1426 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1427>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428
1429template <class _Alloc>
1430struct __has_max_size
1431 : true_type
1432{
1433};
1434
1435template <class _Alloc>
1436struct __has_select_on_container_copy_construction
1437 : false_type
1438{
1439};
1440
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001441#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001443template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1444struct __alloc_traits_difference_type
1445{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001446 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001447};
1448
1449template <class _Alloc, class _Ptr>
1450struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1451{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001452 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001453};
1454
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001455template <class _Tp>
1456struct __is_default_allocator : false_type {};
1457
1458template <class _Tp>
1459struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1460
Eric Fiselier909fe962019-09-13 16:09:33 +00001461
1462
1463template <class _Alloc,
1464 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1465 >
1466struct __is_cpp17_move_insertable;
1467template <class _Alloc>
1468struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1469template <class _Alloc>
1470struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1471
1472template <class _Alloc,
1473 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1474 >
1475struct __is_cpp17_copy_insertable;
1476template <class _Alloc>
1477struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1478template <class _Alloc>
1479struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1480 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1481 __is_cpp17_move_insertable<_Alloc>::value>
1482 {};
1483
1484
1485
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001487struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488{
1489 typedef _Alloc allocator_type;
1490 typedef typename allocator_type::value_type value_type;
1491
1492 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1493 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1494 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1495 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1496
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001497 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1498 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499
1500 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1501 propagate_on_container_copy_assignment;
1502 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1503 propagate_on_container_move_assignment;
1504 typedef typename __propagate_on_container_swap<allocator_type>::type
1505 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001506 typedef typename __is_always_equal<allocator_type>::type
1507 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001509#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001511 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001512 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001513#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 template <class _Tp> struct rebind_alloc
1515 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1516 template <class _Tp> struct rebind_traits
1517 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001518#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519
Marshall Clow0e58cae2017-11-26 02:55:38 +00001520 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 static pointer allocate(allocator_type& __a, size_type __n)
1522 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001523 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001525 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1527
Howard Hinnant756c69b2010-09-22 16:48:34 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001529 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 {__a.deallocate(__p, __n);}
1531
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001535 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001536 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537
1538 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static void destroy(allocator_type& __a, _Tp* __p)
1541 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1542
Howard Hinnant756c69b2010-09-22 16:48:34 +00001543 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001544 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 static allocator_type
1549 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001550 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 __has_select_on_container_copy_construction<const allocator_type>(),
1552 __a);}
1553
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001554 template <class _Ptr>
1555 _LIBCPP_INLINE_VISIBILITY
1556 static
1557 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001558 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001559 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001560 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1561 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001562 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001563 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001564#ifdef _LIBCPP_NO_EXCEPTIONS
1565 _VSTD::move(*__begin1)
1566#else
1567 _VSTD::move_if_noexcept(*__begin1)
1568#endif
1569 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001570 }
1571
1572 template <class _Tp>
1573 _LIBCPP_INLINE_VISIBILITY
1574 static
1575 typename enable_if
1576 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001577 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001578 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1579 is_trivially_move_constructible<_Tp>::value,
1580 void
1581 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001582 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001583 {
1584 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001585 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001586 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001587 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001588 __begin2 += _Np;
1589 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001590 }
1591
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001592 template <class _Iter, class _Ptr>
1593 _LIBCPP_INLINE_VISIBILITY
1594 static
1595 void
1596 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1597 {
1598 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001599 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001600 }
1601
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001602 template <class _SourceTp, class _DestTp,
1603 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1604 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001605 _LIBCPP_INLINE_VISIBILITY
1606 static
1607 typename enable_if
1608 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001609 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001610 is_same<_RawSourceTp, _RawDestTp>::value &&
1611 (__is_default_allocator<allocator_type>::value ||
1612 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001613 void
1614 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001615 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001616 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001617 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001618 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001619 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001620 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001621 __begin2 += _Np;
1622 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001623 }
1624
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001625 template <class _Ptr>
1626 _LIBCPP_INLINE_VISIBILITY
1627 static
1628 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001629 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001631 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1632 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001633 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001634 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001635 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001636#ifdef _LIBCPP_NO_EXCEPTIONS
1637 _VSTD::move(*--__end1)
1638#else
1639 _VSTD::move_if_noexcept(*--__end1)
1640#endif
1641 );
1642 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001643 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001644 }
1645
1646 template <class _Tp>
1647 _LIBCPP_INLINE_VISIBILITY
1648 static
1649 typename enable_if
1650 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001651 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001652 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1653 is_trivially_move_constructible<_Tp>::value,
1654 void
1655 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001656 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001657 {
1658 ptrdiff_t _Np = __end1 - __begin1;
1659 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001660 if (_Np > 0)
1661 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001662 }
1663
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664private:
1665
Howard Hinnant756c69b2010-09-22 16:48:34 +00001666 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001667 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001669 {
1670 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1671 return __a.allocate(__n, __hint);
1672 _LIBCPP_SUPPRESS_DEPRECATED_POP
1673 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001674 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001675 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001676 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677 {return __a.allocate(__n);}
1678
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001682 {
1683 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1684 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1685 _LIBCPP_SUPPRESS_DEPRECATED_POP
1686 }
1687
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1691 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001692 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694
1695 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001698 {
1699 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1700 __a.destroy(__p);
1701 _LIBCPP_SUPPRESS_DEPRECATED_POP
1702 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 static void __destroy(false_type, allocator_type&, _Tp* __p)
1706 {
1707 __p->~_Tp();
1708 }
1709
Howard Hinnant756c69b2010-09-22 16:48:34 +00001710 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001711 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001712 {
1713 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1714 return __a.max_size();
1715 _LIBCPP_SUPPRESS_DEPRECATED_POP
1716 }
1717
Howard Hinnant756c69b2010-09-22 16:48:34 +00001718 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001719 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001720 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721
Howard Hinnant756c69b2010-09-22 16:48:34 +00001722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001724 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001728 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729 {return __a;}
1730};
1731
Marshall Clow940e01c2015-04-07 05:21:38 +00001732template <class _Traits, class _Tp>
1733struct __rebind_alloc_helper
1734{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001735#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001736 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001737#else
1738 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1739#endif
1740};
1741
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742// allocator
1743
1744template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001745class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746{
1747public:
Michael Park99f9e912020-03-04 11:27:14 -05001748#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1749 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1750 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1751 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1752 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1753 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1754 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1755
1756 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1757#endif
1758
1759 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760
Howard Hinnant4931e092011-06-02 21:38:57 +00001761 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001762 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001763
Marshall Clowbc759762018-03-20 23:02:53 +00001764 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1765 allocator() _NOEXCEPT {}
1766
Louis Dionne481a2662018-09-23 18:35:00 +00001767 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001768 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1769 allocator(const allocator<_Up>&) _NOEXCEPT {}
1770
Michael Park99f9e912020-03-04 11:27:14 -05001771#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1772 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1773 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001774 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001775 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1776 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001777 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001778#endif
1779
1780 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001781 {
Michael Park99f9e912020-03-04 11:27:14 -05001782 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1783 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001786 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001787 }
Michael Park99f9e912020-03-04 11:27:14 -05001788
1789#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1790 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1791 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1792#endif
1793
1794 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001795 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001796
1797#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1798 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001799 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001800
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001802 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 void
1804 construct(_Up* __p, _Args&&... __args)
1805 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001806 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 }
Michael Park99f9e912020-03-04 11:27:14 -05001808 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1809#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810};
1811
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001812template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001813class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001814{
1815public:
Michael Park99f9e912020-03-04 11:27:14 -05001816#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1817 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1818 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1819 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1820 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1821 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1822 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1823
1824 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1825#endif
1826
1827 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001828
1829 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001830 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001831
Marshall Clowbc759762018-03-20 23:02:53 +00001832 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1833 allocator() _NOEXCEPT {}
1834
1835 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001836 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001837 allocator(const allocator<_Up>&) _NOEXCEPT {}
1838
Michael Park99f9e912020-03-04 11:27:14 -05001839#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1840 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1841 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001842 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001843#endif
1844
1845 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001846 {
Michael Park99f9e912020-03-04 11:27:14 -05001847 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1848 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001849 __throw_length_error("allocator<const T>::allocate(size_t n)"
1850 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001851 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001852 }
Michael Park99f9e912020-03-04 11:27:14 -05001853
1854#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1855 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1856 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1857#endif
1858
1859 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001860 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001861
1862#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1863 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001864 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001865
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001866#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1867 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001868 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869 void
1870 construct(_Up* __p, _Args&&... __args)
1871 {
1872 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1873 }
1874#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1875 _LIBCPP_INLINE_VISIBILITY
1876 void
1877 construct(pointer __p)
1878 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001879 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001880 }
1881# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001882
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001883 template <class _A0>
1884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001885 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 construct(pointer __p, _A0& __a0)
1887 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001888 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001889 }
1890 template <class _A0>
1891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001892 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001893 construct(pointer __p, const _A0& __a0)
1894 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001895 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001896 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001897# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1898 template <class _A0, class _A1>
1899 _LIBCPP_INLINE_VISIBILITY
1900 void
1901 construct(pointer __p, _A0& __a0, _A1& __a1)
1902 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001903 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001904 }
1905 template <class _A0, class _A1>
1906 _LIBCPP_INLINE_VISIBILITY
1907 void
1908 construct(pointer __p, const _A0& __a0, _A1& __a1)
1909 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001910 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001911 }
1912 template <class _A0, class _A1>
1913 _LIBCPP_INLINE_VISIBILITY
1914 void
1915 construct(pointer __p, _A0& __a0, const _A1& __a1)
1916 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001917 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001918 }
1919 template <class _A0, class _A1>
1920 _LIBCPP_INLINE_VISIBILITY
1921 void
1922 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1923 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001924 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001925 }
1926#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001927 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1928#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001929};
1930
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931template <class _Tp, class _Up>
1932inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001933bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934
1935template <class _Tp, class _Up>
1936inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001937bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938
1939template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001940class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941 : public iterator<output_iterator_tag,
1942 _Tp, // purposefully not C++03
1943 ptrdiff_t, // purposefully not C++03
1944 _Tp*, // purposefully not C++03
1945 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1946{
1947private:
1948 _OutputIterator __x_;
1949public:
1950 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1951 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1952 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001953 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001954#if _LIBCPP_STD_VER >= 14
1955 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001956 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001957#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1959 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1960 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001961#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001962 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001963#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964};
1965
1966template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001967_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001969get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970{
1971 pair<_Tp*, ptrdiff_t> __r(0, 0);
1972 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1973 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1974 / sizeof(_Tp);
1975 if (__n > __m)
1976 __n = __m;
1977 while (__n > 0)
1978 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001979#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001980 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001981 {
1982 std::align_val_t __al =
1983 std::align_val_t(std::alignment_of<_Tp>::value);
1984 __r.first = static_cast<_Tp*>(::operator new(
1985 __n * sizeof(_Tp), __al, nothrow));
1986 } else {
1987 __r.first = static_cast<_Tp*>(::operator new(
1988 __n * sizeof(_Tp), nothrow));
1989 }
1990#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001991 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001992 {
1993 // Since aligned operator new is unavailable, return an empty
1994 // buffer rather than one with invalid alignment.
1995 return __r;
1996 }
1997
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001999#endif
2000
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 if (__r.first)
2002 {
2003 __r.second = __n;
2004 break;
2005 }
2006 __n /= 2;
2007 }
2008 return __r;
2009}
2010
2011template <class _Tp>
2012inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002013void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2014{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002015 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002016}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017
Marshall Clowb22274f2017-01-24 22:22:33 +00002018#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002020struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021{
2022 _Tp* __ptr_;
2023};
2024
2025template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002026class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027{
2028private:
2029 _Tp* __ptr_;
2030public:
2031 typedef _Tp element_type;
2032
Dimitry Andric47269ce2020-03-13 19:36:26 +01002033 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
2034 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002039 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002041 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002043 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044
Dimitry Andric47269ce2020-03-13 19:36:26 +01002045 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002047 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
2048 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
2049 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050 {
2051 _Tp* __t = __ptr_;
2052 __ptr_ = 0;
2053 return __t;
2054 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01002055 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056 {
2057 if (__ptr_ != __p)
2058 delete __ptr_;
2059 __ptr_ = __p;
2060 }
2061
Dimitry Andric47269ce2020-03-13 19:36:26 +01002062 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
2063 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01002065 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066 {return auto_ptr<_Up>(release());}
2067};
2068
2069template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002070class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071{
2072public:
2073 typedef void element_type;
2074};
Marshall Clowb22274f2017-01-24 22:22:33 +00002075#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002077// Tag used to default initialize one or both of the pair's elements.
2078struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002079struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081template <class _Tp, int _Idx,
2082 bool _CanBeEmptyBase =
2083 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2084struct __compressed_pair_elem {
2085 typedef _Tp _ParamT;
2086 typedef _Tp& reference;
2087 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002089 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002090 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002091 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2092 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093
Eric Fiselier9d355982017-04-12 23:45:53 +00002094 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002095 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2096 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002097 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002098 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002099 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002100 : __value_(_VSTD::forward<_Up>(__u))
2101 {
2102 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002104
2105#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002106 template <class... _Args, size_t... _Indexes>
2107 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2108 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2109 __tuple_indices<_Indexes...>)
2110 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002111#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002113
Alex Lorenz76132112017-11-09 17:54:49 +00002114 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2115 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002116 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002119 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120};
2121
Eric Fiselier9d355982017-04-12 23:45:53 +00002122template <class _Tp, int _Idx>
2123struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2124 typedef _Tp _ParamT;
2125 typedef _Tp& reference;
2126 typedef const _Tp& const_reference;
2127 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002129 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2130 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2131 __compressed_pair_elem(__default_init_tag) {}
2132 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2133 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134
Eric Fiselier9d355982017-04-12 23:45:53 +00002135 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002136 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2137 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002138 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002139 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002140 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002141 : __value_type(_VSTD::forward<_Up>(__u))
2142 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002144#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002145 template <class... _Args, size_t... _Indexes>
2146 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2147 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2148 __tuple_indices<_Indexes...>)
2149 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002150#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151
Alex Lorenz76132112017-11-09 17:54:49 +00002152 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2153 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002154 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155};
2156
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002158class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2159 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002160 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2161 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002162
2163 // NOTE: This static assert should never fire because __compressed_pair
2164 // is *almost never* used in a scenario where it's possible for T1 == T2.
2165 // (The exception is std::function where it is possible that the function
2166 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002167 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002168 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2169 "The current implementation is NOT ABI-compatible with the previous "
2170 "implementation for this configuration");
2171
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002173 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002174 class = typename enable_if<
2175 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2176 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2177 >::type
2178 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002179 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002180 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Eric Fiselier9d355982017-04-12 23:45:53 +00002182 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002183 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002184 __compressed_pair(_U1&& __t1, _U2&& __t2)
2185 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002187#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002188 template <class... _Args1, class... _Args2>
2189 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2190 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2191 tuple<_Args2...> __second_args)
2192 : _Base1(__pc, _VSTD::move(__first_args),
2193 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2194 _Base2(__pc, _VSTD::move(__second_args),
2195 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197
Eric Fiselier9d355982017-04-12 23:45:53 +00002198 _LIBCPP_INLINE_VISIBILITY
2199 typename _Base1::reference first() _NOEXCEPT {
2200 return static_cast<_Base1&>(*this).__get();
2201 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202
Eric Fiselier9d355982017-04-12 23:45:53 +00002203 _LIBCPP_INLINE_VISIBILITY
2204 typename _Base1::const_reference first() const _NOEXCEPT {
2205 return static_cast<_Base1 const&>(*this).__get();
2206 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207
Eric Fiselier9d355982017-04-12 23:45:53 +00002208 _LIBCPP_INLINE_VISIBILITY
2209 typename _Base2::reference second() _NOEXCEPT {
2210 return static_cast<_Base2&>(*this).__get();
2211 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Eric Fiselier9d355982017-04-12 23:45:53 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 typename _Base2::const_reference second() const _NOEXCEPT {
2215 return static_cast<_Base2 const&>(*this).__get();
2216 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218 _LIBCPP_INLINE_VISIBILITY
2219 void swap(__compressed_pair& __x)
2220 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2221 __is_nothrow_swappable<_T2>::value)
2222 {
2223 using std::swap;
2224 swap(first(), __x.first());
2225 swap(second(), __x.second());
2226 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227};
2228
2229template <class _T1, class _T2>
2230inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002231void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2232 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2233 __is_nothrow_swappable<_T2>::value) {
2234 __x.swap(__y);
2235}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002237// default_delete
2238
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002240struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002241 static_assert(!is_function<_Tp>::value,
2242 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002243#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002244 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002245#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002246 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002247#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002248 template <class _Up>
2249 _LIBCPP_INLINE_VISIBILITY
2250 default_delete(const default_delete<_Up>&,
2251 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2252 0) _NOEXCEPT {}
2253
2254 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2255 static_assert(sizeof(_Tp) > 0,
2256 "default_delete can not delete incomplete type");
2257 static_assert(!is_void<_Tp>::value,
2258 "default_delete can not delete incomplete type");
2259 delete __ptr;
2260 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261};
2262
2263template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002264struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2265private:
2266 template <class _Up>
2267 struct _EnableIfConvertible
2268 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2269
Howard Hinnant4500ca52011-12-18 21:19:44 +00002270public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002271#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002272 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002273#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002274 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002275#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002276
2277 template <class _Up>
2278 _LIBCPP_INLINE_VISIBILITY
2279 default_delete(const default_delete<_Up[]>&,
2280 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2281
2282 template <class _Up>
2283 _LIBCPP_INLINE_VISIBILITY
2284 typename _EnableIfConvertible<_Up>::type
2285 operator()(_Up* __ptr) const _NOEXCEPT {
2286 static_assert(sizeof(_Tp) > 0,
2287 "default_delete can not delete incomplete type");
2288 static_assert(!is_void<_Tp>::value,
2289 "default_delete can not delete void type");
2290 delete[] __ptr;
2291 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292};
2293
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002294template <class _Deleter>
2295struct __unique_ptr_deleter_sfinae {
2296 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2297 typedef const _Deleter& __lval_ref_type;
2298 typedef _Deleter&& __good_rval_ref_type;
2299 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300};
2301
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002302template <class _Deleter>
2303struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2304 typedef const _Deleter& __lval_ref_type;
2305 typedef const _Deleter&& __bad_rval_ref_type;
2306 typedef false_type __enable_rval_overload;
2307};
2308
2309template <class _Deleter>
2310struct __unique_ptr_deleter_sfinae<_Deleter&> {
2311 typedef _Deleter& __lval_ref_type;
2312 typedef _Deleter&& __bad_rval_ref_type;
2313 typedef false_type __enable_rval_overload;
2314};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002315
2316template <class _Tp, class _Dp = default_delete<_Tp> >
2317class _LIBCPP_TEMPLATE_VIS unique_ptr {
2318public:
2319 typedef _Tp element_type;
2320 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002321 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002322
2323 static_assert(!is_rvalue_reference<deleter_type>::value,
2324 "the specified deleter type cannot be an rvalue reference");
2325
2326private:
2327 __compressed_pair<pointer, deleter_type> __ptr_;
2328
2329 struct __nat { int __for_bool_; };
2330
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002331 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002332
2333 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002334 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002335 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002336
2337 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002338 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002339 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002340
2341 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002342 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002343 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002344
2345 template <bool _Dummy, class _Deleter = typename __dependent_type<
2346 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002347 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002348 typename enable_if<is_default_constructible<_Deleter>::value &&
2349 !is_pointer<_Deleter>::value>::type;
2350
2351 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002352 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002353 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2354
2355 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002356 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002357 is_convertible<typename _UPtr::pointer, pointer>::value &&
2358 !is_array<_Up>::value
2359 >::type;
2360
2361 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002362 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002363 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2364 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2365 >::type;
2366
2367 template <class _UDel>
2368 using _EnableIfDeleterAssignable = typename enable_if<
2369 is_assignable<_Dp&, _UDel&&>::value
2370 >::type;
2371
2372public:
2373 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002374 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002375 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002376 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002377
2378 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002379 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002380 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002381 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002382
2383 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002384 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002385 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002386 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002387
2388 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002389 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002390 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002391 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002392 : __ptr_(__p, __d) {}
2393
2394 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002395 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002396 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002397 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002398 : __ptr_(__p, _VSTD::move(__d)) {
2399 static_assert(!is_reference<deleter_type>::value,
2400 "rvalue deleter bound to reference");
2401 }
2402
2403 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002404 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002405 _LIBCPP_INLINE_VISIBILITY
2406 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2407
2408 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002409 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002410 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2411 }
2412
2413 template <class _Up, class _Ep,
2414 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2415 class = _EnableIfDeleterConvertible<_Ep>
2416 >
2417 _LIBCPP_INLINE_VISIBILITY
2418 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2419 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2420
2421#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2422 template <class _Up>
2423 _LIBCPP_INLINE_VISIBILITY
2424 unique_ptr(auto_ptr<_Up>&& __p,
2425 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002426 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002428 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002429#endif
2430
2431 _LIBCPP_INLINE_VISIBILITY
2432 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2433 reset(__u.release());
2434 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2435 return *this;
2436 }
2437
2438 template <class _Up, class _Ep,
2439 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2440 class = _EnableIfDeleterAssignable<_Ep>
2441 >
2442 _LIBCPP_INLINE_VISIBILITY
2443 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2444 reset(__u.release());
2445 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2446 return *this;
2447 }
2448
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002449#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2450 template <class _Up>
2451 _LIBCPP_INLINE_VISIBILITY
2452 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2453 is_same<_Dp, default_delete<_Tp> >::value,
2454 unique_ptr&>::type
2455 operator=(auto_ptr<_Up> __p) {
2456 reset(__p.release());
2457 return *this;
2458 }
2459#endif
2460
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002461#ifdef _LIBCPP_CXX03_LANG
2462 unique_ptr(unique_ptr const&) = delete;
2463 unique_ptr& operator=(unique_ptr const&) = delete;
2464#endif
2465
2466
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002467 _LIBCPP_INLINE_VISIBILITY
2468 ~unique_ptr() { reset(); }
2469
2470 _LIBCPP_INLINE_VISIBILITY
2471 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2472 reset();
2473 return *this;
2474 }
2475
2476 _LIBCPP_INLINE_VISIBILITY
2477 typename add_lvalue_reference<_Tp>::type
2478 operator*() const {
2479 return *__ptr_.first();
2480 }
2481 _LIBCPP_INLINE_VISIBILITY
2482 pointer operator->() const _NOEXCEPT {
2483 return __ptr_.first();
2484 }
2485 _LIBCPP_INLINE_VISIBILITY
2486 pointer get() const _NOEXCEPT {
2487 return __ptr_.first();
2488 }
2489 _LIBCPP_INLINE_VISIBILITY
2490 deleter_type& get_deleter() _NOEXCEPT {
2491 return __ptr_.second();
2492 }
2493 _LIBCPP_INLINE_VISIBILITY
2494 const deleter_type& get_deleter() const _NOEXCEPT {
2495 return __ptr_.second();
2496 }
2497 _LIBCPP_INLINE_VISIBILITY
2498 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2499 return __ptr_.first() != nullptr;
2500 }
2501
2502 _LIBCPP_INLINE_VISIBILITY
2503 pointer release() _NOEXCEPT {
2504 pointer __t = __ptr_.first();
2505 __ptr_.first() = pointer();
2506 return __t;
2507 }
2508
2509 _LIBCPP_INLINE_VISIBILITY
2510 void reset(pointer __p = pointer()) _NOEXCEPT {
2511 pointer __tmp = __ptr_.first();
2512 __ptr_.first() = __p;
2513 if (__tmp)
2514 __ptr_.second()(__tmp);
2515 }
2516
2517 _LIBCPP_INLINE_VISIBILITY
2518 void swap(unique_ptr& __u) _NOEXCEPT {
2519 __ptr_.swap(__u.__ptr_);
2520 }
2521};
2522
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002523
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002527 typedef _Tp element_type;
2528 typedef _Dp deleter_type;
2529 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2530
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002532 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533
Eric Fiselier31127cd2017-04-16 02:14:31 +00002534 template <class _From>
2535 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536
Eric Fiselier31127cd2017-04-16 02:14:31 +00002537 template <class _FromElem>
2538 struct _CheckArrayPointerConversion<_FromElem*>
2539 : integral_constant<bool,
2540 is_same<_FromElem*, pointer>::value ||
2541 (is_same<pointer, element_type*>::value &&
2542 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2543 >
2544 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545
Eric Fiselier31127cd2017-04-16 02:14:31 +00002546 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002547
2548 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002549 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002550 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002551
2552 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002553 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002554 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002555
2556 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002557 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002558 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002559
2560 template <bool _Dummy, class _Deleter = typename __dependent_type<
2561 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002562 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002563 typename enable_if<is_default_constructible<_Deleter>::value &&
2564 !is_pointer<_Deleter>::value>::type;
2565
2566 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002567 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002568 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2569
2570 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002571 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002572 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002573 >::type;
2574
2575 template <class _UPtr, class _Up,
2576 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002577 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002578 is_array<_Up>::value &&
2579 is_same<pointer, element_type*>::value &&
2580 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2581 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2582 >::type;
2583
2584 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002585 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002586 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2587 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2588 >::type;
2589
2590 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002591 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002592 is_assignable<_Dp&, _UDel&&>::value
2593 >::type;
2594
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002596 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002597 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002598 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002599 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002601 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002602 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002604 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002606 template <class _Pp, bool _Dummy = true,
2607 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002608 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002609 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002610 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002611 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002613 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002614 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2615 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002616 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002617 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002618 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002620 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002621 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002622 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002623 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002624 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002626 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002627 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2628 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002630 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002631 : __ptr_(__p, _VSTD::move(__d)) {
2632 static_assert(!is_reference<deleter_type>::value,
2633 "rvalue deleter bound to reference");
2634 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002636 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002637 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002638 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002639 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002640 : __ptr_(nullptr, _VSTD::move(__d)) {
2641 static_assert(!is_reference<deleter_type>::value,
2642 "rvalue deleter bound to reference");
2643 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002644
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002645 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002646 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2647 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002648 _LIBCPP_INLINE_VISIBILITY
2649 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002650
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002651 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002652 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002653 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2654 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002655
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002656 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002657 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002658 reset(__u.release());
2659 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2660 return *this;
2661 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002663 template <class _Up, class _Ep,
2664 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2665 class = _EnableIfDeleterConvertible<_Ep>
2666 >
2667 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002668 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002669 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002670 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002672 template <class _Up, class _Ep,
2673 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2674 class = _EnableIfDeleterAssignable<_Ep>
2675 >
2676 _LIBCPP_INLINE_VISIBILITY
2677 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002678 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002679 reset(__u.release());
2680 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2681 return *this;
2682 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002684#ifdef _LIBCPP_CXX03_LANG
2685 unique_ptr(unique_ptr const&) = delete;
2686 unique_ptr& operator=(unique_ptr const&) = delete;
2687#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688
2689public:
2690 _LIBCPP_INLINE_VISIBILITY
2691 ~unique_ptr() { reset(); }
2692
2693 _LIBCPP_INLINE_VISIBILITY
2694 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2695 reset();
2696 return *this;
2697 }
2698
2699 _LIBCPP_INLINE_VISIBILITY
2700 typename add_lvalue_reference<_Tp>::type
2701 operator[](size_t __i) const {
2702 return __ptr_.first()[__i];
2703 }
2704 _LIBCPP_INLINE_VISIBILITY
2705 pointer get() const _NOEXCEPT {
2706 return __ptr_.first();
2707 }
2708
2709 _LIBCPP_INLINE_VISIBILITY
2710 deleter_type& get_deleter() _NOEXCEPT {
2711 return __ptr_.second();
2712 }
2713
2714 _LIBCPP_INLINE_VISIBILITY
2715 const deleter_type& get_deleter() const _NOEXCEPT {
2716 return __ptr_.second();
2717 }
2718 _LIBCPP_INLINE_VISIBILITY
2719 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2720 return __ptr_.first() != nullptr;
2721 }
2722
2723 _LIBCPP_INLINE_VISIBILITY
2724 pointer release() _NOEXCEPT {
2725 pointer __t = __ptr_.first();
2726 __ptr_.first() = pointer();
2727 return __t;
2728 }
2729
2730 template <class _Pp>
2731 _LIBCPP_INLINE_VISIBILITY
2732 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002733 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002734 >::type
2735 reset(_Pp __p) _NOEXCEPT {
2736 pointer __tmp = __ptr_.first();
2737 __ptr_.first() = __p;
2738 if (__tmp)
2739 __ptr_.second()(__tmp);
2740 }
2741
2742 _LIBCPP_INLINE_VISIBILITY
2743 void reset(nullptr_t = nullptr) _NOEXCEPT {
2744 pointer __tmp = __ptr_.first();
2745 __ptr_.first() = nullptr;
2746 if (__tmp)
2747 __ptr_.second()(__tmp);
2748 }
2749
2750 _LIBCPP_INLINE_VISIBILITY
2751 void swap(unique_ptr& __u) _NOEXCEPT {
2752 __ptr_.swap(__u.__ptr_);
2753 }
2754
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755};
2756
2757template <class _Tp, class _Dp>
2758inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002759typename enable_if<
2760 __is_swappable<_Dp>::value,
2761 void
2762>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002763swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764
2765template <class _T1, class _D1, class _T2, class _D2>
2766inline _LIBCPP_INLINE_VISIBILITY
2767bool
2768operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2769
2770template <class _T1, class _D1, class _T2, class _D2>
2771inline _LIBCPP_INLINE_VISIBILITY
2772bool
2773operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2774
2775template <class _T1, class _D1, class _T2, class _D2>
2776inline _LIBCPP_INLINE_VISIBILITY
2777bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002778operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2779{
2780 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2781 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002782 typedef typename common_type<_P1, _P2>::type _Vp;
2783 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002784}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785
2786template <class _T1, class _D1, class _T2, class _D2>
2787inline _LIBCPP_INLINE_VISIBILITY
2788bool
2789operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2790
2791template <class _T1, class _D1, class _T2, class _D2>
2792inline _LIBCPP_INLINE_VISIBILITY
2793bool
2794operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2795
2796template <class _T1, class _D1, class _T2, class _D2>
2797inline _LIBCPP_INLINE_VISIBILITY
2798bool
2799operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2800
Howard Hinnantb17caf92012-02-21 21:02:58 +00002801template <class _T1, class _D1>
2802inline _LIBCPP_INLINE_VISIBILITY
2803bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002804operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002805{
2806 return !__x;
2807}
2808
2809template <class _T1, class _D1>
2810inline _LIBCPP_INLINE_VISIBILITY
2811bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002812operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002813{
2814 return !__x;
2815}
2816
2817template <class _T1, class _D1>
2818inline _LIBCPP_INLINE_VISIBILITY
2819bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002820operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002821{
2822 return static_cast<bool>(__x);
2823}
2824
2825template <class _T1, class _D1>
2826inline _LIBCPP_INLINE_VISIBILITY
2827bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002828operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002829{
2830 return static_cast<bool>(__x);
2831}
2832
2833template <class _T1, class _D1>
2834inline _LIBCPP_INLINE_VISIBILITY
2835bool
2836operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2837{
2838 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2839 return less<_P1>()(__x.get(), nullptr);
2840}
2841
2842template <class _T1, class _D1>
2843inline _LIBCPP_INLINE_VISIBILITY
2844bool
2845operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2846{
2847 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2848 return less<_P1>()(nullptr, __x.get());
2849}
2850
2851template <class _T1, class _D1>
2852inline _LIBCPP_INLINE_VISIBILITY
2853bool
2854operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2855{
2856 return nullptr < __x;
2857}
2858
2859template <class _T1, class _D1>
2860inline _LIBCPP_INLINE_VISIBILITY
2861bool
2862operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2863{
2864 return __x < nullptr;
2865}
2866
2867template <class _T1, class _D1>
2868inline _LIBCPP_INLINE_VISIBILITY
2869bool
2870operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2871{
2872 return !(nullptr < __x);
2873}
2874
2875template <class _T1, class _D1>
2876inline _LIBCPP_INLINE_VISIBILITY
2877bool
2878operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2879{
2880 return !(__x < nullptr);
2881}
2882
2883template <class _T1, class _D1>
2884inline _LIBCPP_INLINE_VISIBILITY
2885bool
2886operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2887{
2888 return !(__x < nullptr);
2889}
2890
2891template <class _T1, class _D1>
2892inline _LIBCPP_INLINE_VISIBILITY
2893bool
2894operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2895{
2896 return !(nullptr < __x);
2897}
2898
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002899#if _LIBCPP_STD_VER > 11
2900
2901template<class _Tp>
2902struct __unique_if
2903{
2904 typedef unique_ptr<_Tp> __unique_single;
2905};
2906
2907template<class _Tp>
2908struct __unique_if<_Tp[]>
2909{
2910 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2911};
2912
2913template<class _Tp, size_t _Np>
2914struct __unique_if<_Tp[_Np]>
2915{
2916 typedef void __unique_array_known_bound;
2917};
2918
2919template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002920inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002921typename __unique_if<_Tp>::__unique_single
2922make_unique(_Args&&... __args)
2923{
2924 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2925}
2926
2927template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002928inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002929typename __unique_if<_Tp>::__unique_array_unknown_bound
2930make_unique(size_t __n)
2931{
2932 typedef typename remove_extent<_Tp>::type _Up;
2933 return unique_ptr<_Tp>(new _Up[__n]());
2934}
2935
2936template<class _Tp, class... _Args>
2937 typename __unique_if<_Tp>::__unique_array_known_bound
2938 make_unique(_Args&&...) = delete;
2939
2940#endif // _LIBCPP_STD_VER > 11
2941
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002942template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002943#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002944struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002945#else
2946struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002947 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002948#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002949{
2950 typedef unique_ptr<_Tp, _Dp> argument_type;
2951 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002952 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002953 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002954 {
2955 typedef typename argument_type::pointer pointer;
2956 return hash<pointer>()(__ptr.get());
2957 }
2958};
2959
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960struct __destruct_n
2961{
2962private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002963 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964
2965 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002966 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002967 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968
2969 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002970 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 {}
2972
Howard Hinnant719bda32011-05-28 14:41:13 +00002973 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002974 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002975 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 {}
2977
Howard Hinnant719bda32011-05-28 14:41:13 +00002978 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002979 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002980 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981 {}
2982public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002983 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002984 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985
2986 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002987 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002988 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989
2990 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002991 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002992 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993
2994 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002995 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002996 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997};
2998
2999template <class _Alloc>
3000class __allocator_destructor
3001{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003002 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003004 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3005 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006private:
3007 _Alloc& __alloc_;
3008 size_type __s_;
3009public:
3010 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003011 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003014 void operator()(pointer __p) _NOEXCEPT
3015 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016};
3017
3018template <class _InputIterator, class _ForwardIterator>
3019_ForwardIterator
3020uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3021{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003023#ifndef _LIBCPP_NO_EXCEPTIONS
3024 _ForwardIterator __s = __r;
3025 try
3026 {
3027#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003028 for (; __f != __l; ++__f, (void) ++__r)
3029 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003030#ifndef _LIBCPP_NO_EXCEPTIONS
3031 }
3032 catch (...)
3033 {
3034 for (; __s != __r; ++__s)
3035 __s->~value_type();
3036 throw;
3037 }
3038#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039 return __r;
3040}
3041
3042template <class _InputIterator, class _Size, class _ForwardIterator>
3043_ForwardIterator
3044uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3045{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003047#ifndef _LIBCPP_NO_EXCEPTIONS
3048 _ForwardIterator __s = __r;
3049 try
3050 {
3051#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003052 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3053 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003054#ifndef _LIBCPP_NO_EXCEPTIONS
3055 }
3056 catch (...)
3057 {
3058 for (; __s != __r; ++__s)
3059 __s->~value_type();
3060 throw;
3061 }
3062#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 return __r;
3064}
3065
3066template <class _ForwardIterator, class _Tp>
3067void
3068uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3069{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003071#ifndef _LIBCPP_NO_EXCEPTIONS
3072 _ForwardIterator __s = __f;
3073 try
3074 {
3075#endif
3076 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003077 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003078#ifndef _LIBCPP_NO_EXCEPTIONS
3079 }
3080 catch (...)
3081 {
3082 for (; __s != __f; ++__s)
3083 __s->~value_type();
3084 throw;
3085 }
3086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087}
3088
3089template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003090_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3092{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003094#ifndef _LIBCPP_NO_EXCEPTIONS
3095 _ForwardIterator __s = __f;
3096 try
3097 {
3098#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003099 for (; __n > 0; ++__f, (void) --__n)
3100 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003101#ifndef _LIBCPP_NO_EXCEPTIONS
3102 }
3103 catch (...)
3104 {
3105 for (; __s != __f; ++__s)
3106 __s->~value_type();
3107 throw;
3108 }
3109#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003110 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111}
3112
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003113#if _LIBCPP_STD_VER > 14
3114
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003115template <class _Tp>
3116inline _LIBCPP_INLINE_VISIBILITY
3117void destroy_at(_Tp* __loc) {
3118 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3119 __loc->~_Tp();
3120}
3121
3122template <class _ForwardIterator>
3123inline _LIBCPP_INLINE_VISIBILITY
3124void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3125 for (; __first != __last; ++__first)
3126 _VSTD::destroy_at(_VSTD::addressof(*__first));
3127}
3128
3129template <class _ForwardIterator, class _Size>
3130inline _LIBCPP_INLINE_VISIBILITY
3131_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3132 for (; __n > 0; (void)++__first, --__n)
3133 _VSTD::destroy_at(_VSTD::addressof(*__first));
3134 return __first;
3135}
3136
Eric Fiselier290c07c2016-10-11 21:13:44 +00003137template <class _ForwardIterator>
3138inline _LIBCPP_INLINE_VISIBILITY
3139void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3140 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3141 auto __idx = __first;
3142#ifndef _LIBCPP_NO_EXCEPTIONS
3143 try {
3144#endif
3145 for (; __idx != __last; ++__idx)
3146 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3147#ifndef _LIBCPP_NO_EXCEPTIONS
3148 } catch (...) {
3149 _VSTD::destroy(__first, __idx);
3150 throw;
3151 }
3152#endif
3153}
3154
3155template <class _ForwardIterator, class _Size>
3156inline _LIBCPP_INLINE_VISIBILITY
3157_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3158 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3159 auto __idx = __first;
3160#ifndef _LIBCPP_NO_EXCEPTIONS
3161 try {
3162#endif
3163 for (; __n > 0; (void)++__idx, --__n)
3164 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3165 return __idx;
3166#ifndef _LIBCPP_NO_EXCEPTIONS
3167 } catch (...) {
3168 _VSTD::destroy(__first, __idx);
3169 throw;
3170 }
3171#endif
3172}
3173
3174
3175template <class _ForwardIterator>
3176inline _LIBCPP_INLINE_VISIBILITY
3177void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3178 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3179 auto __idx = __first;
3180#ifndef _LIBCPP_NO_EXCEPTIONS
3181 try {
3182#endif
3183 for (; __idx != __last; ++__idx)
3184 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3185#ifndef _LIBCPP_NO_EXCEPTIONS
3186 } catch (...) {
3187 _VSTD::destroy(__first, __idx);
3188 throw;
3189 }
3190#endif
3191}
3192
3193template <class _ForwardIterator, class _Size>
3194inline _LIBCPP_INLINE_VISIBILITY
3195_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3196 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3197 auto __idx = __first;
3198#ifndef _LIBCPP_NO_EXCEPTIONS
3199 try {
3200#endif
3201 for (; __n > 0; (void)++__idx, --__n)
3202 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3203 return __idx;
3204#ifndef _LIBCPP_NO_EXCEPTIONS
3205 } catch (...) {
3206 _VSTD::destroy(__first, __idx);
3207 throw;
3208 }
3209#endif
3210}
3211
3212
3213template <class _InputIt, class _ForwardIt>
3214inline _LIBCPP_INLINE_VISIBILITY
3215_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3216 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3217 auto __idx = __first_res;
3218#ifndef _LIBCPP_NO_EXCEPTIONS
3219 try {
3220#endif
3221 for (; __first != __last; (void)++__idx, ++__first)
3222 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3223 return __idx;
3224#ifndef _LIBCPP_NO_EXCEPTIONS
3225 } catch (...) {
3226 _VSTD::destroy(__first_res, __idx);
3227 throw;
3228 }
3229#endif
3230}
3231
3232template <class _InputIt, class _Size, class _ForwardIt>
3233inline _LIBCPP_INLINE_VISIBILITY
3234pair<_InputIt, _ForwardIt>
3235uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3236 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3237 auto __idx = __first_res;
3238#ifndef _LIBCPP_NO_EXCEPTIONS
3239 try {
3240#endif
3241 for (; __n > 0; ++__idx, (void)++__first, --__n)
3242 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3243 return {__first, __idx};
3244#ifndef _LIBCPP_NO_EXCEPTIONS
3245 } catch (...) {
3246 _VSTD::destroy(__first_res, __idx);
3247 throw;
3248 }
3249#endif
3250}
3251
3252
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003253#endif // _LIBCPP_STD_VER > 14
3254
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003255// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3256// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003257// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003258#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3259 && defined(__ATOMIC_RELAXED) \
3260 && defined(__ATOMIC_ACQ_REL)
3261# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003262#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003263# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3264#endif
3265
3266template <class _Tp>
3267inline _LIBCPP_INLINE_VISIBILITY _Tp
3268__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3269{
3270#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3271 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3272#else
3273 return __t += 1;
3274#endif
3275}
3276
3277template <class _Tp>
3278inline _LIBCPP_INLINE_VISIBILITY _Tp
3279__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3280{
3281#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3282 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3283#else
3284 return __t -= 1;
3285#endif
3286}
3287
Howard Hinnant756c69b2010-09-22 16:48:34 +00003288class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289 : public std::exception
3290{
3291public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003292 bad_weak_ptr() _NOEXCEPT = default;
3293 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003294 virtual ~bad_weak_ptr() _NOEXCEPT;
3295 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296};
3297
Louis Dionne16fe2952018-07-11 23:14:33 +00003298_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003299void __throw_bad_weak_ptr()
3300{
3301#ifndef _LIBCPP_NO_EXCEPTIONS
3302 throw bad_weak_ptr();
3303#else
3304 _VSTD::abort();
3305#endif
3306}
3307
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003308template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003310class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311{
3312 __shared_count(const __shared_count&);
3313 __shared_count& operator=(const __shared_count&);
3314
3315protected:
3316 long __shared_owners_;
3317 virtual ~__shared_count();
3318private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003319 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320
3321public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003323 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324 : __shared_owners_(__refs) {}
3325
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003326#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003327 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003328 void __add_shared() _NOEXCEPT;
3329 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003330#else
3331 _LIBCPP_INLINE_VISIBILITY
3332 void __add_shared() _NOEXCEPT {
3333 __libcpp_atomic_refcount_increment(__shared_owners_);
3334 }
3335 _LIBCPP_INLINE_VISIBILITY
3336 bool __release_shared() _NOEXCEPT {
3337 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3338 __on_zero_shared();
3339 return true;
3340 }
3341 return false;
3342 }
3343#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003344 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003345 long use_count() const _NOEXCEPT {
3346 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3347 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348};
3349
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003350class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 : private __shared_count
3352{
3353 long __shared_weak_owners_;
3354
3355public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003357 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 : __shared_count(__refs),
3359 __shared_weak_owners_(__refs) {}
3360protected:
3361 virtual ~__shared_weak_count();
3362
3363public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003364#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003365 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003366 void __add_shared() _NOEXCEPT;
3367 void __add_weak() _NOEXCEPT;
3368 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003369#else
3370 _LIBCPP_INLINE_VISIBILITY
3371 void __add_shared() _NOEXCEPT {
3372 __shared_count::__add_shared();
3373 }
3374 _LIBCPP_INLINE_VISIBILITY
3375 void __add_weak() _NOEXCEPT {
3376 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3377 }
3378 _LIBCPP_INLINE_VISIBILITY
3379 void __release_shared() _NOEXCEPT {
3380 if (__shared_count::__release_shared())
3381 __release_weak();
3382 }
3383#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003384 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003386 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3387 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388
Howard Hinnant807d6332013-02-25 15:50:36 +00003389 // Define the function out only if we build static libc++ without RTTI.
3390 // Otherwise we may break clients who need to compile their projects with
3391 // -fno-rtti and yet link against a libc++.dylib compiled
3392 // without -fno-rtti.
3393#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003394 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003395#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003397 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398};
3399
3400template <class _Tp, class _Dp, class _Alloc>
3401class __shared_ptr_pointer
3402 : public __shared_weak_count
3403{
3404 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3405public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003408 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409
Howard Hinnant72f73582010-08-11 17:04:31 +00003410#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003411 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003412#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413
3414private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003415 virtual void __on_zero_shared() _NOEXCEPT;
3416 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417};
3418
Howard Hinnant72f73582010-08-11 17:04:31 +00003419#ifndef _LIBCPP_NO_RTTI
3420
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421template <class _Tp, class _Dp, class _Alloc>
3422const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003423__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003425 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426}
3427
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003428#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003429
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430template <class _Tp, class _Dp, class _Alloc>
3431void
Howard Hinnant719bda32011-05-28 14:41:13 +00003432__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433{
3434 __data_.first().second()(__data_.first().first());
3435 __data_.first().second().~_Dp();
3436}
3437
3438template <class _Tp, class _Dp, class _Alloc>
3439void
Howard Hinnant719bda32011-05-28 14:41:13 +00003440__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003442 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3443 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003444 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3445
Eric Fiselierf8898c82015-02-05 23:01:40 +00003446 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003448 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449}
3450
3451template <class _Tp, class _Alloc>
3452class __shared_ptr_emplace
3453 : public __shared_weak_count
3454{
3455 __compressed_pair<_Alloc, _Tp> __data_;
3456public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457
Howard Hinnant756c69b2010-09-22 16:48:34 +00003458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003460 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003462
3463#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003467 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3468 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469#else // _LIBCPP_HAS_NO_VARIADICS
3470
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3474 : __data_(__a, _Tp(__a0)) {}
3475
3476 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3479 : __data_(__a, _Tp(__a0, __a1)) {}
3480
3481 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3484 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3485
3486#endif // _LIBCPP_HAS_NO_VARIADICS
3487
3488private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003489 virtual void __on_zero_shared() _NOEXCEPT;
3490 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003492 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003493 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494};
3495
3496template <class _Tp, class _Alloc>
3497void
Howard Hinnant719bda32011-05-28 14:41:13 +00003498__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499{
3500 __data_.second().~_Tp();
3501}
3502
3503template <class _Tp, class _Alloc>
3504void
Howard Hinnant719bda32011-05-28 14:41:13 +00003505__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003507 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3508 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003509 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003510 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003512 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513}
3514
Erik Pilkington2a398762017-05-25 15:43:31 +00003515struct __shared_ptr_dummy_rebind_allocator_type;
3516template <>
3517class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3518{
3519public:
3520 template <class _Other>
3521 struct rebind
3522 {
3523 typedef allocator<_Other> other;
3524 };
3525};
3526
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003527template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528
zoecarverd73f42a2020-05-11 18:42:50 -07003529template<class _Tp, class _Up>
3530struct __compatible_with
3531#if _LIBCPP_STD_VER > 14
3532 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3533#else
3534 : is_convertible<_Tp*, _Up*> {};
3535#endif // _LIBCPP_STD_VER > 14
3536
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003538class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003540public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003541#if _LIBCPP_STD_VER > 14
3542 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003543 typedef remove_extent_t<_Tp> element_type;
3544#else
3545 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003546#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003547
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548private:
3549 element_type* __ptr_;
3550 __shared_weak_count* __cntrl_;
3551
3552 struct __nat {int __for_bool_;};
3553public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003555 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003557 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003558 template<class _Yp>
3559 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003560 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003561 template<class _Yp, class _Dp>
3562 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003563 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003564 template<class _Yp, class _Dp, class _Alloc>
3565 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003566 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3568 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003569 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003571 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003575 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003576 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003577#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003579 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003580 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003581 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003582 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003583#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003585 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003586#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003587#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003588 template<class _Yp>
3589 shared_ptr(auto_ptr<_Yp>&& __r,
3590 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003591#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003592 template<class _Yp>
3593 shared_ptr(auto_ptr<_Yp> __r,
3594 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003596#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003597#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003598 template <class _Yp, class _Dp>
3599 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3600 typename enable_if
3601 <
3602 !is_lvalue_reference<_Dp>::value &&
3603 !is_array<_Yp>::value &&
3604 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3605 __nat
3606 >::type = __nat());
3607 template <class _Yp, class _Dp>
3608 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3609 typename enable_if
3610 <
3611 is_lvalue_reference<_Dp>::value &&
3612 !is_array<_Yp>::value &&
3613 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3614 __nat
3615 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003616#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003617 template <class _Yp, class _Dp>
3618 shared_ptr(unique_ptr<_Yp, _Dp>,
3619 typename enable_if
3620 <
3621 !is_lvalue_reference<_Dp>::value &&
3622 !is_array<_Yp>::value &&
3623 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3624 __nat
3625 >::type = __nat());
3626 template <class _Yp, class _Dp>
3627 shared_ptr(unique_ptr<_Yp, _Dp>,
3628 typename enable_if
3629 <
3630 is_lvalue_reference<_Dp>::value &&
3631 !is_array<_Yp>::value &&
3632 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3633 __nat
3634 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003635#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636
3637 ~shared_ptr();
3638
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003640 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003641 template<class _Yp>
3642 typename enable_if
3643 <
zoecarverd73f42a2020-05-11 18:42:50 -07003644 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003645 shared_ptr&
3646 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003648 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003649#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003651 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003652 template<class _Yp>
3653 typename enable_if
3654 <
zoecarverd73f42a2020-05-11 18:42:50 -07003655 __compatible_with<_Yp, element_type>::value,
3656 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003657 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003659 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003660#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003661 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003663 typename enable_if
3664 <
3665 !is_array<_Yp>::value &&
3666 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003667 shared_ptr
3668 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003669 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003670#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003671#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003672#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003673 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003675 typename enable_if
3676 <
3677 !is_array<_Yp>::value &&
3678 is_convertible<_Yp*, element_type*>::value,
3679 shared_ptr&
3680 >::type
3681 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003683#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003684 template <class _Yp, class _Dp>
3685 typename enable_if
3686 <
3687 !is_array<_Yp>::value &&
3688 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3689 shared_ptr&
3690 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003691#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003693 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003694#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003696 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697#endif
3698
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003700 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003702 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003703 template<class _Yp>
3704 typename enable_if
3705 <
zoecarverd73f42a2020-05-11 18:42:50 -07003706 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003707 void
3708 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003710 reset(_Yp* __p);
3711 template<class _Yp, class _Dp>
3712 typename enable_if
3713 <
zoecarverd73f42a2020-05-11 18:42:50 -07003714 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003715 void
3716 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003718 reset(_Yp* __p, _Dp __d);
3719 template<class _Yp, class _Dp, class _Alloc>
3720 typename enable_if
3721 <
zoecarverd73f42a2020-05-11 18:42:50 -07003722 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003723 void
3724 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003726 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727
Howard Hinnant756c69b2010-09-22 16:48:34 +00003728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003729 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003731 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3732 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003733 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003734 element_type* operator->() const _NOEXCEPT
3735 {
3736 static_assert(!_VSTD::is_array<_Tp>::value,
3737 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3738 return __ptr_;
3739 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003741 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003743 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003745 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003746 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003747 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003748 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003750 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003751 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003752 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003754 _LIBCPP_INLINE_VISIBILITY
3755 bool
3756 __owner_equivalent(const shared_ptr& __p) const
3757 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758
zoecarverd73f42a2020-05-11 18:42:50 -07003759#if _LIBCPP_STD_VER > 14
3760 typename add_lvalue_reference<element_type>::type
3761 _LIBCPP_INLINE_VISIBILITY
3762 operator[](ptrdiff_t __i) const
3763 {
3764 static_assert(_VSTD::is_array<_Tp>::value,
3765 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3766 return __ptr_[__i];
3767 }
3768#endif
3769
Howard Hinnant72f73582010-08-11 17:04:31 +00003770#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003773 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003774 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003775 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003776 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003777#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778
Zoe Carverd9040c72019-10-22 15:16:49 +00003779 template<class _Yp, class _CntrlBlk>
3780 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003781 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003782 {
3783 shared_ptr<_Tp> __r;
3784 __r.__ptr_ = __p;
3785 __r.__cntrl_ = __cntrl;
3786 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3787 return __r;
3788 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003789
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003791 template <class _Yp, bool = is_function<_Yp>::value>
3792 struct __shared_ptr_default_allocator
3793 {
3794 typedef allocator<_Yp> type;
3795 };
3796
3797 template <class _Yp>
3798 struct __shared_ptr_default_allocator<_Yp, true>
3799 {
3800 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3801 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003803 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003805 typename enable_if<is_convertible<_OrigPtr*,
3806 const enable_shared_from_this<_Yp>*
3807 >::value,
3808 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003809 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3810 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003812 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003813 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003814 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003815 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3816 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003817 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818 }
3819
Erik Pilkington2a398762017-05-25 15:43:31 +00003820 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821
zoecarverd73f42a2020-05-11 18:42:50 -07003822 template <class, class _Yp>
3823 struct __shared_ptr_default_delete
3824 : default_delete<_Yp> {};
3825
3826 template <class _Yp, class _Un, size_t _Sz>
3827 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3828 : default_delete<_Yp[]> {};
3829
3830 template <class _Yp, class _Un>
3831 struct __shared_ptr_default_delete<_Yp[], _Un>
3832 : default_delete<_Yp[]> {};
3833
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003834 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3835 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836};
3837
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003838#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3839template<class _Tp>
3840shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3841template<class _Tp, class _Dp>
3842shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3843#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003844
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003846inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003847_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003848shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 : __ptr_(0),
3850 __cntrl_(0)
3851{
3852}
3853
3854template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003855inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003856_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003857shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858 : __ptr_(0),
3859 __cntrl_(0)
3860{
3861}
3862
3863template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003864template<class _Yp>
3865shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003866 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 : __ptr_(__p)
3868{
3869 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003870 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003871 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3872 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003874 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875}
3876
3877template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003878template<class _Yp, class _Dp>
3879shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003880 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881 : __ptr_(__p)
3882{
3883#ifndef _LIBCPP_NO_EXCEPTIONS
3884 try
3885 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003886#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003887 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3888 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3889 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003890 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891#ifndef _LIBCPP_NO_EXCEPTIONS
3892 }
3893 catch (...)
3894 {
3895 __d(__p);
3896 throw;
3897 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003898#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003899}
3900
3901template<class _Tp>
3902template<class _Dp>
3903shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3904 : __ptr_(0)
3905{
3906#ifndef _LIBCPP_NO_EXCEPTIONS
3907 try
3908 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003909#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003910 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3911 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3912 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913#ifndef _LIBCPP_NO_EXCEPTIONS
3914 }
3915 catch (...)
3916 {
3917 __d(__p);
3918 throw;
3919 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003920#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921}
3922
3923template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003924template<class _Yp, class _Dp, class _Alloc>
3925shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003926 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927 : __ptr_(__p)
3928{
3929#ifndef _LIBCPP_NO_EXCEPTIONS
3930 try
3931 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003932#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003934 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 typedef __allocator_destructor<_A2> _D2;
3936 _A2 __a2(__a);
3937 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003938 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3939 _CntrlBlk(__p, __d, __a);
3940 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003941 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942#ifndef _LIBCPP_NO_EXCEPTIONS
3943 }
3944 catch (...)
3945 {
3946 __d(__p);
3947 throw;
3948 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003949#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950}
3951
3952template<class _Tp>
3953template<class _Dp, class _Alloc>
3954shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3955 : __ptr_(0)
3956{
3957#ifndef _LIBCPP_NO_EXCEPTIONS
3958 try
3959 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003960#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003962 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963 typedef __allocator_destructor<_A2> _D2;
3964 _A2 __a2(__a);
3965 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003966 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3967 _CntrlBlk(__p, __d, __a);
3968 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969#ifndef _LIBCPP_NO_EXCEPTIONS
3970 }
3971 catch (...)
3972 {
3973 __d(__p);
3974 throw;
3975 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003976#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977}
3978
3979template<class _Tp>
3980template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003981inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003982shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983 : __ptr_(__p),
3984 __cntrl_(__r.__cntrl_)
3985{
3986 if (__cntrl_)
3987 __cntrl_->__add_shared();
3988}
3989
3990template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003991inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003992shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993 : __ptr_(__r.__ptr_),
3994 __cntrl_(__r.__cntrl_)
3995{
3996 if (__cntrl_)
3997 __cntrl_->__add_shared();
3998}
3999
4000template<class _Tp>
4001template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004002inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07004004 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004005 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006 : __ptr_(__r.__ptr_),
4007 __cntrl_(__r.__cntrl_)
4008{
4009 if (__cntrl_)
4010 __cntrl_->__add_shared();
4011}
4012
Howard Hinnant74279a52010-09-04 23:28:19 +00004013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014
4015template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004016inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004017shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018 : __ptr_(__r.__ptr_),
4019 __cntrl_(__r.__cntrl_)
4020{
4021 __r.__ptr_ = 0;
4022 __r.__cntrl_ = 0;
4023}
4024
4025template<class _Tp>
4026template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004027inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07004029 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004030 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031 : __ptr_(__r.__ptr_),
4032 __cntrl_(__r.__cntrl_)
4033{
4034 __r.__ptr_ = 0;
4035 __r.__cntrl_ = 0;
4036}
4037
Howard Hinnant74279a52010-09-04 23:28:19 +00004038#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039
Marshall Clowb22274f2017-01-24 22:22:33 +00004040#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004042template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004043#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004044shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004046shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004048 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049 : __ptr_(__r.get())
4050{
4051 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4052 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004053 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054 __r.release();
4055}
Marshall Clowb22274f2017-01-24 22:22:33 +00004056#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057
4058template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004059template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004060#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4062#else
4063shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4064#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004065 typename enable_if
4066 <
4067 !is_lvalue_reference<_Dp>::value &&
4068 !is_array<_Yp>::value &&
4069 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4070 __nat
4071 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072 : __ptr_(__r.get())
4073{
Marshall Clow35cde742015-05-10 13:59:45 +00004074#if _LIBCPP_STD_VER > 11
4075 if (__ptr_ == nullptr)
4076 __cntrl_ = nullptr;
4077 else
4078#endif
4079 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004080 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4081 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4082 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004083 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004084 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085 __r.release();
4086}
4087
4088template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004089template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004090#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4092#else
4093shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4094#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004095 typename enable_if
4096 <
4097 is_lvalue_reference<_Dp>::value &&
4098 !is_array<_Yp>::value &&
4099 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4100 __nat
4101 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102 : __ptr_(__r.get())
4103{
Marshall Clow35cde742015-05-10 13:59:45 +00004104#if _LIBCPP_STD_VER > 11
4105 if (__ptr_ == nullptr)
4106 __cntrl_ = nullptr;
4107 else
4108#endif
4109 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004110 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004111 typedef __shared_ptr_pointer<_Yp*,
4112 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004113 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004114 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004115 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004116 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117 __r.release();
4118}
4119
Zoe Carver6cd05c32019-08-19 15:47:16 +00004120template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121shared_ptr<_Tp>::~shared_ptr()
4122{
4123 if (__cntrl_)
4124 __cntrl_->__release_shared();
4125}
4126
4127template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004128inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004130shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131{
4132 shared_ptr(__r).swap(*this);
4133 return *this;
4134}
4135
4136template<class _Tp>
4137template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004138inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004139typename enable_if
4140<
zoecarverd73f42a2020-05-11 18:42:50 -07004141 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004142 shared_ptr<_Tp>&
4143>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004144shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145{
4146 shared_ptr(__r).swap(*this);
4147 return *this;
4148}
4149
Howard Hinnant74279a52010-09-04 23:28:19 +00004150#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151
4152template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004153inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004155shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004157 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158 return *this;
4159}
4160
4161template<class _Tp>
4162template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004163inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004164typename enable_if
4165<
zoecarverd73f42a2020-05-11 18:42:50 -07004166 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004167 shared_ptr<_Tp>&
4168>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4170{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004171 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172 return *this;
4173}
4174
Marshall Clowb22274f2017-01-24 22:22:33 +00004175#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176template<class _Tp>
4177template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004178inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004179typename enable_if
4180<
4181 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004182 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004183 shared_ptr<_Tp>
4184>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4186{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004187 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188 return *this;
4189}
Marshall Clowb22274f2017-01-24 22:22:33 +00004190#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191
4192template<class _Tp>
4193template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004194inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004195typename enable_if
4196<
4197 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004198 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004199 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004200 shared_ptr<_Tp>&
4201>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4203{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004204 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205 return *this;
4206}
4207
Howard Hinnant74279a52010-09-04 23:28:19 +00004208#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209
Marshall Clowb22274f2017-01-24 22:22:33 +00004210#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211template<class _Tp>
4212template<class _Yp>
4213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004214typename enable_if
4215<
4216 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004217 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004218 shared_ptr<_Tp>&
4219>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004220shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221{
4222 shared_ptr(__r).swap(*this);
4223 return *this;
4224}
Marshall Clowb22274f2017-01-24 22:22:33 +00004225#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226
4227template<class _Tp>
4228template <class _Yp, class _Dp>
4229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004230typename enable_if
4231<
4232 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004233 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004234 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004235 shared_ptr<_Tp>&
4236>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4238{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004239 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240 return *this;
4241}
4242
Howard Hinnant74279a52010-09-04 23:28:19 +00004243#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244
4245template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004246inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247void
Howard Hinnant719bda32011-05-28 14:41:13 +00004248shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004250 _VSTD::swap(__ptr_, __r.__ptr_);
4251 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252}
4253
4254template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004255inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256void
Howard Hinnant719bda32011-05-28 14:41:13 +00004257shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258{
4259 shared_ptr().swap(*this);
4260}
4261
4262template<class _Tp>
4263template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004264inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004265typename enable_if
4266<
zoecarverd73f42a2020-05-11 18:42:50 -07004267 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004268 void
4269>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270shared_ptr<_Tp>::reset(_Yp* __p)
4271{
4272 shared_ptr(__p).swap(*this);
4273}
4274
4275template<class _Tp>
4276template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004277inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004278typename enable_if
4279<
zoecarverd73f42a2020-05-11 18:42:50 -07004280 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004281 void
4282>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4284{
4285 shared_ptr(__p, __d).swap(*this);
4286}
4287
4288template<class _Tp>
4289template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004290inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004291typename enable_if
4292<
zoecarverd73f42a2020-05-11 18:42:50 -07004293 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004294 void
4295>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4297{
4298 shared_ptr(__p, __d, __a).swap(*this);
4299}
4300
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004301template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004303typename enable_if
4304<
4305 !is_array<_Tp>::value,
4306 shared_ptr<_Tp>
4307>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308make_shared(_Args&& ...__args)
4309{
Zoe Carverd9040c72019-10-22 15:16:49 +00004310 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4311 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4312 typedef allocator<_CntrlBlk> _A2;
4313 typedef __allocator_destructor<_A2> _D2;
4314
4315 _A2 __a2;
4316 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4317 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4318
4319 _Tp *__ptr = __hold2.get()->get();
4320 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321}
4322
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004323template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004325typename enable_if
4326<
4327 !is_array<_Tp>::value,
4328 shared_ptr<_Tp>
4329>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330allocate_shared(const _Alloc& __a, _Args&& ...__args)
4331{
zoecarver505730a2020-02-25 16:50:57 -08004332 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4333
4334 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4335 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4336 typedef __allocator_destructor<_A2> _D2;
4337
4338 _A2 __a2(__a);
4339 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4340 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4341 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4342
4343 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4344 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345}
4346
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347template<class _Tp, class _Up>
4348inline _LIBCPP_INLINE_VISIBILITY
4349bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004350operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351{
4352 return __x.get() == __y.get();
4353}
4354
4355template<class _Tp, class _Up>
4356inline _LIBCPP_INLINE_VISIBILITY
4357bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004358operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359{
4360 return !(__x == __y);
4361}
4362
4363template<class _Tp, class _Up>
4364inline _LIBCPP_INLINE_VISIBILITY
4365bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004366operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004368#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004369 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4370 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004371#else
4372 return less<>()(__x.get(), __y.get());
4373#endif
4374
Howard Hinnantb17caf92012-02-21 21:02:58 +00004375}
4376
4377template<class _Tp, class _Up>
4378inline _LIBCPP_INLINE_VISIBILITY
4379bool
4380operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4381{
4382 return __y < __x;
4383}
4384
4385template<class _Tp, class _Up>
4386inline _LIBCPP_INLINE_VISIBILITY
4387bool
4388operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4389{
4390 return !(__y < __x);
4391}
4392
4393template<class _Tp, class _Up>
4394inline _LIBCPP_INLINE_VISIBILITY
4395bool
4396operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4397{
4398 return !(__x < __y);
4399}
4400
4401template<class _Tp>
4402inline _LIBCPP_INLINE_VISIBILITY
4403bool
4404operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4405{
4406 return !__x;
4407}
4408
4409template<class _Tp>
4410inline _LIBCPP_INLINE_VISIBILITY
4411bool
4412operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4413{
4414 return !__x;
4415}
4416
4417template<class _Tp>
4418inline _LIBCPP_INLINE_VISIBILITY
4419bool
4420operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4421{
4422 return static_cast<bool>(__x);
4423}
4424
4425template<class _Tp>
4426inline _LIBCPP_INLINE_VISIBILITY
4427bool
4428operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4429{
4430 return static_cast<bool>(__x);
4431}
4432
4433template<class _Tp>
4434inline _LIBCPP_INLINE_VISIBILITY
4435bool
4436operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4437{
4438 return less<_Tp*>()(__x.get(), nullptr);
4439}
4440
4441template<class _Tp>
4442inline _LIBCPP_INLINE_VISIBILITY
4443bool
4444operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4445{
4446 return less<_Tp*>()(nullptr, __x.get());
4447}
4448
4449template<class _Tp>
4450inline _LIBCPP_INLINE_VISIBILITY
4451bool
4452operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4453{
4454 return nullptr < __x;
4455}
4456
4457template<class _Tp>
4458inline _LIBCPP_INLINE_VISIBILITY
4459bool
4460operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4461{
4462 return __x < nullptr;
4463}
4464
4465template<class _Tp>
4466inline _LIBCPP_INLINE_VISIBILITY
4467bool
4468operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4469{
4470 return !(nullptr < __x);
4471}
4472
4473template<class _Tp>
4474inline _LIBCPP_INLINE_VISIBILITY
4475bool
4476operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4477{
4478 return !(__x < nullptr);
4479}
4480
4481template<class _Tp>
4482inline _LIBCPP_INLINE_VISIBILITY
4483bool
4484operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4485{
4486 return !(__x < nullptr);
4487}
4488
4489template<class _Tp>
4490inline _LIBCPP_INLINE_VISIBILITY
4491bool
4492operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4493{
4494 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495}
4496
4497template<class _Tp>
4498inline _LIBCPP_INLINE_VISIBILITY
4499void
Howard Hinnant719bda32011-05-28 14:41:13 +00004500swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501{
4502 __x.swap(__y);
4503}
4504
4505template<class _Tp, class _Up>
4506inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004507shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004508static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509{
zoecarverd73f42a2020-05-11 18:42:50 -07004510 return shared_ptr<_Tp>(__r,
4511 static_cast<
4512 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004513}
4514
4515template<class _Tp, class _Up>
4516inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004517shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004518dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519{
zoecarverd73f42a2020-05-11 18:42:50 -07004520 typedef typename shared_ptr<_Tp>::element_type _ET;
4521 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004522 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4523}
4524
4525template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004526shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004527const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528{
zoecarverd73f42a2020-05-11 18:42:50 -07004529 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004530 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531}
4532
zoecarverd73f42a2020-05-11 18:42:50 -07004533template<class _Tp, class _Up>
4534shared_ptr<_Tp>
4535reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4536{
4537 return shared_ptr<_Tp>(__r,
4538 reinterpret_cast<
4539 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4540}
4541
Howard Hinnant72f73582010-08-11 17:04:31 +00004542#ifndef _LIBCPP_NO_RTTI
4543
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544template<class _Dp, class _Tp>
4545inline _LIBCPP_INLINE_VISIBILITY
4546_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004547get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548{
4549 return __p.template __get_deleter<_Dp>();
4550}
4551
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004552#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004553
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004555class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004557public:
4558 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559private:
4560 element_type* __ptr_;
4561 __shared_weak_count* __cntrl_;
4562
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004563public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004565 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004566 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004567 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4568 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004570 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004571 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004572 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4573 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004574
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004575#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004577 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004578 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004579 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4580 _NOEXCEPT;
4581#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004582 ~weak_ptr();
4583
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004585 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004586 template<class _Yp>
4587 typename enable_if
4588 <
4589 is_convertible<_Yp*, element_type*>::value,
4590 weak_ptr&
4591 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004593 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4594
4595#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4596
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004598 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4599 template<class _Yp>
4600 typename enable_if
4601 <
4602 is_convertible<_Yp*, element_type*>::value,
4603 weak_ptr&
4604 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004606 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4607
4608#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4609
4610 template<class _Yp>
4611 typename enable_if
4612 <
4613 is_convertible<_Yp*, element_type*>::value,
4614 weak_ptr&
4615 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004617 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004618
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004620 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004622 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004623
Howard Hinnant756c69b2010-09-22 16:48:34 +00004624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004625 long use_count() const _NOEXCEPT
4626 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004628 bool expired() const _NOEXCEPT
4629 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4630 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004631 template<class _Up>
4632 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004633 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004634 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004635 template<class _Up>
4636 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004637 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004638 {return __cntrl_ < __r.__cntrl_;}
4639
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004640 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4641 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004642};
4643
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004644#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4645template<class _Tp>
4646weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4647#endif
4648
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004650inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004651_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004652weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653 : __ptr_(0),
4654 __cntrl_(0)
4655{
4656}
4657
4658template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004659inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004660weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004661 : __ptr_(__r.__ptr_),
4662 __cntrl_(__r.__cntrl_)
4663{
4664 if (__cntrl_)
4665 __cntrl_->__add_weak();
4666}
4667
4668template<class _Tp>
4669template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004670inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004671weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004672 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004673 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004674 : __ptr_(__r.__ptr_),
4675 __cntrl_(__r.__cntrl_)
4676{
4677 if (__cntrl_)
4678 __cntrl_->__add_weak();
4679}
4680
4681template<class _Tp>
4682template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004683inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004684weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004685 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004686 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004687 : __ptr_(__r.__ptr_),
4688 __cntrl_(__r.__cntrl_)
4689{
4690 if (__cntrl_)
4691 __cntrl_->__add_weak();
4692}
4693
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004694#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4695
4696template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004697inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004698weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4699 : __ptr_(__r.__ptr_),
4700 __cntrl_(__r.__cntrl_)
4701{
4702 __r.__ptr_ = 0;
4703 __r.__cntrl_ = 0;
4704}
4705
4706template<class _Tp>
4707template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004708inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004709weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4710 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4711 _NOEXCEPT
4712 : __ptr_(__r.__ptr_),
4713 __cntrl_(__r.__cntrl_)
4714{
4715 __r.__ptr_ = 0;
4716 __r.__cntrl_ = 0;
4717}
4718
4719#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4720
Howard Hinnantc51e1022010-05-11 19:42:16 +00004721template<class _Tp>
4722weak_ptr<_Tp>::~weak_ptr()
4723{
4724 if (__cntrl_)
4725 __cntrl_->__release_weak();
4726}
4727
4728template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004729inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004730weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004731weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004732{
4733 weak_ptr(__r).swap(*this);
4734 return *this;
4735}
4736
4737template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004738template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004739inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004740typename enable_if
4741<
4742 is_convertible<_Yp*, _Tp*>::value,
4743 weak_ptr<_Tp>&
4744>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004745weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004746{
4747 weak_ptr(__r).swap(*this);
4748 return *this;
4749}
4750
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4752
4753template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004754inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004755weak_ptr<_Tp>&
4756weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4757{
4758 weak_ptr(_VSTD::move(__r)).swap(*this);
4759 return *this;
4760}
4761
Howard Hinnantc51e1022010-05-11 19:42:16 +00004762template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004763template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004764inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004765typename enable_if
4766<
4767 is_convertible<_Yp*, _Tp*>::value,
4768 weak_ptr<_Tp>&
4769>::type
4770weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4771{
4772 weak_ptr(_VSTD::move(__r)).swap(*this);
4773 return *this;
4774}
4775
4776#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4777
4778template<class _Tp>
4779template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004780inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004781typename enable_if
4782<
4783 is_convertible<_Yp*, _Tp*>::value,
4784 weak_ptr<_Tp>&
4785>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004786weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004787{
4788 weak_ptr(__r).swap(*this);
4789 return *this;
4790}
4791
4792template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004794void
Howard Hinnant719bda32011-05-28 14:41:13 +00004795weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004796{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004797 _VSTD::swap(__ptr_, __r.__ptr_);
4798 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004799}
4800
4801template<class _Tp>
4802inline _LIBCPP_INLINE_VISIBILITY
4803void
Howard Hinnant719bda32011-05-28 14:41:13 +00004804swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004805{
4806 __x.swap(__y);
4807}
4808
4809template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004810inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004811void
Howard Hinnant719bda32011-05-28 14:41:13 +00004812weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004813{
4814 weak_ptr().swap(*this);
4815}
4816
4817template<class _Tp>
4818template<class _Yp>
4819shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004820 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004821 : __ptr_(__r.__ptr_),
4822 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4823{
4824 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004825 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004826}
4827
4828template<class _Tp>
4829shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004830weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004831{
4832 shared_ptr<_Tp> __r;
4833 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4834 if (__r.__cntrl_)
4835 __r.__ptr_ = __ptr_;
4836 return __r;
4837}
4838
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004839#if _LIBCPP_STD_VER > 14
4840template <class _Tp = void> struct owner_less;
4841#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004842template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004843#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004844
4845template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004846struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004847 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004848{
4849 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004850 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004851 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004852 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004853 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004854 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004855 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004856 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004857 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004858 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004859};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004860
4861template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004862struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004863 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4864{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004865 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004866 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004867 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004868 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004869 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004870 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004871 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004872 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004873 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004874 {return __x.owner_before(__y);}
4875};
4876
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004877#if _LIBCPP_STD_VER > 14
4878template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004879struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004880{
4881 template <class _Tp, class _Up>
4882 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004883 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004884 {return __x.owner_before(__y);}
4885 template <class _Tp, class _Up>
4886 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004887 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004888 {return __x.owner_before(__y);}
4889 template <class _Tp, class _Up>
4890 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004891 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004892 {return __x.owner_before(__y);}
4893 template <class _Tp, class _Up>
4894 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004895 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004896 {return __x.owner_before(__y);}
4897 typedef void is_transparent;
4898};
4899#endif
4900
Howard Hinnantc51e1022010-05-11 19:42:16 +00004901template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004902class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004903{
4904 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004905protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004906 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004907 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004909 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004911 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4912 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004914 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004915public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004917 shared_ptr<_Tp> shared_from_this()
4918 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004920 shared_ptr<_Tp const> shared_from_this() const
4921 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922
Eric Fiselier84006862016-06-02 00:15:35 +00004923#if _LIBCPP_STD_VER > 14
4924 _LIBCPP_INLINE_VISIBILITY
4925 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4926 { return __weak_this_; }
4927
4928 _LIBCPP_INLINE_VISIBILITY
4929 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4930 { return __weak_this_; }
4931#endif // _LIBCPP_STD_VER > 14
4932
Howard Hinnantc51e1022010-05-11 19:42:16 +00004933 template <class _Up> friend class shared_ptr;
4934};
4935
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004936template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004937struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004938{
4939 typedef shared_ptr<_Tp> argument_type;
4940 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004941
Howard Hinnant756c69b2010-09-22 16:48:34 +00004942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004943 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004944 {
zoecarverd73f42a2020-05-11 18:42:50 -07004945 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004946 }
4947};
4948
Howard Hinnantc834c512011-11-29 18:15:50 +00004949template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004950inline _LIBCPP_INLINE_VISIBILITY
4951basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004952operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004953
Eric Fiselier9b492672016-06-18 02:12:53 +00004954
4955#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004956
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004957class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004958{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004959 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004960public:
4961 void lock() _NOEXCEPT;
4962 void unlock() _NOEXCEPT;
4963
4964private:
4965 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4966 __sp_mut(const __sp_mut&);
4967 __sp_mut& operator=(const __sp_mut&);
4968
Howard Hinnant8331b762013-03-06 23:30:19 +00004969 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004970};
4971
Mehdi Amini228053d2017-05-04 17:08:54 +00004972_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4973__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004974
4975template <class _Tp>
4976inline _LIBCPP_INLINE_VISIBILITY
4977bool
4978atomic_is_lock_free(const shared_ptr<_Tp>*)
4979{
4980 return false;
4981}
4982
4983template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004984_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004985shared_ptr<_Tp>
4986atomic_load(const shared_ptr<_Tp>* __p)
4987{
4988 __sp_mut& __m = __get_sp_mut(__p);
4989 __m.lock();
4990 shared_ptr<_Tp> __q = *__p;
4991 __m.unlock();
4992 return __q;
4993}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004994
Howard Hinnant9fa30202012-07-30 01:40:57 +00004995template <class _Tp>
4996inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004997_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004998shared_ptr<_Tp>
4999atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5000{
5001 return atomic_load(__p);
5002}
5003
5004template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005005_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005006void
5007atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5008{
5009 __sp_mut& __m = __get_sp_mut(__p);
5010 __m.lock();
5011 __p->swap(__r);
5012 __m.unlock();
5013}
5014
5015template <class _Tp>
5016inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005017_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005018void
5019atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5020{
5021 atomic_store(__p, __r);
5022}
5023
5024template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005025_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005026shared_ptr<_Tp>
5027atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5028{
5029 __sp_mut& __m = __get_sp_mut(__p);
5030 __m.lock();
5031 __p->swap(__r);
5032 __m.unlock();
5033 return __r;
5034}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005035
Howard Hinnant9fa30202012-07-30 01:40:57 +00005036template <class _Tp>
5037inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005038_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005039shared_ptr<_Tp>
5040atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5041{
5042 return atomic_exchange(__p, __r);
5043}
5044
5045template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005046_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005047bool
5048atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5049{
Marshall Clow4201ee82016-05-18 17:50:13 +00005050 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005051 __sp_mut& __m = __get_sp_mut(__p);
5052 __m.lock();
5053 if (__p->__owner_equivalent(*__v))
5054 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005055 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005056 *__p = __w;
5057 __m.unlock();
5058 return true;
5059 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005060 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005061 *__v = *__p;
5062 __m.unlock();
5063 return false;
5064}
5065
5066template <class _Tp>
5067inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005068_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005069bool
5070atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5071{
5072 return atomic_compare_exchange_strong(__p, __v, __w);
5073}
5074
5075template <class _Tp>
5076inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005077_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005078bool
5079atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5080 shared_ptr<_Tp> __w, memory_order, memory_order)
5081{
5082 return atomic_compare_exchange_strong(__p, __v, __w);
5083}
5084
5085template <class _Tp>
5086inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005087_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005088bool
5089atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5090 shared_ptr<_Tp> __w, memory_order, memory_order)
5091{
5092 return atomic_compare_exchange_weak(__p, __v, __w);
5093}
5094
Eric Fiselier9b492672016-06-18 02:12:53 +00005095#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005096
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005097//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005098#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5099# ifndef _LIBCPP_CXX03_LANG
5100enum class pointer_safety : unsigned char {
5101 relaxed,
5102 preferred,
5103 strict
5104};
5105# endif
5106#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005107struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005108{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005109 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005110 {
5111 relaxed,
5112 preferred,
5113 strict
5114 };
5115
Howard Hinnant49e145e2012-10-30 19:06:59 +00005116 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005117
Howard Hinnant756c69b2010-09-22 16:48:34 +00005118 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005119 pointer_safety() : __v_() {}
5120
5121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005122 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005124 operator int() const {return __v_;}
5125};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005126#endif
5127
5128#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005129 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005130_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5131#else
5132// This function is only offered in C++03 under ABI v1.
5133# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5134inline _LIBCPP_INLINE_VISIBILITY
5135pointer_safety get_pointer_safety() _NOEXCEPT {
5136 return pointer_safety::relaxed;
5137}
5138# endif
5139#endif
5140
Howard Hinnantc51e1022010-05-11 19:42:16 +00005141
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005142_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5143_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5144_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005145_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005146
5147template <class _Tp>
5148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005149_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005150undeclare_reachable(_Tp* __p)
5151{
5152 return static_cast<_Tp*>(__undeclare_reachable(__p));
5153}
5154
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005155_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005156
Marshall Clow8982dcd2015-07-13 20:04:56 +00005157// --- Helper for container swap --
5158template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005159inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005160void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5161#if _LIBCPP_STD_VER >= 14
5162 _NOEXCEPT
5163#else
5164 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5165#endif
5166{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005167 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005168 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5169}
5170
5171template <typename _Alloc>
5172_LIBCPP_INLINE_VISIBILITY
5173void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5174#if _LIBCPP_STD_VER >= 14
5175 _NOEXCEPT
5176#else
5177 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5178#endif
5179{
5180 using _VSTD::swap;
5181 swap(__a1, __a2);
5182}
5183
5184template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005185inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005186void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5187
Marshall Clowff91de82015-08-18 19:51:37 +00005188template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005189struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005190 _Traits::propagate_on_container_move_assignment::value
5191#if _LIBCPP_STD_VER > 14
5192 || _Traits::is_always_equal::value
5193#else
5194 && is_nothrow_move_assignable<_Alloc>::value
5195#endif
5196 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005197
Marshall Clowa591b9a2016-07-11 21:38:08 +00005198
5199#ifndef _LIBCPP_HAS_NO_VARIADICS
5200template <class _Tp, class _Alloc>
5201struct __temp_value {
5202 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005203
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005204 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005205 _Alloc &__a;
5206
5207 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5208 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005209
Marshall Clowa591b9a2016-07-11 21:38:08 +00005210 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005211 _LIBCPP_NO_CFI
5212 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5213 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5214 _VSTD::forward<_Args>(__args)...);
5215 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005216
Marshall Clowa591b9a2016-07-11 21:38:08 +00005217 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5218 };
5219#endif
5220
Marshall Clowe46031a2018-07-02 18:41:15 +00005221template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005222struct __is_allocator : false_type {};
5223
5224template<typename _Alloc>
5225struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005226 typename __void_t<typename _Alloc::value_type>::type,
5227 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5228 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005229 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005230
Eric Fiselier74ebee62019-06-08 01:31:19 +00005231// __builtin_new_allocator -- A non-templated helper for allocating and
5232// deallocating memory using __builtin_operator_new and
5233// __builtin_operator_delete. It should be used in preference to
5234// `std::allocator<T>` to avoid additional instantiations.
5235struct __builtin_new_allocator {
5236 struct __builtin_new_deleter {
5237 typedef void* pointer_type;
5238
5239 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5240 : __size_(__size), __align_(__align) {}
5241
5242 void operator()(void* p) const _NOEXCEPT {
5243 std::__libcpp_deallocate(p, __size_, __align_);
5244 }
5245
5246 private:
5247 size_t __size_;
5248 size_t __align_;
5249 };
5250
5251 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5252
5253 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5254 return __holder_t(std::__libcpp_allocate(__s, __align),
5255 __builtin_new_deleter(__s, __align));
5256 }
5257
5258 static void __deallocate_bytes(void* __p, size_t __s,
5259 size_t __align) _NOEXCEPT {
5260 std::__libcpp_deallocate(__p, __s, __align);
5261 }
5262
5263 template <class _Tp>
5264 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5265 static __holder_t __allocate_type(size_t __n) {
5266 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5267 }
5268
5269 template <class _Tp>
5270 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5271 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5272 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5273 }
5274};
5275
5276
Howard Hinnantc51e1022010-05-11 19:42:16 +00005277_LIBCPP_END_NAMESPACE_STD
5278
Eric Fiselierf4433a32017-05-31 22:07:49 +00005279_LIBCPP_POP_MACROS
5280
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005281#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005282# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005283#endif
5284
Howard Hinnantc51e1022010-05-11 19:42:16 +00005285#endif // _LIBCPP_MEMORY