blob: bea55d65fb9993241a8f956706ac39dedfd1c082 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
50
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Marshall Clow0e58cae2017-11-26 02:55:38 +000086 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant719bda32011-05-28 14:41:13 +000089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args);
93
94 template <class T>
95 static void destroy(allocator_type& a, T* p);
96
Marshall Clow4f834b52013-08-27 20:22:15 +000097 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 static allocator_type
100 select_on_container_copy_construction(const allocator_type& a);
101};
102
103template <>
Michael Park99f9e912020-03-04 11:27:14 -0500104class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105{
106public:
107 typedef void* pointer;
108 typedef const void* const_pointer;
109 typedef void value_type;
110
111 template <class _Up> struct rebind {typedef allocator<_Up> other;};
112};
113
114template <class T>
115class allocator
116{
117public:
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef size_t size_type; // deprecated in C++17, removed in C++20
119 typedef ptrdiff_t difference_type; // deprecated in C++17, removed in C++20
120 typedef T* pointer; // deprecated in C++17, removed in C++20
121 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<T>::type
123 reference; // deprecated in C++17, removed in C++20
124 typedef typename add_lvalue_reference<const T>::type
125 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Michael Park99f9e912020-03-04 11:27:14 -0500127 typedef T value_type;
128
129 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
130
131 typedef true_type propagate_on_container_move_assignment;
132 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Marshall Clowbc759762018-03-20 23:02:53 +0000134 constexpr allocator() noexcept; // constexpr in C++20
135 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
136 template <class U>
137 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000138 ~allocator();
Michael Park99f9e912020-03-04 11:27:14 -0500139 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
140 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
141 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
142 T* allocate(size_t n);
143 void deallocate(T* p, size_t n) noexcept;
144 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000147 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500148 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149};
150
151template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000152bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
154template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000155bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
157template <class OutputIterator, class T>
158class raw_storage_iterator
159 : public iterator<output_iterator_tag,
160 T, // purposefully not C++03
161 ptrdiff_t, // purposefully not C++03
162 T*, // purposefully not C++03
163 raw_storage_iterator&> // purposefully not C++03
164{
165public:
166 explicit raw_storage_iterator(OutputIterator x);
167 raw_storage_iterator& operator*();
168 raw_storage_iterator& operator=(const T& element);
169 raw_storage_iterator& operator++();
170 raw_storage_iterator operator++(int);
171};
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
174template <class T> void return_temporary_buffer(T* p) noexcept;
175
176template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000177template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179template <class InputIterator, class ForwardIterator>
180ForwardIterator
181uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
182
Howard Hinnant719bda32011-05-28 14:41:13 +0000183template <class InputIterator, class Size, class ForwardIterator>
184ForwardIterator
185uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
186
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187template <class ForwardIterator, class T>
188void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
189
190template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000191ForwardIterator
192uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000194template <class T>
195void destroy_at(T* location);
196
197template <class ForwardIterator>
198 void destroy(ForwardIterator first, ForwardIterator last);
199
200template <class ForwardIterator, class Size>
201 ForwardIterator destroy_n(ForwardIterator first, Size n);
202
203template <class InputIterator, class ForwardIterator>
204 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
205
206template <class InputIterator, class Size, class ForwardIterator>
207 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
208
209template <class ForwardIterator>
210 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
214
215template <class ForwardIterator>
216 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
217
218template <class ForwardIterator, class Size>
219 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
220
Louis Dionne481a2662018-09-23 18:35:00 +0000221template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222
223template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000224class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225{
226public:
227 typedef X element_type;
228
229 explicit auto_ptr(X* p =0) throw();
230 auto_ptr(auto_ptr&) throw();
231 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
232 auto_ptr& operator=(auto_ptr&) throw();
233 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
234 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
235 ~auto_ptr() throw();
236
237 typename add_lvalue_reference<X>::type operator*() const throw();
238 X* operator->() const throw();
239 X* get() const throw();
240 X* release() throw();
241 void reset(X* p =0) throw();
242
243 auto_ptr(auto_ptr_ref<X>) throw();
244 template<class Y> operator auto_ptr_ref<Y>() throw();
245 template<class Y> operator auto_ptr<Y>() throw();
246};
247
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248template <class T>
249struct default_delete
250{
Howard Hinnant719bda32011-05-28 14:41:13 +0000251 constexpr default_delete() noexcept = default;
252 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000253
Howard Hinnant719bda32011-05-28 14:41:13 +0000254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255};
256
257template <class T>
258struct default_delete<T[]>
259{
Howard Hinnant719bda32011-05-28 14:41:13 +0000260 constexpr default_delete() noexcept = default;
261 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000262 template <class U> void operator()(U*) const = delete;
263};
264
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000265template <class T, class D = default_delete<T>>
266class unique_ptr
267{
268public:
269 typedef see below pointer;
270 typedef T element_type;
271 typedef D deleter_type;
272
273 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 constexpr unique_ptr() noexcept;
275 explicit unique_ptr(pointer p) noexcept;
276 unique_ptr(pointer p, see below d1) noexcept;
277 unique_ptr(pointer p, see below d2) noexcept;
278 unique_ptr(unique_ptr&& u) noexcept;
279 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000280 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000281 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000283 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000284
285 // destructor
286 ~unique_ptr();
287
288 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 unique_ptr& operator=(unique_ptr&& u) noexcept;
290 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
291 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000292
293 // observers
294 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer operator->() const noexcept;
296 pointer get() const noexcept;
297 deleter_type& get_deleter() noexcept;
298 const deleter_type& get_deleter() const noexcept;
299 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000300
301 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000302 pointer release() noexcept;
303 void reset(pointer p = pointer()) noexcept;
304 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000305};
306
307template <class T, class D>
308class unique_ptr<T[], D>
309{
310public:
311 typedef implementation-defined pointer;
312 typedef T element_type;
313 typedef D deleter_type;
314
315 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000316 constexpr unique_ptr() noexcept;
317 explicit unique_ptr(pointer p) noexcept;
318 unique_ptr(pointer p, see below d) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(unique_ptr&& u) noexcept;
321 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000324 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000325
326 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000327 unique_ptr& operator=(unique_ptr&& u) noexcept;
328 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // observers
331 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 pointer get() const noexcept;
333 deleter_type& get_deleter() noexcept;
334 const deleter_type& get_deleter() const noexcept;
335 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336
337 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000338 pointer release() noexcept;
339 void reset(pointer p = pointer()) noexcept;
340 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000341 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000342 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000343};
344
345template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000346 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000347
348template <class T1, class D1, class T2, class D2>
349 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354template <class T1, class D1, class T2, class D2>
355 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
356template <class T1, class D1, class T2, class D2>
357 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
358template <class T1, class D1, class T2, class D2>
359 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
360
Howard Hinnant719bda32011-05-28 14:41:13 +0000361template <class T, class D>
362 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
363template <class T, class D>
364 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
365template <class T, class D>
366 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
367template <class T, class D>
368 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
369
370template <class T, class D>
371 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
372template <class T, class D>
373 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
374template <class T, class D>
375 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
376template <class T, class D>
377 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
378template <class T, class D>
379 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
380template <class T, class D>
381 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
382template <class T, class D>
383 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
384template <class T, class D>
385 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387class bad_weak_ptr
388 : public std::exception
389{
Howard Hinnant719bda32011-05-28 14:41:13 +0000390 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000391};
392
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000393template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
394template<class T> unique_ptr<T> make_unique(size_t n); // C++14
395template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
396
Marshall Clowe52a3242017-11-27 15:51:36 +0000397template<class E, class T, class Y, class D>
398 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
399
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000400template<class T>
401class shared_ptr
402{
403public:
404 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000405 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406
407 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000408 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(Y* p);
410 template<class Y, class D> shared_ptr(Y* p, D d);
411 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
412 template <class D> shared_ptr(nullptr_t p, D d);
413 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
415 shared_ptr(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
417 shared_ptr(shared_ptr&& r) noexcept;
418 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000419 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000420 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
422 shared_ptr(nullptr_t) : shared_ptr() { }
423
424 // destructor:
425 ~shared_ptr();
426
427 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000428 shared_ptr& operator=(const shared_ptr& r) noexcept;
429 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
430 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000432 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000433 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
434
435 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000436 void swap(shared_ptr& r) noexcept;
437 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438 template<class Y> void reset(Y* p);
439 template<class Y, class D> void reset(Y* p, D d);
440 template<class Y, class D, class A> void reset(Y* p, D d, A a);
441
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 // observers:
443 T* get() const noexcept;
444 T& operator*() const noexcept;
445 T* operator->() const noexcept;
446 long use_count() const noexcept;
447 bool unique() const noexcept;
448 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000449 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
450 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451};
452
453// shared_ptr comparisons:
454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000457 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000459 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000460template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000461 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000462template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000463 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000464template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000465 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
466
467template <class T>
468 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
469template <class T>
470 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
471template <class T>
472 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
473template <class T>
474 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
475template <class T>
476 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
477template <class T>
478bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
479template <class T>
480 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
481template <class T>
482 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
483template <class T>
484 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
485template <class T>
486 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
487template <class T>
488 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
489template <class T>
490 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491
492// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000493template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000494
495// shared_ptr casts:
496template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000497 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000498template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000499 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000501 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503// shared_ptr I/O:
504template<class E, class T, class Y>
505 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
506
507// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000508template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000509
510template<class T, class... Args>
511 shared_ptr<T> make_shared(Args&&... args);
512template<class T, class A, class... Args>
513 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
514
515template<class T>
516class weak_ptr
517{
518public:
519 typedef T element_type;
520
521 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000522 constexpr weak_ptr() noexcept;
523 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
524 weak_ptr(weak_ptr const& r) noexcept;
525 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000526 weak_ptr(weak_ptr&& r) noexcept; // C++14
527 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // destructor
530 ~weak_ptr();
531
532 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 weak_ptr& operator=(weak_ptr const& r) noexcept;
534 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
535 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000536 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
537 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000538
539 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000540 void swap(weak_ptr& r) noexcept;
541 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542
543 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000544 long use_count() const noexcept;
545 bool expired() const noexcept;
546 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000547 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
548 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000549};
550
551// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000552template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000553
554// class owner_less:
555template<class T> struct owner_less;
556
557template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000558struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
560{
561 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000562 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
563 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
564 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000565};
566
567template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000568struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000569 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
570{
571 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000572 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
574 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
575};
576
577template <> // Added in C++14
578struct owner_less<void>
579{
580 template <class _Tp, class _Up>
581 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
582 template <class _Tp, class _Up>
583 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
584 template <class _Tp, class _Up>
585 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
586 template <class _Tp, class _Up>
587 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
588
589 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000590};
591
592template<class T>
593class enable_shared_from_this
594{
595protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000596 constexpr enable_shared_from_this() noexcept;
597 enable_shared_from_this(enable_shared_from_this const&) noexcept;
598 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599 ~enable_shared_from_this();
600public:
601 shared_ptr<T> shared_from_this();
602 shared_ptr<T const> shared_from_this() const;
603};
604
605template<class T>
606 bool atomic_is_lock_free(const shared_ptr<T>* p);
607template<class T>
608 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
609template<class T>
610 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
611template<class T>
612 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
613template<class T>
614 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
615template<class T>
616 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
617template<class T>
618 shared_ptr<T>
619 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
620template<class T>
621 bool
622 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
623template<class T>
624 bool
625 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
626template<class T>
627 bool
628 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
629 shared_ptr<T> w, memory_order success,
630 memory_order failure);
631template<class T>
632 bool
633 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
634 shared_ptr<T> w, memory_order success,
635 memory_order failure);
636// Hash support
637template <class T> struct hash;
638template <class T, class D> struct hash<unique_ptr<T, D> >;
639template <class T> struct hash<shared_ptr<T> >;
640
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000641template <class T, class Alloc>
642 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
643
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000644// Pointer safety
645enum class pointer_safety { relaxed, preferred, strict };
646void declare_reachable(void *p);
647template <class T> T *undeclare_reachable(T *p);
648void declare_no_pointers(char *p, size_t n);
649void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000650pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000651
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
653
654} // std
655
656*/
657
658#include <__config>
659#include <type_traits>
660#include <typeinfo>
661#include <cstddef>
662#include <cstdint>
663#include <new>
664#include <utility>
665#include <limits>
666#include <iterator>
667#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000668#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000669#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000670#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000671#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000672#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000673# include <atomic>
674#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000675#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000676
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000677#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000679#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680
Eric Fiselierf4433a32017-05-31 22:07:49 +0000681_LIBCPP_PUSH_MACROS
682#include <__undef_macros>
683
684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685_LIBCPP_BEGIN_NAMESPACE_STD
686
Eric Fiselier89659d12015-07-07 00:27:16 +0000687template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000688inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000689_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
690#if !defined(_LIBCPP_HAS_NO_THREADS) && \
691 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000692 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000693 return __atomic_load_n(__value, __ATOMIC_RELAXED);
694#else
695 return *__value;
696#endif
697}
698
Kuba Breckade9d6792016-09-04 09:55:12 +0000699template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000700inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000701_ValueType __libcpp_acquire_load(_ValueType const* __value) {
702#if !defined(_LIBCPP_HAS_NO_THREADS) && \
703 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000704 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000705 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
706#else
707 return *__value;
708#endif
709}
710
Marshall Clow78dbe462016-11-14 18:22:19 +0000711// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713template <class _Tp> class allocator;
714
Michael Park99f9e912020-03-04 11:27:14 -0500715#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716template <>
Michael Park99f9e912020-03-04 11:27:14 -0500717class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718{
719public:
720 typedef void* pointer;
721 typedef const void* const_pointer;
722 typedef void value_type;
723
724 template <class _Up> struct rebind {typedef allocator<_Up> other;};
725};
726
Howard Hinnant9f771052012-01-19 23:15:22 +0000727template <>
Michael Park99f9e912020-03-04 11:27:14 -0500728class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000729{
730public:
731 typedef const void* pointer;
732 typedef const void* const_pointer;
733 typedef const void value_type;
734
735 template <class _Up> struct rebind {typedef allocator<_Up> other;};
736};
Michael Park99f9e912020-03-04 11:27:14 -0500737#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000738
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739// pointer_traits
740
Marshall Clow0be70c32017-06-14 21:23:57 +0000741template <class _Tp, class = void>
742struct __has_element_type : false_type {};
743
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000745struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000746 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
748template <class _Ptr, bool = __has_element_type<_Ptr>::value>
749struct __pointer_traits_element_type;
750
751template <class _Ptr>
752struct __pointer_traits_element_type<_Ptr, true>
753{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000754 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755};
756
757#ifndef _LIBCPP_HAS_NO_VARIADICS
758
759template <template <class, class...> class _Sp, class _Tp, class ..._Args>
760struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
761{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000762 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763};
764
765template <template <class, class...> class _Sp, class _Tp, class ..._Args>
766struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
767{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000768 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000771#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
773template <template <class> class _Sp, class _Tp>
774struct __pointer_traits_element_type<_Sp<_Tp>, true>
775{
776 typedef typename _Sp<_Tp>::element_type type;
777};
778
779template <template <class> class _Sp, class _Tp>
780struct __pointer_traits_element_type<_Sp<_Tp>, false>
781{
782 typedef _Tp type;
783};
784
785template <template <class, class> class _Sp, class _Tp, class _A0>
786struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
787{
788 typedef typename _Sp<_Tp, _A0>::element_type type;
789};
790
791template <template <class, class> class _Sp, class _Tp, class _A0>
792struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
793{
794 typedef _Tp type;
795};
796
797template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
798struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
799{
800 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
801};
802
803template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
805{
806 typedef _Tp type;
807};
808
809template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
810 class _A1, class _A2>
811struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
812{
813 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
814};
815
816template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
817 class _A1, class _A2>
818struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
819{
820 typedef _Tp type;
821};
822
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000823#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
Marshall Clow0be70c32017-06-14 21:23:57 +0000825template <class _Tp, class = void>
826struct __has_difference_type : false_type {};
827
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000829struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000830 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831
832template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
833struct __pointer_traits_difference_type
834{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000835 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836};
837
838template <class _Ptr>
839struct __pointer_traits_difference_type<_Ptr, true>
840{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000841 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842};
843
844template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500845struct __has_rebind
846{
847private:
848 struct __two {char __lx; char __lxx;};
849 template <class _Xp> static __two __test(...);
850 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
851public:
852 static const bool value = sizeof(__test<_Tp>(0)) == 1;
853};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854
855template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
856struct __pointer_traits_rebind
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000859 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000861 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862#endif
863};
864
865#ifndef _LIBCPP_HAS_NO_VARIADICS
866
867template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
868struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
869{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000870#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000871 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000873 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874#endif
875};
876
877template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
879{
880 typedef _Sp<_Up, _Args...> type;
881};
882
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000883#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884
885template <template <class> class _Sp, class _Tp, class _Up>
886struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
887{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000888#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889 typedef typename _Sp<_Tp>::template rebind<_Up> type;
890#else
891 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
892#endif
893};
894
895template <template <class> class _Sp, class _Tp, class _Up>
896struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
897{
898 typedef _Sp<_Up> type;
899};
900
901template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
902struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
903{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000904#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
906#else
907 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
908#endif
909};
910
911template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
913{
914 typedef _Sp<_Up, _A0> type;
915};
916
917template <template <class, class, class> class _Sp, class _Tp, class _A0,
918 class _A1, class _Up>
919struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
920{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000921#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
923#else
924 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
925#endif
926};
927
928template <template <class, class, class> class _Sp, class _Tp, class _A0,
929 class _A1, class _Up>
930struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
931{
932 typedef _Sp<_Up, _A0, _A1> type;
933};
934
935template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
936 class _A1, class _A2, class _Up>
937struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
938{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000939#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
941#else
942 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
943#endif
944};
945
946template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
947 class _A1, class _A2, class _Up>
948struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
949{
950 typedef _Sp<_Up, _A0, _A1, _A2> type;
951};
952
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000953#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954
955template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000956struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957{
958 typedef _Ptr pointer;
959 typedef typename __pointer_traits_element_type<pointer>::type element_type;
960 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
961
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000962#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000963 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964#else
965 template <class _Up> struct rebind
966 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000967#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968
969private:
970 struct __nat {};
971public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 static pointer pointer_to(typename conditional<is_void<element_type>::value,
974 __nat, element_type>::type& __r)
975 {return pointer::pointer_to(__r);}
976};
977
978template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000979struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980{
981 typedef _Tp* pointer;
982 typedef _Tp element_type;
983 typedef ptrdiff_t difference_type;
984
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000985#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 template <class _Up> using rebind = _Up*;
987#else
988 template <class _Up> struct rebind {typedef _Up* other;};
989#endif
990
991private:
992 struct __nat {};
993public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000994 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000996 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000997 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998};
999
Eric Fiselierae3ab842015-08-23 02:56:05 +00001000template <class _From, class _To>
1001struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001002#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +00001003 typedef typename pointer_traits<_From>::template rebind<_To> type;
1004#else
1005 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
1006#endif
1007};
1008
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009// allocator_traits
1010
Marshall Clow0be70c32017-06-14 21:23:57 +00001011template <class _Tp, class = void>
1012struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013
1014template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001015struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001016 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017
1018namespace __pointer_type_imp
1019{
1020
1021template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1022struct __pointer_type
1023{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001024 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025};
1026
1027template <class _Tp, class _Dp>
1028struct __pointer_type<_Tp, _Dp, false>
1029{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001030 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031};
1032
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001033} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034
1035template <class _Tp, class _Dp>
1036struct __pointer_type
1037{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001038 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 +00001039};
1040
Marshall Clow0be70c32017-06-14 21:23:57 +00001041template <class _Tp, class = void>
1042struct __has_const_pointer : false_type {};
1043
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001045struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001046 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047
1048template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1049struct __const_pointer
1050{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001051 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052};
1053
1054template <class _Tp, class _Ptr, class _Alloc>
1055struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1056{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001057#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001058 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059#else
1060 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1061#endif
1062};
1063
Marshall Clow0be70c32017-06-14 21:23:57 +00001064template <class _Tp, class = void>
1065struct __has_void_pointer : false_type {};
1066
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001068struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001069 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070
1071template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1072struct __void_pointer
1073{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001074 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075};
1076
1077template <class _Ptr, class _Alloc>
1078struct __void_pointer<_Ptr, _Alloc, false>
1079{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001080#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001081 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001083 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084#endif
1085};
1086
Marshall Clow0be70c32017-06-14 21:23:57 +00001087template <class _Tp, class = void>
1088struct __has_const_void_pointer : false_type {};
1089
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001091struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001092 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093
1094template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1095struct __const_void_pointer
1096{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001097 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098};
1099
1100template <class _Ptr, class _Alloc>
1101struct __const_void_pointer<_Ptr, _Alloc, false>
1102{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001103#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001104 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001106 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107#endif
1108};
1109
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001110
1111template <bool _UsePointerTraits> struct __to_address_helper;
1112
1113template <> struct __to_address_helper<true> {
1114 template <class _Pointer>
1115 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1116
1117 template <class _Pointer>
1118 _LIBCPP_CONSTEXPR
1119 static __return_type<_Pointer>
1120 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1121};
1122
1123template <class _Pointer, bool _Dummy = true>
1124using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1125
1126
Howard Hinnantc834c512011-11-29 18:15:50 +00001127template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001128inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001129_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001130__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001132 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 return __p;
1134}
1135
1136template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001137inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1138typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1139__to_address(const _Pointer& __p) _NOEXCEPT {
1140 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001141}
1142
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001143template <> struct __to_address_helper<false> {
1144 template <class _Pointer>
1145 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001146
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001147 template <class _Pointer>
1148 _LIBCPP_CONSTEXPR
1149 static __return_type<_Pointer>
1150 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1151};
1152
1153
1154#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001155template <class _Tp>
1156inline _LIBCPP_INLINE_VISIBILITY constexpr
1157_Tp*
1158to_address(_Tp* __p) _NOEXCEPT
1159{
1160 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1161 return __p;
1162}
1163
1164template <class _Pointer>
1165inline _LIBCPP_INLINE_VISIBILITY
1166auto
1167to_address(const _Pointer& __p) _NOEXCEPT
1168{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001169 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001170}
1171#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172
Marshall Clow0be70c32017-06-14 21:23:57 +00001173template <class _Tp, class = void>
1174struct __has_size_type : false_type {};
1175
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001177struct __has_size_type<_Tp,
1178 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001180template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181struct __size_type
1182{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001183 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184};
1185
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001186template <class _Alloc, class _DiffType>
1187struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001189 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190};
1191
Marshall Clow0be70c32017-06-14 21:23:57 +00001192template <class _Tp, class = void>
1193struct __has_propagate_on_container_copy_assignment : false_type {};
1194
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001196struct __has_propagate_on_container_copy_assignment<_Tp,
1197 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1198 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199
1200template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1201struct __propagate_on_container_copy_assignment
1202{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001203 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204};
1205
1206template <class _Alloc>
1207struct __propagate_on_container_copy_assignment<_Alloc, true>
1208{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001209 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210};
1211
Marshall Clow0be70c32017-06-14 21:23:57 +00001212template <class _Tp, class = void>
1213struct __has_propagate_on_container_move_assignment : false_type {};
1214
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001216struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001217 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1218 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
1220template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1221struct __propagate_on_container_move_assignment
1222{
1223 typedef false_type type;
1224};
1225
1226template <class _Alloc>
1227struct __propagate_on_container_move_assignment<_Alloc, true>
1228{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001229 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230};
1231
Marshall Clow0be70c32017-06-14 21:23:57 +00001232template <class _Tp, class = void>
1233struct __has_propagate_on_container_swap : false_type {};
1234
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001236struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001237 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1238 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239
1240template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1241struct __propagate_on_container_swap
1242{
1243 typedef false_type type;
1244};
1245
1246template <class _Alloc>
1247struct __propagate_on_container_swap<_Alloc, true>
1248{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001249 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250};
1251
Marshall Clow0be70c32017-06-14 21:23:57 +00001252template <class _Tp, class = void>
1253struct __has_is_always_equal : false_type {};
1254
Marshall Clow0b587562015-06-02 16:34:03 +00001255template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001256struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001257 typename __void_t<typename _Tp::is_always_equal>::type>
1258 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001259
1260template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1261struct __is_always_equal
1262{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001263 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001264};
1265
1266template <class _Alloc>
1267struct __is_always_equal<_Alloc, true>
1268{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001269 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001270};
1271
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1273struct __has_rebind_other
1274{
1275private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001276 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001278 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001280 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281public:
1282 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1283};
1284
1285template <class _Tp, class _Up>
1286struct __has_rebind_other<_Tp, _Up, false>
1287{
1288 static const bool value = false;
1289};
1290
1291template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1292struct __allocator_traits_rebind
1293{
Michael Park99f9e912020-03-04 11:27:14 -05001294 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001295 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001296 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297};
1298
1299#ifndef _LIBCPP_HAS_NO_VARIADICS
1300
1301template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1302struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1303{
Michael Park99f9e912020-03-04 11:27:14 -05001304 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001305 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001306 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307};
1308
1309template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1311{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001312 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313};
1314
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001315#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316
1317template <template <class> class _Alloc, class _Tp, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1319{
1320 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1321};
1322
1323template <template <class> class _Alloc, class _Tp, class _Up>
1324struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1325{
1326 typedef _Alloc<_Up> type;
1327};
1328
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1330struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1331{
1332 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1333};
1334
1335template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1336struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1337{
1338 typedef _Alloc<_Up, _A0> type;
1339};
1340
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1342 class _A1, class _Up>
1343struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1344{
1345 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1346};
1347
1348template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1349 class _A1, class _Up>
1350struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1351{
1352 typedef _Alloc<_Up, _A0, _A1> type;
1353};
1354
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1356 class _A1, class _A2, class _Up>
1357struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1358{
1359 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1360};
1361
1362template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1363 class _A1, class _A2, class _Up>
1364struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1365{
1366 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1367};
1368
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001369#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001371#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
Michael Park99f9e912020-03-04 11:27:14 -05001373_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1375auto
1376__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001377 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001378_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379
1380template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1381auto
1382__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1383 -> false_type;
1384
1385template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1386struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001387 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1388 declval<_SizeType>(),
1389 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390{
1391};
1392
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001393#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394
1395template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1396struct __has_allocate_hint
1397 : true_type
1398{
1399};
1400
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001401#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001403#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404
Michael Park99f9e912020-03-04 11:27:14 -05001405_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1406template <class _Alloc, class _Tp, class... _Args>
1407auto
1408__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&&... __args)
1409 -> decltype(__a.construct(__p, _VSTD::forward<_Args>(__args)...), true_type());
1410_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411
1412template <class _Alloc, class _Pointer, class ..._Args>
Michael Park99f9e912020-03-04 11:27:14 -05001413auto
1414__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args)
1415 -> false_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416
1417template <class _Alloc, class _Pointer, class ..._Args>
1418struct __has_construct
Michael Park99f9e912020-03-04 11:27:14 -05001419 : decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
1420 declval<_Pointer>(),
1421 declval<_Args>()...))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422{
1423};
1424
Michael Park99f9e912020-03-04 11:27:14 -05001425_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426template <class _Alloc, class _Pointer>
1427auto
1428__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1429 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001430_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431
1432template <class _Alloc, class _Pointer>
1433auto
1434__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1435 -> false_type;
1436
1437template <class _Alloc, class _Pointer>
1438struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001439 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1440 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441{
1442};
1443
Michael Park99f9e912020-03-04 11:27:14 -05001444_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445template <class _Alloc>
1446auto
1447__has_max_size_test(_Alloc&& __a)
1448 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001449_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
1451template <class _Alloc>
1452auto
1453__has_max_size_test(const volatile _Alloc& __a)
1454 -> false_type;
1455
1456template <class _Alloc>
1457struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001458 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459{
1460};
1461
1462template <class _Alloc>
1463auto
1464__has_select_on_container_copy_construction_test(_Alloc&& __a)
1465 -> decltype(__a.select_on_container_copy_construction(), true_type());
1466
1467template <class _Alloc>
1468auto
1469__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1470 -> false_type;
1471
1472template <class _Alloc>
1473struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001474 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475{
1476};
1477
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001478#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001480template <class _Alloc, class _Pointer, class _Tp, class = void>
1481struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001483template <class _Alloc, class _Pointer, class _Tp>
1484struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1485 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1486>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001488template <class _Alloc, class _Pointer, class = void>
1489struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490
1491template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001492struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1493 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1494>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495
1496template <class _Alloc>
1497struct __has_max_size
1498 : true_type
1499{
1500};
1501
1502template <class _Alloc>
1503struct __has_select_on_container_copy_construction
1504 : false_type
1505{
1506};
1507
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001508#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001510template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1511struct __alloc_traits_difference_type
1512{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001513 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001514};
1515
1516template <class _Alloc, class _Ptr>
1517struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1518{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001519 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001520};
1521
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001522template <class _Tp>
1523struct __is_default_allocator : false_type {};
1524
1525template <class _Tp>
1526struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1527
Eric Fiselier909fe962019-09-13 16:09:33 +00001528
1529
1530template <class _Alloc,
1531 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1532 >
1533struct __is_cpp17_move_insertable;
1534template <class _Alloc>
1535struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1536template <class _Alloc>
1537struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1538
1539template <class _Alloc,
1540 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1541 >
1542struct __is_cpp17_copy_insertable;
1543template <class _Alloc>
1544struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1545template <class _Alloc>
1546struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1547 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1548 __is_cpp17_move_insertable<_Alloc>::value>
1549 {};
1550
1551
1552
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001554struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555{
1556 typedef _Alloc allocator_type;
1557 typedef typename allocator_type::value_type value_type;
1558
1559 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1560 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1561 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1562 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1563
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001564 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1565 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566
1567 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1568 propagate_on_container_copy_assignment;
1569 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1570 propagate_on_container_move_assignment;
1571 typedef typename __propagate_on_container_swap<allocator_type>::type
1572 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001573 typedef typename __is_always_equal<allocator_type>::type
1574 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001576#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001578 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001579 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001580#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 template <class _Tp> struct rebind_alloc
1582 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1583 template <class _Tp> struct rebind_traits
1584 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001585#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
Marshall Clow0e58cae2017-11-26 02:55:38 +00001587 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static pointer allocate(allocator_type& __a, size_type __n)
1589 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001590 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001592 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001596 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 {__a.deallocate(__p, __n);}
1598
1599#ifndef _LIBCPP_HAS_NO_VARIADICS
1600 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001603 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001604 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001605#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001607 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001608 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609 {
1610 ::new ((void*)__p) _Tp();
1611 }
1612 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001613 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001614 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001616 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1617 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 }
1619 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001620 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001621 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 const _A1& __a1)
1623 {
1624 ::new ((void*)__p) _Tp(__a0, __a1);
1625 }
1626 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001627 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001628 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 const _A1& __a1, const _A2& __a2)
1630 {
1631 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1632 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001633#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634
1635 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 static void destroy(allocator_type& __a, _Tp* __p)
1638 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1639
Howard Hinnant756c69b2010-09-22 16:48:34 +00001640 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001641 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1643
Howard Hinnant756c69b2010-09-22 16:48:34 +00001644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645 static allocator_type
1646 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001647 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 __has_select_on_container_copy_construction<const allocator_type>(),
1649 __a);}
1650
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001651 template <class _Ptr>
1652 _LIBCPP_INLINE_VISIBILITY
1653 static
1654 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001655 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001656 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001657 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1658 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001659 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001660 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001661#ifdef _LIBCPP_NO_EXCEPTIONS
1662 _VSTD::move(*__begin1)
1663#else
1664 _VSTD::move_if_noexcept(*__begin1)
1665#endif
1666 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001667 }
1668
1669 template <class _Tp>
1670 _LIBCPP_INLINE_VISIBILITY
1671 static
1672 typename enable_if
1673 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001674 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001675 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1676 is_trivially_move_constructible<_Tp>::value,
1677 void
1678 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001679 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001680 {
1681 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001682 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001683 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001684 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001685 __begin2 += _Np;
1686 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 }
1688
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001689 template <class _Iter, class _Ptr>
1690 _LIBCPP_INLINE_VISIBILITY
1691 static
1692 void
1693 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1694 {
1695 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001696 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001697 }
1698
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001699 template <class _SourceTp, class _DestTp,
1700 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1701 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001702 _LIBCPP_INLINE_VISIBILITY
1703 static
1704 typename enable_if
1705 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001706 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001707 is_same<_RawSourceTp, _RawDestTp>::value &&
1708 (__is_default_allocator<allocator_type>::value ||
1709 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001710 void
1711 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001712 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001713 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001714 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001715 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001716 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001717 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001718 __begin2 += _Np;
1719 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001720 }
1721
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001722 template <class _Ptr>
1723 _LIBCPP_INLINE_VISIBILITY
1724 static
1725 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001726 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001727 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001728 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1729 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001730 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001731 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001732 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001733#ifdef _LIBCPP_NO_EXCEPTIONS
1734 _VSTD::move(*--__end1)
1735#else
1736 _VSTD::move_if_noexcept(*--__end1)
1737#endif
1738 );
1739 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001740 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001741 }
1742
1743 template <class _Tp>
1744 _LIBCPP_INLINE_VISIBILITY
1745 static
1746 typename enable_if
1747 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001748 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001749 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1750 is_trivially_move_constructible<_Tp>::value,
1751 void
1752 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001753 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001754 {
1755 ptrdiff_t _Np = __end1 - __begin1;
1756 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001757 if (_Np > 0)
1758 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001759 }
1760
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761private:
1762
Howard Hinnant756c69b2010-09-22 16:48:34 +00001763 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001764 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001766 {
1767 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1768 return __a.allocate(__n, __hint);
1769 _LIBCPP_SUPPRESS_DEPRECATED_POP
1770 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00001771 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001772 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001773 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 {return __a.allocate(__n);}
1775
1776#ifndef _LIBCPP_HAS_NO_VARIADICS
1777 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001780 {
1781 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1782 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1783 _LIBCPP_SUPPRESS_DEPRECATED_POP
1784 }
1785
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1789 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001790 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001792#else // _LIBCPP_HAS_NO_VARIADICS
1793 template <class _Tp, class _A0>
1794 _LIBCPP_INLINE_VISIBILITY
1795 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1796 const _A0& __a0)
1797 {__a.construct(__p, __a0);}
1798 template <class _Tp, class _A0>
1799 _LIBCPP_INLINE_VISIBILITY
1800 static void __construct(false_type, allocator_type&, _Tp* __p,
1801 const _A0& __a0)
1802 {
1803 ::new ((void*)__p) _Tp(__a0);
1804 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001805#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806
1807 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001810 {
1811 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1812 __a.destroy(__p);
1813 _LIBCPP_SUPPRESS_DEPRECATED_POP
1814 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 static void __destroy(false_type, allocator_type&, _Tp* __p)
1818 {
1819 __p->~_Tp();
1820 }
1821
Howard Hinnant756c69b2010-09-22 16:48:34 +00001822 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001823 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001824 {
1825 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1826 return __a.max_size();
1827 _LIBCPP_SUPPRESS_DEPRECATED_POP
1828 }
1829
Howard Hinnant756c69b2010-09-22 16:48:34 +00001830 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001831 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001832 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833
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(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001840 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 {return __a;}
1842};
1843
Marshall Clow940e01c2015-04-07 05:21:38 +00001844template <class _Traits, class _Tp>
1845struct __rebind_alloc_helper
1846{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001847#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001848 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001849#else
1850 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1851#endif
1852};
1853
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854// allocator
1855
1856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858{
1859public:
Michael Park99f9e912020-03-04 11:27:14 -05001860#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1861 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1862 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1863 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1864 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1865 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1866 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1867
1868 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1869#endif
1870
1871 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872
Howard Hinnant4931e092011-06-02 21:38:57 +00001873 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001874 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001875
Marshall Clowbc759762018-03-20 23:02:53 +00001876 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1877 allocator() _NOEXCEPT {}
1878
Louis Dionne481a2662018-09-23 18:35:00 +00001879 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001880 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1881 allocator(const allocator<_Up>&) _NOEXCEPT {}
1882
Michael Park99f9e912020-03-04 11:27:14 -05001883#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1884 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1885 pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001886 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001887 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1888 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001889 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05001890#endif
1891
1892 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00001893 {
Michael Park99f9e912020-03-04 11:27:14 -05001894 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
1895 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00001896 __throw_length_error("allocator<T>::allocate(size_t n)"
1897 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001898 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001899 }
Michael Park99f9e912020-03-04 11:27:14 -05001900
1901#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1902 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1903 _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
1904#endif
1905
1906 _LIBCPP_INLINE_VISIBILITY void deallocate(_Tp* __p, size_t __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001907 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05001908
1909#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1910 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant719bda32011-05-28 14:41:13 +00001911 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05001912
Howard Hinnant74279a52010-09-04 23:28:19 +00001913#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05001915 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 void
1917 construct(_Up* __p, _Args&&... __args)
1918 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001919 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001921#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922 _LIBCPP_INLINE_VISIBILITY
1923 void
1924 construct(pointer __p)
1925 {
1926 ::new((void*)__p) _Tp();
1927 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001928# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001929
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 template <class _A0>
1931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001932 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933 construct(pointer __p, _A0& __a0)
1934 {
1935 ::new((void*)__p) _Tp(__a0);
1936 }
1937 template <class _A0>
1938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001939 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 construct(pointer __p, const _A0& __a0)
1941 {
1942 ::new((void*)__p) _Tp(__a0);
1943 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001944# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 template <class _A0, class _A1>
1946 _LIBCPP_INLINE_VISIBILITY
1947 void
1948 construct(pointer __p, _A0& __a0, _A1& __a1)
1949 {
1950 ::new((void*)__p) _Tp(__a0, __a1);
1951 }
1952 template <class _A0, class _A1>
1953 _LIBCPP_INLINE_VISIBILITY
1954 void
1955 construct(pointer __p, const _A0& __a0, _A1& __a1)
1956 {
1957 ::new((void*)__p) _Tp(__a0, __a1);
1958 }
1959 template <class _A0, class _A1>
1960 _LIBCPP_INLINE_VISIBILITY
1961 void
1962 construct(pointer __p, _A0& __a0, const _A1& __a1)
1963 {
1964 ::new((void*)__p) _Tp(__a0, __a1);
1965 }
1966 template <class _A0, class _A1>
1967 _LIBCPP_INLINE_VISIBILITY
1968 void
1969 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1970 {
1971 ::new((void*)__p) _Tp(__a0, __a1);
1972 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001973#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05001974 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1975#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976};
1977
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001978template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001979class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001980{
1981public:
Michael Park99f9e912020-03-04 11:27:14 -05001982#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1983 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1984 _LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
1985 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1986 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1987 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1988 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1989
1990 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
1991#endif
1992
1993 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001994
1995 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001996 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001997
Marshall Clowbc759762018-03-20 23:02:53 +00001998 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1999 allocator() _NOEXCEPT {}
2000
2001 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00002002 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00002003 allocator(const allocator<_Up>&) _NOEXCEPT {}
2004
Michael Park99f9e912020-03-04 11:27:14 -05002005#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2006 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
2007 const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002008 {return _VSTD::addressof(__x);}
Michael Park99f9e912020-03-04 11:27:14 -05002009#endif
2010
2011 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY const _Tp* allocate(size_t __n)
Marshall Clow813ffb32016-03-03 12:04:39 +00002012 {
Michael Park99f9e912020-03-04 11:27:14 -05002013 // TODO(mpark): Replace with `allocator_traits<allocator>::max_size(*this)`.
2014 if (__n > (size_t(~0) / sizeof(_Tp)))
Marshall Clowed3e2292016-08-25 17:47:09 +00002015 __throw_length_error("allocator<const T>::allocate(size_t n)"
2016 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05002017 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00002018 }
Michael Park99f9e912020-03-04 11:27:14 -05002019
2020#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2021 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
2022 const _Tp* allocate(size_t __n, const void*) { return allocate(__n); }
2023#endif
2024
2025 _LIBCPP_INLINE_VISIBILITY void deallocate(const _Tp* __p, size_t __n)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002026 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Michael Park99f9e912020-03-04 11:27:14 -05002027
2028#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
2029 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002030 {return size_type(~0) / sizeof(_Tp);}
Michael Park99f9e912020-03-04 11:27:14 -05002031
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002032#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2033 template <class _Up, class... _Args>
Michael Park99f9e912020-03-04 11:27:14 -05002034 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002035 void
2036 construct(_Up* __p, _Args&&... __args)
2037 {
2038 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
2039 }
2040#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2041 _LIBCPP_INLINE_VISIBILITY
2042 void
2043 construct(pointer __p)
2044 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002045 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002046 }
2047# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00002048
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002049 template <class _A0>
2050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002051 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002052 construct(pointer __p, _A0& __a0)
2053 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002054 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002055 }
2056 template <class _A0>
2057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00002058 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002059 construct(pointer __p, const _A0& __a0)
2060 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002061 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002062 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002063# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
2064 template <class _A0, class _A1>
2065 _LIBCPP_INLINE_VISIBILITY
2066 void
2067 construct(pointer __p, _A0& __a0, _A1& __a1)
2068 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002069 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002070 }
2071 template <class _A0, class _A1>
2072 _LIBCPP_INLINE_VISIBILITY
2073 void
2074 construct(pointer __p, const _A0& __a0, _A1& __a1)
2075 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002076 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002077 }
2078 template <class _A0, class _A1>
2079 _LIBCPP_INLINE_VISIBILITY
2080 void
2081 construct(pointer __p, _A0& __a0, const _A1& __a1)
2082 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002083 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002084 }
2085 template <class _A0, class _A1>
2086 _LIBCPP_INLINE_VISIBILITY
2087 void
2088 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2089 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002090 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002091 }
2092#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Michael Park99f9e912020-03-04 11:27:14 -05002093 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2094#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002095};
2096
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097template <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 true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100
2101template <class _Tp, class _Up>
2102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002103bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104
2105template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002106class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107 : public iterator<output_iterator_tag,
2108 _Tp, // purposefully not C++03
2109 ptrdiff_t, // purposefully not C++03
2110 _Tp*, // purposefully not C++03
2111 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2112{
2113private:
2114 _OutputIterator __x_;
2115public:
2116 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2117 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2118 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002119 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002120#if _LIBCPP_STD_VER >= 14
2121 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002122 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002123#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2125 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2126 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002127#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002128 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002129#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130};
2131
2132template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002133_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002135get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136{
2137 pair<_Tp*, ptrdiff_t> __r(0, 0);
2138 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2139 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2140 / sizeof(_Tp);
2141 if (__n > __m)
2142 __n = __m;
2143 while (__n > 0)
2144 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002145#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002146 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002147 {
2148 std::align_val_t __al =
2149 std::align_val_t(std::alignment_of<_Tp>::value);
2150 __r.first = static_cast<_Tp*>(::operator new(
2151 __n * sizeof(_Tp), __al, nothrow));
2152 } else {
2153 __r.first = static_cast<_Tp*>(::operator new(
2154 __n * sizeof(_Tp), nothrow));
2155 }
2156#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002157 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002158 {
2159 // Since aligned operator new is unavailable, return an empty
2160 // buffer rather than one with invalid alignment.
2161 return __r;
2162 }
2163
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002165#endif
2166
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 if (__r.first)
2168 {
2169 __r.second = __n;
2170 break;
2171 }
2172 __n /= 2;
2173 }
2174 return __r;
2175}
2176
2177template <class _Tp>
2178inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002179void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2180{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002181 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002182}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183
Marshall Clowb22274f2017-01-24 22:22:33 +00002184#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002186struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187{
2188 _Tp* __ptr_;
2189};
2190
2191template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002192class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193{
2194private:
2195 _Tp* __ptr_;
2196public:
2197 typedef _Tp element_type;
2198
2199 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2200 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2201 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2202 : __ptr_(__p.release()) {}
2203 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2204 {reset(__p.release()); return *this;}
2205 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2206 {reset(__p.release()); return *this;}
2207 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2208 {reset(__p.__ptr_); return *this;}
2209 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2210
2211 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2212 {return *__ptr_;}
2213 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2214 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2215 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2216 {
2217 _Tp* __t = __ptr_;
2218 __ptr_ = 0;
2219 return __t;
2220 }
2221 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2222 {
2223 if (__ptr_ != __p)
2224 delete __ptr_;
2225 __ptr_ = __p;
2226 }
2227
2228 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2229 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2230 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2231 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2232 {return auto_ptr<_Up>(release());}
2233};
2234
2235template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002236class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237{
2238public:
2239 typedef void element_type;
2240};
Marshall Clowb22274f2017-01-24 22:22:33 +00002241#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002243// Tag used to default initialize one or both of the pair's elements.
2244struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002245struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002246
Eric Fiselier9d355982017-04-12 23:45:53 +00002247template <class _Tp, int _Idx,
2248 bool _CanBeEmptyBase =
2249 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2250struct __compressed_pair_elem {
2251 typedef _Tp _ParamT;
2252 typedef _Tp& reference;
2253 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002255 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05002256 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2258 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259
Eric Fiselier9d355982017-04-12 23:45:53 +00002260 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002261 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2262 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002263 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002264 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002265 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002266 : __value_(_VSTD::forward<_Up>(__u))
2267 {
2268 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002270
2271#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002272 template <class... _Args, size_t... _Indexes>
2273 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2274 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2275 __tuple_indices<_Indexes...>)
2276 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002279
Alex Lorenz76132112017-11-09 17:54:49 +00002280 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2281 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002285 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286};
2287
Eric Fiselier9d355982017-04-12 23:45:53 +00002288template <class _Tp, int _Idx>
2289struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2290 typedef _Tp _ParamT;
2291 typedef _Tp& reference;
2292 typedef const _Tp& const_reference;
2293 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002295 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2296 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2297 __compressed_pair_elem(__default_init_tag) {}
2298 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2299 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300
Eric Fiselier9d355982017-04-12 23:45:53 +00002301 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002302 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2303 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002304 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002305 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002306 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002307 : __value_type(_VSTD::forward<_Up>(__u))
2308 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002310#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002311 template <class... _Args, size_t... _Indexes>
2312 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2313 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2314 __tuple_indices<_Indexes...>)
2315 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002316#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317
Alex Lorenz76132112017-11-09 17:54:49 +00002318 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2319 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002320 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321};
2322
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002324class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2325 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002326 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2327 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002328
2329 // NOTE: This static assert should never fire because __compressed_pair
2330 // is *almost never* used in a scenario where it's possible for T1 == T2.
2331 // (The exception is std::function where it is possible that the function
2332 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002333 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002334 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2335 "The current implementation is NOT ABI-compatible with the previous "
2336 "implementation for this configuration");
2337
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002339 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002340 class = typename enable_if<
2341 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2342 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2343 >::type
2344 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002345 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002346 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347
Eric Fiselier9d355982017-04-12 23:45:53 +00002348 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002349 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002350 __compressed_pair(_U1&& __t1, _U2&& __t2)
2351 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002353#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002354 template <class... _Args1, class... _Args2>
2355 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2356 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2357 tuple<_Args2...> __second_args)
2358 : _Base1(__pc, _VSTD::move(__first_args),
2359 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2360 _Base2(__pc, _VSTD::move(__second_args),
2361 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002362#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363
Eric Fiselier9d355982017-04-12 23:45:53 +00002364 _LIBCPP_INLINE_VISIBILITY
2365 typename _Base1::reference first() _NOEXCEPT {
2366 return static_cast<_Base1&>(*this).__get();
2367 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368
Eric Fiselier9d355982017-04-12 23:45:53 +00002369 _LIBCPP_INLINE_VISIBILITY
2370 typename _Base1::const_reference first() const _NOEXCEPT {
2371 return static_cast<_Base1 const&>(*this).__get();
2372 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373
Eric Fiselier9d355982017-04-12 23:45:53 +00002374 _LIBCPP_INLINE_VISIBILITY
2375 typename _Base2::reference second() _NOEXCEPT {
2376 return static_cast<_Base2&>(*this).__get();
2377 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378
Eric Fiselier9d355982017-04-12 23:45:53 +00002379 _LIBCPP_INLINE_VISIBILITY
2380 typename _Base2::const_reference second() const _NOEXCEPT {
2381 return static_cast<_Base2 const&>(*this).__get();
2382 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383
Eric Fiselier9d355982017-04-12 23:45:53 +00002384 _LIBCPP_INLINE_VISIBILITY
2385 void swap(__compressed_pair& __x)
2386 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2387 __is_nothrow_swappable<_T2>::value)
2388 {
2389 using std::swap;
2390 swap(first(), __x.first());
2391 swap(second(), __x.second());
2392 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393};
2394
2395template <class _T1, class _T2>
2396inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002397void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2398 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2399 __is_nothrow_swappable<_T2>::value) {
2400 __x.swap(__y);
2401}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002403// default_delete
2404
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002406struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002407 static_assert(!is_function<_Tp>::value,
2408 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002409#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002410 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002411#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002412 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002413#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002414 template <class _Up>
2415 _LIBCPP_INLINE_VISIBILITY
2416 default_delete(const default_delete<_Up>&,
2417 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2418 0) _NOEXCEPT {}
2419
2420 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2421 static_assert(sizeof(_Tp) > 0,
2422 "default_delete can not delete incomplete type");
2423 static_assert(!is_void<_Tp>::value,
2424 "default_delete can not delete incomplete type");
2425 delete __ptr;
2426 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427};
2428
2429template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002430struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2431private:
2432 template <class _Up>
2433 struct _EnableIfConvertible
2434 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2435
Howard Hinnant4500ca52011-12-18 21:19:44 +00002436public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002437#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002438 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002439#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002440 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002441#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002442
2443 template <class _Up>
2444 _LIBCPP_INLINE_VISIBILITY
2445 default_delete(const default_delete<_Up[]>&,
2446 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2447
2448 template <class _Up>
2449 _LIBCPP_INLINE_VISIBILITY
2450 typename _EnableIfConvertible<_Up>::type
2451 operator()(_Up* __ptr) const _NOEXCEPT {
2452 static_assert(sizeof(_Tp) > 0,
2453 "default_delete can not delete incomplete type");
2454 static_assert(!is_void<_Tp>::value,
2455 "default_delete can not delete void type");
2456 delete[] __ptr;
2457 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458};
2459
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002460template <class _Deleter>
2461struct __unique_ptr_deleter_sfinae {
2462 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2463 typedef const _Deleter& __lval_ref_type;
2464 typedef _Deleter&& __good_rval_ref_type;
2465 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466};
2467
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002468template <class _Deleter>
2469struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2470 typedef const _Deleter& __lval_ref_type;
2471 typedef const _Deleter&& __bad_rval_ref_type;
2472 typedef false_type __enable_rval_overload;
2473};
2474
2475template <class _Deleter>
2476struct __unique_ptr_deleter_sfinae<_Deleter&> {
2477 typedef _Deleter& __lval_ref_type;
2478 typedef _Deleter&& __bad_rval_ref_type;
2479 typedef false_type __enable_rval_overload;
2480};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002481
2482template <class _Tp, class _Dp = default_delete<_Tp> >
2483class _LIBCPP_TEMPLATE_VIS unique_ptr {
2484public:
2485 typedef _Tp element_type;
2486 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002487 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002488
2489 static_assert(!is_rvalue_reference<deleter_type>::value,
2490 "the specified deleter type cannot be an rvalue reference");
2491
2492private:
2493 __compressed_pair<pointer, deleter_type> __ptr_;
2494
2495 struct __nat { int __for_bool_; };
2496
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002497 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498
2499 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002500 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002501 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002502
2503 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002504 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002505 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002506
2507 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002508 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002509 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002510
2511 template <bool _Dummy, class _Deleter = typename __dependent_type<
2512 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002513 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002514 typename enable_if<is_default_constructible<_Deleter>::value &&
2515 !is_pointer<_Deleter>::value>::type;
2516
2517 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002518 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002519 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2520
2521 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002522 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002523 is_convertible<typename _UPtr::pointer, pointer>::value &&
2524 !is_array<_Up>::value
2525 >::type;
2526
2527 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002528 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002529 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2530 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2531 >::type;
2532
2533 template <class _UDel>
2534 using _EnableIfDeleterAssignable = typename enable_if<
2535 is_assignable<_Dp&, _UDel&&>::value
2536 >::type;
2537
2538public:
2539 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002540 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002541 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002542 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002543
2544 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002545 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002546 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002547 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548
2549 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002550 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002551 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002552 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002553
2554 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002555 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002556 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002557 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002558 : __ptr_(__p, __d) {}
2559
2560 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002561 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002562 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002563 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002564 : __ptr_(__p, _VSTD::move(__d)) {
2565 static_assert(!is_reference<deleter_type>::value,
2566 "rvalue deleter bound to reference");
2567 }
2568
2569 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002570 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002571 _LIBCPP_INLINE_VISIBILITY
2572 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2573
2574 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002575 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002576 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2577 }
2578
2579 template <class _Up, class _Ep,
2580 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2581 class = _EnableIfDeleterConvertible<_Ep>
2582 >
2583 _LIBCPP_INLINE_VISIBILITY
2584 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2585 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2586
2587#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2588 template <class _Up>
2589 _LIBCPP_INLINE_VISIBILITY
2590 unique_ptr(auto_ptr<_Up>&& __p,
2591 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002592 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002593 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002594 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002595#endif
2596
2597 _LIBCPP_INLINE_VISIBILITY
2598 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2599 reset(__u.release());
2600 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2601 return *this;
2602 }
2603
2604 template <class _Up, class _Ep,
2605 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2606 class = _EnableIfDeleterAssignable<_Ep>
2607 >
2608 _LIBCPP_INLINE_VISIBILITY
2609 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2610 reset(__u.release());
2611 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2612 return *this;
2613 }
2614
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002615#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2616 template <class _Up>
2617 _LIBCPP_INLINE_VISIBILITY
2618 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2619 is_same<_Dp, default_delete<_Tp> >::value,
2620 unique_ptr&>::type
2621 operator=(auto_ptr<_Up> __p) {
2622 reset(__p.release());
2623 return *this;
2624 }
2625#endif
2626
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002627#ifdef _LIBCPP_CXX03_LANG
2628 unique_ptr(unique_ptr const&) = delete;
2629 unique_ptr& operator=(unique_ptr const&) = delete;
2630#endif
2631
2632
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002633 _LIBCPP_INLINE_VISIBILITY
2634 ~unique_ptr() { reset(); }
2635
2636 _LIBCPP_INLINE_VISIBILITY
2637 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2638 reset();
2639 return *this;
2640 }
2641
2642 _LIBCPP_INLINE_VISIBILITY
2643 typename add_lvalue_reference<_Tp>::type
2644 operator*() const {
2645 return *__ptr_.first();
2646 }
2647 _LIBCPP_INLINE_VISIBILITY
2648 pointer operator->() const _NOEXCEPT {
2649 return __ptr_.first();
2650 }
2651 _LIBCPP_INLINE_VISIBILITY
2652 pointer get() const _NOEXCEPT {
2653 return __ptr_.first();
2654 }
2655 _LIBCPP_INLINE_VISIBILITY
2656 deleter_type& get_deleter() _NOEXCEPT {
2657 return __ptr_.second();
2658 }
2659 _LIBCPP_INLINE_VISIBILITY
2660 const deleter_type& get_deleter() const _NOEXCEPT {
2661 return __ptr_.second();
2662 }
2663 _LIBCPP_INLINE_VISIBILITY
2664 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2665 return __ptr_.first() != nullptr;
2666 }
2667
2668 _LIBCPP_INLINE_VISIBILITY
2669 pointer release() _NOEXCEPT {
2670 pointer __t = __ptr_.first();
2671 __ptr_.first() = pointer();
2672 return __t;
2673 }
2674
2675 _LIBCPP_INLINE_VISIBILITY
2676 void reset(pointer __p = pointer()) _NOEXCEPT {
2677 pointer __tmp = __ptr_.first();
2678 __ptr_.first() = __p;
2679 if (__tmp)
2680 __ptr_.second()(__tmp);
2681 }
2682
2683 _LIBCPP_INLINE_VISIBILITY
2684 void swap(unique_ptr& __u) _NOEXCEPT {
2685 __ptr_.swap(__u.__ptr_);
2686 }
2687};
2688
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002689
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002691class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002693 typedef _Tp element_type;
2694 typedef _Dp deleter_type;
2695 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2696
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002698 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699
Eric Fiselier31127cd2017-04-16 02:14:31 +00002700 template <class _From>
2701 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702
Eric Fiselier31127cd2017-04-16 02:14:31 +00002703 template <class _FromElem>
2704 struct _CheckArrayPointerConversion<_FromElem*>
2705 : integral_constant<bool,
2706 is_same<_FromElem*, pointer>::value ||
2707 (is_same<pointer, element_type*>::value &&
2708 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2709 >
2710 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711
Eric Fiselier31127cd2017-04-16 02:14:31 +00002712 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002713
2714 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002715 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002716 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717
2718 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002719 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002720 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002721
2722 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002723 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002724 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002725
2726 template <bool _Dummy, class _Deleter = typename __dependent_type<
2727 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002728 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002729 typename enable_if<is_default_constructible<_Deleter>::value &&
2730 !is_pointer<_Deleter>::value>::type;
2731
2732 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002733 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002734 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2735
2736 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002737 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002738 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002739 >::type;
2740
2741 template <class _UPtr, class _Up,
2742 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002743 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 is_array<_Up>::value &&
2745 is_same<pointer, element_type*>::value &&
2746 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2747 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2748 >::type;
2749
2750 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002751 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002752 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2753 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2754 >::type;
2755
2756 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002757 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 is_assignable<_Dp&, _UDel&&>::value
2759 >::type;
2760
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002762 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002763 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002764 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002765 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002767 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002768 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002769 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002770 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002772 template <class _Pp, bool _Dummy = true,
2773 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002774 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002775 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002776 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002777 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002779 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002780 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2781 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002783 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002784 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002786 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002787 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002788 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002789 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002790 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002792 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002793 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2794 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002795 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002796 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002797 : __ptr_(__p, _VSTD::move(__d)) {
2798 static_assert(!is_reference<deleter_type>::value,
2799 "rvalue deleter bound to reference");
2800 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002802 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002803 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002804 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002805 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002806 : __ptr_(nullptr, _VSTD::move(__d)) {
2807 static_assert(!is_reference<deleter_type>::value,
2808 "rvalue deleter bound to reference");
2809 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002810
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002811 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002812 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2813 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002814 _LIBCPP_INLINE_VISIBILITY
2815 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002816
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002817 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002818 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002819 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2820 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002821
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002822 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002823 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 reset(__u.release());
2825 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2826 return *this;
2827 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002829 template <class _Up, class _Ep,
2830 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2831 class = _EnableIfDeleterConvertible<_Ep>
2832 >
2833 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002834 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002835 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002836 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002838 template <class _Up, class _Ep,
2839 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2840 class = _EnableIfDeleterAssignable<_Ep>
2841 >
2842 _LIBCPP_INLINE_VISIBILITY
2843 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002844 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002845 reset(__u.release());
2846 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2847 return *this;
2848 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002850#ifdef _LIBCPP_CXX03_LANG
2851 unique_ptr(unique_ptr const&) = delete;
2852 unique_ptr& operator=(unique_ptr const&) = delete;
2853#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002854
2855public:
2856 _LIBCPP_INLINE_VISIBILITY
2857 ~unique_ptr() { reset(); }
2858
2859 _LIBCPP_INLINE_VISIBILITY
2860 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2861 reset();
2862 return *this;
2863 }
2864
2865 _LIBCPP_INLINE_VISIBILITY
2866 typename add_lvalue_reference<_Tp>::type
2867 operator[](size_t __i) const {
2868 return __ptr_.first()[__i];
2869 }
2870 _LIBCPP_INLINE_VISIBILITY
2871 pointer get() const _NOEXCEPT {
2872 return __ptr_.first();
2873 }
2874
2875 _LIBCPP_INLINE_VISIBILITY
2876 deleter_type& get_deleter() _NOEXCEPT {
2877 return __ptr_.second();
2878 }
2879
2880 _LIBCPP_INLINE_VISIBILITY
2881 const deleter_type& get_deleter() const _NOEXCEPT {
2882 return __ptr_.second();
2883 }
2884 _LIBCPP_INLINE_VISIBILITY
2885 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2886 return __ptr_.first() != nullptr;
2887 }
2888
2889 _LIBCPP_INLINE_VISIBILITY
2890 pointer release() _NOEXCEPT {
2891 pointer __t = __ptr_.first();
2892 __ptr_.first() = pointer();
2893 return __t;
2894 }
2895
2896 template <class _Pp>
2897 _LIBCPP_INLINE_VISIBILITY
2898 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002899 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002900 >::type
2901 reset(_Pp __p) _NOEXCEPT {
2902 pointer __tmp = __ptr_.first();
2903 __ptr_.first() = __p;
2904 if (__tmp)
2905 __ptr_.second()(__tmp);
2906 }
2907
2908 _LIBCPP_INLINE_VISIBILITY
2909 void reset(nullptr_t = nullptr) _NOEXCEPT {
2910 pointer __tmp = __ptr_.first();
2911 __ptr_.first() = nullptr;
2912 if (__tmp)
2913 __ptr_.second()(__tmp);
2914 }
2915
2916 _LIBCPP_INLINE_VISIBILITY
2917 void swap(unique_ptr& __u) _NOEXCEPT {
2918 __ptr_.swap(__u.__ptr_);
2919 }
2920
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921};
2922
2923template <class _Tp, class _Dp>
2924inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002925typename enable_if<
2926 __is_swappable<_Dp>::value,
2927 void
2928>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002929swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930
2931template <class _T1, class _D1, class _T2, class _D2>
2932inline _LIBCPP_INLINE_VISIBILITY
2933bool
2934operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2935
2936template <class _T1, class _D1, class _T2, class _D2>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
2939operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2940
2941template <class _T1, class _D1, class _T2, class _D2>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002944operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2945{
2946 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2947 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002948 typedef typename common_type<_P1, _P2>::type _Vp;
2949 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002950}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951
2952template <class _T1, class _D1, class _T2, class _D2>
2953inline _LIBCPP_INLINE_VISIBILITY
2954bool
2955operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2956
2957template <class _T1, class _D1, class _T2, class _D2>
2958inline _LIBCPP_INLINE_VISIBILITY
2959bool
2960operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2961
2962template <class _T1, class _D1, class _T2, class _D2>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2966
Howard Hinnantb17caf92012-02-21 21:02:58 +00002967template <class _T1, class _D1>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002970operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002971{
2972 return !__x;
2973}
2974
2975template <class _T1, class _D1>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002978operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002979{
2980 return !__x;
2981}
2982
2983template <class _T1, class _D1>
2984inline _LIBCPP_INLINE_VISIBILITY
2985bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002986operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002987{
2988 return static_cast<bool>(__x);
2989}
2990
2991template <class _T1, class _D1>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002994operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002995{
2996 return static_cast<bool>(__x);
2997}
2998
2999template <class _T1, class _D1>
3000inline _LIBCPP_INLINE_VISIBILITY
3001bool
3002operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3003{
3004 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3005 return less<_P1>()(__x.get(), nullptr);
3006}
3007
3008template <class _T1, class _D1>
3009inline _LIBCPP_INLINE_VISIBILITY
3010bool
3011operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3012{
3013 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3014 return less<_P1>()(nullptr, __x.get());
3015}
3016
3017template <class _T1, class _D1>
3018inline _LIBCPP_INLINE_VISIBILITY
3019bool
3020operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3021{
3022 return nullptr < __x;
3023}
3024
3025template <class _T1, class _D1>
3026inline _LIBCPP_INLINE_VISIBILITY
3027bool
3028operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3029{
3030 return __x < nullptr;
3031}
3032
3033template <class _T1, class _D1>
3034inline _LIBCPP_INLINE_VISIBILITY
3035bool
3036operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3037{
3038 return !(nullptr < __x);
3039}
3040
3041template <class _T1, class _D1>
3042inline _LIBCPP_INLINE_VISIBILITY
3043bool
3044operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3045{
3046 return !(__x < nullptr);
3047}
3048
3049template <class _T1, class _D1>
3050inline _LIBCPP_INLINE_VISIBILITY
3051bool
3052operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3053{
3054 return !(__x < nullptr);
3055}
3056
3057template <class _T1, class _D1>
3058inline _LIBCPP_INLINE_VISIBILITY
3059bool
3060operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3061{
3062 return !(nullptr < __x);
3063}
3064
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003065#if _LIBCPP_STD_VER > 11
3066
3067template<class _Tp>
3068struct __unique_if
3069{
3070 typedef unique_ptr<_Tp> __unique_single;
3071};
3072
3073template<class _Tp>
3074struct __unique_if<_Tp[]>
3075{
3076 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3077};
3078
3079template<class _Tp, size_t _Np>
3080struct __unique_if<_Tp[_Np]>
3081{
3082 typedef void __unique_array_known_bound;
3083};
3084
3085template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003086inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003087typename __unique_if<_Tp>::__unique_single
3088make_unique(_Args&&... __args)
3089{
3090 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3091}
3092
3093template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003094inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003095typename __unique_if<_Tp>::__unique_array_unknown_bound
3096make_unique(size_t __n)
3097{
3098 typedef typename remove_extent<_Tp>::type _Up;
3099 return unique_ptr<_Tp>(new _Up[__n]());
3100}
3101
3102template<class _Tp, class... _Args>
3103 typename __unique_if<_Tp>::__unique_array_known_bound
3104 make_unique(_Args&&...) = delete;
3105
3106#endif // _LIBCPP_STD_VER > 11
3107
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003108template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003109#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003110struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003111#else
3112struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003113 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003114#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003115{
3116 typedef unique_ptr<_Tp, _Dp> argument_type;
3117 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003118 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003119 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003120 {
3121 typedef typename argument_type::pointer pointer;
3122 return hash<pointer>()(__ptr.get());
3123 }
3124};
3125
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126struct __destruct_n
3127{
3128private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003129 size_t __size_;
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* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003133 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134
3135 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003136 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137 {}
3138
Howard Hinnant719bda32011-05-28 14:41:13 +00003139 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003140 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003141 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142 {}
3143
Howard Hinnant719bda32011-05-28 14:41:13 +00003144 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003145 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003146 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 {}
3148public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003149 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003150 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151
3152 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003153 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003154 {__incr(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 __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003158 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159
3160 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003161 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003162 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163};
3164
3165template <class _Alloc>
3166class __allocator_destructor
3167{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003168 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003170 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3171 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172private:
3173 _Alloc& __alloc_;
3174 size_type __s_;
3175public:
3176 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003177 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003180 void operator()(pointer __p) _NOEXCEPT
3181 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182};
3183
3184template <class _InputIterator, class _ForwardIterator>
3185_ForwardIterator
3186uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3187{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003189#ifndef _LIBCPP_NO_EXCEPTIONS
3190 _ForwardIterator __s = __r;
3191 try
3192 {
3193#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003194 for (; __f != __l; ++__f, (void) ++__r)
3195 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003196#ifndef _LIBCPP_NO_EXCEPTIONS
3197 }
3198 catch (...)
3199 {
3200 for (; __s != __r; ++__s)
3201 __s->~value_type();
3202 throw;
3203 }
3204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205 return __r;
3206}
3207
3208template <class _InputIterator, class _Size, class _ForwardIterator>
3209_ForwardIterator
3210uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3211{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003213#ifndef _LIBCPP_NO_EXCEPTIONS
3214 _ForwardIterator __s = __r;
3215 try
3216 {
3217#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003218 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3219 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003220#ifndef _LIBCPP_NO_EXCEPTIONS
3221 }
3222 catch (...)
3223 {
3224 for (; __s != __r; ++__s)
3225 __s->~value_type();
3226 throw;
3227 }
3228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 return __r;
3230}
3231
3232template <class _ForwardIterator, class _Tp>
3233void
3234uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3235{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003237#ifndef _LIBCPP_NO_EXCEPTIONS
3238 _ForwardIterator __s = __f;
3239 try
3240 {
3241#endif
3242 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003243 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003244#ifndef _LIBCPP_NO_EXCEPTIONS
3245 }
3246 catch (...)
3247 {
3248 for (; __s != __f; ++__s)
3249 __s->~value_type();
3250 throw;
3251 }
3252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253}
3254
3255template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003256_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3258{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003260#ifndef _LIBCPP_NO_EXCEPTIONS
3261 _ForwardIterator __s = __f;
3262 try
3263 {
3264#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003265 for (; __n > 0; ++__f, (void) --__n)
3266 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003267#ifndef _LIBCPP_NO_EXCEPTIONS
3268 }
3269 catch (...)
3270 {
3271 for (; __s != __f; ++__s)
3272 __s->~value_type();
3273 throw;
3274 }
3275#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003276 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277}
3278
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003279#if _LIBCPP_STD_VER > 14
3280
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003281template <class _Tp>
3282inline _LIBCPP_INLINE_VISIBILITY
3283void destroy_at(_Tp* __loc) {
3284 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3285 __loc->~_Tp();
3286}
3287
3288template <class _ForwardIterator>
3289inline _LIBCPP_INLINE_VISIBILITY
3290void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3291 for (; __first != __last; ++__first)
3292 _VSTD::destroy_at(_VSTD::addressof(*__first));
3293}
3294
3295template <class _ForwardIterator, class _Size>
3296inline _LIBCPP_INLINE_VISIBILITY
3297_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3298 for (; __n > 0; (void)++__first, --__n)
3299 _VSTD::destroy_at(_VSTD::addressof(*__first));
3300 return __first;
3301}
3302
Eric Fiselier290c07c2016-10-11 21:13:44 +00003303template <class _ForwardIterator>
3304inline _LIBCPP_INLINE_VISIBILITY
3305void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3306 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3307 auto __idx = __first;
3308#ifndef _LIBCPP_NO_EXCEPTIONS
3309 try {
3310#endif
3311 for (; __idx != __last; ++__idx)
3312 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3313#ifndef _LIBCPP_NO_EXCEPTIONS
3314 } catch (...) {
3315 _VSTD::destroy(__first, __idx);
3316 throw;
3317 }
3318#endif
3319}
3320
3321template <class _ForwardIterator, class _Size>
3322inline _LIBCPP_INLINE_VISIBILITY
3323_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3324 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3325 auto __idx = __first;
3326#ifndef _LIBCPP_NO_EXCEPTIONS
3327 try {
3328#endif
3329 for (; __n > 0; (void)++__idx, --__n)
3330 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3331 return __idx;
3332#ifndef _LIBCPP_NO_EXCEPTIONS
3333 } catch (...) {
3334 _VSTD::destroy(__first, __idx);
3335 throw;
3336 }
3337#endif
3338}
3339
3340
3341template <class _ForwardIterator>
3342inline _LIBCPP_INLINE_VISIBILITY
3343void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3344 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3345 auto __idx = __first;
3346#ifndef _LIBCPP_NO_EXCEPTIONS
3347 try {
3348#endif
3349 for (; __idx != __last; ++__idx)
3350 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3351#ifndef _LIBCPP_NO_EXCEPTIONS
3352 } catch (...) {
3353 _VSTD::destroy(__first, __idx);
3354 throw;
3355 }
3356#endif
3357}
3358
3359template <class _ForwardIterator, class _Size>
3360inline _LIBCPP_INLINE_VISIBILITY
3361_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3362 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3363 auto __idx = __first;
3364#ifndef _LIBCPP_NO_EXCEPTIONS
3365 try {
3366#endif
3367 for (; __n > 0; (void)++__idx, --__n)
3368 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3369 return __idx;
3370#ifndef _LIBCPP_NO_EXCEPTIONS
3371 } catch (...) {
3372 _VSTD::destroy(__first, __idx);
3373 throw;
3374 }
3375#endif
3376}
3377
3378
3379template <class _InputIt, class _ForwardIt>
3380inline _LIBCPP_INLINE_VISIBILITY
3381_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3382 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3383 auto __idx = __first_res;
3384#ifndef _LIBCPP_NO_EXCEPTIONS
3385 try {
3386#endif
3387 for (; __first != __last; (void)++__idx, ++__first)
3388 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3389 return __idx;
3390#ifndef _LIBCPP_NO_EXCEPTIONS
3391 } catch (...) {
3392 _VSTD::destroy(__first_res, __idx);
3393 throw;
3394 }
3395#endif
3396}
3397
3398template <class _InputIt, class _Size, class _ForwardIt>
3399inline _LIBCPP_INLINE_VISIBILITY
3400pair<_InputIt, _ForwardIt>
3401uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3402 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3403 auto __idx = __first_res;
3404#ifndef _LIBCPP_NO_EXCEPTIONS
3405 try {
3406#endif
3407 for (; __n > 0; ++__idx, (void)++__first, --__n)
3408 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3409 return {__first, __idx};
3410#ifndef _LIBCPP_NO_EXCEPTIONS
3411 } catch (...) {
3412 _VSTD::destroy(__first_res, __idx);
3413 throw;
3414 }
3415#endif
3416}
3417
3418
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003419#endif // _LIBCPP_STD_VER > 14
3420
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003421// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3422// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003423// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003424#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3425 && defined(__ATOMIC_RELAXED) \
3426 && defined(__ATOMIC_ACQ_REL)
3427# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003428#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003429# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3430#endif
3431
3432template <class _Tp>
3433inline _LIBCPP_INLINE_VISIBILITY _Tp
3434__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3435{
3436#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3437 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3438#else
3439 return __t += 1;
3440#endif
3441}
3442
3443template <class _Tp>
3444inline _LIBCPP_INLINE_VISIBILITY _Tp
3445__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3446{
3447#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3448 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3449#else
3450 return __t -= 1;
3451#endif
3452}
3453
Howard Hinnant756c69b2010-09-22 16:48:34 +00003454class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455 : public std::exception
3456{
3457public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003458 virtual ~bad_weak_ptr() _NOEXCEPT;
3459 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460};
3461
Louis Dionne16fe2952018-07-11 23:14:33 +00003462_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003463void __throw_bad_weak_ptr()
3464{
3465#ifndef _LIBCPP_NO_EXCEPTIONS
3466 throw bad_weak_ptr();
3467#else
3468 _VSTD::abort();
3469#endif
3470}
3471
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003472template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003474class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475{
3476 __shared_count(const __shared_count&);
3477 __shared_count& operator=(const __shared_count&);
3478
3479protected:
3480 long __shared_owners_;
3481 virtual ~__shared_count();
3482private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003483 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484
3485public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003487 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488 : __shared_owners_(__refs) {}
3489
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003490#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003491 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003492 void __add_shared() _NOEXCEPT;
3493 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003494#else
3495 _LIBCPP_INLINE_VISIBILITY
3496 void __add_shared() _NOEXCEPT {
3497 __libcpp_atomic_refcount_increment(__shared_owners_);
3498 }
3499 _LIBCPP_INLINE_VISIBILITY
3500 bool __release_shared() _NOEXCEPT {
3501 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3502 __on_zero_shared();
3503 return true;
3504 }
3505 return false;
3506 }
3507#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003508 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003509 long use_count() const _NOEXCEPT {
3510 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3511 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512};
3513
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003514class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515 : private __shared_count
3516{
3517 long __shared_weak_owners_;
3518
3519public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003521 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 : __shared_count(__refs),
3523 __shared_weak_owners_(__refs) {}
3524protected:
3525 virtual ~__shared_weak_count();
3526
3527public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003528#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003529 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003530 void __add_shared() _NOEXCEPT;
3531 void __add_weak() _NOEXCEPT;
3532 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003533#else
3534 _LIBCPP_INLINE_VISIBILITY
3535 void __add_shared() _NOEXCEPT {
3536 __shared_count::__add_shared();
3537 }
3538 _LIBCPP_INLINE_VISIBILITY
3539 void __add_weak() _NOEXCEPT {
3540 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3541 }
3542 _LIBCPP_INLINE_VISIBILITY
3543 void __release_shared() _NOEXCEPT {
3544 if (__shared_count::__release_shared())
3545 __release_weak();
3546 }
3547#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003548 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3551 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552
Howard Hinnant807d6332013-02-25 15:50:36 +00003553 // Define the function out only if we build static libc++ without RTTI.
3554 // Otherwise we may break clients who need to compile their projects with
3555 // -fno-rtti and yet link against a libc++.dylib compiled
3556 // without -fno-rtti.
3557#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003558 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003559#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003560private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003561 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562};
3563
3564template <class _Tp, class _Dp, class _Alloc>
3565class __shared_ptr_pointer
3566 : public __shared_weak_count
3567{
3568 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3569public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003572 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573
Howard Hinnant72f73582010-08-11 17:04:31 +00003574#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003575 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003576#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577
3578private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003579 virtual void __on_zero_shared() _NOEXCEPT;
3580 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581};
3582
Howard Hinnant72f73582010-08-11 17:04:31 +00003583#ifndef _LIBCPP_NO_RTTI
3584
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585template <class _Tp, class _Dp, class _Alloc>
3586const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003587__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003589 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003592#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003593
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594template <class _Tp, class _Dp, class _Alloc>
3595void
Howard Hinnant719bda32011-05-28 14:41:13 +00003596__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597{
3598 __data_.first().second()(__data_.first().first());
3599 __data_.first().second().~_Dp();
3600}
3601
3602template <class _Tp, class _Dp, class _Alloc>
3603void
Howard Hinnant719bda32011-05-28 14:41:13 +00003604__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003606 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3607 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003608 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3609
Eric Fiselierf8898c82015-02-05 23:01:40 +00003610 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003612 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613}
3614
3615template <class _Tp, class _Alloc>
3616class __shared_ptr_emplace
3617 : public __shared_weak_count
3618{
3619 __compressed_pair<_Alloc, _Tp> __data_;
3620public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621
Howard Hinnant756c69b2010-09-22 16:48:34 +00003622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003624 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003626
3627#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003631 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3632 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633#else // _LIBCPP_HAS_NO_VARIADICS
3634
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3638 : __data_(__a, _Tp(__a0)) {}
3639
3640 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3643 : __data_(__a, _Tp(__a0, __a1)) {}
3644
3645 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3648 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3649
3650#endif // _LIBCPP_HAS_NO_VARIADICS
3651
3652private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003653 virtual void __on_zero_shared() _NOEXCEPT;
3654 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003656 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003657 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658};
3659
3660template <class _Tp, class _Alloc>
3661void
Howard Hinnant719bda32011-05-28 14:41:13 +00003662__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663{
3664 __data_.second().~_Tp();
3665}
3666
3667template <class _Tp, class _Alloc>
3668void
Howard Hinnant719bda32011-05-28 14:41:13 +00003669__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003671 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3672 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003673 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003674 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003676 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677}
3678
Erik Pilkington2a398762017-05-25 15:43:31 +00003679struct __shared_ptr_dummy_rebind_allocator_type;
3680template <>
3681class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3682{
3683public:
3684 template <class _Other>
3685 struct rebind
3686 {
3687 typedef allocator<_Other> other;
3688 };
3689};
3690
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003691template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692
3693template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003694class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003696public:
3697 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003698
Eric Fiselierae5b6672016-06-27 01:02:43 +00003699#if _LIBCPP_STD_VER > 14
3700 typedef weak_ptr<_Tp> weak_type;
3701#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702private:
3703 element_type* __ptr_;
3704 __shared_weak_count* __cntrl_;
3705
3706 struct __nat {int __for_bool_;};
3707public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003709 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003711 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003712 template<class _Yp>
3713 explicit shared_ptr(_Yp* __p,
3714 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3715 template<class _Yp, class _Dp>
3716 shared_ptr(_Yp* __p, _Dp __d,
3717 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3718 template<class _Yp, class _Dp, class _Alloc>
3719 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3720 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3722 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003723 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003725 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003729 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003730 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003731#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003733 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003734 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003735 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003736 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003737#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003739 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003740#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003741#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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 Hinnant3b6579a2010-08-22 00:02:43 +00003745#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003746 template<class _Yp>
3747 shared_ptr(auto_ptr<_Yp> __r,
3748 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003750#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003751#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003752 template <class _Yp, class _Dp>
3753 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3754 typename enable_if
3755 <
3756 !is_lvalue_reference<_Dp>::value &&
3757 !is_array<_Yp>::value &&
3758 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3759 __nat
3760 >::type = __nat());
3761 template <class _Yp, class _Dp>
3762 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3763 typename enable_if
3764 <
3765 is_lvalue_reference<_Dp>::value &&
3766 !is_array<_Yp>::value &&
3767 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3768 __nat
3769 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003770#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003771 template <class _Yp, class _Dp>
3772 shared_ptr(unique_ptr<_Yp, _Dp>,
3773 typename enable_if
3774 <
3775 !is_lvalue_reference<_Dp>::value &&
3776 !is_array<_Yp>::value &&
3777 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3778 __nat
3779 >::type = __nat());
3780 template <class _Yp, class _Dp>
3781 shared_ptr(unique_ptr<_Yp, _Dp>,
3782 typename enable_if
3783 <
3784 is_lvalue_reference<_Dp>::value &&
3785 !is_array<_Yp>::value &&
3786 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3787 __nat
3788 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003789#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790
3791 ~shared_ptr();
3792
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003794 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003795 template<class _Yp>
3796 typename enable_if
3797 <
3798 is_convertible<_Yp*, element_type*>::value,
3799 shared_ptr&
3800 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003802 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003803#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003805 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003806 template<class _Yp>
3807 typename enable_if
3808 <
3809 is_convertible<_Yp*, element_type*>::value,
3810 shared_ptr<_Tp>&
3811 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003813 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003814#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003815 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003817 typename enable_if
3818 <
3819 !is_array<_Yp>::value &&
3820 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003821 shared_ptr
3822 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003823 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003824#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003825#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003826#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003827 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003829 typename enable_if
3830 <
3831 !is_array<_Yp>::value &&
3832 is_convertible<_Yp*, element_type*>::value,
3833 shared_ptr&
3834 >::type
3835 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003837#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003838 template <class _Yp, class _Dp>
3839 typename enable_if
3840 <
3841 !is_array<_Yp>::value &&
3842 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3843 shared_ptr&
3844 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003845#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003847 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003848#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003850 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851#endif
3852
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003854 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003856 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003857 template<class _Yp>
3858 typename enable_if
3859 <
3860 is_convertible<_Yp*, element_type*>::value,
3861 void
3862 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003864 reset(_Yp* __p);
3865 template<class _Yp, class _Dp>
3866 typename enable_if
3867 <
3868 is_convertible<_Yp*, element_type*>::value,
3869 void
3870 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003872 reset(_Yp* __p, _Dp __d);
3873 template<class _Yp, class _Dp, class _Alloc>
3874 typename enable_if
3875 <
3876 is_convertible<_Yp*, element_type*>::value,
3877 void
3878 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003880 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881
Howard Hinnant756c69b2010-09-22 16:48:34 +00003882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003883 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003885 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3886 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003888 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003890 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003894 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
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(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003899 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003900 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003901 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003903 _LIBCPP_INLINE_VISIBILITY
3904 bool
3905 __owner_equivalent(const shared_ptr& __p) const
3906 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907
Howard Hinnant72f73582010-08-11 17:04:31 +00003908#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003911 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003912 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003913 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003914 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003915#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916
Zoe Carverd9040c72019-10-22 15:16:49 +00003917 template<class _Yp, class _CntrlBlk>
3918 static shared_ptr<_Tp>
3919 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3920 {
3921 shared_ptr<_Tp> __r;
3922 __r.__ptr_ = __p;
3923 __r.__cntrl_ = __cntrl;
3924 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3925 return __r;
3926 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003927
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003929 template <class _Yp, bool = is_function<_Yp>::value>
3930 struct __shared_ptr_default_allocator
3931 {
3932 typedef allocator<_Yp> type;
3933 };
3934
3935 template <class _Yp>
3936 struct __shared_ptr_default_allocator<_Yp, true>
3937 {
3938 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3939 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003941 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003942 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003943 typename enable_if<is_convertible<_OrigPtr*,
3944 const enable_shared_from_this<_Yp>*
3945 >::value,
3946 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003947 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3948 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003950 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003951 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003952 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003953 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3954 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003955 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956 }
3957
Erik Pilkington2a398762017-05-25 15:43:31 +00003958 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003960 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3961 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962};
3963
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003964
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003966inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003967_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003968shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969 : __ptr_(0),
3970 __cntrl_(0)
3971{
3972}
3973
3974template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003975inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003976_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003977shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978 : __ptr_(0),
3979 __cntrl_(0)
3980{
3981}
3982
3983template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003984template<class _Yp>
3985shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3986 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 : __ptr_(__p)
3988{
3989 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003990 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3991 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3992 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003994 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995}
3996
3997template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003998template<class _Yp, class _Dp>
3999shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4000 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001 : __ptr_(__p)
4002{
4003#ifndef _LIBCPP_NO_EXCEPTIONS
4004 try
4005 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004006#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004007 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4008 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4009 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004010 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011#ifndef _LIBCPP_NO_EXCEPTIONS
4012 }
4013 catch (...)
4014 {
4015 __d(__p);
4016 throw;
4017 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004018#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019}
4020
4021template<class _Tp>
4022template<class _Dp>
4023shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4024 : __ptr_(0)
4025{
4026#ifndef _LIBCPP_NO_EXCEPTIONS
4027 try
4028 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004029#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004030 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4031 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4032 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033#ifndef _LIBCPP_NO_EXCEPTIONS
4034 }
4035 catch (...)
4036 {
4037 __d(__p);
4038 throw;
4039 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004040#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041}
4042
4043template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004044template<class _Yp, class _Dp, class _Alloc>
4045shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4046 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047 : __ptr_(__p)
4048{
4049#ifndef _LIBCPP_NO_EXCEPTIONS
4050 try
4051 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004052#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004054 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055 typedef __allocator_destructor<_A2> _D2;
4056 _A2 __a2(__a);
4057 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004058 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4059 _CntrlBlk(__p, __d, __a);
4060 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004061 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062#ifndef _LIBCPP_NO_EXCEPTIONS
4063 }
4064 catch (...)
4065 {
4066 __d(__p);
4067 throw;
4068 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004069#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070}
4071
4072template<class _Tp>
4073template<class _Dp, class _Alloc>
4074shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4075 : __ptr_(0)
4076{
4077#ifndef _LIBCPP_NO_EXCEPTIONS
4078 try
4079 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004080#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004082 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 typedef __allocator_destructor<_A2> _D2;
4084 _A2 __a2(__a);
4085 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004086 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4087 _CntrlBlk(__p, __d, __a);
4088 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089#ifndef _LIBCPP_NO_EXCEPTIONS
4090 }
4091 catch (...)
4092 {
4093 __d(__p);
4094 throw;
4095 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097}
4098
4099template<class _Tp>
4100template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004101inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004102shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103 : __ptr_(__p),
4104 __cntrl_(__r.__cntrl_)
4105{
4106 if (__cntrl_)
4107 __cntrl_->__add_shared();
4108}
4109
4110template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004111inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004112shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113 : __ptr_(__r.__ptr_),
4114 __cntrl_(__r.__cntrl_)
4115{
4116 if (__cntrl_)
4117 __cntrl_->__add_shared();
4118}
4119
4120template<class _Tp>
4121template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004122inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004124 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004125 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126 : __ptr_(__r.__ptr_),
4127 __cntrl_(__r.__cntrl_)
4128{
4129 if (__cntrl_)
4130 __cntrl_->__add_shared();
4131}
4132
Howard Hinnant74279a52010-09-04 23:28:19 +00004133#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134
4135template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004136inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004137shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138 : __ptr_(__r.__ptr_),
4139 __cntrl_(__r.__cntrl_)
4140{
4141 __r.__ptr_ = 0;
4142 __r.__cntrl_ = 0;
4143}
4144
4145template<class _Tp>
4146template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004147inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004149 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004150 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151 : __ptr_(__r.__ptr_),
4152 __cntrl_(__r.__cntrl_)
4153{
4154 __r.__ptr_ = 0;
4155 __r.__cntrl_ = 0;
4156}
4157
Howard Hinnant74279a52010-09-04 23:28:19 +00004158#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159
Marshall Clowb22274f2017-01-24 22:22:33 +00004160#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004162template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004163#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004164shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004166shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004168 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 : __ptr_(__r.get())
4170{
4171 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4172 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004173 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174 __r.release();
4175}
Marshall Clowb22274f2017-01-24 22:22:33 +00004176#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177
4178template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004179template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004180#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4182#else
4183shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4184#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004185 typename enable_if
4186 <
4187 !is_lvalue_reference<_Dp>::value &&
4188 !is_array<_Yp>::value &&
4189 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4190 __nat
4191 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192 : __ptr_(__r.get())
4193{
Marshall Clow35cde742015-05-10 13:59:45 +00004194#if _LIBCPP_STD_VER > 11
4195 if (__ptr_ == nullptr)
4196 __cntrl_ = nullptr;
4197 else
4198#endif
4199 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004200 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4201 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4202 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004203 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004204 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205 __r.release();
4206}
4207
4208template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004209template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004210#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4212#else
4213shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4214#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004215 typename enable_if
4216 <
4217 is_lvalue_reference<_Dp>::value &&
4218 !is_array<_Yp>::value &&
4219 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4220 __nat
4221 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222 : __ptr_(__r.get())
4223{
Marshall Clow35cde742015-05-10 13:59:45 +00004224#if _LIBCPP_STD_VER > 11
4225 if (__ptr_ == nullptr)
4226 __cntrl_ = nullptr;
4227 else
4228#endif
4229 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004230 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004231 typedef __shared_ptr_pointer<_Yp*,
4232 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004233 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05004234 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004235 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004236 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237 __r.release();
4238}
4239
Zoe Carver6cd05c32019-08-19 15:47:16 +00004240template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241shared_ptr<_Tp>::~shared_ptr()
4242{
4243 if (__cntrl_)
4244 __cntrl_->__release_shared();
4245}
4246
4247template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004250shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251{
4252 shared_ptr(__r).swap(*this);
4253 return *this;
4254}
4255
4256template<class _Tp>
4257template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004258inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004259typename enable_if
4260<
Marshall Clow7e384b72017-01-10 16:59:33 +00004261 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004262 shared_ptr<_Tp>&
4263>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004264shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265{
4266 shared_ptr(__r).swap(*this);
4267 return *this;
4268}
4269
Howard Hinnant74279a52010-09-04 23:28:19 +00004270#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271
4272template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004273inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004275shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 return *this;
4279}
4280
4281template<class _Tp>
4282template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004283inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004284typename enable_if
4285<
Marshall Clow7e384b72017-01-10 16:59:33 +00004286 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004287 shared_ptr<_Tp>&
4288>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4290{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004291 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 return *this;
4293}
4294
Marshall Clowb22274f2017-01-24 22:22:33 +00004295#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296template<class _Tp>
4297template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004298inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004299typename enable_if
4300<
4301 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004302 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004303 shared_ptr<_Tp>
4304>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4306{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004307 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308 return *this;
4309}
Marshall Clowb22274f2017-01-24 22:22:33 +00004310#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311
4312template<class _Tp>
4313template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004315typename enable_if
4316<
4317 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004318 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004319 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004320 shared_ptr<_Tp>&
4321>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4323{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004324 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325 return *this;
4326}
4327
Howard Hinnant74279a52010-09-04 23:28:19 +00004328#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329
Marshall Clowb22274f2017-01-24 22:22:33 +00004330#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331template<class _Tp>
4332template<class _Yp>
4333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004334typename enable_if
4335<
4336 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004337 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004338 shared_ptr<_Tp>&
4339>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004340shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341{
4342 shared_ptr(__r).swap(*this);
4343 return *this;
4344}
Marshall Clowb22274f2017-01-24 22:22:33 +00004345#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346
4347template<class _Tp>
4348template <class _Yp, class _Dp>
4349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004350typename enable_if
4351<
4352 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004353 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004354 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004355 shared_ptr<_Tp>&
4356>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4358{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004359 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360 return *this;
4361}
4362
Howard Hinnant74279a52010-09-04 23:28:19 +00004363#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364
4365template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004366inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367void
Howard Hinnant719bda32011-05-28 14:41:13 +00004368shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004370 _VSTD::swap(__ptr_, __r.__ptr_);
4371 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372}
4373
4374template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004375inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376void
Howard Hinnant719bda32011-05-28 14:41:13 +00004377shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378{
4379 shared_ptr().swap(*this);
4380}
4381
4382template<class _Tp>
4383template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004384inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004385typename enable_if
4386<
Marshall Clow7e384b72017-01-10 16:59:33 +00004387 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004388 void
4389>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390shared_ptr<_Tp>::reset(_Yp* __p)
4391{
4392 shared_ptr(__p).swap(*this);
4393}
4394
4395template<class _Tp>
4396template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004397inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004398typename enable_if
4399<
Marshall Clow7e384b72017-01-10 16:59:33 +00004400 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004401 void
4402>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4404{
4405 shared_ptr(__p, __d).swap(*this);
4406}
4407
4408template<class _Tp>
4409template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004410inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004411typename enable_if
4412<
Marshall Clow7e384b72017-01-10 16:59:33 +00004413 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004414 void
4415>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4417{
4418 shared_ptr(__p, __d, __a).swap(*this);
4419}
4420
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004421template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004423typename enable_if
4424<
4425 !is_array<_Tp>::value,
4426 shared_ptr<_Tp>
4427>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428make_shared(_Args&& ...__args)
4429{
Zoe Carverd9040c72019-10-22 15:16:49 +00004430 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4431 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4432 typedef allocator<_CntrlBlk> _A2;
4433 typedef __allocator_destructor<_A2> _D2;
4434
4435 _A2 __a2;
4436 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4437 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4438
4439 _Tp *__ptr = __hold2.get()->get();
4440 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441}
4442
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004443template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004445typename enable_if
4446<
4447 !is_array<_Tp>::value,
4448 shared_ptr<_Tp>
4449>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450allocate_shared(const _Alloc& __a, _Args&& ...__args)
4451{
zoecarver505730a2020-02-25 16:50:57 -08004452 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4453
4454 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4455 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4456 typedef __allocator_destructor<_A2> _D2;
4457
4458 _A2 __a2(__a);
4459 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4460 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4461 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4462
4463 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4464 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004465}
4466
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467template<class _Tp, class _Up>
4468inline _LIBCPP_INLINE_VISIBILITY
4469bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004470operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471{
4472 return __x.get() == __y.get();
4473}
4474
4475template<class _Tp, class _Up>
4476inline _LIBCPP_INLINE_VISIBILITY
4477bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004478operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479{
4480 return !(__x == __y);
4481}
4482
4483template<class _Tp, class _Up>
4484inline _LIBCPP_INLINE_VISIBILITY
4485bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004486operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004488#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004489 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4490 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004491#else
4492 return less<>()(__x.get(), __y.get());
4493#endif
4494
Howard Hinnantb17caf92012-02-21 21:02:58 +00004495}
4496
4497template<class _Tp, class _Up>
4498inline _LIBCPP_INLINE_VISIBILITY
4499bool
4500operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4501{
4502 return __y < __x;
4503}
4504
4505template<class _Tp, class _Up>
4506inline _LIBCPP_INLINE_VISIBILITY
4507bool
4508operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4509{
4510 return !(__y < __x);
4511}
4512
4513template<class _Tp, class _Up>
4514inline _LIBCPP_INLINE_VISIBILITY
4515bool
4516operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4517{
4518 return !(__x < __y);
4519}
4520
4521template<class _Tp>
4522inline _LIBCPP_INLINE_VISIBILITY
4523bool
4524operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4525{
4526 return !__x;
4527}
4528
4529template<class _Tp>
4530inline _LIBCPP_INLINE_VISIBILITY
4531bool
4532operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4533{
4534 return !__x;
4535}
4536
4537template<class _Tp>
4538inline _LIBCPP_INLINE_VISIBILITY
4539bool
4540operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4541{
4542 return static_cast<bool>(__x);
4543}
4544
4545template<class _Tp>
4546inline _LIBCPP_INLINE_VISIBILITY
4547bool
4548operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4549{
4550 return static_cast<bool>(__x);
4551}
4552
4553template<class _Tp>
4554inline _LIBCPP_INLINE_VISIBILITY
4555bool
4556operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4557{
4558 return less<_Tp*>()(__x.get(), nullptr);
4559}
4560
4561template<class _Tp>
4562inline _LIBCPP_INLINE_VISIBILITY
4563bool
4564operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4565{
4566 return less<_Tp*>()(nullptr, __x.get());
4567}
4568
4569template<class _Tp>
4570inline _LIBCPP_INLINE_VISIBILITY
4571bool
4572operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4573{
4574 return nullptr < __x;
4575}
4576
4577template<class _Tp>
4578inline _LIBCPP_INLINE_VISIBILITY
4579bool
4580operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4581{
4582 return __x < nullptr;
4583}
4584
4585template<class _Tp>
4586inline _LIBCPP_INLINE_VISIBILITY
4587bool
4588operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4589{
4590 return !(nullptr < __x);
4591}
4592
4593template<class _Tp>
4594inline _LIBCPP_INLINE_VISIBILITY
4595bool
4596operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4597{
4598 return !(__x < nullptr);
4599}
4600
4601template<class _Tp>
4602inline _LIBCPP_INLINE_VISIBILITY
4603bool
4604operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4605{
4606 return !(__x < nullptr);
4607}
4608
4609template<class _Tp>
4610inline _LIBCPP_INLINE_VISIBILITY
4611bool
4612operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4613{
4614 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004615}
4616
4617template<class _Tp>
4618inline _LIBCPP_INLINE_VISIBILITY
4619void
Howard Hinnant719bda32011-05-28 14:41:13 +00004620swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004621{
4622 __x.swap(__y);
4623}
4624
4625template<class _Tp, class _Up>
4626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004627typename enable_if
4628<
4629 !is_array<_Tp>::value && !is_array<_Up>::value,
4630 shared_ptr<_Tp>
4631>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004632static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004633{
4634 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4635}
4636
4637template<class _Tp, class _Up>
4638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004639typename enable_if
4640<
4641 !is_array<_Tp>::value && !is_array<_Up>::value,
4642 shared_ptr<_Tp>
4643>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004644dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004645{
4646 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4647 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4648}
4649
4650template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004651typename enable_if
4652<
4653 is_array<_Tp>::value == is_array<_Up>::value,
4654 shared_ptr<_Tp>
4655>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004656const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004657{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004658 typedef typename remove_extent<_Tp>::type _RTp;
4659 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004660}
4661
Howard Hinnant72f73582010-08-11 17:04:31 +00004662#ifndef _LIBCPP_NO_RTTI
4663
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664template<class _Dp, class _Tp>
4665inline _LIBCPP_INLINE_VISIBILITY
4666_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004667get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004668{
4669 return __p.template __get_deleter<_Dp>();
4670}
4671
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004672#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004673
Howard Hinnantc51e1022010-05-11 19:42:16 +00004674template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004675class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004676{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004677public:
4678 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004679private:
4680 element_type* __ptr_;
4681 __shared_weak_count* __cntrl_;
4682
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004683public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004685 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004686 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004687 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4688 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004690 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004691 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004692 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4693 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004694
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004695#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004697 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004698 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004699 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4700 _NOEXCEPT;
4701#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004702 ~weak_ptr();
4703
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004705 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004706 template<class _Yp>
4707 typename enable_if
4708 <
4709 is_convertible<_Yp*, element_type*>::value,
4710 weak_ptr&
4711 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004713 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4714
4715#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4716
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004718 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4719 template<class _Yp>
4720 typename enable_if
4721 <
4722 is_convertible<_Yp*, element_type*>::value,
4723 weak_ptr&
4724 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004726 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4727
4728#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4729
4730 template<class _Yp>
4731 typename enable_if
4732 <
4733 is_convertible<_Yp*, element_type*>::value,
4734 weak_ptr&
4735 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004737 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004738
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004740 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004742 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004743
Howard Hinnant756c69b2010-09-22 16:48:34 +00004744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004745 long use_count() const _NOEXCEPT
4746 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004748 bool expired() const _NOEXCEPT
4749 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4750 shared_ptr<_Tp> lock() const _NOEXCEPT;
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 shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004754 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004755 template<class _Up>
4756 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004757 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004758 {return __cntrl_ < __r.__cntrl_;}
4759
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004760 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4761 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004762};
4763
4764template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004765inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004766_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004767weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004768 : __ptr_(0),
4769 __cntrl_(0)
4770{
4771}
4772
4773template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004774inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004775weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004776 : __ptr_(__r.__ptr_),
4777 __cntrl_(__r.__cntrl_)
4778{
4779 if (__cntrl_)
4780 __cntrl_->__add_weak();
4781}
4782
4783template<class _Tp>
4784template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004785inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004786weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004787 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004788 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004789 : __ptr_(__r.__ptr_),
4790 __cntrl_(__r.__cntrl_)
4791{
4792 if (__cntrl_)
4793 __cntrl_->__add_weak();
4794}
4795
4796template<class _Tp>
4797template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004799weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004800 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004801 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004802 : __ptr_(__r.__ptr_),
4803 __cntrl_(__r.__cntrl_)
4804{
4805 if (__cntrl_)
4806 __cntrl_->__add_weak();
4807}
4808
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004809#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4810
4811template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004812inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004813weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4814 : __ptr_(__r.__ptr_),
4815 __cntrl_(__r.__cntrl_)
4816{
4817 __r.__ptr_ = 0;
4818 __r.__cntrl_ = 0;
4819}
4820
4821template<class _Tp>
4822template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004823inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004824weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4825 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4826 _NOEXCEPT
4827 : __ptr_(__r.__ptr_),
4828 __cntrl_(__r.__cntrl_)
4829{
4830 __r.__ptr_ = 0;
4831 __r.__cntrl_ = 0;
4832}
4833
4834#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4835
Howard Hinnantc51e1022010-05-11 19:42:16 +00004836template<class _Tp>
4837weak_ptr<_Tp>::~weak_ptr()
4838{
4839 if (__cntrl_)
4840 __cntrl_->__release_weak();
4841}
4842
4843template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004844inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004845weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004846weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004847{
4848 weak_ptr(__r).swap(*this);
4849 return *this;
4850}
4851
4852template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004853template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004854inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004855typename enable_if
4856<
4857 is_convertible<_Yp*, _Tp*>::value,
4858 weak_ptr<_Tp>&
4859>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004860weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004861{
4862 weak_ptr(__r).swap(*this);
4863 return *this;
4864}
4865
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004866#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4867
4868template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004869inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004870weak_ptr<_Tp>&
4871weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4872{
4873 weak_ptr(_VSTD::move(__r)).swap(*this);
4874 return *this;
4875}
4876
Howard Hinnantc51e1022010-05-11 19:42:16 +00004877template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004878template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004879inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004880typename enable_if
4881<
4882 is_convertible<_Yp*, _Tp*>::value,
4883 weak_ptr<_Tp>&
4884>::type
4885weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4886{
4887 weak_ptr(_VSTD::move(__r)).swap(*this);
4888 return *this;
4889}
4890
4891#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4892
4893template<class _Tp>
4894template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004895inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004896typename enable_if
4897<
4898 is_convertible<_Yp*, _Tp*>::value,
4899 weak_ptr<_Tp>&
4900>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004901weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004902{
4903 weak_ptr(__r).swap(*this);
4904 return *this;
4905}
4906
4907template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004908inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004909void
Howard Hinnant719bda32011-05-28 14:41:13 +00004910weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004912 _VSTD::swap(__ptr_, __r.__ptr_);
4913 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004914}
4915
4916template<class _Tp>
4917inline _LIBCPP_INLINE_VISIBILITY
4918void
Howard Hinnant719bda32011-05-28 14:41:13 +00004919swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004920{
4921 __x.swap(__y);
4922}
4923
4924template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004925inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004926void
Howard Hinnant719bda32011-05-28 14:41:13 +00004927weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004928{
4929 weak_ptr().swap(*this);
4930}
4931
4932template<class _Tp>
4933template<class _Yp>
4934shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004935 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004936 : __ptr_(__r.__ptr_),
4937 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4938{
4939 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004940 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004941}
4942
4943template<class _Tp>
4944shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004945weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004946{
4947 shared_ptr<_Tp> __r;
4948 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4949 if (__r.__cntrl_)
4950 __r.__ptr_ = __ptr_;
4951 return __r;
4952}
4953
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004954#if _LIBCPP_STD_VER > 14
4955template <class _Tp = void> struct owner_less;
4956#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004957template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004958#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004959
4960template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004961struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004962 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004963{
4964 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004965 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004966 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004967 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004968 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004969 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004970 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004971 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004972 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004973 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004974};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975
4976template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004977struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004978 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4979{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004980 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004981 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004982 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004983 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004984 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004985 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004987 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004988 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004989 {return __x.owner_before(__y);}
4990};
4991
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004992#if _LIBCPP_STD_VER > 14
4993template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004994struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004995{
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, shared_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()( shared_ptr<_Tp> const& __x, weak_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, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005007 {return __x.owner_before(__y);}
5008 template <class _Tp, class _Up>
5009 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005010 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005011 {return __x.owner_before(__y);}
5012 typedef void is_transparent;
5013};
5014#endif
5015
Howard Hinnantc51e1022010-05-11 19:42:16 +00005016template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005017class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018{
5019 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005020protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005021 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005022 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005024 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005026 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5027 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005029 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005030public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005032 shared_ptr<_Tp> shared_from_this()
5033 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005035 shared_ptr<_Tp const> shared_from_this() const
5036 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005037
Eric Fiselier84006862016-06-02 00:15:35 +00005038#if _LIBCPP_STD_VER > 14
5039 _LIBCPP_INLINE_VISIBILITY
5040 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5041 { return __weak_this_; }
5042
5043 _LIBCPP_INLINE_VISIBILITY
5044 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5045 { return __weak_this_; }
5046#endif // _LIBCPP_STD_VER > 14
5047
Howard Hinnantc51e1022010-05-11 19:42:16 +00005048 template <class _Up> friend class shared_ptr;
5049};
5050
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005051template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005052struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005053{
5054 typedef shared_ptr<_Tp> argument_type;
5055 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005056
Howard Hinnant756c69b2010-09-22 16:48:34 +00005057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005058 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005059 {
5060 return hash<_Tp*>()(__ptr.get());
5061 }
5062};
5063
Howard Hinnantc834c512011-11-29 18:15:50 +00005064template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005065inline _LIBCPP_INLINE_VISIBILITY
5066basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005067operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005068
Eric Fiselier9b492672016-06-18 02:12:53 +00005069
5070#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005071
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005072class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005073{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005074 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005075public:
5076 void lock() _NOEXCEPT;
5077 void unlock() _NOEXCEPT;
5078
5079private:
5080 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5081 __sp_mut(const __sp_mut&);
5082 __sp_mut& operator=(const __sp_mut&);
5083
Howard Hinnant8331b762013-03-06 23:30:19 +00005084 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005085};
5086
Mehdi Amini228053d2017-05-04 17:08:54 +00005087_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5088__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005089
5090template <class _Tp>
5091inline _LIBCPP_INLINE_VISIBILITY
5092bool
5093atomic_is_lock_free(const shared_ptr<_Tp>*)
5094{
5095 return false;
5096}
5097
5098template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005099_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005100shared_ptr<_Tp>
5101atomic_load(const shared_ptr<_Tp>* __p)
5102{
5103 __sp_mut& __m = __get_sp_mut(__p);
5104 __m.lock();
5105 shared_ptr<_Tp> __q = *__p;
5106 __m.unlock();
5107 return __q;
5108}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005109
Howard Hinnant9fa30202012-07-30 01:40:57 +00005110template <class _Tp>
5111inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005112_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005113shared_ptr<_Tp>
5114atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5115{
5116 return atomic_load(__p);
5117}
5118
5119template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005120_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005121void
5122atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5123{
5124 __sp_mut& __m = __get_sp_mut(__p);
5125 __m.lock();
5126 __p->swap(__r);
5127 __m.unlock();
5128}
5129
5130template <class _Tp>
5131inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005132_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005133void
5134atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5135{
5136 atomic_store(__p, __r);
5137}
5138
5139template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005140_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005141shared_ptr<_Tp>
5142atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5143{
5144 __sp_mut& __m = __get_sp_mut(__p);
5145 __m.lock();
5146 __p->swap(__r);
5147 __m.unlock();
5148 return __r;
5149}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005150
Howard Hinnant9fa30202012-07-30 01:40:57 +00005151template <class _Tp>
5152inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005153_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005154shared_ptr<_Tp>
5155atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5156{
5157 return atomic_exchange(__p, __r);
5158}
5159
5160template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005161_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005162bool
5163atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5164{
Marshall Clow4201ee82016-05-18 17:50:13 +00005165 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005166 __sp_mut& __m = __get_sp_mut(__p);
5167 __m.lock();
5168 if (__p->__owner_equivalent(*__v))
5169 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005170 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005171 *__p = __w;
5172 __m.unlock();
5173 return true;
5174 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005175 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005176 *__v = *__p;
5177 __m.unlock();
5178 return false;
5179}
5180
5181template <class _Tp>
5182inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005183_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005184bool
5185atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5186{
5187 return atomic_compare_exchange_strong(__p, __v, __w);
5188}
5189
5190template <class _Tp>
5191inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005192_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005193bool
5194atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5195 shared_ptr<_Tp> __w, memory_order, memory_order)
5196{
5197 return atomic_compare_exchange_strong(__p, __v, __w);
5198}
5199
5200template <class _Tp>
5201inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005202_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005203bool
5204atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5205 shared_ptr<_Tp> __w, memory_order, memory_order)
5206{
5207 return atomic_compare_exchange_weak(__p, __v, __w);
5208}
5209
Eric Fiselier9b492672016-06-18 02:12:53 +00005210#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005211
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005212//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005213#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5214# ifndef _LIBCPP_CXX03_LANG
5215enum class pointer_safety : unsigned char {
5216 relaxed,
5217 preferred,
5218 strict
5219};
5220# endif
5221#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005222struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005223{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005224 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225 {
5226 relaxed,
5227 preferred,
5228 strict
5229 };
5230
Howard Hinnant49e145e2012-10-30 19:06:59 +00005231 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005232
Howard Hinnant756c69b2010-09-22 16:48:34 +00005233 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005234 pointer_safety() : __v_() {}
5235
5236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005237 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005239 operator int() const {return __v_;}
5240};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005241#endif
5242
5243#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005244 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005245_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5246#else
5247// This function is only offered in C++03 under ABI v1.
5248# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5249inline _LIBCPP_INLINE_VISIBILITY
5250pointer_safety get_pointer_safety() _NOEXCEPT {
5251 return pointer_safety::relaxed;
5252}
5253# endif
5254#endif
5255
Howard Hinnantc51e1022010-05-11 19:42:16 +00005256
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005257_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5258_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5259_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005260_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005261
5262template <class _Tp>
5263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005264_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005265undeclare_reachable(_Tp* __p)
5266{
5267 return static_cast<_Tp*>(__undeclare_reachable(__p));
5268}
5269
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005270_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005271
Marshall Clow8982dcd2015-07-13 20:04:56 +00005272// --- Helper for container swap --
5273template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005274inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005275void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5276#if _LIBCPP_STD_VER >= 14
5277 _NOEXCEPT
5278#else
5279 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5280#endif
5281{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005282 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005283 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5284}
5285
5286template <typename _Alloc>
5287_LIBCPP_INLINE_VISIBILITY
5288void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5289#if _LIBCPP_STD_VER >= 14
5290 _NOEXCEPT
5291#else
5292 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5293#endif
5294{
5295 using _VSTD::swap;
5296 swap(__a1, __a2);
5297}
5298
5299template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005300inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005301void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5302
Marshall Clowff91de82015-08-18 19:51:37 +00005303template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005304struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005305 _Traits::propagate_on_container_move_assignment::value
5306#if _LIBCPP_STD_VER > 14
5307 || _Traits::is_always_equal::value
5308#else
5309 && is_nothrow_move_assignable<_Alloc>::value
5310#endif
5311 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005312
Marshall Clowa591b9a2016-07-11 21:38:08 +00005313
5314#ifndef _LIBCPP_HAS_NO_VARIADICS
5315template <class _Tp, class _Alloc>
5316struct __temp_value {
5317 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005318
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005319 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005320 _Alloc &__a;
5321
5322 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5323 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005324
Marshall Clowa591b9a2016-07-11 21:38:08 +00005325 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005326 _LIBCPP_NO_CFI
5327 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5328 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5329 _VSTD::forward<_Args>(__args)...);
5330 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005331
Marshall Clowa591b9a2016-07-11 21:38:08 +00005332 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5333 };
5334#endif
5335
Marshall Clowe46031a2018-07-02 18:41:15 +00005336template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005337struct __is_allocator : false_type {};
5338
5339template<typename _Alloc>
5340struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005341 typename __void_t<typename _Alloc::value_type>::type,
5342 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5343 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005344 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005345
Eric Fiselier74ebee62019-06-08 01:31:19 +00005346// __builtin_new_allocator -- A non-templated helper for allocating and
5347// deallocating memory using __builtin_operator_new and
5348// __builtin_operator_delete. It should be used in preference to
5349// `std::allocator<T>` to avoid additional instantiations.
5350struct __builtin_new_allocator {
5351 struct __builtin_new_deleter {
5352 typedef void* pointer_type;
5353
5354 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5355 : __size_(__size), __align_(__align) {}
5356
5357 void operator()(void* p) const _NOEXCEPT {
5358 std::__libcpp_deallocate(p, __size_, __align_);
5359 }
5360
5361 private:
5362 size_t __size_;
5363 size_t __align_;
5364 };
5365
5366 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5367
5368 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5369 return __holder_t(std::__libcpp_allocate(__s, __align),
5370 __builtin_new_deleter(__s, __align));
5371 }
5372
5373 static void __deallocate_bytes(void* __p, size_t __s,
5374 size_t __align) _NOEXCEPT {
5375 std::__libcpp_deallocate(__p, __s, __align);
5376 }
5377
5378 template <class _Tp>
5379 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5380 static __holder_t __allocate_type(size_t __n) {
5381 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5382 }
5383
5384 template <class _Tp>
5385 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5386 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5387 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5388 }
5389};
5390
5391
Howard Hinnantc51e1022010-05-11 19:42:16 +00005392_LIBCPP_END_NAMESPACE_STD
5393
Eric Fiselierf4433a32017-05-31 22:07:49 +00005394_LIBCPP_POP_MACROS
5395
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005396#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005397# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005398#endif
5399
Howard Hinnantc51e1022010-05-11 19:42:16 +00005400#endif // _LIBCPP_MEMORY