blob: 9d56e8167b47c3d422e5cb076c2fac35e808bab5 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
50
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Marshall Clow0e58cae2017-11-26 02:55:38 +000086 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant719bda32011-05-28 14:41:13 +000089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args);
93
94 template <class T>
95 static void destroy(allocator_type& a, T* p);
96
Marshall Clow4f834b52013-08-27 20:22:15 +000097 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 static allocator_type
100 select_on_container_copy_construction(const allocator_type& a);
101};
102
103template <>
Michael Park99f9e912020-03-04 11:27:14 -0500104class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
106public:
107 typedef void* pointer;
108 typedef const void* const_pointer;
109 typedef void value_type;
110
111 template <class _Up> struct rebind {typedef allocator<_Up> other;};
112};
113
114template <class T>
115class allocator
116{
117public:
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef size_t size_type; // deprecated in C++17, removed in C++20
119 typedef ptrdiff_t difference_type; // deprecated in C++17, removed in C++20
120 typedef T* pointer; // deprecated in C++17, removed in C++20
121 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<T>::type
123 reference; // deprecated in C++17, removed in C++20
124 typedef typename add_lvalue_reference<const T>::type
125 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Michael Park99f9e912020-03-04 11:27:14 -0500127 typedef T value_type;
128
129 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
130
131 typedef true_type propagate_on_container_move_assignment;
132 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Marshall Clowbc759762018-03-20 23:02:53 +0000134 constexpr allocator() noexcept; // constexpr in C++20
135 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
136 template <class U>
137 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000138 ~allocator();
Michael Park99f9e912020-03-04 11:27:14 -0500139 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
140 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
141 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
142 T* allocate(size_t n);
143 void deallocate(T* p, size_t n) noexcept;
144 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000147 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500148 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149};
150
151template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000152bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
154template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000155bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
157template <class OutputIterator, class T>
158class raw_storage_iterator
159 : public iterator<output_iterator_tag,
160 T, // purposefully not C++03
161 ptrdiff_t, // purposefully not C++03
162 T*, // purposefully not C++03
163 raw_storage_iterator&> // purposefully not C++03
164{
165public:
166 explicit raw_storage_iterator(OutputIterator x);
167 raw_storage_iterator& operator*();
168 raw_storage_iterator& operator=(const T& element);
169 raw_storage_iterator& operator++();
170 raw_storage_iterator operator++(int);
171};
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
174template <class T> void return_temporary_buffer(T* p) noexcept;
175
176template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000177template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179template <class InputIterator, class ForwardIterator>
180ForwardIterator
181uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
182
Howard Hinnant719bda32011-05-28 14:41:13 +0000183template <class InputIterator, class Size, class ForwardIterator>
184ForwardIterator
185uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
186
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187template <class ForwardIterator, class T>
188void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
189
190template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000191ForwardIterator
192uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000194template <class T>
195void destroy_at(T* location);
196
197template <class ForwardIterator>
198 void destroy(ForwardIterator first, ForwardIterator last);
199
200template <class ForwardIterator, class Size>
201 ForwardIterator destroy_n(ForwardIterator first, Size n);
202
203template <class InputIterator, class ForwardIterator>
204 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
205
206template <class InputIterator, class Size, class ForwardIterator>
207 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
208
209template <class ForwardIterator>
210 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
214
215template <class ForwardIterator>
216 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
217
218template <class ForwardIterator, class Size>
219 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
220
Louis Dionne481a2662018-09-23 18:35:00 +0000221template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222
223template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000224class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225{
226public:
227 typedef X element_type;
228
229 explicit auto_ptr(X* p =0) throw();
230 auto_ptr(auto_ptr&) throw();
231 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
232 auto_ptr& operator=(auto_ptr&) throw();
233 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
234 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
235 ~auto_ptr() throw();
236
237 typename add_lvalue_reference<X>::type operator*() const throw();
238 X* operator->() const throw();
239 X* get() const throw();
240 X* release() throw();
241 void reset(X* p =0) throw();
242
243 auto_ptr(auto_ptr_ref<X>) throw();
244 template<class Y> operator auto_ptr_ref<Y>() throw();
245 template<class Y> operator auto_ptr<Y>() throw();
246};
247
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248template <class T>
249struct default_delete
250{
Howard Hinnant719bda32011-05-28 14:41:13 +0000251 constexpr default_delete() noexcept = default;
252 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000253
Howard Hinnant719bda32011-05-28 14:41:13 +0000254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255};
256
257template <class T>
258struct default_delete<T[]>
259{
Howard Hinnant719bda32011-05-28 14:41:13 +0000260 constexpr default_delete() noexcept = default;
261 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000262 template <class U> void operator()(U*) const = delete;
263};
264
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000265template <class T, class D = default_delete<T>>
266class unique_ptr
267{
268public:
269 typedef see below pointer;
270 typedef T element_type;
271 typedef D deleter_type;
272
273 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 constexpr unique_ptr() noexcept;
275 explicit unique_ptr(pointer p) noexcept;
276 unique_ptr(pointer p, see below d1) noexcept;
277 unique_ptr(pointer p, see below d2) noexcept;
278 unique_ptr(unique_ptr&& u) noexcept;
279 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000280 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000281 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000283 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284
285 // destructor
286 ~unique_ptr();
287
288 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
291 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000292
293 // observers
294 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer operator->() const noexcept;
296 pointer get() const noexcept;
297 deleter_type& get_deleter() noexcept;
298 const deleter_type& get_deleter() const noexcept;
299 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000300
301 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000302 pointer release() noexcept;
303 void reset(pointer p = pointer()) noexcept;
304 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
308class unique_ptr<T[], D>
309{
310public:
311 typedef implementation-defined pointer;
312 typedef T element_type;
313 typedef D deleter_type;
314
315 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000316 constexpr unique_ptr() noexcept;
317 explicit unique_ptr(pointer p) noexcept;
318 unique_ptr(pointer p, see below d) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(unique_ptr&& u) noexcept;
321 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000324 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000325
326 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000327 unique_ptr& operator=(unique_ptr&& u) noexcept;
328 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // observers
331 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 pointer get() const noexcept;
333 deleter_type& get_deleter() noexcept;
334 const deleter_type& get_deleter() const noexcept;
335 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336
337 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000338 pointer release() noexcept;
339 void reset(pointer p = pointer()) noexcept;
340 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000341 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000342 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000343};
344
345template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000346 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000347
348template <class T1, class D1, class T2, class D2>
349 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354template <class T1, class D1, class T2, class D2>
355 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
356template <class T1, class D1, class T2, class D2>
357 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
358template <class T1, class D1, class T2, class D2>
359 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
360
Howard Hinnant719bda32011-05-28 14:41:13 +0000361template <class T, class D>
362 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
363template <class T, class D>
364 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
365template <class T, class D>
366 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
367template <class T, class D>
368 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
369
370template <class T, class D>
371 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
372template <class T, class D>
373 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
374template <class T, class D>
375 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
376template <class T, class D>
377 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
378template <class T, class D>
379 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
380template <class T, class D>
381 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
382template <class T, class D>
383 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
384template <class T, class D>
385 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387class bad_weak_ptr
388 : public std::exception
389{
Howard Hinnant719bda32011-05-28 14:41:13 +0000390 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000391};
392
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000393template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
394template<class T> unique_ptr<T> make_unique(size_t n); // C++14
395template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
396
Marshall Clowe52a3242017-11-27 15:51:36 +0000397template<class E, class T, class Y, class D>
398 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
399
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000400template<class T>
401class shared_ptr
402{
403public:
404 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000405 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406
407 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000408 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(Y* p);
410 template<class Y, class D> shared_ptr(Y* p, D d);
411 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
412 template <class D> shared_ptr(nullptr_t p, D d);
413 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
415 shared_ptr(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
417 shared_ptr(shared_ptr&& r) noexcept;
418 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000419 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000420 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
422 shared_ptr(nullptr_t) : shared_ptr() { }
423
424 // destructor:
425 ~shared_ptr();
426
427 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000428 shared_ptr& operator=(const shared_ptr& r) noexcept;
429 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
430 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000432 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000433 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
434
435 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000436 void swap(shared_ptr& r) noexcept;
437 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438 template<class Y> void reset(Y* p);
439 template<class Y, class D> void reset(Y* p, D d);
440 template<class Y, class D, class A> void reset(Y* p, D d, A a);
441
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 // observers:
443 T* get() const noexcept;
444 T& operator*() const noexcept;
445 T* operator->() const noexcept;
446 long use_count() const noexcept;
447 bool unique() const noexcept;
448 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000449 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
450 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451};
452
453// shared_ptr comparisons:
454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000457 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000459 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
466
467template <class T>
468 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
469template <class T>
470 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
471template <class T>
472 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
473template <class T>
474 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
475template <class T>
476 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
477template <class T>
478bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
479template <class T>
480 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
481template <class T>
482 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
483template <class T>
484 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
485template <class T>
486 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
487template <class T>
488 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
489template <class T>
490 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491
492// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000493template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000494
495// shared_ptr casts:
496template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000497 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000498template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000499 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000501 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503// shared_ptr I/O:
504template<class E, class T, class Y>
505 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
506
507// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000508template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000509
510template<class T, class... Args>
511 shared_ptr<T> make_shared(Args&&... args);
512template<class T, class A, class... Args>
513 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
514
515template<class T>
516class weak_ptr
517{
518public:
519 typedef T element_type;
520
521 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000522 constexpr weak_ptr() noexcept;
523 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
524 weak_ptr(weak_ptr const& r) noexcept;
525 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000526 weak_ptr(weak_ptr&& r) noexcept; // C++14
527 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // destructor
530 ~weak_ptr();
531
532 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 weak_ptr& operator=(weak_ptr const& r) noexcept;
534 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
535 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000536 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
537 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000538
539 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000540 void swap(weak_ptr& r) noexcept;
541 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542
543 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000544 long use_count() const noexcept;
545 bool expired() const noexcept;
546 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000547 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
548 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000549};
550
551// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000552template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000553
554// class owner_less:
555template<class T> struct owner_less;
556
557template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000558struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
560{
561 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000562 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
563 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
564 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000565};
566
567template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000568struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000569 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
570{
571 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000572 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
574 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
575};
576
577template <> // Added in C++14
578struct owner_less<void>
579{
580 template <class _Tp, class _Up>
581 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
582 template <class _Tp, class _Up>
583 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
584 template <class _Tp, class _Up>
585 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
586 template <class _Tp, class _Up>
587 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
588
589 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000590};
591
592template<class T>
593class enable_shared_from_this
594{
595protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000596 constexpr enable_shared_from_this() noexcept;
597 enable_shared_from_this(enable_shared_from_this const&) noexcept;
598 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599 ~enable_shared_from_this();
600public:
601 shared_ptr<T> shared_from_this();
602 shared_ptr<T const> shared_from_this() const;
603};
604
605template<class T>
606 bool atomic_is_lock_free(const shared_ptr<T>* p);
607template<class T>
608 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
609template<class T>
610 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
611template<class T>
612 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
613template<class T>
614 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
615template<class T>
616 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
617template<class T>
618 shared_ptr<T>
619 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
620template<class T>
621 bool
622 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
623template<class T>
624 bool
625 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
626template<class T>
627 bool
628 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
629 shared_ptr<T> w, memory_order success,
630 memory_order failure);
631template<class T>
632 bool
633 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
634 shared_ptr<T> w, memory_order success,
635 memory_order failure);
636// Hash support
637template <class T> struct hash;
638template <class T, class D> struct hash<unique_ptr<T, D> >;
639template <class T> struct hash<shared_ptr<T> >;
640
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000641template <class T, class Alloc>
642 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
643
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000644// Pointer safety
645enum class pointer_safety { relaxed, preferred, strict };
646void declare_reachable(void *p);
647template <class T> T *undeclare_reachable(T *p);
648void declare_no_pointers(char *p, size_t n);
649void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000650pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000651
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
653
654} // std
655
656*/
657
658#include <__config>
659#include <type_traits>
660#include <typeinfo>
661#include <cstddef>
662#include <cstdint>
663#include <new>
664#include <utility>
665#include <limits>
666#include <iterator>
667#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000668#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000669#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000670#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000671#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000672#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000673# include <atomic>
674#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000675#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000676
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000677#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680
Eric Fiselierf4433a32017-05-31 22:07:49 +0000681_LIBCPP_PUSH_MACROS
682#include <__undef_macros>
683
684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685_LIBCPP_BEGIN_NAMESPACE_STD
686
Eric Fiselier89659d12015-07-07 00:27:16 +0000687template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000688inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000689_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
690#if !defined(_LIBCPP_HAS_NO_THREADS) && \
691 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000692 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000693 return __atomic_load_n(__value, __ATOMIC_RELAXED);
694#else
695 return *__value;
696#endif
697}
698
Kuba Breckade9d6792016-09-04 09:55:12 +0000699template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000700inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000701_ValueType __libcpp_acquire_load(_ValueType const* __value) {
702#if !defined(_LIBCPP_HAS_NO_THREADS) && \
703 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000704 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000705 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
706#else
707 return *__value;
708#endif
709}
710
Marshall Clow78dbe462016-11-14 18:22:19 +0000711// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713template <class _Tp> class allocator;
714
Michael Park99f9e912020-03-04 11:27:14 -0500715#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716template <>
Michael Park99f9e912020-03-04 11:27:14 -0500717class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718{
719public:
720 typedef void* pointer;
721 typedef const void* const_pointer;
722 typedef void value_type;
723
724 template <class _Up> struct rebind {typedef allocator<_Up> other;};
725};
726
Howard Hinnant9f771052012-01-19 23:15:22 +0000727template <>
Michael Park99f9e912020-03-04 11:27:14 -0500728class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000729{
730public:
731 typedef const void* pointer;
732 typedef const void* const_pointer;
733 typedef const void value_type;
734
735 template <class _Up> struct rebind {typedef allocator<_Up> other;};
736};
Michael Park99f9e912020-03-04 11:27:14 -0500737#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000738
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739// pointer_traits
740
Marshall Clow0be70c32017-06-14 21:23:57 +0000741template <class _Tp, class = void>
742struct __has_element_type : false_type {};
743
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000745struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000746 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
748template <class _Ptr, bool = __has_element_type<_Ptr>::value>
749struct __pointer_traits_element_type;
750
751template <class _Ptr>
752struct __pointer_traits_element_type<_Ptr, true>
753{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000754 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755};
756
757#ifndef _LIBCPP_HAS_NO_VARIADICS
758
759template <template <class, class...> class _Sp, class _Tp, class ..._Args>
760struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
765template <template <class, class...> class _Sp, class _Tp, class ..._Args>
766struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
767{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000768 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000771#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
773template <template <class> class _Sp, class _Tp>
774struct __pointer_traits_element_type<_Sp<_Tp>, true>
775{
776 typedef typename _Sp<_Tp>::element_type type;
777};
778
779template <template <class> class _Sp, class _Tp>
780struct __pointer_traits_element_type<_Sp<_Tp>, false>
781{
782 typedef _Tp type;
783};
784
785template <template <class, class> class _Sp, class _Tp, class _A0>
786struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
787{
788 typedef typename _Sp<_Tp, _A0>::element_type type;
789};
790
791template <template <class, class> class _Sp, class _Tp, class _A0>
792struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
793{
794 typedef _Tp type;
795};
796
797template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
798struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
799{
800 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
801};
802
803template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
805{
806 typedef _Tp type;
807};
808
809template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
810 class _A1, class _A2>
811struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
812{
813 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
814};
815
816template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
817 class _A1, class _A2>
818struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
819{
820 typedef _Tp type;
821};
822
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000823#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
Marshall Clow0be70c32017-06-14 21:23:57 +0000825template <class _Tp, class = void>
826struct __has_difference_type : false_type {};
827
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000829struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000830 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831
832template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
833struct __pointer_traits_difference_type
834{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000835 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836};
837
838template <class _Ptr>
839struct __pointer_traits_difference_type<_Ptr, true>
840{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000841 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842};
843
Michael Park99f9e912020-03-04 11:27:14 -0500844template <class _Tp, class _Up, class = void>
845struct __has_rebind : false_type {};
846
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847template <class _Tp, class _Up>
Michael Park99f9e912020-03-04 11:27:14 -0500848struct __has_rebind<_Tp, _Up,
849 typename __void_t<typename _Tp::template rebind<_Up> >::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850
851template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
852struct __pointer_traits_rebind
853{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000854#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000855 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000857 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858#endif
859};
860
861#ifndef _LIBCPP_HAS_NO_VARIADICS
862
863template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
864struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
865{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000866#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000867 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000869 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870#endif
871};
872
873template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
875{
876 typedef _Sp<_Up, _Args...> type;
877};
878
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000879#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880
881template <template <class> class _Sp, class _Tp, class _Up>
882struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
883{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000884#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885 typedef typename _Sp<_Tp>::template rebind<_Up> type;
886#else
887 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
888#endif
889};
890
891template <template <class> class _Sp, class _Tp, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
893{
894 typedef _Sp<_Up> type;
895};
896
897template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
898struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
899{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000900#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
902#else
903 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
904#endif
905};
906
907template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
908struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
909{
910 typedef _Sp<_Up, _A0> type;
911};
912
913template <template <class, class, class> class _Sp, class _Tp, class _A0,
914 class _A1, class _Up>
915struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
916{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000917#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
919#else
920 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
921#endif
922};
923
924template <template <class, class, class> class _Sp, class _Tp, class _A0,
925 class _A1, class _Up>
926struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
927{
928 typedef _Sp<_Up, _A0, _A1> type;
929};
930
931template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
932 class _A1, class _A2, class _Up>
933struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
934{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000935#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
937#else
938 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
939#endif
940};
941
942template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
943 class _A1, class _A2, class _Up>
944struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
945{
946 typedef _Sp<_Up, _A0, _A1, _A2> type;
947};
948
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000949#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
951template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000952struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953{
954 typedef _Ptr pointer;
955 typedef typename __pointer_traits_element_type<pointer>::type element_type;
956 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
957
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000958#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000959 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960#else
961 template <class _Up> struct rebind
962 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000963#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
965private:
966 struct __nat {};
967public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 static pointer pointer_to(typename conditional<is_void<element_type>::value,
970 __nat, element_type>::type& __r)
971 {return pointer::pointer_to(__r);}
972};
973
974template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000975struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976{
977 typedef _Tp* pointer;
978 typedef _Tp element_type;
979 typedef ptrdiff_t difference_type;
980
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000981#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 template <class _Up> using rebind = _Up*;
983#else
984 template <class _Up> struct rebind {typedef _Up* other;};
985#endif
986
987private:
988 struct __nat {};
989public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000990 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000992 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000993 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994};
995
Eric Fiselierae3ab842015-08-23 02:56:05 +0000996template <class _From, class _To>
997struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000998#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000999 typedef typename pointer_traits<_From>::template rebind<_To> type;
1000#else
1001 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
1002#endif
1003};
1004
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005// allocator_traits
1006
Marshall Clow0be70c32017-06-14 21:23:57 +00001007template <class _Tp, class = void>
1008struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009
1010template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001011struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001012 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013
1014namespace __pointer_type_imp
1015{
1016
1017template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1018struct __pointer_type
1019{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001020 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021};
1022
1023template <class _Tp, class _Dp>
1024struct __pointer_type<_Tp, _Dp, false>
1025{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001026 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027};
1028
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001029} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030
1031template <class _Tp, class _Dp>
1032struct __pointer_type
1033{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001034 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 +00001035};
1036
Marshall Clow0be70c32017-06-14 21:23:57 +00001037template <class _Tp, class = void>
1038struct __has_const_pointer : false_type {};
1039
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001041struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001042 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043
1044template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1045struct __const_pointer
1046{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001047 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048};
1049
1050template <class _Tp, class _Ptr, class _Alloc>
1051struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1052{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001053#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001054 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055#else
1056 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1057#endif
1058};
1059
Marshall Clow0be70c32017-06-14 21:23:57 +00001060template <class _Tp, class = void>
1061struct __has_void_pointer : false_type {};
1062
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001064struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001065 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066
1067template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1068struct __void_pointer
1069{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001070 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071};
1072
1073template <class _Ptr, class _Alloc>
1074struct __void_pointer<_Ptr, _Alloc, false>
1075{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001076#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001077 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001079 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080#endif
1081};
1082
Marshall Clow0be70c32017-06-14 21:23:57 +00001083template <class _Tp, class = void>
1084struct __has_const_void_pointer : false_type {};
1085
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001087struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001088 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089
1090template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1091struct __const_void_pointer
1092{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001093 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094};
1095
1096template <class _Ptr, class _Alloc>
1097struct __const_void_pointer<_Ptr, _Alloc, false>
1098{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001099#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001100 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001102 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103#endif
1104};
1105
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001106
1107template <bool _UsePointerTraits> struct __to_address_helper;
1108
1109template <> struct __to_address_helper<true> {
1110 template <class _Pointer>
1111 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1112
1113 template <class _Pointer>
1114 _LIBCPP_CONSTEXPR
1115 static __return_type<_Pointer>
1116 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1117};
1118
1119template <class _Pointer, bool _Dummy = true>
1120using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1121
1122
Howard Hinnantc834c512011-11-29 18:15:50 +00001123template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001124inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001125_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001126__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001128 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 return __p;
1130}
1131
1132template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001133inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1134typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1135__to_address(const _Pointer& __p) _NOEXCEPT {
1136 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001137}
1138
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001139template <> struct __to_address_helper<false> {
1140 template <class _Pointer>
1141 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001142
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001143 template <class _Pointer>
1144 _LIBCPP_CONSTEXPR
1145 static __return_type<_Pointer>
1146 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1147};
1148
1149
1150#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001151template <class _Tp>
1152inline _LIBCPP_INLINE_VISIBILITY constexpr
1153_Tp*
1154to_address(_Tp* __p) _NOEXCEPT
1155{
1156 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1157 return __p;
1158}
1159
1160template <class _Pointer>
1161inline _LIBCPP_INLINE_VISIBILITY
1162auto
1163to_address(const _Pointer& __p) _NOEXCEPT
1164{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001165 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001166}
1167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168
Marshall Clow0be70c32017-06-14 21:23:57 +00001169template <class _Tp, class = void>
1170struct __has_size_type : false_type {};
1171
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001173struct __has_size_type<_Tp,
1174 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001176template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177struct __size_type
1178{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001179 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180};
1181
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001182template <class _Alloc, class _DiffType>
1183struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001185 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186};
1187
Marshall Clow0be70c32017-06-14 21:23:57 +00001188template <class _Tp, class = void>
1189struct __has_propagate_on_container_copy_assignment : false_type {};
1190
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001192struct __has_propagate_on_container_copy_assignment<_Tp,
1193 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1194 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195
1196template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1197struct __propagate_on_container_copy_assignment
1198{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001199 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200};
1201
1202template <class _Alloc>
1203struct __propagate_on_container_copy_assignment<_Alloc, true>
1204{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001205 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206};
1207
Marshall Clow0be70c32017-06-14 21:23:57 +00001208template <class _Tp, class = void>
1209struct __has_propagate_on_container_move_assignment : false_type {};
1210
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001212struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001213 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1214 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215
1216template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1217struct __propagate_on_container_move_assignment
1218{
1219 typedef false_type type;
1220};
1221
1222template <class _Alloc>
1223struct __propagate_on_container_move_assignment<_Alloc, true>
1224{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001225 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226};
1227
Marshall Clow0be70c32017-06-14 21:23:57 +00001228template <class _Tp, class = void>
1229struct __has_propagate_on_container_swap : false_type {};
1230
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001232struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001233 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1234 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
1236template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1237struct __propagate_on_container_swap
1238{
1239 typedef false_type type;
1240};
1241
1242template <class _Alloc>
1243struct __propagate_on_container_swap<_Alloc, true>
1244{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001245 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246};
1247
Marshall Clow0be70c32017-06-14 21:23:57 +00001248template <class _Tp, class = void>
1249struct __has_is_always_equal : false_type {};
1250
Marshall Clow0b587562015-06-02 16:34:03 +00001251template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001252struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001253 typename __void_t<typename _Tp::is_always_equal>::type>
1254 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001255
1256template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1257struct __is_always_equal
1258{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001259 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001260};
1261
1262template <class _Alloc>
1263struct __is_always_equal<_Alloc, true>
1264{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001265 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001266};
1267
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1269struct __has_rebind_other
1270{
1271private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001272 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001274 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001276 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277public:
1278 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1279};
1280
1281template <class _Tp, class _Up>
1282struct __has_rebind_other<_Tp, _Up, false>
1283{
1284 static const bool value = false;
1285};
1286
1287template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1288struct __allocator_traits_rebind
1289{
Michael Park99f9e912020-03-04 11:27:14 -05001290 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001291 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001292 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293};
1294
1295#ifndef _LIBCPP_HAS_NO_VARIADICS
1296
1297template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1299{
Michael Park99f9e912020-03-04 11:27:14 -05001300 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001301 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001302 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303};
1304
1305template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1306struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1307{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001308 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309};
1310
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001311#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312
1313template <template <class> class _Alloc, class _Tp, class _Up>
1314struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1315{
1316 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1317};
1318
1319template <template <class> class _Alloc, class _Tp, class _Up>
1320struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1321{
1322 typedef _Alloc<_Up> type;
1323};
1324
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1326struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1327{
1328 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1329};
1330
1331template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1333{
1334 typedef _Alloc<_Up, _A0> type;
1335};
1336
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1338 class _A1, class _Up>
1339struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1340{
1341 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1342};
1343
1344template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1345 class _A1, class _Up>
1346struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1347{
1348 typedef _Alloc<_Up, _A0, _A1> type;
1349};
1350
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1352 class _A1, class _A2, class _Up>
1353struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1354{
1355 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1356};
1357
1358template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1359 class _A1, class _A2, class _Up>
1360struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1361{
1362 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1363};
1364
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001365#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001367#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368
Michael Park99f9e912020-03-04 11:27:14 -05001369_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1371auto
1372__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001373 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001374_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375
1376template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1377auto
1378__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1379 -> false_type;
1380
1381template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1382struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001383 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1384 declval<_SizeType>(),
1385 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386{
1387};
1388
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001389#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390
1391template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1392struct __has_allocate_hint
1393 : true_type
1394{
1395};
1396
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001397#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001399#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400
Michael Park99f9e912020-03-04 11:27:14 -05001401_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1402template <class _Alloc, class _Tp, class... _Args>
1403auto
1404__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1405 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1406_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407
1408template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001409auto
1410__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1411 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412
1413template <class _Alloc, class _Pointer, class ..._Args>
1414struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001415 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1416 declval<_Pointer>(),
1417 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418{
1419};
1420
Michael Park99f9e912020-03-04 11:27:14 -05001421_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422template <class _Alloc, class _Pointer>
1423auto
1424__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1425 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001426_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427
1428template <class _Alloc, class _Pointer>
1429auto
1430__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1431 -> false_type;
1432
1433template <class _Alloc, class _Pointer>
1434struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001435 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1436 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437{
1438};
1439
Michael Park99f9e912020-03-04 11:27:14 -05001440_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441template <class _Alloc>
1442auto
1443__has_max_size_test(_Alloc&& __a)
1444 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001445_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
1447template <class _Alloc>
1448auto
1449__has_max_size_test(const volatile _Alloc& __a)
1450 -> false_type;
1451
1452template <class _Alloc>
1453struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001454 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455{
1456};
1457
1458template <class _Alloc>
1459auto
1460__has_select_on_container_copy_construction_test(_Alloc&& __a)
1461 -> decltype(__a.select_on_container_copy_construction(), true_type());
1462
1463template <class _Alloc>
1464auto
1465__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1466 -> false_type;
1467
1468template <class _Alloc>
1469struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001470 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471{
1472};
1473
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001474#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001476template <class _Alloc, class _Pointer, class _Tp, class = void>
1477struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001479template <class _Alloc, class _Pointer, class _Tp>
1480struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1481 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1482>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001484template <class _Alloc, class _Pointer, class = void>
1485struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486
1487template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001488struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1489 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1490>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491
1492template <class _Alloc>
1493struct __has_max_size
1494 : true_type
1495{
1496};
1497
1498template <class _Alloc>
1499struct __has_select_on_container_copy_construction
1500 : false_type
1501{
1502};
1503
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001504#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001506template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1507struct __alloc_traits_difference_type
1508{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001509 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001510};
1511
1512template <class _Alloc, class _Ptr>
1513struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1514{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001515 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516};
1517
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001518template <class _Tp>
1519struct __is_default_allocator : false_type {};
1520
1521template <class _Tp>
1522struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1523
Eric Fiselier909fe962019-09-13 16:09:33 +00001524
1525
1526template <class _Alloc,
1527 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1528 >
1529struct __is_cpp17_move_insertable;
1530template <class _Alloc>
1531struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1532template <class _Alloc>
1533struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1534
1535template <class _Alloc,
1536 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1537 >
1538struct __is_cpp17_copy_insertable;
1539template <class _Alloc>
1540struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1541template <class _Alloc>
1542struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1543 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1544 __is_cpp17_move_insertable<_Alloc>::value>
1545 {};
1546
1547
1548
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001550struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551{
1552 typedef _Alloc allocator_type;
1553 typedef typename allocator_type::value_type value_type;
1554
1555 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1556 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1557 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1558 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1559
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001560 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1561 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562
1563 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1564 propagate_on_container_copy_assignment;
1565 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1566 propagate_on_container_move_assignment;
1567 typedef typename __propagate_on_container_swap<allocator_type>::type
1568 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001569 typedef typename __is_always_equal<allocator_type>::type
1570 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001572#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001574 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001575 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001576#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 template <class _Tp> struct rebind_alloc
1578 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1579 template <class _Tp> struct rebind_traits
1580 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001581#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582
Marshall Clow0e58cae2017-11-26 02:55:38 +00001583 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 static pointer allocate(allocator_type& __a, size_type __n)
1585 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001586 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001588 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001592 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {__a.deallocate(__p, __n);}
1594
1595#ifndef _LIBCPP_HAS_NO_VARIADICS
1596 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001599 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001600 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001601#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001604 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 {
1606 ::new ((void*)__p) _Tp();
1607 }
1608 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001609 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001610 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001612 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1613 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 }
1615 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001616 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001617 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 const _A1& __a1)
1619 {
1620 ::new ((void*)__p) _Tp(__a0, __a1);
1621 }
1622 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001623 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001624 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 const _A1& __a1, const _A2& __a2)
1626 {
1627 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1628 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001629#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630
1631 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 static void destroy(allocator_type& __a, _Tp* __p)
1634 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1635
Howard Hinnant756c69b2010-09-22 16:48:34 +00001636 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001637 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1639
Howard Hinnant756c69b2010-09-22 16:48:34 +00001640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 static allocator_type
1642 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001643 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 __has_select_on_container_copy_construction<const allocator_type>(),
1645 __a);}
1646
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001647 template <class _Ptr>
1648 _LIBCPP_INLINE_VISIBILITY
1649 static
1650 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001651 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001652 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001653 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1654 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001655 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001656 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001657#ifdef _LIBCPP_NO_EXCEPTIONS
1658 _VSTD::move(*__begin1)
1659#else
1660 _VSTD::move_if_noexcept(*__begin1)
1661#endif
1662 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 }
1664
1665 template <class _Tp>
1666 _LIBCPP_INLINE_VISIBILITY
1667 static
1668 typename enable_if
1669 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001670 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001671 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1672 is_trivially_move_constructible<_Tp>::value,
1673 void
1674 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001675 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001676 {
1677 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001678 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001679 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001680 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001681 __begin2 += _Np;
1682 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001683 }
1684
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001685 template <class _Iter, class _Ptr>
1686 _LIBCPP_INLINE_VISIBILITY
1687 static
1688 void
1689 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1690 {
1691 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001692 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001693 }
1694
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001695 template <class _SourceTp, class _DestTp,
1696 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1697 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001698 _LIBCPP_INLINE_VISIBILITY
1699 static
1700 typename enable_if
1701 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001702 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001703 is_same<_RawSourceTp, _RawDestTp>::value &&
1704 (__is_default_allocator<allocator_type>::value ||
1705 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001706 void
1707 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001708 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001709 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001710 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001711 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001712 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001713 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001714 __begin2 += _Np;
1715 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001716 }
1717
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001718 template <class _Ptr>
1719 _LIBCPP_INLINE_VISIBILITY
1720 static
1721 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001722 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001723 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001724 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1725 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001726 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001727 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001728 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001729#ifdef _LIBCPP_NO_EXCEPTIONS
1730 _VSTD::move(*--__end1)
1731#else
1732 _VSTD::move_if_noexcept(*--__end1)
1733#endif
1734 );
1735 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001736 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001737 }
1738
1739 template <class _Tp>
1740 _LIBCPP_INLINE_VISIBILITY
1741 static
1742 typename enable_if
1743 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001744 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001745 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1746 is_trivially_move_constructible<_Tp>::value,
1747 void
1748 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001749 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001750 {
1751 ptrdiff_t _Np = __end1 - __begin1;
1752 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001753 if (_Np > 0)
1754 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001755 }
1756
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757private:
1758
Howard Hinnant756c69b2010-09-22 16:48:34 +00001759 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001760 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001762 {
1763 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1764 return __a.allocate(__n, __hint);
1765 _LIBCPP_SUPPRESS_DEPRECATED_POP
1766 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001767 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001768 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001769 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 {return __a.allocate(__n);}
1771
1772#ifndef _LIBCPP_HAS_NO_VARIADICS
1773 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001776 {
1777 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1778 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1779 _LIBCPP_SUPPRESS_DEPRECATED_POP
1780 }
1781
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1785 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001786 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001788#else // _LIBCPP_HAS_NO_VARIADICS
1789 template <class _Tp, class _A0>
1790 _LIBCPP_INLINE_VISIBILITY
1791 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1792 const _A0& __a0)
1793 {__a.construct(__p, __a0);}
1794 template <class _Tp, class _A0>
1795 _LIBCPP_INLINE_VISIBILITY
1796 static void __construct(false_type, allocator_type&, _Tp* __p,
1797 const _A0& __a0)
1798 {
1799 ::new ((void*)__p) _Tp(__a0);
1800 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001801#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802
1803 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001806 {
1807 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1808 __a.destroy(__p);
1809 _LIBCPP_SUPPRESS_DEPRECATED_POP
1810 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 static void __destroy(false_type, allocator_type&, _Tp* __p)
1814 {
1815 __p->~_Tp();
1816 }
1817
Howard Hinnant756c69b2010-09-22 16:48:34 +00001818 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001819 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001820 {
1821 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1822 return __a.max_size();
1823 _LIBCPP_SUPPRESS_DEPRECATED_POP
1824 }
1825
Howard Hinnant756c69b2010-09-22 16:48:34 +00001826 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001827 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001828 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829
Howard Hinnant756c69b2010-09-22 16:48:34 +00001830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001832 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001836 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 {return __a;}
1838};
1839
Marshall Clow940e01c2015-04-07 05:21:38 +00001840template <class _Traits, class _Tp>
1841struct __rebind_alloc_helper
1842{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001843#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001844 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001845#else
1846 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1847#endif
1848};
1849
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850// allocator
1851
1852template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001853class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854{
1855public:
Michael Park99f9e912020-03-04 11:27:14 -05001856#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1857 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1858 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1859 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1860 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1861 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1862 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1863
1864 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1865#endif
1866
1867 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868
Howard Hinnant4931e092011-06-02 21:38:57 +00001869 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001870 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001871
Marshall Clowbc759762018-03-20 23:02:53 +00001872 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1873 allocator() _NOEXCEPT {}
1874
Louis Dionne481a2662018-09-23 18:35:00 +00001875 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001876 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1877 allocator(const allocator<_Up>&) _NOEXCEPT {}
1878
Michael Park99f9e912020-03-04 11:27:14 -05001879#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1880 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1881 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001882 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001883 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1884 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001885 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001886#endif
1887
1888 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001889 {
Michael Park99f9e912020-03-04 11:27:14 -05001890 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1891 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001892 __throw_length_error("allocator<T>::allocate(size_t n)"
1893 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001894 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001895 }
Michael Park99f9e912020-03-04 11:27:14 -05001896
1897#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1898 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1899 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1900#endif
1901
1902 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001903 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001904
1905#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1906 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001907 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001908
Howard Hinnant74279a52010-09-04 23:28:19 +00001909#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001911 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 void
1913 construct(_Up* __p, _Args&&... __args)
1914 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001915 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001917#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918 _LIBCPP_INLINE_VISIBILITY
1919 void
1920 construct(pointer __p)
1921 {
1922 ::new((void*)__p) _Tp();
1923 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001924# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001925
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 template <class _A0>
1927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001928 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 construct(pointer __p, _A0& __a0)
1930 {
1931 ::new((void*)__p) _Tp(__a0);
1932 }
1933 template <class _A0>
1934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001935 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 construct(pointer __p, const _A0& __a0)
1937 {
1938 ::new((void*)__p) _Tp(__a0);
1939 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001940# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, _A0& __a0, _A1& __a1)
1945 {
1946 ::new((void*)__p) _Tp(__a0, __a1);
1947 }
1948 template <class _A0, class _A1>
1949 _LIBCPP_INLINE_VISIBILITY
1950 void
1951 construct(pointer __p, const _A0& __a0, _A1& __a1)
1952 {
1953 ::new((void*)__p) _Tp(__a0, __a1);
1954 }
1955 template <class _A0, class _A1>
1956 _LIBCPP_INLINE_VISIBILITY
1957 void
1958 construct(pointer __p, _A0& __a0, const _A1& __a1)
1959 {
1960 ::new((void*)__p) _Tp(__a0, __a1);
1961 }
1962 template <class _A0, class _A1>
1963 _LIBCPP_INLINE_VISIBILITY
1964 void
1965 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1966 {
1967 ::new((void*)__p) _Tp(__a0, __a1);
1968 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001969#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001970 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1971#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972};
1973
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001974template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001975class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001976{
1977public:
Michael Park99f9e912020-03-04 11:27:14 -05001978#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1979 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1980 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1981 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1982 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1983 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1984 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1985
1986 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1987#endif
1988
1989 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001990
1991 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001992 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001993
Marshall Clowbc759762018-03-20 23:02:53 +00001994 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1995 allocator() _NOEXCEPT {}
1996
1997 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001998 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001999 allocator(const allocator<_Up>&) _NOEXCEPT {}
2000
Michael Park99f9e912020-03-04 11:27:14 -05002001#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2002 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
2003 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002004 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05002005#endif
2006
2007 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00002008 {
Michael Park99f9e912020-03-04 11:27:14 -05002009 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
2010 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00002011 __throw_length_error("allocator<const T>::allocate(size_t n)"
2012 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05002013 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00002014 }
Michael Park99f9e912020-03-04 11:27:14 -05002015
2016#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2017 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
2018 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
2019#endif
2020
2021 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002022 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05002023
2024#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2025 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002026 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05002027
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002028#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2029 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05002030 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002031 void
2032 construct(_Up* __p, _Args&&... __args)
2033 {
2034 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
2035 }
2036#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2037 _LIBCPP_INLINE_VISIBILITY
2038 void
2039 construct(pointer __p)
2040 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002041 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002042 }
2043# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00002044
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002045 template <class _A0>
2046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002047 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002048 construct(pointer __p, _A0& __a0)
2049 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002050 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002051 }
2052 template <class _A0>
2053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002054 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002055 construct(pointer __p, const _A0& __a0)
2056 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002057 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002058 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002059# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2060 template <class _A0, class _A1>
2061 _LIBCPP_INLINE_VISIBILITY
2062 void
2063 construct(pointer __p, _A0& __a0, _A1& __a1)
2064 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002065 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002066 }
2067 template <class _A0, class _A1>
2068 _LIBCPP_INLINE_VISIBILITY
2069 void
2070 construct(pointer __p, const _A0& __a0, _A1& __a1)
2071 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002072 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002073 }
2074 template <class _A0, class _A1>
2075 _LIBCPP_INLINE_VISIBILITY
2076 void
2077 construct(pointer __p, _A0& __a0, const _A1& __a1)
2078 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002079 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002080 }
2081 template <class _A0, class _A1>
2082 _LIBCPP_INLINE_VISIBILITY
2083 void
2084 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2085 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002086 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002087 }
2088#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05002089 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2090#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002091};
2092
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093template <class _Tp, class _Up>
2094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002095bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096
2097template <class _Tp, class _Up>
2098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002099bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100
2101template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002102class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103 : public iterator<output_iterator_tag,
2104 _Tp, // purposefully not C++03
2105 ptrdiff_t, // purposefully not C++03
2106 _Tp*, // purposefully not C++03
2107 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2108{
2109private:
2110 _OutputIterator __x_;
2111public:
2112 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2113 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2114 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002115 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002116#if _LIBCPP_STD_VER >= 14
2117 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002118 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002119#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2121 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2122 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002123#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002124 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002125#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126};
2127
2128template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002129_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002131get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132{
2133 pair<_Tp*, ptrdiff_t> __r(0, 0);
2134 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2135 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2136 / sizeof(_Tp);
2137 if (__n > __m)
2138 __n = __m;
2139 while (__n > 0)
2140 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002141#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002142 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002143 {
2144 std::align_val_t __al =
2145 std::align_val_t(std::alignment_of<_Tp>::value);
2146 __r.first = static_cast<_Tp*>(::operator new(
2147 __n * sizeof(_Tp), __al, nothrow));
2148 } else {
2149 __r.first = static_cast<_Tp*>(::operator new(
2150 __n * sizeof(_Tp), nothrow));
2151 }
2152#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002153 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002154 {
2155 // Since aligned operator new is unavailable, return an empty
2156 // buffer rather than one with invalid alignment.
2157 return __r;
2158 }
2159
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002161#endif
2162
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163 if (__r.first)
2164 {
2165 __r.second = __n;
2166 break;
2167 }
2168 __n /= 2;
2169 }
2170 return __r;
2171}
2172
2173template <class _Tp>
2174inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002175void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2176{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002177 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002178}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179
Marshall Clowb22274f2017-01-24 22:22:33 +00002180#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002182struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183{
2184 _Tp* __ptr_;
2185};
2186
2187template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002188class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189{
2190private:
2191 _Tp* __ptr_;
2192public:
2193 typedef _Tp element_type;
2194
2195 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2196 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2197 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2198 : __ptr_(__p.release()) {}
2199 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2200 {reset(__p.release()); return *this;}
2201 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2202 {reset(__p.release()); return *this;}
2203 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2204 {reset(__p.__ptr_); return *this;}
2205 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2206
2207 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2208 {return *__ptr_;}
2209 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2210 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2211 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2212 {
2213 _Tp* __t = __ptr_;
2214 __ptr_ = 0;
2215 return __t;
2216 }
2217 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2218 {
2219 if (__ptr_ != __p)
2220 delete __ptr_;
2221 __ptr_ = __p;
2222 }
2223
2224 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2225 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2226 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2227 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2228 {return auto_ptr<_Up>(release());}
2229};
2230
2231template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002232class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233{
2234public:
2235 typedef void element_type;
2236};
Marshall Clowb22274f2017-01-24 22:22:33 +00002237#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002239// Tag used to default initialize one or both of the pair's elements.
2240struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002241struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002242
Eric Fiselier9d355982017-04-12 23:45:53 +00002243template <class _Tp, int _Idx,
2244 bool _CanBeEmptyBase =
2245 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2246struct __compressed_pair_elem {
2247 typedef _Tp _ParamT;
2248 typedef _Tp& reference;
2249 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002251 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002252 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002253 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2254 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
Eric Fiselier9d355982017-04-12 23:45:53 +00002256 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002257 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2258 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002259 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002260 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002261 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002262 : __value_(_VSTD::forward<_Up>(__u))
2263 {
2264 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002266
2267#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002268 template <class... _Args, size_t... _Indexes>
2269 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2270 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2271 __tuple_indices<_Indexes...>)
2272 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002273#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002275
Alex Lorenz76132112017-11-09 17:54:49 +00002276 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2277 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002278 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002281 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282};
2283
Eric Fiselier9d355982017-04-12 23:45:53 +00002284template <class _Tp, int _Idx>
2285struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2286 typedef _Tp _ParamT;
2287 typedef _Tp& reference;
2288 typedef const _Tp& const_reference;
2289 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002291 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2292 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2293 __compressed_pair_elem(__default_init_tag) {}
2294 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2295 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
Eric Fiselier9d355982017-04-12 23:45:53 +00002297 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002298 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2299 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002300 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002301 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002302 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002303 : __value_type(_VSTD::forward<_Up>(__u))
2304 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002306#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002307 template <class... _Args, size_t... _Indexes>
2308 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2309 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2310 __tuple_indices<_Indexes...>)
2311 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002312#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313
Alex Lorenz76132112017-11-09 17:54:49 +00002314 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2315 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002316 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317};
2318
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002320class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2321 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002322 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2323 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002324
2325 // NOTE: This static assert should never fire because __compressed_pair
2326 // is *almost never* used in a scenario where it's possible for T1 == T2.
2327 // (The exception is std::function where it is possible that the function
2328 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002329 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002330 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2331 "The current implementation is NOT ABI-compatible with the previous "
2332 "implementation for this configuration");
2333
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002335 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002336 class = typename enable_if<
2337 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2338 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2339 >::type
2340 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002341 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002342 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343
Eric Fiselier9d355982017-04-12 23:45:53 +00002344 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002345 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002346 __compressed_pair(_U1&& __t1, _U2&& __t2)
2347 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002349#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002350 template <class... _Args1, class... _Args2>
2351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2352 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2353 tuple<_Args2...> __second_args)
2354 : _Base1(__pc, _VSTD::move(__first_args),
2355 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2356 _Base2(__pc, _VSTD::move(__second_args),
2357 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002358#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359
Eric Fiselier9d355982017-04-12 23:45:53 +00002360 _LIBCPP_INLINE_VISIBILITY
2361 typename _Base1::reference first() _NOEXCEPT {
2362 return static_cast<_Base1&>(*this).__get();
2363 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364
Eric Fiselier9d355982017-04-12 23:45:53 +00002365 _LIBCPP_INLINE_VISIBILITY
2366 typename _Base1::const_reference first() const _NOEXCEPT {
2367 return static_cast<_Base1 const&>(*this).__get();
2368 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369
Eric Fiselier9d355982017-04-12 23:45:53 +00002370 _LIBCPP_INLINE_VISIBILITY
2371 typename _Base2::reference second() _NOEXCEPT {
2372 return static_cast<_Base2&>(*this).__get();
2373 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374
Eric Fiselier9d355982017-04-12 23:45:53 +00002375 _LIBCPP_INLINE_VISIBILITY
2376 typename _Base2::const_reference second() const _NOEXCEPT {
2377 return static_cast<_Base2 const&>(*this).__get();
2378 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379
Eric Fiselier9d355982017-04-12 23:45:53 +00002380 _LIBCPP_INLINE_VISIBILITY
2381 void swap(__compressed_pair& __x)
2382 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2383 __is_nothrow_swappable<_T2>::value)
2384 {
2385 using std::swap;
2386 swap(first(), __x.first());
2387 swap(second(), __x.second());
2388 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389};
2390
2391template <class _T1, class _T2>
2392inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002393void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2394 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2395 __is_nothrow_swappable<_T2>::value) {
2396 __x.swap(__y);
2397}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002399// default_delete
2400
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002402struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002403 static_assert(!is_function<_Tp>::value,
2404 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002405#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002406 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002407#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002408 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002409#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002410 template <class _Up>
2411 _LIBCPP_INLINE_VISIBILITY
2412 default_delete(const default_delete<_Up>&,
2413 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2414 0) _NOEXCEPT {}
2415
2416 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2417 static_assert(sizeof(_Tp) > 0,
2418 "default_delete can not delete incomplete type");
2419 static_assert(!is_void<_Tp>::value,
2420 "default_delete can not delete incomplete type");
2421 delete __ptr;
2422 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423};
2424
2425template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002426struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2427private:
2428 template <class _Up>
2429 struct _EnableIfConvertible
2430 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2431
Howard Hinnant4500ca52011-12-18 21:19:44 +00002432public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002433#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002434 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002435#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002436 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002437#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002438
2439 template <class _Up>
2440 _LIBCPP_INLINE_VISIBILITY
2441 default_delete(const default_delete<_Up[]>&,
2442 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2443
2444 template <class _Up>
2445 _LIBCPP_INLINE_VISIBILITY
2446 typename _EnableIfConvertible<_Up>::type
2447 operator()(_Up* __ptr) const _NOEXCEPT {
2448 static_assert(sizeof(_Tp) > 0,
2449 "default_delete can not delete incomplete type");
2450 static_assert(!is_void<_Tp>::value,
2451 "default_delete can not delete void type");
2452 delete[] __ptr;
2453 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454};
2455
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002456template <class _Deleter>
2457struct __unique_ptr_deleter_sfinae {
2458 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2459 typedef const _Deleter& __lval_ref_type;
2460 typedef _Deleter&& __good_rval_ref_type;
2461 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462};
2463
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002464template <class _Deleter>
2465struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2466 typedef const _Deleter& __lval_ref_type;
2467 typedef const _Deleter&& __bad_rval_ref_type;
2468 typedef false_type __enable_rval_overload;
2469};
2470
2471template <class _Deleter>
2472struct __unique_ptr_deleter_sfinae<_Deleter&> {
2473 typedef _Deleter& __lval_ref_type;
2474 typedef _Deleter&& __bad_rval_ref_type;
2475 typedef false_type __enable_rval_overload;
2476};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477
2478template <class _Tp, class _Dp = default_delete<_Tp> >
2479class _LIBCPP_TEMPLATE_VIS unique_ptr {
2480public:
2481 typedef _Tp element_type;
2482 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002483 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002484
2485 static_assert(!is_rvalue_reference<deleter_type>::value,
2486 "the specified deleter type cannot be an rvalue reference");
2487
2488private:
2489 __compressed_pair<pointer, deleter_type> __ptr_;
2490
2491 struct __nat { int __for_bool_; };
2492
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002493 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002494
2495 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002496 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002497 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498
2499 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002500 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002501 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002502
2503 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002504 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002505 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002506
2507 template <bool _Dummy, class _Deleter = typename __dependent_type<
2508 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002509 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002510 typename enable_if<is_default_constructible<_Deleter>::value &&
2511 !is_pointer<_Deleter>::value>::type;
2512
2513 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002514 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002515 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2516
2517 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002518 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002519 is_convertible<typename _UPtr::pointer, pointer>::value &&
2520 !is_array<_Up>::value
2521 >::type;
2522
2523 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002524 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2526 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2527 >::type;
2528
2529 template <class _UDel>
2530 using _EnableIfDeleterAssignable = typename enable_if<
2531 is_assignable<_Dp&, _UDel&&>::value
2532 >::type;
2533
2534public:
2535 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002536 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002537 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002538 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002539
2540 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002541 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002542 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002543 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002544
2545 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002546 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002547 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002548 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002549
2550 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002551 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002552 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002553 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002554 : __ptr_(__p, __d) {}
2555
2556 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002557 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002559 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002560 : __ptr_(__p, _VSTD::move(__d)) {
2561 static_assert(!is_reference<deleter_type>::value,
2562 "rvalue deleter bound to reference");
2563 }
2564
2565 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002566 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002567 _LIBCPP_INLINE_VISIBILITY
2568 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2569
2570 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002571 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002572 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2573 }
2574
2575 template <class _Up, class _Ep,
2576 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2577 class = _EnableIfDeleterConvertible<_Ep>
2578 >
2579 _LIBCPP_INLINE_VISIBILITY
2580 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2581 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2582
2583#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2584 template <class _Up>
2585 _LIBCPP_INLINE_VISIBILITY
2586 unique_ptr(auto_ptr<_Up>&& __p,
2587 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002588 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002589 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002590 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002591#endif
2592
2593 _LIBCPP_INLINE_VISIBILITY
2594 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2595 reset(__u.release());
2596 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2597 return *this;
2598 }
2599
2600 template <class _Up, class _Ep,
2601 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2602 class = _EnableIfDeleterAssignable<_Ep>
2603 >
2604 _LIBCPP_INLINE_VISIBILITY
2605 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2606 reset(__u.release());
2607 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2608 return *this;
2609 }
2610
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002611#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2612 template <class _Up>
2613 _LIBCPP_INLINE_VISIBILITY
2614 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2615 is_same<_Dp, default_delete<_Tp> >::value,
2616 unique_ptr&>::type
2617 operator=(auto_ptr<_Up> __p) {
2618 reset(__p.release());
2619 return *this;
2620 }
2621#endif
2622
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002623#ifdef _LIBCPP_CXX03_LANG
2624 unique_ptr(unique_ptr const&) = delete;
2625 unique_ptr& operator=(unique_ptr const&) = delete;
2626#endif
2627
2628
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629 _LIBCPP_INLINE_VISIBILITY
2630 ~unique_ptr() { reset(); }
2631
2632 _LIBCPP_INLINE_VISIBILITY
2633 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2634 reset();
2635 return *this;
2636 }
2637
2638 _LIBCPP_INLINE_VISIBILITY
2639 typename add_lvalue_reference<_Tp>::type
2640 operator*() const {
2641 return *__ptr_.first();
2642 }
2643 _LIBCPP_INLINE_VISIBILITY
2644 pointer operator->() const _NOEXCEPT {
2645 return __ptr_.first();
2646 }
2647 _LIBCPP_INLINE_VISIBILITY
2648 pointer get() const _NOEXCEPT {
2649 return __ptr_.first();
2650 }
2651 _LIBCPP_INLINE_VISIBILITY
2652 deleter_type& get_deleter() _NOEXCEPT {
2653 return __ptr_.second();
2654 }
2655 _LIBCPP_INLINE_VISIBILITY
2656 const deleter_type& get_deleter() const _NOEXCEPT {
2657 return __ptr_.second();
2658 }
2659 _LIBCPP_INLINE_VISIBILITY
2660 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2661 return __ptr_.first() != nullptr;
2662 }
2663
2664 _LIBCPP_INLINE_VISIBILITY
2665 pointer release() _NOEXCEPT {
2666 pointer __t = __ptr_.first();
2667 __ptr_.first() = pointer();
2668 return __t;
2669 }
2670
2671 _LIBCPP_INLINE_VISIBILITY
2672 void reset(pointer __p = pointer()) _NOEXCEPT {
2673 pointer __tmp = __ptr_.first();
2674 __ptr_.first() = __p;
2675 if (__tmp)
2676 __ptr_.second()(__tmp);
2677 }
2678
2679 _LIBCPP_INLINE_VISIBILITY
2680 void swap(unique_ptr& __u) _NOEXCEPT {
2681 __ptr_.swap(__u.__ptr_);
2682 }
2683};
2684
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002685
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002687class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002689 typedef _Tp element_type;
2690 typedef _Dp deleter_type;
2691 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2692
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002694 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695
Eric Fiselier31127cd2017-04-16 02:14:31 +00002696 template <class _From>
2697 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698
Eric Fiselier31127cd2017-04-16 02:14:31 +00002699 template <class _FromElem>
2700 struct _CheckArrayPointerConversion<_FromElem*>
2701 : integral_constant<bool,
2702 is_same<_FromElem*, pointer>::value ||
2703 (is_same<pointer, element_type*>::value &&
2704 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2705 >
2706 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707
Eric Fiselier31127cd2017-04-16 02:14:31 +00002708 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002709
2710 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002711 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002712 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002713
2714 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002715 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002716 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717
2718 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002719 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002720 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002721
2722 template <bool _Dummy, class _Deleter = typename __dependent_type<
2723 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002724 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002725 typename enable_if<is_default_constructible<_Deleter>::value &&
2726 !is_pointer<_Deleter>::value>::type;
2727
2728 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002729 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002730 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2731
2732 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002733 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002734 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735 >::type;
2736
2737 template <class _UPtr, class _Up,
2738 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002739 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002740 is_array<_Up>::value &&
2741 is_same<pointer, element_type*>::value &&
2742 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2743 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2744 >::type;
2745
2746 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002747 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002748 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2749 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2750 >::type;
2751
2752 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002753 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 is_assignable<_Dp&, _UDel&&>::value
2755 >::type;
2756
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002759 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002760 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002761 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002763 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002764 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002765 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002766 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002768 template <class _Pp, bool _Dummy = true,
2769 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002770 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002771 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002772 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002773 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002775 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002776 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2777 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002778 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002779 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002780 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002783 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002784 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002785 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002786 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002788 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002789 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2790 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002791 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002792 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002793 : __ptr_(__p, _VSTD::move(__d)) {
2794 static_assert(!is_reference<deleter_type>::value,
2795 "rvalue deleter bound to reference");
2796 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002798 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002799 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002800 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002801 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002802 : __ptr_(nullptr, _VSTD::move(__d)) {
2803 static_assert(!is_reference<deleter_type>::value,
2804 "rvalue deleter bound to reference");
2805 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002806
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002807 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002808 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2809 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002810 _LIBCPP_INLINE_VISIBILITY
2811 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002812
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002813 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002814 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002815 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2816 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002817
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002818 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002819 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002820 reset(__u.release());
2821 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2822 return *this;
2823 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002825 template <class _Up, class _Ep,
2826 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2827 class = _EnableIfDeleterConvertible<_Ep>
2828 >
2829 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002830 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002831 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002832 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002834 template <class _Up, class _Ep,
2835 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2836 class = _EnableIfDeleterAssignable<_Ep>
2837 >
2838 _LIBCPP_INLINE_VISIBILITY
2839 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002840 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002841 reset(__u.release());
2842 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2843 return *this;
2844 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002846#ifdef _LIBCPP_CXX03_LANG
2847 unique_ptr(unique_ptr const&) = delete;
2848 unique_ptr& operator=(unique_ptr const&) = delete;
2849#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002850
2851public:
2852 _LIBCPP_INLINE_VISIBILITY
2853 ~unique_ptr() { reset(); }
2854
2855 _LIBCPP_INLINE_VISIBILITY
2856 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2857 reset();
2858 return *this;
2859 }
2860
2861 _LIBCPP_INLINE_VISIBILITY
2862 typename add_lvalue_reference<_Tp>::type
2863 operator[](size_t __i) const {
2864 return __ptr_.first()[__i];
2865 }
2866 _LIBCPP_INLINE_VISIBILITY
2867 pointer get() const _NOEXCEPT {
2868 return __ptr_.first();
2869 }
2870
2871 _LIBCPP_INLINE_VISIBILITY
2872 deleter_type& get_deleter() _NOEXCEPT {
2873 return __ptr_.second();
2874 }
2875
2876 _LIBCPP_INLINE_VISIBILITY
2877 const deleter_type& get_deleter() const _NOEXCEPT {
2878 return __ptr_.second();
2879 }
2880 _LIBCPP_INLINE_VISIBILITY
2881 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2882 return __ptr_.first() != nullptr;
2883 }
2884
2885 _LIBCPP_INLINE_VISIBILITY
2886 pointer release() _NOEXCEPT {
2887 pointer __t = __ptr_.first();
2888 __ptr_.first() = pointer();
2889 return __t;
2890 }
2891
2892 template <class _Pp>
2893 _LIBCPP_INLINE_VISIBILITY
2894 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002895 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002896 >::type
2897 reset(_Pp __p) _NOEXCEPT {
2898 pointer __tmp = __ptr_.first();
2899 __ptr_.first() = __p;
2900 if (__tmp)
2901 __ptr_.second()(__tmp);
2902 }
2903
2904 _LIBCPP_INLINE_VISIBILITY
2905 void reset(nullptr_t = nullptr) _NOEXCEPT {
2906 pointer __tmp = __ptr_.first();
2907 __ptr_.first() = nullptr;
2908 if (__tmp)
2909 __ptr_.second()(__tmp);
2910 }
2911
2912 _LIBCPP_INLINE_VISIBILITY
2913 void swap(unique_ptr& __u) _NOEXCEPT {
2914 __ptr_.swap(__u.__ptr_);
2915 }
2916
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917};
2918
2919template <class _Tp, class _Dp>
2920inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002921typename enable_if<
2922 __is_swappable<_Dp>::value,
2923 void
2924>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002925swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926
2927template <class _T1, class _D1, class _T2, class _D2>
2928inline _LIBCPP_INLINE_VISIBILITY
2929bool
2930operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2931
2932template <class _T1, class _D1, class _T2, class _D2>
2933inline _LIBCPP_INLINE_VISIBILITY
2934bool
2935operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2936
2937template <class _T1, class _D1, class _T2, class _D2>
2938inline _LIBCPP_INLINE_VISIBILITY
2939bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002940operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2941{
2942 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2943 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002944 typedef typename common_type<_P1, _P2>::type _Vp;
2945 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002946}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947
2948template <class _T1, class _D1, class _T2, class _D2>
2949inline _LIBCPP_INLINE_VISIBILITY
2950bool
2951operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2952
2953template <class _T1, class _D1, class _T2, class _D2>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
2956operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2957
2958template <class _T1, class _D1, class _T2, class _D2>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
2961operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2962
Howard Hinnantb17caf92012-02-21 21:02:58 +00002963template <class _T1, class _D1>
2964inline _LIBCPP_INLINE_VISIBILITY
2965bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002966operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002967{
2968 return !__x;
2969}
2970
2971template <class _T1, class _D1>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002974operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002975{
2976 return !__x;
2977}
2978
2979template <class _T1, class _D1>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002982operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002983{
2984 return static_cast<bool>(__x);
2985}
2986
2987template <class _T1, class _D1>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002990operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002991{
2992 return static_cast<bool>(__x);
2993}
2994
2995template <class _T1, class _D1>
2996inline _LIBCPP_INLINE_VISIBILITY
2997bool
2998operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2999{
3000 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3001 return less<_P1>()(__x.get(), nullptr);
3002}
3003
3004template <class _T1, class _D1>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
3007operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3008{
3009 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3010 return less<_P1>()(nullptr, __x.get());
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
3016operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3017{
3018 return nullptr < __x;
3019}
3020
3021template <class _T1, class _D1>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
3024operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3025{
3026 return __x < nullptr;
3027}
3028
3029template <class _T1, class _D1>
3030inline _LIBCPP_INLINE_VISIBILITY
3031bool
3032operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3033{
3034 return !(nullptr < __x);
3035}
3036
3037template <class _T1, class _D1>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3041{
3042 return !(__x < nullptr);
3043}
3044
3045template <class _T1, class _D1>
3046inline _LIBCPP_INLINE_VISIBILITY
3047bool
3048operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3049{
3050 return !(__x < nullptr);
3051}
3052
3053template <class _T1, class _D1>
3054inline _LIBCPP_INLINE_VISIBILITY
3055bool
3056operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3057{
3058 return !(nullptr < __x);
3059}
3060
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003061#if _LIBCPP_STD_VER > 11
3062
3063template<class _Tp>
3064struct __unique_if
3065{
3066 typedef unique_ptr<_Tp> __unique_single;
3067};
3068
3069template<class _Tp>
3070struct __unique_if<_Tp[]>
3071{
3072 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3073};
3074
3075template<class _Tp, size_t _Np>
3076struct __unique_if<_Tp[_Np]>
3077{
3078 typedef void __unique_array_known_bound;
3079};
3080
3081template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003082inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003083typename __unique_if<_Tp>::__unique_single
3084make_unique(_Args&&... __args)
3085{
3086 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3087}
3088
3089template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003090inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003091typename __unique_if<_Tp>::__unique_array_unknown_bound
3092make_unique(size_t __n)
3093{
3094 typedef typename remove_extent<_Tp>::type _Up;
3095 return unique_ptr<_Tp>(new _Up[__n]());
3096}
3097
3098template<class _Tp, class... _Args>
3099 typename __unique_if<_Tp>::__unique_array_known_bound
3100 make_unique(_Args&&...) = delete;
3101
3102#endif // _LIBCPP_STD_VER > 11
3103
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003104template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003105#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003106struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003107#else
3108struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003109 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003110#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003111{
3112 typedef unique_ptr<_Tp, _Dp> argument_type;
3113 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003114 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003115 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003116 {
3117 typedef typename argument_type::pointer pointer;
3118 return hash<pointer>()(__ptr.get());
3119 }
3120};
3121
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122struct __destruct_n
3123{
3124private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003125 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126
3127 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003128 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003129 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130
3131 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003132 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 {}
3134
Howard Hinnant719bda32011-05-28 14:41:13 +00003135 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003136 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003137 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138 {}
3139
Howard Hinnant719bda32011-05-28 14:41:13 +00003140 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003141 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003142 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143 {}
3144public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003145 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003146 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147
3148 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003149 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003150 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151
3152 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003153 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003154 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155
3156 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003157 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003158 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159};
3160
3161template <class _Alloc>
3162class __allocator_destructor
3163{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003164 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003166 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3167 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168private:
3169 _Alloc& __alloc_;
3170 size_type __s_;
3171public:
3172 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003173 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003176 void operator()(pointer __p) _NOEXCEPT
3177 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178};
3179
3180template <class _InputIterator, class _ForwardIterator>
3181_ForwardIterator
3182uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3183{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003185#ifndef _LIBCPP_NO_EXCEPTIONS
3186 _ForwardIterator __s = __r;
3187 try
3188 {
3189#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003190 for (; __f != __l; ++__f, (void) ++__r)
3191 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003192#ifndef _LIBCPP_NO_EXCEPTIONS
3193 }
3194 catch (...)
3195 {
3196 for (; __s != __r; ++__s)
3197 __s->~value_type();
3198 throw;
3199 }
3200#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 return __r;
3202}
3203
3204template <class _InputIterator, class _Size, class _ForwardIterator>
3205_ForwardIterator
3206uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3207{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003209#ifndef _LIBCPP_NO_EXCEPTIONS
3210 _ForwardIterator __s = __r;
3211 try
3212 {
3213#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003214 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3215 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003216#ifndef _LIBCPP_NO_EXCEPTIONS
3217 }
3218 catch (...)
3219 {
3220 for (; __s != __r; ++__s)
3221 __s->~value_type();
3222 throw;
3223 }
3224#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225 return __r;
3226}
3227
3228template <class _ForwardIterator, class _Tp>
3229void
3230uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3231{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003233#ifndef _LIBCPP_NO_EXCEPTIONS
3234 _ForwardIterator __s = __f;
3235 try
3236 {
3237#endif
3238 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003239 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003240#ifndef _LIBCPP_NO_EXCEPTIONS
3241 }
3242 catch (...)
3243 {
3244 for (; __s != __f; ++__s)
3245 __s->~value_type();
3246 throw;
3247 }
3248#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249}
3250
3251template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003252_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3254{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003256#ifndef _LIBCPP_NO_EXCEPTIONS
3257 _ForwardIterator __s = __f;
3258 try
3259 {
3260#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003261 for (; __n > 0; ++__f, (void) --__n)
3262 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003263#ifndef _LIBCPP_NO_EXCEPTIONS
3264 }
3265 catch (...)
3266 {
3267 for (; __s != __f; ++__s)
3268 __s->~value_type();
3269 throw;
3270 }
3271#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003272 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273}
3274
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003275#if _LIBCPP_STD_VER > 14
3276
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003277template <class _Tp>
3278inline _LIBCPP_INLINE_VISIBILITY
3279void destroy_at(_Tp* __loc) {
3280 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3281 __loc->~_Tp();
3282}
3283
3284template <class _ForwardIterator>
3285inline _LIBCPP_INLINE_VISIBILITY
3286void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3287 for (; __first != __last; ++__first)
3288 _VSTD::destroy_at(_VSTD::addressof(*__first));
3289}
3290
3291template <class _ForwardIterator, class _Size>
3292inline _LIBCPP_INLINE_VISIBILITY
3293_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3294 for (; __n > 0; (void)++__first, --__n)
3295 _VSTD::destroy_at(_VSTD::addressof(*__first));
3296 return __first;
3297}
3298
Eric Fiselier290c07c2016-10-11 21:13:44 +00003299template <class _ForwardIterator>
3300inline _LIBCPP_INLINE_VISIBILITY
3301void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3302 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3303 auto __idx = __first;
3304#ifndef _LIBCPP_NO_EXCEPTIONS
3305 try {
3306#endif
3307 for (; __idx != __last; ++__idx)
3308 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3309#ifndef _LIBCPP_NO_EXCEPTIONS
3310 } catch (...) {
3311 _VSTD::destroy(__first, __idx);
3312 throw;
3313 }
3314#endif
3315}
3316
3317template <class _ForwardIterator, class _Size>
3318inline _LIBCPP_INLINE_VISIBILITY
3319_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3320 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3321 auto __idx = __first;
3322#ifndef _LIBCPP_NO_EXCEPTIONS
3323 try {
3324#endif
3325 for (; __n > 0; (void)++__idx, --__n)
3326 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3327 return __idx;
3328#ifndef _LIBCPP_NO_EXCEPTIONS
3329 } catch (...) {
3330 _VSTD::destroy(__first, __idx);
3331 throw;
3332 }
3333#endif
3334}
3335
3336
3337template <class _ForwardIterator>
3338inline _LIBCPP_INLINE_VISIBILITY
3339void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3340 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3341 auto __idx = __first;
3342#ifndef _LIBCPP_NO_EXCEPTIONS
3343 try {
3344#endif
3345 for (; __idx != __last; ++__idx)
3346 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3347#ifndef _LIBCPP_NO_EXCEPTIONS
3348 } catch (...) {
3349 _VSTD::destroy(__first, __idx);
3350 throw;
3351 }
3352#endif
3353}
3354
3355template <class _ForwardIterator, class _Size>
3356inline _LIBCPP_INLINE_VISIBILITY
3357_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3358 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3359 auto __idx = __first;
3360#ifndef _LIBCPP_NO_EXCEPTIONS
3361 try {
3362#endif
3363 for (; __n > 0; (void)++__idx, --__n)
3364 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3365 return __idx;
3366#ifndef _LIBCPP_NO_EXCEPTIONS
3367 } catch (...) {
3368 _VSTD::destroy(__first, __idx);
3369 throw;
3370 }
3371#endif
3372}
3373
3374
3375template <class _InputIt, class _ForwardIt>
3376inline _LIBCPP_INLINE_VISIBILITY
3377_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3378 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3379 auto __idx = __first_res;
3380#ifndef _LIBCPP_NO_EXCEPTIONS
3381 try {
3382#endif
3383 for (; __first != __last; (void)++__idx, ++__first)
3384 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3385 return __idx;
3386#ifndef _LIBCPP_NO_EXCEPTIONS
3387 } catch (...) {
3388 _VSTD::destroy(__first_res, __idx);
3389 throw;
3390 }
3391#endif
3392}
3393
3394template <class _InputIt, class _Size, class _ForwardIt>
3395inline _LIBCPP_INLINE_VISIBILITY
3396pair<_InputIt, _ForwardIt>
3397uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3398 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3399 auto __idx = __first_res;
3400#ifndef _LIBCPP_NO_EXCEPTIONS
3401 try {
3402#endif
3403 for (; __n > 0; ++__idx, (void)++__first, --__n)
3404 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3405 return {__first, __idx};
3406#ifndef _LIBCPP_NO_EXCEPTIONS
3407 } catch (...) {
3408 _VSTD::destroy(__first_res, __idx);
3409 throw;
3410 }
3411#endif
3412}
3413
3414
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003415#endif // _LIBCPP_STD_VER > 14
3416
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003417// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3418// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003419// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003420#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3421 && defined(__ATOMIC_RELAXED) \
3422 && defined(__ATOMIC_ACQ_REL)
3423# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003424#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003425# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3426#endif
3427
3428template <class _Tp>
3429inline _LIBCPP_INLINE_VISIBILITY _Tp
3430__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3431{
3432#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3433 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3434#else
3435 return __t += 1;
3436#endif
3437}
3438
3439template <class _Tp>
3440inline _LIBCPP_INLINE_VISIBILITY _Tp
3441__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3442{
3443#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3444 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3445#else
3446 return __t -= 1;
3447#endif
3448}
3449
Howard Hinnant756c69b2010-09-22 16:48:34 +00003450class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451 : public std::exception
3452{
3453public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003454 virtual ~bad_weak_ptr() _NOEXCEPT;
3455 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456};
3457
Louis Dionne16fe2952018-07-11 23:14:33 +00003458_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003459void __throw_bad_weak_ptr()
3460{
3461#ifndef _LIBCPP_NO_EXCEPTIONS
3462 throw bad_weak_ptr();
3463#else
3464 _VSTD::abort();
3465#endif
3466}
3467
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003468template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003470class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471{
3472 __shared_count(const __shared_count&);
3473 __shared_count& operator=(const __shared_count&);
3474
3475protected:
3476 long __shared_owners_;
3477 virtual ~__shared_count();
3478private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003479 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480
3481public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003483 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484 : __shared_owners_(__refs) {}
3485
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003486#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003487 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003488 void __add_shared() _NOEXCEPT;
3489 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003490#else
3491 _LIBCPP_INLINE_VISIBILITY
3492 void __add_shared() _NOEXCEPT {
3493 __libcpp_atomic_refcount_increment(__shared_owners_);
3494 }
3495 _LIBCPP_INLINE_VISIBILITY
3496 bool __release_shared() _NOEXCEPT {
3497 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3498 __on_zero_shared();
3499 return true;
3500 }
3501 return false;
3502 }
3503#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003504 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003505 long use_count() const _NOEXCEPT {
3506 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3507 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508};
3509
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003510class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 : private __shared_count
3512{
3513 long __shared_weak_owners_;
3514
3515public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003517 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518 : __shared_count(__refs),
3519 __shared_weak_owners_(__refs) {}
3520protected:
3521 virtual ~__shared_weak_count();
3522
3523public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003524#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003525 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003526 void __add_shared() _NOEXCEPT;
3527 void __add_weak() _NOEXCEPT;
3528 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003529#else
3530 _LIBCPP_INLINE_VISIBILITY
3531 void __add_shared() _NOEXCEPT {
3532 __shared_count::__add_shared();
3533 }
3534 _LIBCPP_INLINE_VISIBILITY
3535 void __add_weak() _NOEXCEPT {
3536 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3537 }
3538 _LIBCPP_INLINE_VISIBILITY
3539 void __release_shared() _NOEXCEPT {
3540 if (__shared_count::__release_shared())
3541 __release_weak();
3542 }
3543#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003544 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003546 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3547 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548
Howard Hinnant807d6332013-02-25 15:50:36 +00003549 // Define the function out only if we build static libc++ without RTTI.
3550 // Otherwise we may break clients who need to compile their projects with
3551 // -fno-rtti and yet link against a libc++.dylib compiled
3552 // without -fno-rtti.
3553#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003554 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003555#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003557 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558};
3559
3560template <class _Tp, class _Dp, class _Alloc>
3561class __shared_ptr_pointer
3562 : public __shared_weak_count
3563{
3564 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3565public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003568 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569
Howard Hinnant72f73582010-08-11 17:04:31 +00003570#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003571 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003572#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573
3574private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003575 virtual void __on_zero_shared() _NOEXCEPT;
3576 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577};
3578
Howard Hinnant72f73582010-08-11 17:04:31 +00003579#ifndef _LIBCPP_NO_RTTI
3580
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581template <class _Tp, class _Dp, class _Alloc>
3582const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003583__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003585 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586}
3587
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003588#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003589
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590template <class _Tp, class _Dp, class _Alloc>
3591void
Howard Hinnant719bda32011-05-28 14:41:13 +00003592__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593{
3594 __data_.first().second()(__data_.first().first());
3595 __data_.first().second().~_Dp();
3596}
3597
3598template <class _Tp, class _Dp, class _Alloc>
3599void
Howard Hinnant719bda32011-05-28 14:41:13 +00003600__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003602 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3603 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003604 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3605
Eric Fiselierf8898c82015-02-05 23:01:40 +00003606 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003608 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609}
3610
3611template <class _Tp, class _Alloc>
3612class __shared_ptr_emplace
3613 : public __shared_weak_count
3614{
3615 __compressed_pair<_Alloc, _Tp> __data_;
3616public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617
Howard Hinnant756c69b2010-09-22 16:48:34 +00003618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003620 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003622
3623#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003627 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3628 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629#else // _LIBCPP_HAS_NO_VARIADICS
3630
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3634 : __data_(__a, _Tp(__a0)) {}
3635
3636 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3639 : __data_(__a, _Tp(__a0, __a1)) {}
3640
3641 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3644 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3645
3646#endif // _LIBCPP_HAS_NO_VARIADICS
3647
3648private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003649 virtual void __on_zero_shared() _NOEXCEPT;
3650 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003652 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003653 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654};
3655
3656template <class _Tp, class _Alloc>
3657void
Howard Hinnant719bda32011-05-28 14:41:13 +00003658__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659{
3660 __data_.second().~_Tp();
3661}
3662
3663template <class _Tp, class _Alloc>
3664void
Howard Hinnant719bda32011-05-28 14:41:13 +00003665__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003667 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3668 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003669 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003670 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003672 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673}
3674
Erik Pilkington2a398762017-05-25 15:43:31 +00003675struct __shared_ptr_dummy_rebind_allocator_type;
3676template <>
3677class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3678{
3679public:
3680 template <class _Other>
3681 struct rebind
3682 {
3683 typedef allocator<_Other> other;
3684 };
3685};
3686
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003687template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688
3689template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003690class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003692public:
3693 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003694
Eric Fiselierae5b6672016-06-27 01:02:43 +00003695#if _LIBCPP_STD_VER > 14
3696 typedef weak_ptr<_Tp> weak_type;
3697#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698private:
3699 element_type* __ptr_;
3700 __shared_weak_count* __cntrl_;
3701
3702 struct __nat {int __for_bool_;};
3703public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003705 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003707 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003708 template<class _Yp>
3709 explicit shared_ptr(_Yp* __p,
3710 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3711 template<class _Yp, class _Dp>
3712 shared_ptr(_Yp* __p, _Dp __d,
3713 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3714 template<class _Yp, class _Dp, class _Alloc>
3715 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3716 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3718 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003719 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003721 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003725 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003726 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003727#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003729 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003730 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003731 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003732 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003733#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003735 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003736#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003737#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003738 template<class _Yp>
3739 shared_ptr(auto_ptr<_Yp>&& __r,
3740 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003741#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003742 template<class _Yp>
3743 shared_ptr(auto_ptr<_Yp> __r,
3744 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003746#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003747#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003748 template <class _Yp, class _Dp>
3749 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3750 typename enable_if
3751 <
3752 !is_lvalue_reference<_Dp>::value &&
3753 !is_array<_Yp>::value &&
3754 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3755 __nat
3756 >::type = __nat());
3757 template <class _Yp, class _Dp>
3758 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3759 typename enable_if
3760 <
3761 is_lvalue_reference<_Dp>::value &&
3762 !is_array<_Yp>::value &&
3763 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3764 __nat
3765 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003766#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003767 template <class _Yp, class _Dp>
3768 shared_ptr(unique_ptr<_Yp, _Dp>,
3769 typename enable_if
3770 <
3771 !is_lvalue_reference<_Dp>::value &&
3772 !is_array<_Yp>::value &&
3773 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3774 __nat
3775 >::type = __nat());
3776 template <class _Yp, class _Dp>
3777 shared_ptr(unique_ptr<_Yp, _Dp>,
3778 typename enable_if
3779 <
3780 is_lvalue_reference<_Dp>::value &&
3781 !is_array<_Yp>::value &&
3782 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3783 __nat
3784 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003785#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786
3787 ~shared_ptr();
3788
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003790 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003791 template<class _Yp>
3792 typename enable_if
3793 <
3794 is_convertible<_Yp*, element_type*>::value,
3795 shared_ptr&
3796 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003798 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003799#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003800 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003801 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003802 template<class _Yp>
3803 typename enable_if
3804 <
3805 is_convertible<_Yp*, element_type*>::value,
3806 shared_ptr<_Tp>&
3807 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003809 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003810#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003811 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003813 typename enable_if
3814 <
3815 !is_array<_Yp>::value &&
3816 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003817 shared_ptr
3818 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003819 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003820#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003821#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003822#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003823 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003825 typename enable_if
3826 <
3827 !is_array<_Yp>::value &&
3828 is_convertible<_Yp*, element_type*>::value,
3829 shared_ptr&
3830 >::type
3831 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003833#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003834 template <class _Yp, class _Dp>
3835 typename enable_if
3836 <
3837 !is_array<_Yp>::value &&
3838 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3839 shared_ptr&
3840 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003841#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003843 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003844#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003846 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847#endif
3848
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003850 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003852 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003853 template<class _Yp>
3854 typename enable_if
3855 <
3856 is_convertible<_Yp*, element_type*>::value,
3857 void
3858 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003860 reset(_Yp* __p);
3861 template<class _Yp, class _Dp>
3862 typename enable_if
3863 <
3864 is_convertible<_Yp*, element_type*>::value,
3865 void
3866 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003868 reset(_Yp* __p, _Dp __d);
3869 template<class _Yp, class _Dp, class _Alloc>
3870 typename enable_if
3871 <
3872 is_convertible<_Yp*, element_type*>::value,
3873 void
3874 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003876 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877
Howard Hinnant756c69b2010-09-22 16:48:34 +00003878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003879 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003881 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3882 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003884 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003886 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003888 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003890 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003891 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003892 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003893 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003895 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003896 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003897 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003899 _LIBCPP_INLINE_VISIBILITY
3900 bool
3901 __owner_equivalent(const shared_ptr& __p) const
3902 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903
Howard Hinnant72f73582010-08-11 17:04:31 +00003904#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003907 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003908 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003909 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003910 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003911#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912
Zoe Carverd9040c72019-10-22 15:16:49 +00003913 template<class _Yp, class _CntrlBlk>
3914 static shared_ptr<_Tp>
3915 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3916 {
3917 shared_ptr<_Tp> __r;
3918 __r.__ptr_ = __p;
3919 __r.__cntrl_ = __cntrl;
3920 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3921 return __r;
3922 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003923
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003925 template <class _Yp, bool = is_function<_Yp>::value>
3926 struct __shared_ptr_default_allocator
3927 {
3928 typedef allocator<_Yp> type;
3929 };
3930
3931 template <class _Yp>
3932 struct __shared_ptr_default_allocator<_Yp, true>
3933 {
3934 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3935 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003937 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003938 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003939 typename enable_if<is_convertible<_OrigPtr*,
3940 const enable_shared_from_this<_Yp>*
3941 >::value,
3942 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003943 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3944 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003946 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003947 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003948 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003949 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3950 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003951 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952 }
3953
Erik Pilkington2a398762017-05-25 15:43:31 +00003954 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003956 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3957 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958};
3959
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003960
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003962inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003963_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003964shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965 : __ptr_(0),
3966 __cntrl_(0)
3967{
3968}
3969
3970template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003971inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003972_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003973shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 : __ptr_(0),
3975 __cntrl_(0)
3976{
3977}
3978
3979template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003980template<class _Yp>
3981shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3982 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983 : __ptr_(__p)
3984{
3985 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003986 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3987 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3988 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003990 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991}
3992
3993template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003994template<class _Yp, class _Dp>
3995shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3996 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997 : __ptr_(__p)
3998{
3999#ifndef _LIBCPP_NO_EXCEPTIONS
4000 try
4001 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004002#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004003 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4004 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4005 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004006 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007#ifndef _LIBCPP_NO_EXCEPTIONS
4008 }
4009 catch (...)
4010 {
4011 __d(__p);
4012 throw;
4013 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004014#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015}
4016
4017template<class _Tp>
4018template<class _Dp>
4019shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4020 : __ptr_(0)
4021{
4022#ifndef _LIBCPP_NO_EXCEPTIONS
4023 try
4024 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004025#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004026 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4027 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4028 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029#ifndef _LIBCPP_NO_EXCEPTIONS
4030 }
4031 catch (...)
4032 {
4033 __d(__p);
4034 throw;
4035 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004036#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037}
4038
4039template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004040template<class _Yp, class _Dp, class _Alloc>
4041shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4042 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043 : __ptr_(__p)
4044{
4045#ifndef _LIBCPP_NO_EXCEPTIONS
4046 try
4047 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004048#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004050 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051 typedef __allocator_destructor<_A2> _D2;
4052 _A2 __a2(__a);
4053 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004054 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4055 _CntrlBlk(__p, __d, __a);
4056 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004057 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058#ifndef _LIBCPP_NO_EXCEPTIONS
4059 }
4060 catch (...)
4061 {
4062 __d(__p);
4063 throw;
4064 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004065#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066}
4067
4068template<class _Tp>
4069template<class _Dp, class _Alloc>
4070shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4071 : __ptr_(0)
4072{
4073#ifndef _LIBCPP_NO_EXCEPTIONS
4074 try
4075 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004076#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004078 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079 typedef __allocator_destructor<_A2> _D2;
4080 _A2 __a2(__a);
4081 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004082 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4083 _CntrlBlk(__p, __d, __a);
4084 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085#ifndef _LIBCPP_NO_EXCEPTIONS
4086 }
4087 catch (...)
4088 {
4089 __d(__p);
4090 throw;
4091 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004092#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093}
4094
4095template<class _Tp>
4096template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004097inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004098shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099 : __ptr_(__p),
4100 __cntrl_(__r.__cntrl_)
4101{
4102 if (__cntrl_)
4103 __cntrl_->__add_shared();
4104}
4105
4106template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004107inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004108shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109 : __ptr_(__r.__ptr_),
4110 __cntrl_(__r.__cntrl_)
4111{
4112 if (__cntrl_)
4113 __cntrl_->__add_shared();
4114}
4115
4116template<class _Tp>
4117template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004118inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004120 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004121 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122 : __ptr_(__r.__ptr_),
4123 __cntrl_(__r.__cntrl_)
4124{
4125 if (__cntrl_)
4126 __cntrl_->__add_shared();
4127}
4128
Howard Hinnant74279a52010-09-04 23:28:19 +00004129#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130
4131template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004132inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004133shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134 : __ptr_(__r.__ptr_),
4135 __cntrl_(__r.__cntrl_)
4136{
4137 __r.__ptr_ = 0;
4138 __r.__cntrl_ = 0;
4139}
4140
4141template<class _Tp>
4142template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004143inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004145 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004146 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147 : __ptr_(__r.__ptr_),
4148 __cntrl_(__r.__cntrl_)
4149{
4150 __r.__ptr_ = 0;
4151 __r.__cntrl_ = 0;
4152}
4153
Howard Hinnant74279a52010-09-04 23:28:19 +00004154#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155
Marshall Clowb22274f2017-01-24 22:22:33 +00004156#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004158template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004159#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004160shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004162shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004164 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165 : __ptr_(__r.get())
4166{
4167 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4168 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004169 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170 __r.release();
4171}
Marshall Clowb22274f2017-01-24 22:22:33 +00004172#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173
4174template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004175template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004176#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4178#else
4179shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4180#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004181 typename enable_if
4182 <
4183 !is_lvalue_reference<_Dp>::value &&
4184 !is_array<_Yp>::value &&
4185 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4186 __nat
4187 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188 : __ptr_(__r.get())
4189{
Marshall Clow35cde742015-05-10 13:59:45 +00004190#if _LIBCPP_STD_VER > 11
4191 if (__ptr_ == nullptr)
4192 __cntrl_ = nullptr;
4193 else
4194#endif
4195 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004196 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4197 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4198 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004199 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004200 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201 __r.release();
4202}
4203
4204template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004205template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004206#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4208#else
4209shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4210#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004211 typename enable_if
4212 <
4213 is_lvalue_reference<_Dp>::value &&
4214 !is_array<_Yp>::value &&
4215 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4216 __nat
4217 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218 : __ptr_(__r.get())
4219{
Marshall Clow35cde742015-05-10 13:59:45 +00004220#if _LIBCPP_STD_VER > 11
4221 if (__ptr_ == nullptr)
4222 __cntrl_ = nullptr;
4223 else
4224#endif
4225 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004226 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004227 typedef __shared_ptr_pointer<_Yp*,
4228 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004229 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004230 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004231 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004232 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233 __r.release();
4234}
4235
Zoe Carver6cd05c32019-08-19 15:47:16 +00004236template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237shared_ptr<_Tp>::~shared_ptr()
4238{
4239 if (__cntrl_)
4240 __cntrl_->__release_shared();
4241}
4242
4243template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004244inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004246shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
4248 shared_ptr(__r).swap(*this);
4249 return *this;
4250}
4251
4252template<class _Tp>
4253template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004254inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004255typename enable_if
4256<
Marshall Clow7e384b72017-01-10 16:59:33 +00004257 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004258 shared_ptr<_Tp>&
4259>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004260shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261{
4262 shared_ptr(__r).swap(*this);
4263 return *this;
4264}
4265
Howard Hinnant74279a52010-09-04 23:28:19 +00004266#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267
4268template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004269inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004271shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004273 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274 return *this;
4275}
4276
4277template<class _Tp>
4278template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004279inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004280typename enable_if
4281<
Marshall Clow7e384b72017-01-10 16:59:33 +00004282 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004283 shared_ptr<_Tp>&
4284>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4286{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004287 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288 return *this;
4289}
4290
Marshall Clowb22274f2017-01-24 22:22:33 +00004291#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292template<class _Tp>
4293template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004294inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004295typename enable_if
4296<
4297 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004298 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004299 shared_ptr<_Tp>
4300>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4302{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004303 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304 return *this;
4305}
Marshall Clowb22274f2017-01-24 22:22:33 +00004306#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307
4308template<class _Tp>
4309template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004310inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004311typename enable_if
4312<
4313 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004314 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004315 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004316 shared_ptr<_Tp>&
4317>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4319{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004320 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321 return *this;
4322}
4323
Howard Hinnant74279a52010-09-04 23:28:19 +00004324#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325
Marshall Clowb22274f2017-01-24 22:22:33 +00004326#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327template<class _Tp>
4328template<class _Yp>
4329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004330typename enable_if
4331<
4332 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004333 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004334 shared_ptr<_Tp>&
4335>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004336shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337{
4338 shared_ptr(__r).swap(*this);
4339 return *this;
4340}
Marshall Clowb22274f2017-01-24 22:22:33 +00004341#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342
4343template<class _Tp>
4344template <class _Yp, class _Dp>
4345inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004346typename enable_if
4347<
4348 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004349 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004350 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004351 shared_ptr<_Tp>&
4352>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4354{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004355 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356 return *this;
4357}
4358
Howard Hinnant74279a52010-09-04 23:28:19 +00004359#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360
4361template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004362inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363void
Howard Hinnant719bda32011-05-28 14:41:13 +00004364shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004366 _VSTD::swap(__ptr_, __r.__ptr_);
4367 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368}
4369
4370template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004371inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372void
Howard Hinnant719bda32011-05-28 14:41:13 +00004373shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374{
4375 shared_ptr().swap(*this);
4376}
4377
4378template<class _Tp>
4379template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004380inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004381typename enable_if
4382<
Marshall Clow7e384b72017-01-10 16:59:33 +00004383 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004384 void
4385>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386shared_ptr<_Tp>::reset(_Yp* __p)
4387{
4388 shared_ptr(__p).swap(*this);
4389}
4390
4391template<class _Tp>
4392template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004393inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004394typename enable_if
4395<
Marshall Clow7e384b72017-01-10 16:59:33 +00004396 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004397 void
4398>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004399shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4400{
4401 shared_ptr(__p, __d).swap(*this);
4402}
4403
4404template<class _Tp>
4405template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004406inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004407typename enable_if
4408<
Marshall Clow7e384b72017-01-10 16:59:33 +00004409 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004410 void
4411>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4413{
4414 shared_ptr(__p, __d, __a).swap(*this);
4415}
4416
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004417template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004419typename enable_if
4420<
4421 !is_array<_Tp>::value,
4422 shared_ptr<_Tp>
4423>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424make_shared(_Args&& ...__args)
4425{
Zoe Carverd9040c72019-10-22 15:16:49 +00004426 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4427 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4428 typedef allocator<_CntrlBlk> _A2;
4429 typedef __allocator_destructor<_A2> _D2;
4430
4431 _A2 __a2;
4432 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4433 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4434
4435 _Tp *__ptr = __hold2.get()->get();
4436 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437}
4438
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004439template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004441typename enable_if
4442<
4443 !is_array<_Tp>::value,
4444 shared_ptr<_Tp>
4445>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446allocate_shared(const _Alloc& __a, _Args&& ...__args)
4447{
zoecarver505730a2020-02-25 16:50:57 -08004448 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4449
4450 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4451 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4452 typedef __allocator_destructor<_A2> _D2;
4453
4454 _A2 __a2(__a);
4455 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4456 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4457 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4458
4459 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4460 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461}
4462
Howard Hinnantc51e1022010-05-11 19:42:16 +00004463template<class _Tp, class _Up>
4464inline _LIBCPP_INLINE_VISIBILITY
4465bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004466operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467{
4468 return __x.get() == __y.get();
4469}
4470
4471template<class _Tp, class _Up>
4472inline _LIBCPP_INLINE_VISIBILITY
4473bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004474operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475{
4476 return !(__x == __y);
4477}
4478
4479template<class _Tp, class _Up>
4480inline _LIBCPP_INLINE_VISIBILITY
4481bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004482operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004484#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004485 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4486 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004487#else
4488 return less<>()(__x.get(), __y.get());
4489#endif
4490
Howard Hinnantb17caf92012-02-21 21:02:58 +00004491}
4492
4493template<class _Tp, class _Up>
4494inline _LIBCPP_INLINE_VISIBILITY
4495bool
4496operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4497{
4498 return __y < __x;
4499}
4500
4501template<class _Tp, class _Up>
4502inline _LIBCPP_INLINE_VISIBILITY
4503bool
4504operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4505{
4506 return !(__y < __x);
4507}
4508
4509template<class _Tp, class _Up>
4510inline _LIBCPP_INLINE_VISIBILITY
4511bool
4512operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4513{
4514 return !(__x < __y);
4515}
4516
4517template<class _Tp>
4518inline _LIBCPP_INLINE_VISIBILITY
4519bool
4520operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4521{
4522 return !__x;
4523}
4524
4525template<class _Tp>
4526inline _LIBCPP_INLINE_VISIBILITY
4527bool
4528operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4529{
4530 return !__x;
4531}
4532
4533template<class _Tp>
4534inline _LIBCPP_INLINE_VISIBILITY
4535bool
4536operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4537{
4538 return static_cast<bool>(__x);
4539}
4540
4541template<class _Tp>
4542inline _LIBCPP_INLINE_VISIBILITY
4543bool
4544operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4545{
4546 return static_cast<bool>(__x);
4547}
4548
4549template<class _Tp>
4550inline _LIBCPP_INLINE_VISIBILITY
4551bool
4552operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4553{
4554 return less<_Tp*>()(__x.get(), nullptr);
4555}
4556
4557template<class _Tp>
4558inline _LIBCPP_INLINE_VISIBILITY
4559bool
4560operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4561{
4562 return less<_Tp*>()(nullptr, __x.get());
4563}
4564
4565template<class _Tp>
4566inline _LIBCPP_INLINE_VISIBILITY
4567bool
4568operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4569{
4570 return nullptr < __x;
4571}
4572
4573template<class _Tp>
4574inline _LIBCPP_INLINE_VISIBILITY
4575bool
4576operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4577{
4578 return __x < nullptr;
4579}
4580
4581template<class _Tp>
4582inline _LIBCPP_INLINE_VISIBILITY
4583bool
4584operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4585{
4586 return !(nullptr < __x);
4587}
4588
4589template<class _Tp>
4590inline _LIBCPP_INLINE_VISIBILITY
4591bool
4592operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4593{
4594 return !(__x < nullptr);
4595}
4596
4597template<class _Tp>
4598inline _LIBCPP_INLINE_VISIBILITY
4599bool
4600operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4601{
4602 return !(__x < nullptr);
4603}
4604
4605template<class _Tp>
4606inline _LIBCPP_INLINE_VISIBILITY
4607bool
4608operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4609{
4610 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611}
4612
4613template<class _Tp>
4614inline _LIBCPP_INLINE_VISIBILITY
4615void
Howard Hinnant719bda32011-05-28 14:41:13 +00004616swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004617{
4618 __x.swap(__y);
4619}
4620
4621template<class _Tp, class _Up>
4622inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004623typename enable_if
4624<
4625 !is_array<_Tp>::value && !is_array<_Up>::value,
4626 shared_ptr<_Tp>
4627>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004628static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004629{
4630 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4631}
4632
4633template<class _Tp, class _Up>
4634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004635typename enable_if
4636<
4637 !is_array<_Tp>::value && !is_array<_Up>::value,
4638 shared_ptr<_Tp>
4639>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004640dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641{
4642 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4643 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4644}
4645
4646template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004647typename enable_if
4648<
4649 is_array<_Tp>::value == is_array<_Up>::value,
4650 shared_ptr<_Tp>
4651>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004652const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004654 typedef typename remove_extent<_Tp>::type _RTp;
4655 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656}
4657
Howard Hinnant72f73582010-08-11 17:04:31 +00004658#ifndef _LIBCPP_NO_RTTI
4659
Howard Hinnantc51e1022010-05-11 19:42:16 +00004660template<class _Dp, class _Tp>
4661inline _LIBCPP_INLINE_VISIBILITY
4662_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004663get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664{
4665 return __p.template __get_deleter<_Dp>();
4666}
4667
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004668#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004669
Howard Hinnantc51e1022010-05-11 19:42:16 +00004670template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004671class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004672{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004673public:
4674 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004675private:
4676 element_type* __ptr_;
4677 __shared_weak_count* __cntrl_;
4678
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004679public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004681 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004682 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004683 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4684 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004686 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004687 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004688 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4689 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004690
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004691#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004693 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004694 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004695 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4696 _NOEXCEPT;
4697#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004698 ~weak_ptr();
4699
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004701 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004702 template<class _Yp>
4703 typename enable_if
4704 <
4705 is_convertible<_Yp*, element_type*>::value,
4706 weak_ptr&
4707 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004709 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4710
4711#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4712
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004714 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4715 template<class _Yp>
4716 typename enable_if
4717 <
4718 is_convertible<_Yp*, element_type*>::value,
4719 weak_ptr&
4720 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004722 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4723
4724#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4725
4726 template<class _Yp>
4727 typename enable_if
4728 <
4729 is_convertible<_Yp*, element_type*>::value,
4730 weak_ptr&
4731 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004733 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004734
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004736 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004738 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004739
Howard Hinnant756c69b2010-09-22 16:48:34 +00004740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004741 long use_count() const _NOEXCEPT
4742 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004744 bool expired() const _NOEXCEPT
4745 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4746 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004747 template<class _Up>
4748 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004749 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004750 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004751 template<class _Up>
4752 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004753 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004754 {return __cntrl_ < __r.__cntrl_;}
4755
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004756 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4757 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004758};
4759
4760template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004761inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004762_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004763weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004764 : __ptr_(0),
4765 __cntrl_(0)
4766{
4767}
4768
4769template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004770inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004771weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004772 : __ptr_(__r.__ptr_),
4773 __cntrl_(__r.__cntrl_)
4774{
4775 if (__cntrl_)
4776 __cntrl_->__add_weak();
4777}
4778
4779template<class _Tp>
4780template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004781inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004782weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004783 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004784 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004785 : __ptr_(__r.__ptr_),
4786 __cntrl_(__r.__cntrl_)
4787{
4788 if (__cntrl_)
4789 __cntrl_->__add_weak();
4790}
4791
4792template<class _Tp>
4793template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004794inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004795weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004796 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004797 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004798 : __ptr_(__r.__ptr_),
4799 __cntrl_(__r.__cntrl_)
4800{
4801 if (__cntrl_)
4802 __cntrl_->__add_weak();
4803}
4804
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4806
4807template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004808inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004809weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4810 : __ptr_(__r.__ptr_),
4811 __cntrl_(__r.__cntrl_)
4812{
4813 __r.__ptr_ = 0;
4814 __r.__cntrl_ = 0;
4815}
4816
4817template<class _Tp>
4818template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004819inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004820weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4821 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4822 _NOEXCEPT
4823 : __ptr_(__r.__ptr_),
4824 __cntrl_(__r.__cntrl_)
4825{
4826 __r.__ptr_ = 0;
4827 __r.__cntrl_ = 0;
4828}
4829
4830#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4831
Howard Hinnantc51e1022010-05-11 19:42:16 +00004832template<class _Tp>
4833weak_ptr<_Tp>::~weak_ptr()
4834{
4835 if (__cntrl_)
4836 __cntrl_->__release_weak();
4837}
4838
4839template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004840inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004841weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004842weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004843{
4844 weak_ptr(__r).swap(*this);
4845 return *this;
4846}
4847
4848template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004849template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004850inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004851typename enable_if
4852<
4853 is_convertible<_Yp*, _Tp*>::value,
4854 weak_ptr<_Tp>&
4855>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004856weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004857{
4858 weak_ptr(__r).swap(*this);
4859 return *this;
4860}
4861
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004862#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4863
4864template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004865inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004866weak_ptr<_Tp>&
4867weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4868{
4869 weak_ptr(_VSTD::move(__r)).swap(*this);
4870 return *this;
4871}
4872
Howard Hinnantc51e1022010-05-11 19:42:16 +00004873template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004874template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004875inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004876typename enable_if
4877<
4878 is_convertible<_Yp*, _Tp*>::value,
4879 weak_ptr<_Tp>&
4880>::type
4881weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4882{
4883 weak_ptr(_VSTD::move(__r)).swap(*this);
4884 return *this;
4885}
4886
4887#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4888
4889template<class _Tp>
4890template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004891inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004892typename enable_if
4893<
4894 is_convertible<_Yp*, _Tp*>::value,
4895 weak_ptr<_Tp>&
4896>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004897weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004898{
4899 weak_ptr(__r).swap(*this);
4900 return *this;
4901}
4902
4903template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004904inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004905void
Howard Hinnant719bda32011-05-28 14:41:13 +00004906weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004907{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004908 _VSTD::swap(__ptr_, __r.__ptr_);
4909 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004910}
4911
4912template<class _Tp>
4913inline _LIBCPP_INLINE_VISIBILITY
4914void
Howard Hinnant719bda32011-05-28 14:41:13 +00004915swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916{
4917 __x.swap(__y);
4918}
4919
4920template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004921inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922void
Howard Hinnant719bda32011-05-28 14:41:13 +00004923weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924{
4925 weak_ptr().swap(*this);
4926}
4927
4928template<class _Tp>
4929template<class _Yp>
4930shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004931 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004932 : __ptr_(__r.__ptr_),
4933 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4934{
4935 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004936 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004937}
4938
4939template<class _Tp>
4940shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004941weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004942{
4943 shared_ptr<_Tp> __r;
4944 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4945 if (__r.__cntrl_)
4946 __r.__ptr_ = __ptr_;
4947 return __r;
4948}
4949
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004950#if _LIBCPP_STD_VER > 14
4951template <class _Tp = void> struct owner_less;
4952#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004953template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004954#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004955
4956template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004957struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004958 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004959{
4960 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004961 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004962 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004964 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004965 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004966 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004967 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004968 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004970};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004971
4972template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004973struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004974 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4975{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004976 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004977 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004978 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004979 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004980 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004981 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004983 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004984 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985 {return __x.owner_before(__y);}
4986};
4987
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004988#if _LIBCPP_STD_VER > 14
4989template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004990struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004991{
4992 template <class _Tp, class _Up>
4993 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004994 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004995 {return __x.owner_before(__y);}
4996 template <class _Tp, class _Up>
4997 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004998 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004999 {return __x.owner_before(__y);}
5000 template <class _Tp, class _Up>
5001 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005002 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005003 {return __x.owner_before(__y);}
5004 template <class _Tp, class _Up>
5005 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005006 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005007 {return __x.owner_before(__y);}
5008 typedef void is_transparent;
5009};
5010#endif
5011
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005013class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005014{
5015 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005016protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005017 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005018 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005020 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005022 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5023 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005025 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005026public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005028 shared_ptr<_Tp> shared_from_this()
5029 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005031 shared_ptr<_Tp const> shared_from_this() const
5032 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005033
Eric Fiselier84006862016-06-02 00:15:35 +00005034#if _LIBCPP_STD_VER > 14
5035 _LIBCPP_INLINE_VISIBILITY
5036 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5037 { return __weak_this_; }
5038
5039 _LIBCPP_INLINE_VISIBILITY
5040 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5041 { return __weak_this_; }
5042#endif // _LIBCPP_STD_VER > 14
5043
Howard Hinnantc51e1022010-05-11 19:42:16 +00005044 template <class _Up> friend class shared_ptr;
5045};
5046
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005047template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005048struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005049{
5050 typedef shared_ptr<_Tp> argument_type;
5051 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005052
Howard Hinnant756c69b2010-09-22 16:48:34 +00005053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005054 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005055 {
5056 return hash<_Tp*>()(__ptr.get());
5057 }
5058};
5059
Howard Hinnantc834c512011-11-29 18:15:50 +00005060template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005061inline _LIBCPP_INLINE_VISIBILITY
5062basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005063operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005064
Eric Fiselier9b492672016-06-18 02:12:53 +00005065
5066#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005067
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005068class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005069{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005070 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005071public:
5072 void lock() _NOEXCEPT;
5073 void unlock() _NOEXCEPT;
5074
5075private:
5076 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5077 __sp_mut(const __sp_mut&);
5078 __sp_mut& operator=(const __sp_mut&);
5079
Howard Hinnant8331b762013-03-06 23:30:19 +00005080 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005081};
5082
Mehdi Amini228053d2017-05-04 17:08:54 +00005083_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5084__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005085
5086template <class _Tp>
5087inline _LIBCPP_INLINE_VISIBILITY
5088bool
5089atomic_is_lock_free(const shared_ptr<_Tp>*)
5090{
5091 return false;
5092}
5093
5094template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005095_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005096shared_ptr<_Tp>
5097atomic_load(const shared_ptr<_Tp>* __p)
5098{
5099 __sp_mut& __m = __get_sp_mut(__p);
5100 __m.lock();
5101 shared_ptr<_Tp> __q = *__p;
5102 __m.unlock();
5103 return __q;
5104}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005105
Howard Hinnant9fa30202012-07-30 01:40:57 +00005106template <class _Tp>
5107inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005108_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005109shared_ptr<_Tp>
5110atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5111{
5112 return atomic_load(__p);
5113}
5114
5115template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005116_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005117void
5118atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5119{
5120 __sp_mut& __m = __get_sp_mut(__p);
5121 __m.lock();
5122 __p->swap(__r);
5123 __m.unlock();
5124}
5125
5126template <class _Tp>
5127inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005128_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005129void
5130atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5131{
5132 atomic_store(__p, __r);
5133}
5134
5135template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005136_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005137shared_ptr<_Tp>
5138atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5139{
5140 __sp_mut& __m = __get_sp_mut(__p);
5141 __m.lock();
5142 __p->swap(__r);
5143 __m.unlock();
5144 return __r;
5145}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005146
Howard Hinnant9fa30202012-07-30 01:40:57 +00005147template <class _Tp>
5148inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005149_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005150shared_ptr<_Tp>
5151atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5152{
5153 return atomic_exchange(__p, __r);
5154}
5155
5156template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005157_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005158bool
5159atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5160{
Marshall Clow4201ee82016-05-18 17:50:13 +00005161 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005162 __sp_mut& __m = __get_sp_mut(__p);
5163 __m.lock();
5164 if (__p->__owner_equivalent(*__v))
5165 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005166 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005167 *__p = __w;
5168 __m.unlock();
5169 return true;
5170 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005171 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005172 *__v = *__p;
5173 __m.unlock();
5174 return false;
5175}
5176
5177template <class _Tp>
5178inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005179_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005180bool
5181atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5182{
5183 return atomic_compare_exchange_strong(__p, __v, __w);
5184}
5185
5186template <class _Tp>
5187inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005188_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005189bool
5190atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5191 shared_ptr<_Tp> __w, memory_order, memory_order)
5192{
5193 return atomic_compare_exchange_strong(__p, __v, __w);
5194}
5195
5196template <class _Tp>
5197inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005198_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005199bool
5200atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5201 shared_ptr<_Tp> __w, memory_order, memory_order)
5202{
5203 return atomic_compare_exchange_weak(__p, __v, __w);
5204}
5205
Eric Fiselier9b492672016-06-18 02:12:53 +00005206#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005207
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005208//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005209#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5210# ifndef _LIBCPP_CXX03_LANG
5211enum class pointer_safety : unsigned char {
5212 relaxed,
5213 preferred,
5214 strict
5215};
5216# endif
5217#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005218struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005219{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005220 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005221 {
5222 relaxed,
5223 preferred,
5224 strict
5225 };
5226
Howard Hinnant49e145e2012-10-30 19:06:59 +00005227 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005228
Howard Hinnant756c69b2010-09-22 16:48:34 +00005229 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005230 pointer_safety() : __v_() {}
5231
5232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005233 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005235 operator int() const {return __v_;}
5236};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005237#endif
5238
5239#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005240 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005241_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5242#else
5243// This function is only offered in C++03 under ABI v1.
5244# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5245inline _LIBCPP_INLINE_VISIBILITY
5246pointer_safety get_pointer_safety() _NOEXCEPT {
5247 return pointer_safety::relaxed;
5248}
5249# endif
5250#endif
5251
Howard Hinnantc51e1022010-05-11 19:42:16 +00005252
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005253_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5254_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5255_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005256_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005257
5258template <class _Tp>
5259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005260_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005261undeclare_reachable(_Tp* __p)
5262{
5263 return static_cast<_Tp*>(__undeclare_reachable(__p));
5264}
5265
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005266_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005267
Marshall Clow8982dcd2015-07-13 20:04:56 +00005268// --- Helper for container swap --
5269template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005270inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005271void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5272#if _LIBCPP_STD_VER >= 14
5273 _NOEXCEPT
5274#else
5275 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5276#endif
5277{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005278 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005279 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5280}
5281
5282template <typename _Alloc>
5283_LIBCPP_INLINE_VISIBILITY
5284void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5285#if _LIBCPP_STD_VER >= 14
5286 _NOEXCEPT
5287#else
5288 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5289#endif
5290{
5291 using _VSTD::swap;
5292 swap(__a1, __a2);
5293}
5294
5295template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005296inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005297void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5298
Marshall Clowff91de82015-08-18 19:51:37 +00005299template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005300struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005301 _Traits::propagate_on_container_move_assignment::value
5302#if _LIBCPP_STD_VER > 14
5303 || _Traits::is_always_equal::value
5304#else
5305 && is_nothrow_move_assignable<_Alloc>::value
5306#endif
5307 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005308
Marshall Clowa591b9a2016-07-11 21:38:08 +00005309
5310#ifndef _LIBCPP_HAS_NO_VARIADICS
5311template <class _Tp, class _Alloc>
5312struct __temp_value {
5313 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005314
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005315 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005316 _Alloc &__a;
5317
5318 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5319 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005320
Marshall Clowa591b9a2016-07-11 21:38:08 +00005321 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005322 _LIBCPP_NO_CFI
5323 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5324 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5325 _VSTD::forward<_Args>(__args)...);
5326 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005327
Marshall Clowa591b9a2016-07-11 21:38:08 +00005328 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5329 };
5330#endif
5331
Marshall Clowe46031a2018-07-02 18:41:15 +00005332template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005333struct __is_allocator : false_type {};
5334
5335template<typename _Alloc>
5336struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005337 typename __void_t<typename _Alloc::value_type>::type,
5338 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5339 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005340 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005341
Eric Fiselier74ebee62019-06-08 01:31:19 +00005342// __builtin_new_allocator -- A non-templated helper for allocating and
5343// deallocating memory using __builtin_operator_new and
5344// __builtin_operator_delete. It should be used in preference to
5345// `std::allocator<T>` to avoid additional instantiations.
5346struct __builtin_new_allocator {
5347 struct __builtin_new_deleter {
5348 typedef void* pointer_type;
5349
5350 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5351 : __size_(__size), __align_(__align) {}
5352
5353 void operator()(void* p) const _NOEXCEPT {
5354 std::__libcpp_deallocate(p, __size_, __align_);
5355 }
5356
5357 private:
5358 size_t __size_;
5359 size_t __align_;
5360 };
5361
5362 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5363
5364 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5365 return __holder_t(std::__libcpp_allocate(__s, __align),
5366 __builtin_new_deleter(__s, __align));
5367 }
5368
5369 static void __deallocate_bytes(void* __p, size_t __s,
5370 size_t __align) _NOEXCEPT {
5371 std::__libcpp_deallocate(__p, __s, __align);
5372 }
5373
5374 template <class _Tp>
5375 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5376 static __holder_t __allocate_type(size_t __n) {
5377 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5378 }
5379
5380 template <class _Tp>
5381 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5382 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5383 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5384 }
5385};
5386
5387
Howard Hinnantc51e1022010-05-11 19:42:16 +00005388_LIBCPP_END_NAMESPACE_STD
5389
Eric Fiselierf4433a32017-05-31 22:07:49 +00005390_LIBCPP_POP_MACROS
5391
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005392#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005393# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005394#endif
5395
Howard Hinnantc51e1022010-05-11 19:42:16 +00005396#endif // _LIBCPP_MEMORY