blob: 883a01d7d2f092d9cb33f806858dbee5bd1dd79d [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
Marek Kurdej1f0cde92020-12-06 15:23:46 +010049template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
Eric Fiselier45c9aac2017-11-22 19:49:21 +000050
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
Louis Dionne99f59432020-09-17 12:06:13 -040086 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Louis Dionne99f59432020-09-17 12:06:13 -040089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
Louis Dionne99f59432020-09-17 12:06:13 -040092 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
94 template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -040095 static void destroy(allocator_type& a, T* p); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Louis Dionne99f59432020-09-17 12:06:13 -040097 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000099};
100
101template <>
Michael Park99f9e912020-03-04 11:27:14 -0500102class allocator<void> // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
Louis Dionnec0056af2020-08-28 12:31:16 -0400116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
Michael Park99f9e912020-03-04 11:27:14 -0500118 typedef T* pointer; // deprecated in C++17, removed in C++20
119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20
120 typedef typename add_lvalue_reference<T>::type
121 reference; // deprecated in C++17, removed in C++20
122 typedef typename add_lvalue_reference<const T>::type
123 const_reference; // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
Michael Park99f9e912020-03-04 11:27:14 -0500125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
128
129 typedef true_type propagate_on_container_move_assignment;
130 typedef true_type is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
Marshall Clowbc759762018-03-20 23:02:53 +0000132 constexpr allocator() noexcept; // constexpr in C++20
133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
134 template <class U>
135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400136 ~allocator(); // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
Louis Dionne99f59432020-09-17 12:06:13 -0400140 T* allocate(size_t n); // constexpr in C++20
141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
Michael Park99f9e912020-03-04 11:27:14 -0500142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000143 template<class U, class... Args>
Michael Park99f9e912020-03-04 11:27:14 -0500144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000145 template <class U>
Michael Park99f9e912020-03-04 11:27:14 -0500146 void destroy(U* p); // deprecated in C++17, removed in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147};
148
149template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
152template <class T, class U>
Louis Dionne99f59432020-09-17 12:06:13 -0400153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
155template <class OutputIterator, class T>
156class raw_storage_iterator
157 : public iterator<output_iterator_tag,
158 T, // purposefully not C++03
159 ptrdiff_t, // purposefully not C++03
160 T*, // purposefully not C++03
161 raw_storage_iterator&> // purposefully not C++03
162{
163public:
164 explicit raw_storage_iterator(OutputIterator x);
165 raw_storage_iterator& operator*();
166 raw_storage_iterator& operator=(const T& element);
167 raw_storage_iterator& operator++();
168 raw_storage_iterator operator++(int);
169};
170
Howard Hinnant719bda32011-05-28 14:41:13 +0000171template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
172template <class T> void return_temporary_buffer(T* p) noexcept;
173
174template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000175template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177template <class InputIterator, class ForwardIterator>
178ForwardIterator
179uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
180
Howard Hinnant719bda32011-05-28 14:41:13 +0000181template <class InputIterator, class Size, class ForwardIterator>
182ForwardIterator
183uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185template <class ForwardIterator, class T>
186void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
187
188template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000189ForwardIterator
190uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Louis Dionne99f59432020-09-17 12:06:13 -0400192template <class T, class ...Args>
193constexpr T* construct_at(T* location, Args&& ...args); // since C++20
194
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000195template <class T>
Louis Dionne99f59432020-09-17 12:06:13 -0400196void destroy_at(T* location); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000197
198template <class ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -0400199void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000200
201template <class ForwardIterator, class Size>
Louis Dionne99f59432020-09-17 12:06:13 -0400202ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000203
204template <class InputIterator, class ForwardIterator>
205 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
206
207template <class InputIterator, class Size, class ForwardIterator>
208 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
209
210template <class ForwardIterator>
211 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
212
213template <class ForwardIterator, class Size>
214 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
215
216template <class ForwardIterator>
217 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
218
219template <class ForwardIterator, class Size>
220 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
221
Louis Dionne481a2662018-09-23 18:35:00 +0000222template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223
224template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000225class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226{
227public:
228 typedef X element_type;
229
230 explicit auto_ptr(X* p =0) throw();
231 auto_ptr(auto_ptr&) throw();
232 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
233 auto_ptr& operator=(auto_ptr&) throw();
234 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
235 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
236 ~auto_ptr() throw();
237
238 typename add_lvalue_reference<X>::type operator*() const throw();
239 X* operator->() const throw();
240 X* get() const throw();
241 X* release() throw();
242 void reset(X* p =0) throw();
243
244 auto_ptr(auto_ptr_ref<X>) throw();
245 template<class Y> operator auto_ptr_ref<Y>() throw();
246 template<class Y> operator auto_ptr<Y>() throw();
247};
248
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000249template <class T>
250struct default_delete
251{
Howard Hinnant719bda32011-05-28 14:41:13 +0000252 constexpr default_delete() noexcept = default;
253 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254
Howard Hinnant719bda32011-05-28 14:41:13 +0000255 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000256};
257
258template <class T>
259struct default_delete<T[]>
260{
Howard Hinnant719bda32011-05-28 14:41:13 +0000261 constexpr default_delete() noexcept = default;
262 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000263 template <class U> void operator()(U*) const = delete;
264};
265
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000266template <class T, class D = default_delete<T>>
267class unique_ptr
268{
269public:
270 typedef see below pointer;
271 typedef T element_type;
272 typedef D deleter_type;
273
274 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000275 constexpr unique_ptr() noexcept;
276 explicit unique_ptr(pointer p) noexcept;
277 unique_ptr(pointer p, see below d1) noexcept;
278 unique_ptr(pointer p, see below d2) noexcept;
279 unique_ptr(unique_ptr&& u) noexcept;
280 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000281 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000283 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000284 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // destructor
287 ~unique_ptr();
288
289 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000290 unique_ptr& operator=(unique_ptr&& u) noexcept;
291 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
292 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // observers
295 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000296 pointer operator->() const noexcept;
297 pointer get() const noexcept;
298 deleter_type& get_deleter() noexcept;
299 const deleter_type& get_deleter() const noexcept;
300 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000301
302 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000303 pointer release() noexcept;
304 void reset(pointer p = pointer()) noexcept;
305 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000306};
307
308template <class T, class D>
309class unique_ptr<T[], D>
310{
311public:
312 typedef implementation-defined pointer;
313 typedef T element_type;
314 typedef D deleter_type;
315
316 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 constexpr unique_ptr() noexcept;
318 explicit unique_ptr(pointer p) noexcept;
319 unique_ptr(pointer p, see below d) noexcept;
320 unique_ptr(pointer p, see below d) noexcept;
321 unique_ptr(unique_ptr&& u) noexcept;
322 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000323
324 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 unique_ptr& operator=(unique_ptr&& u) noexcept;
329 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330
331 // observers
332 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000333 pointer get() const noexcept;
334 deleter_type& get_deleter() noexcept;
335 const deleter_type& get_deleter() const noexcept;
336 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 pointer release() noexcept;
340 void reset(pointer p = pointer()) noexcept;
341 void reset(nullptr_t) noexcept;
Vy Nguyene369bd92020-07-13 12:34:37 -0400342 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000343 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000344};
345
346template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000347 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000348
349template <class T1, class D1, class T2, class D2>
350 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353template <class T1, class D1, class T2, class D2>
354 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
355template <class T1, class D1, class T2, class D2>
356 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
357template <class T1, class D1, class T2, class D2>
358 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
359template <class T1, class D1, class T2, class D2>
360 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
361
Howard Hinnant719bda32011-05-28 14:41:13 +0000362template <class T, class D>
363 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
364template <class T, class D>
365 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
366template <class T, class D>
367 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
368template <class T, class D>
369 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
370
371template <class T, class D>
372 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
379template <class T, class D>
380 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
381template <class T, class D>
382 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
383template <class T, class D>
384 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
385template <class T, class D>
386 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
387
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000388class bad_weak_ptr
389 : public std::exception
390{
Howard Hinnant719bda32011-05-28 14:41:13 +0000391 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000392};
393
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000394template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
395template<class T> unique_ptr<T> make_unique(size_t n); // C++14
396template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
397
Marshall Clowe52a3242017-11-27 15:51:36 +0000398template<class E, class T, class Y, class D>
399 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
400
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000401template<class T>
402class shared_ptr
403{
404public:
405 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000406 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000407
408 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000409 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000410 template<class Y> explicit shared_ptr(Y* p);
411 template<class Y, class D> shared_ptr(Y* p, D d);
412 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
413 template <class D> shared_ptr(nullptr_t p, D d);
414 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
416 shared_ptr(const shared_ptr& r) noexcept;
417 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
418 shared_ptr(shared_ptr&& r) noexcept;
419 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000421 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000422 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
423 shared_ptr(nullptr_t) : shared_ptr() { }
424
425 // destructor:
426 ~shared_ptr();
427
428 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 shared_ptr& operator=(const shared_ptr& r) noexcept;
430 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
431 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000432 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000433 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000434 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
435
436 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000437 void swap(shared_ptr& r) noexcept;
438 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000439 template<class Y> void reset(Y* p);
440 template<class Y, class D> void reset(Y* p, D d);
441 template<class Y, class D, class A> void reset(Y* p, D d, A a);
442
Howard Hinnant719bda32011-05-28 14:41:13 +0000443 // observers:
444 T* get() const noexcept;
445 T& operator*() const noexcept;
446 T* operator->() const noexcept;
447 long use_count() const noexcept;
448 bool unique() const noexcept;
449 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000450 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
451 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452};
453
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400454template<class T>
455shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
456template<class T, class D>
457shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
458
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000459// shared_ptr comparisons:
460template<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;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000466template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000467 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000468template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000469 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000470template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000471 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
472
473template <class T>
474 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
485template <class T>
486 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
487template <class T>
488 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
489template <class T>
490 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
491template <class T>
492 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
493template <class T>
494 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
495template <class T>
496 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000497
498// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000499template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000500
501// shared_ptr casts:
502template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000503 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000504template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000505 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000506template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000507 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000508
509// shared_ptr I/O:
510template<class E, class T, class Y>
511 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
512
513// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000514template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516template<class T, class... Args>
517 shared_ptr<T> make_shared(Args&&... args);
518template<class T, class A, class... Args>
519 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
520
521template<class T>
522class weak_ptr
523{
524public:
525 typedef T element_type;
526
527 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000528 constexpr weak_ptr() noexcept;
529 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
530 weak_ptr(weak_ptr const& r) noexcept;
531 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000532 weak_ptr(weak_ptr&& r) noexcept; // C++14
533 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000534
535 // destructor
536 ~weak_ptr();
537
538 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000539 weak_ptr& operator=(weak_ptr const& r) noexcept;
540 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
541 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000542 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
543 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000544
545 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000546 void swap(weak_ptr& r) noexcept;
547 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000548
549 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000550 long use_count() const noexcept;
551 bool expired() const noexcept;
552 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000553 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
554 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
Logan Smitha5e4d7e2020-05-07 12:07:01 -0400557template<class T>
558weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
559
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000560// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000561template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562
563// class owner_less:
564template<class T> struct owner_less;
565
566template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000567struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000568 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
569{
570 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000571 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
572 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
573 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000574};
575
576template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000577struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000578 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
579{
580 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000581 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
582 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
583 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
584};
585
586template <> // Added in C++14
587struct owner_less<void>
588{
589 template <class _Tp, class _Up>
590 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
591 template <class _Tp, class _Up>
592 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
593 template <class _Tp, class _Up>
594 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
595 template <class _Tp, class _Up>
596 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
597
598 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000599};
600
601template<class T>
602class enable_shared_from_this
603{
604protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000605 constexpr enable_shared_from_this() noexcept;
606 enable_shared_from_this(enable_shared_from_this const&) noexcept;
607 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000608 ~enable_shared_from_this();
609public:
610 shared_ptr<T> shared_from_this();
611 shared_ptr<T const> shared_from_this() const;
612};
613
614template<class T>
615 bool atomic_is_lock_free(const shared_ptr<T>* p);
616template<class T>
617 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
618template<class T>
619 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
620template<class T>
621 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
622template<class T>
623 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
624template<class T>
625 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
626template<class T>
627 shared_ptr<T>
628 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
629template<class T>
630 bool
631 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
632template<class T>
633 bool
634 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
635template<class T>
636 bool
637 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
638 shared_ptr<T> w, memory_order success,
639 memory_order failure);
640template<class T>
641 bool
642 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
643 shared_ptr<T> w, memory_order success,
644 memory_order failure);
645// Hash support
646template <class T> struct hash;
647template <class T, class D> struct hash<unique_ptr<T, D> >;
648template <class T> struct hash<shared_ptr<T> >;
649
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000650template <class T, class Alloc>
651 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
652
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000653// Pointer safety
654enum class pointer_safety { relaxed, preferred, strict };
655void declare_reachable(void *p);
656template <class T> T *undeclare_reachable(T *p);
657void declare_no_pointers(char *p, size_t n);
658void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000659pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
662
663} // std
664
665*/
666
667#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500668#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669#include <type_traits>
670#include <typeinfo>
671#include <cstddef>
672#include <cstdint>
673#include <new>
674#include <utility>
675#include <limits>
676#include <iterator>
677#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000678#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000679#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000680#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000681#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000682#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000683# include <atomic>
684#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000685#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000686
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000687#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000689#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
Eric Fiselierf4433a32017-05-31 22:07:49 +0000691_LIBCPP_PUSH_MACROS
692#include <__undef_macros>
693
694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695_LIBCPP_BEGIN_NAMESPACE_STD
696
Eric Fiselier89659d12015-07-07 00:27:16 +0000697template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000698inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000699_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
700#if !defined(_LIBCPP_HAS_NO_THREADS) && \
701 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000702 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000703 return __atomic_load_n(__value, __ATOMIC_RELAXED);
704#else
705 return *__value;
706#endif
707}
708
Kuba Breckade9d6792016-09-04 09:55:12 +0000709template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000710inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000711_ValueType __libcpp_acquire_load(_ValueType const* __value) {
712#if !defined(_LIBCPP_HAS_NO_THREADS) && \
713 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000714 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000715 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
716#else
717 return *__value;
718#endif
719}
720
Marshall Clow78dbe462016-11-14 18:22:19 +0000721// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000722
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723template <class _Tp> class allocator;
724
Michael Park99f9e912020-03-04 11:27:14 -0500725#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726template <>
Michael Park99f9e912020-03-04 11:27:14 -0500727class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728{
729public:
730 typedef void* pointer;
731 typedef const void* const_pointer;
732 typedef void value_type;
733
734 template <class _Up> struct rebind {typedef allocator<_Up> other;};
735};
736
Howard Hinnant9f771052012-01-19 23:15:22 +0000737template <>
Michael Park99f9e912020-03-04 11:27:14 -0500738class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000739{
740public:
741 typedef const void* pointer;
742 typedef const void* const_pointer;
743 typedef const void value_type;
744
745 template <class _Up> struct rebind {typedef allocator<_Up> other;};
746};
Michael Park99f9e912020-03-04 11:27:14 -0500747#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000748
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749// pointer_traits
750
Marshall Clow0be70c32017-06-14 21:23:57 +0000751template <class _Tp, class = void>
752struct __has_element_type : false_type {};
753
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000755struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000756 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
758template <class _Ptr, bool = __has_element_type<_Ptr>::value>
759struct __pointer_traits_element_type;
760
761template <class _Ptr>
762struct __pointer_traits_element_type<_Ptr, true>
763{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000764 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765};
766
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767template <template <class, class...> class _Sp, class _Tp, class ..._Args>
768struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
769{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000770 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771};
772
773template <template <class, class...> class _Sp, class _Tp, class ..._Args>
774struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
775{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000776 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777};
778
Marshall Clow0be70c32017-06-14 21:23:57 +0000779template <class _Tp, class = void>
780struct __has_difference_type : false_type {};
781
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000783struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000784 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
786template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
787struct __pointer_traits_difference_type
788{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000789 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790};
791
792template <class _Ptr>
793struct __pointer_traits_difference_type<_Ptr, true>
794{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000795 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796};
797
798template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500799struct __has_rebind
800{
801private:
802 struct __two {char __lx; char __lxx;};
803 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500804 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500805 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500806 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500807public:
808 static const bool value = sizeof(__test<_Tp>(0)) == 1;
809};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810
811template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
812struct __pointer_traits_rebind
813{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000814#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000815 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000817 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818#endif
819};
820
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
823{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000824#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000825 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000827 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828#endif
829};
830
831template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
832struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
833{
834 typedef _Sp<_Up, _Args...> type;
835};
836
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000838struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839{
840 typedef _Ptr pointer;
841 typedef typename __pointer_traits_element_type<pointer>::type element_type;
842 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
843
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000844#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000845 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846#else
847 template <class _Up> struct rebind
848 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000849#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850
851private:
852 struct __nat {};
853public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855 static pointer pointer_to(typename conditional<is_void<element_type>::value,
856 __nat, element_type>::type& __r)
857 {return pointer::pointer_to(__r);}
858};
859
860template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000861struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862{
863 typedef _Tp* pointer;
864 typedef _Tp element_type;
865 typedef ptrdiff_t difference_type;
866
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000867#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 template <class _Up> using rebind = _Up*;
869#else
870 template <class _Up> struct rebind {typedef _Up* other;};
871#endif
872
873private:
874 struct __nat {};
875public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000876 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000878 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000879 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880};
881
Eric Fiselierae3ab842015-08-23 02:56:05 +0000882template <class _From, class _To>
883struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000884#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000885 typedef typename pointer_traits<_From>::template rebind<_To> type;
886#else
887 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
888#endif
889};
890
Louis Dionne99f59432020-09-17 12:06:13 -0400891// construct_at
892
893#if _LIBCPP_STD_VER > 17
894
Louis Dionne99f59432020-09-17 12:06:13 -0400895template<class _Tp, class ..._Args, class = decltype(
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500896 ::new (declval<void*>()) _Tp(declval<_Args>()...)
Louis Dionne99f59432020-09-17 12:06:13 -0400897)>
898_LIBCPP_INLINE_VISIBILITY
899constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
900 _LIBCPP_ASSERT(__location, "null pointer given to construct_at");
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500901 return ::new ((void*)__location) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne99f59432020-09-17 12:06:13 -0400902}
903
904#endif
905
906// destroy_at
907
908#if _LIBCPP_STD_VER > 14
909
910template <class _Tp>
911inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
912void destroy_at(_Tp* __loc) {
913 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
914 __loc->~_Tp();
915}
916
917#endif
918
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919// allocator_traits
920
Marshall Clow0be70c32017-06-14 21:23:57 +0000921template <class _Tp, class = void>
922struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923
924template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000925struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000926 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
928namespace __pointer_type_imp
929{
930
931template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
932struct __pointer_type
933{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000934 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935};
936
937template <class _Tp, class _Dp>
938struct __pointer_type<_Tp, _Dp, false>
939{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000940 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941};
942
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000943} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944
945template <class _Tp, class _Dp>
946struct __pointer_type
947{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000948 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 +0000949};
950
Marshall Clow0be70c32017-06-14 21:23:57 +0000951template <class _Tp, class = void>
952struct __has_const_pointer : false_type {};
953
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000955struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000956 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957
958template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
959struct __const_pointer
960{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000961 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962};
963
964template <class _Tp, class _Ptr, class _Alloc>
965struct __const_pointer<_Tp, _Ptr, _Alloc, false>
966{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000967#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000968 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969#else
970 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
971#endif
972};
973
Marshall Clow0be70c32017-06-14 21:23:57 +0000974template <class _Tp, class = void>
975struct __has_void_pointer : false_type {};
976
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000978struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000979 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
981template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
982struct __void_pointer
983{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000984 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985};
986
987template <class _Ptr, class _Alloc>
988struct __void_pointer<_Ptr, _Alloc, false>
989{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000991 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000993 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994#endif
995};
996
Marshall Clow0be70c32017-06-14 21:23:57 +0000997template <class _Tp, class = void>
998struct __has_const_void_pointer : false_type {};
999
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001001struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001002 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003
1004template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1005struct __const_void_pointer
1006{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001007 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008};
1009
1010template <class _Ptr, class _Alloc>
1011struct __const_void_pointer<_Ptr, _Alloc, false>
1012{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001013#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001014 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001016 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017#endif
1018};
1019
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001020
1021template <bool _UsePointerTraits> struct __to_address_helper;
1022
1023template <> struct __to_address_helper<true> {
1024 template <class _Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001025 using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001026
1027 template <class _Pointer>
1028 _LIBCPP_CONSTEXPR
1029 static __return_type<_Pointer>
1030 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1031};
1032
1033template <class _Pointer, bool _Dummy = true>
1034using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1035
1036
Howard Hinnantc834c512011-11-29 18:15:50 +00001037template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001038inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001039_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001040__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001042 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 return __p;
1044}
1045
1046template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001047inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1048typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1049__to_address(const _Pointer& __p) _NOEXCEPT {
1050 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001051}
1052
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001053template <> struct __to_address_helper<false> {
1054 template <class _Pointer>
1055 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001056
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001057 template <class _Pointer>
1058 _LIBCPP_CONSTEXPR
1059 static __return_type<_Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001060 __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001061};
1062
1063
1064#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001065template <class _Tp>
1066inline _LIBCPP_INLINE_VISIBILITY constexpr
1067_Tp*
1068to_address(_Tp* __p) _NOEXCEPT
1069{
1070 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1071 return __p;
1072}
1073
1074template <class _Pointer>
Marek Kurdej1f0cde92020-12-06 15:23:46 +01001075inline _LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001076auto
1077to_address(const _Pointer& __p) _NOEXCEPT
1078{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001079 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001080}
1081#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082
Marshall Clow0be70c32017-06-14 21:23:57 +00001083template <class _Tp, class = void>
1084struct __has_size_type : false_type {};
1085
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001087struct __has_size_type<_Tp,
1088 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001090template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091struct __size_type
1092{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001093 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094};
1095
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001096template <class _Alloc, class _DiffType>
1097struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001099 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100};
1101
Marshall Clow0be70c32017-06-14 21:23:57 +00001102template <class _Tp, class = void>
1103struct __has_propagate_on_container_copy_assignment : false_type {};
1104
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001106struct __has_propagate_on_container_copy_assignment<_Tp,
1107 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1108 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109
1110template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1111struct __propagate_on_container_copy_assignment
1112{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001113 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114};
1115
1116template <class _Alloc>
1117struct __propagate_on_container_copy_assignment<_Alloc, true>
1118{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001119 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120};
1121
Marshall Clow0be70c32017-06-14 21:23:57 +00001122template <class _Tp, class = void>
1123struct __has_propagate_on_container_move_assignment : false_type {};
1124
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001126struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001127 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1128 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129
1130template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1131struct __propagate_on_container_move_assignment
1132{
1133 typedef false_type type;
1134};
1135
1136template <class _Alloc>
1137struct __propagate_on_container_move_assignment<_Alloc, true>
1138{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001139 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140};
1141
Marshall Clow0be70c32017-06-14 21:23:57 +00001142template <class _Tp, class = void>
1143struct __has_propagate_on_container_swap : false_type {};
1144
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001146struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001147 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1148 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149
1150template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1151struct __propagate_on_container_swap
1152{
1153 typedef false_type type;
1154};
1155
1156template <class _Alloc>
1157struct __propagate_on_container_swap<_Alloc, true>
1158{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001159 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160};
1161
Marshall Clow0be70c32017-06-14 21:23:57 +00001162template <class _Tp, class = void>
1163struct __has_is_always_equal : false_type {};
1164
Marshall Clow0b587562015-06-02 16:34:03 +00001165template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001166struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001167 typename __void_t<typename _Tp::is_always_equal>::type>
1168 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001169
1170template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1171struct __is_always_equal
1172{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001173 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001174};
1175
1176template <class _Alloc>
1177struct __is_always_equal<_Alloc, true>
1178{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001179 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001180};
1181
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1183struct __has_rebind_other
1184{
1185private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001186 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001188 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001190 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191public:
1192 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1193};
1194
1195template <class _Tp, class _Up>
1196struct __has_rebind_other<_Tp, _Up, false>
1197{
1198 static const bool value = false;
1199};
1200
1201template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1202struct __allocator_traits_rebind
1203{
Michael Park99f9e912020-03-04 11:27:14 -05001204 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001205 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001206 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207};
1208
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1210struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1211{
Michael Park99f9e912020-03-04 11:27:14 -05001212 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001213 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001214 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215};
1216
1217template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1218struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1219{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001220 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221};
1222
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001223#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
Michael Park99f9e912020-03-04 11:27:14 -05001225_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1227auto
1228__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001229 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001230_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231
1232template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1233auto
1234__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1235 -> false_type;
1236
1237template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1238struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001239 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1240 declval<_SizeType>(),
1241 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242{
1243};
1244
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001245#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246
1247template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1248struct __has_allocate_hint
1249 : true_type
1250{
1251};
1252
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001253#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
zoecarver75816d42020-05-23 14:32:12 -07001255_LIBCPP_SUPPRESS_DEPRECATED_PUSH
zoecarver20590dd2020-05-23 14:03:04 -07001256template <class _Alloc, class ..._Args,
1257 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1258static true_type __test_has_construct(int);
zoecarver75816d42020-05-23 14:32:12 -07001259_LIBCPP_SUPPRESS_DEPRECATED_POP
1260
zoecarver20590dd2020-05-23 14:03:04 -07001261template <class _Alloc, class...>
1262static false_type __test_has_construct(...);
1263
1264template <class _Alloc, class ..._Args>
1265struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1266
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001267#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268
Michael Park99f9e912020-03-04 11:27:14 -05001269_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270template <class _Alloc, class _Pointer>
1271auto
1272__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1273 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001274_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
1276template <class _Alloc, class _Pointer>
1277auto
1278__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1279 -> false_type;
1280
1281template <class _Alloc, class _Pointer>
1282struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001283 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1284 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285{
1286};
1287
Michael Park99f9e912020-03-04 11:27:14 -05001288_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289template <class _Alloc>
1290auto
1291__has_max_size_test(_Alloc&& __a)
1292 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001293_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294
1295template <class _Alloc>
1296auto
1297__has_max_size_test(const volatile _Alloc& __a)
1298 -> false_type;
1299
1300template <class _Alloc>
1301struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001302 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303{
1304};
1305
1306template <class _Alloc>
1307auto
1308__has_select_on_container_copy_construction_test(_Alloc&& __a)
1309 -> decltype(__a.select_on_container_copy_construction(), true_type());
1310
1311template <class _Alloc>
1312auto
1313__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1314 -> false_type;
1315
1316template <class _Alloc>
1317struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001318 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319{
1320};
1321
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001322#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001324template <class _Alloc, class _Pointer, class = void>
1325struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
1327template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001328struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1329 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001330>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
1332template <class _Alloc>
1333struct __has_max_size
1334 : true_type
1335{
1336};
1337
1338template <class _Alloc>
1339struct __has_select_on_container_copy_construction
1340 : false_type
1341{
1342};
1343
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001344#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001346template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1347struct __alloc_traits_difference_type
1348{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001349 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001350};
1351
1352template <class _Alloc, class _Ptr>
1353struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1354{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001355 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001356};
1357
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001358template <class _Tp>
1359struct __is_default_allocator : false_type {};
1360
1361template <class _Tp>
1362struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1363
Eric Fiselier909fe962019-09-13 16:09:33 +00001364
1365
1366template <class _Alloc,
1367 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1368 >
1369struct __is_cpp17_move_insertable;
1370template <class _Alloc>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001371struct __is_cpp17_move_insertable<_Alloc, true> : true_type {};
Eric Fiselier909fe962019-09-13 16:09:33 +00001372template <class _Alloc>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001373struct __is_cpp17_move_insertable<_Alloc, false> : is_move_constructible<typename _Alloc::value_type> {};
Eric Fiselier909fe962019-09-13 16:09:33 +00001374
1375template <class _Alloc,
1376 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1377 >
1378struct __is_cpp17_copy_insertable;
1379template <class _Alloc>
1380struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1381template <class _Alloc>
1382struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001383 is_copy_constructible<typename _Alloc::value_type>::value &&
Eric Fiselier909fe962019-09-13 16:09:33 +00001384 __is_cpp17_move_insertable<_Alloc>::value>
1385 {};
1386
1387
1388
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001390struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391{
1392 typedef _Alloc allocator_type;
1393 typedef typename allocator_type::value_type value_type;
1394
1395 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1396 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1397 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1398 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1399
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001400 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1401 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402
1403 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1404 propagate_on_container_copy_assignment;
1405 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1406 propagate_on_container_move_assignment;
1407 typedef typename __propagate_on_container_swap<allocator_type>::type
1408 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001409 typedef typename __is_always_equal<allocator_type>::type
1410 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001412#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001414 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001415 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001416#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 template <class _Tp> struct rebind_alloc
1418 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1419 template <class _Tp> struct rebind_traits
1420 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001421#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422
Louis Dionne99f59432020-09-17 12:06:13 -04001423 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 static pointer allocate(allocator_type& __a, size_type __n)
1425 {return __a.allocate(__n);}
Louis Dionne99f59432020-09-17 12:06:13 -04001426 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001428 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1430
Louis Dionne99f59432020-09-17 12:06:13 -04001431 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001432 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 {__a.deallocate(__p, __n);}
1434
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001436 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001438 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001439 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440
1441 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001442 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 static void destroy(allocator_type& __a, _Tp* __p)
1444 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1445
Louis Dionne99f59432020-09-17 12:06:13 -04001446 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow4f834b52013-08-27 20:22:15 +00001447 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1449
Louis Dionne99f59432020-09-17 12:06:13 -04001450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451 static allocator_type
1452 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001453 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 __has_select_on_container_copy_construction<const allocator_type>(),
1455 __a);}
1456
1457private:
Louis Dionne99f59432020-09-17 12:06:13 -04001458 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001459 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001461 {
1462 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1463 return __a.allocate(__n, __hint);
1464 _LIBCPP_SUPPRESS_DEPRECATED_POP
1465 }
Louis Dionne99f59432020-09-17 12:06:13 -04001466 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001467 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001468 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469 {return __a.allocate(__n);}
1470
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001472 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001474 {
1475 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1476 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1477 _LIBCPP_SUPPRESS_DEPRECATED_POP
1478 }
1479
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001481 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1483 {
Louis Dionne99f59432020-09-17 12:06:13 -04001484#if _LIBCPP_STD_VER > 17
1485 _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
1486#else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001487 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne99f59432020-09-17 12:06:13 -04001488#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490
1491 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001492 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001494 {
1495 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1496 __a.destroy(__p);
1497 _LIBCPP_SUPPRESS_DEPRECATED_POP
1498 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001500 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501 static void __destroy(false_type, allocator_type&, _Tp* __p)
1502 {
Louis Dionne99f59432020-09-17 12:06:13 -04001503#if _LIBCPP_STD_VER > 17
1504 _VSTD::destroy_at(__p);
1505#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 __p->~_Tp();
Louis Dionne99f59432020-09-17 12:06:13 -04001507#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508 }
1509
Louis Dionne99f59432020-09-17 12:06:13 -04001510 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001511 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001512 {
1513 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1514 return __a.max_size();
1515 _LIBCPP_SUPPRESS_DEPRECATED_POP
1516 }
1517
Louis Dionne99f59432020-09-17 12:06:13 -04001518 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001519 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001520 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521
Louis Dionne99f59432020-09-17 12:06:13 -04001522 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001524 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 {return __a.select_on_container_copy_construction();}
Louis Dionne99f59432020-09-17 12:06:13 -04001526 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001528 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 {return __a;}
1530};
1531
Marshall Clow940e01c2015-04-07 05:21:38 +00001532template <class _Traits, class _Tp>
1533struct __rebind_alloc_helper
1534{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001535#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001536 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001537#else
1538 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1539#endif
1540};
1541
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542// allocator
1543
1544template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001545class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546{
1547public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001548 typedef size_t size_type;
1549 typedef ptrdiff_t difference_type;
1550 typedef _Tp value_type;
1551 typedef true_type propagate_on_container_move_assignment;
1552 typedef true_type is_always_equal;
1553
1554 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1555 allocator() _NOEXCEPT { }
1556
1557 template <class _Up>
1558 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1559 allocator(const allocator<_Up>&) _NOEXCEPT { }
1560
1561 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1562 _Tp* allocate(size_t __n) {
1563 if (__n > allocator_traits<allocator>::max_size(*this))
1564 __throw_length_error("allocator<T>::allocate(size_t n)"
1565 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -04001566 if (__libcpp_is_constant_evaluated()) {
1567 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
1568 } else {
1569 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1570 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001571 }
1572
1573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1574 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -04001575 if (__libcpp_is_constant_evaluated()) {
1576 ::operator delete(__p);
1577 } else {
1578 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1579 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001580 }
1581
1582 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001583#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001584 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1585 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1586 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1587 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1588
Louis Dionne481a2662018-09-23 18:35:00 +00001589 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001590 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1591 typedef allocator<_Up> other;
1592 };
Marshall Clowbc759762018-03-20 23:02:53 +00001593
Michael Park99f9e912020-03-04 11:27:14 -05001594 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001595 pointer address(reference __x) const _NOEXCEPT {
1596 return _VSTD::addressof(__x);
1597 }
Michael Park99f9e912020-03-04 11:27:14 -05001598 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001599 const_pointer address(const_reference __x) const _NOEXCEPT {
1600 return _VSTD::addressof(__x);
1601 }
Michael Park99f9e912020-03-04 11:27:14 -05001602
Michael Park99f9e912020-03-04 11:27:14 -05001603 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001604 _Tp* allocate(size_t __n, const void*) {
1605 return allocate(__n);
1606 }
Michael Park99f9e912020-03-04 11:27:14 -05001607
Louis Dionnef8b6c022020-09-21 10:36:37 -04001608 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1609 return size_type(~0) / sizeof(_Tp);
1610 }
Michael Park99f9e912020-03-04 11:27:14 -05001611
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001613 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1614 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001615 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -04001616 }
1617
1618 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1619 void destroy(pointer __p) {
1620 __p->~_Tp();
1621 }
Michael Park99f9e912020-03-04 11:27:14 -05001622#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623};
1624
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001625template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001626class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001627{
1628public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001629 typedef size_t size_type;
1630 typedef ptrdiff_t difference_type;
1631 typedef const _Tp value_type;
1632 typedef true_type propagate_on_container_move_assignment;
1633 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001634
Marshall Clowbc759762018-03-20 23:02:53 +00001635 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001636 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +00001637
1638 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001639 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001640 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -05001641
Louis Dionne99f59432020-09-17 12:06:13 -04001642 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001643 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -04001644 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +00001645 __throw_length_error("allocator<const T>::allocate(size_t n)"
1646 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -04001647 if (__libcpp_is_constant_evaluated()) {
1648 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
1649 } else {
1650 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1651 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001652 }
Michael Park99f9e912020-03-04 11:27:14 -05001653
Louis Dionne99f59432020-09-17 12:06:13 -04001654 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001655 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -04001656 if (__libcpp_is_constant_evaluated()) {
1657 ::operator delete(const_cast<_Tp*>(__p));
1658 } else {
1659 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1660 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001661 }
Michael Park99f9e912020-03-04 11:27:14 -05001662
Louis Dionnef8b6c022020-09-21 10:36:37 -04001663 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001664#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -04001665 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1666 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1667 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1668 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1669
1670 template <class _Up>
1671 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1672 typedef allocator<_Up> other;
1673 };
1674
1675 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1676 const_pointer address(const_reference __x) const _NOEXCEPT {
1677 return _VSTD::addressof(__x);
1678 }
1679
1680 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1681 const _Tp* allocate(size_t __n, const void*) {
1682 return allocate(__n);
1683 }
1684
1685 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1686 return size_type(~0) / sizeof(_Tp);
1687 }
Michael Park99f9e912020-03-04 11:27:14 -05001688
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001689 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001690 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1691 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001692 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -04001693 }
Howard Hinnant9e028f12012-05-01 15:37:54 +00001694
Louis Dionnef8b6c022020-09-21 10:36:37 -04001695 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1696 void destroy(pointer __p) {
1697 __p->~_Tp();
1698 }
Michael Park99f9e912020-03-04 11:27:14 -05001699#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001700};
1701
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001703inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001704bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705
1706template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001707inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001708bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709
Louis Dionned6651542020-11-03 12:05:55 -05001710template <class _Alloc, class _Ptr>
1711_LIBCPP_INLINE_VISIBILITY
1712void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
1713 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -05001714 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -05001715 typedef allocator_traits<_Alloc> _Traits;
1716 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
1717 _Traits::construct(__a, _VSTD::__to_address(__begin2),
1718#ifdef _LIBCPP_NO_EXCEPTIONS
1719 _VSTD::move(*__begin1)
1720#else
1721 _VSTD::move_if_noexcept(*__begin1)
1722#endif
1723 );
1724 }
1725}
1726
1727template <class _Alloc, class _Tp, typename enable_if<
1728 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1729 is_trivially_move_constructible<_Tp>::value
1730>::type>
1731_LIBCPP_INLINE_VISIBILITY
1732void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1733 ptrdiff_t _Np = __end1 - __begin1;
1734 if (_Np > 0) {
1735 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1736 __begin2 += _Np;
1737 }
1738}
1739
1740template <class _Alloc, class _Iter, class _Ptr>
1741_LIBCPP_INLINE_VISIBILITY
1742void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1743 typedef allocator_traits<_Alloc> _Traits;
1744 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1745 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1746 }
1747}
1748
1749template <class _Alloc, class _Source, class _Dest,
1750 class _RawSource = typename remove_const<_Source>::type,
1751 class _RawDest = typename remove_const<_Dest>::type,
1752 class =
1753 typename enable_if<
1754 is_trivially_copy_constructible<_Dest>::value &&
1755 is_same<_RawSource, _RawDest>::value &&
1756 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1757 >::type>
1758_LIBCPP_INLINE_VISIBILITY
1759void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1760 ptrdiff_t _Np = __end1 - __begin1;
1761 if (_Np > 0) {
1762 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1763 __begin2 += _Np;
1764 }
1765}
1766
1767template <class _Alloc, class _Ptr>
1768_LIBCPP_INLINE_VISIBILITY
1769void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1770 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1771 "The specified type does not meet the requirements of Cpp17MoveInsertable");
1772 typedef allocator_traits<_Alloc> _Traits;
1773 while (__end1 != __begin1) {
1774 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1775#ifdef _LIBCPP_NO_EXCEPTIONS
1776 _VSTD::move(*--__end1)
1777#else
1778 _VSTD::move_if_noexcept(*--__end1)
1779#endif
1780 );
1781 --__end2;
1782 }
1783}
1784
1785template <class _Alloc, class _Tp, class = typename enable_if<
1786 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1787 is_trivially_move_constructible<_Tp>::value
1788>::type>
1789_LIBCPP_INLINE_VISIBILITY
1790void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1791 ptrdiff_t _Np = __end1 - __begin1;
1792 __end2 -= _Np;
1793 if (_Np > 0)
1794 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1795}
1796
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001798class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 : public iterator<output_iterator_tag,
1800 _Tp, // purposefully not C++03
1801 ptrdiff_t, // purposefully not C++03
1802 _Tp*, // purposefully not C++03
1803 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1804{
1805private:
1806 _OutputIterator __x_;
1807public:
1808 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1809 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1810 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001811 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001812#if _LIBCPP_STD_VER >= 14
1813 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001814 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001815#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1817 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1818 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001819#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001820 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001821#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822};
1823
1824template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001825_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001827get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828{
1829 pair<_Tp*, ptrdiff_t> __r(0, 0);
1830 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1831 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1832 / sizeof(_Tp);
1833 if (__n > __m)
1834 __n = __m;
1835 while (__n > 0)
1836 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001837#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001838 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001839 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001840 align_val_t __al =
1841 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001842 __r.first = static_cast<_Tp*>(::operator new(
1843 __n * sizeof(_Tp), __al, nothrow));
1844 } else {
1845 __r.first = static_cast<_Tp*>(::operator new(
1846 __n * sizeof(_Tp), nothrow));
1847 }
1848#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001849 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001850 {
1851 // Since aligned operator new is unavailable, return an empty
1852 // buffer rather than one with invalid alignment.
1853 return __r;
1854 }
1855
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001857#endif
1858
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 if (__r.first)
1860 {
1861 __r.second = __n;
1862 break;
1863 }
1864 __n /= 2;
1865 }
1866 return __r;
1867}
1868
1869template <class _Tp>
1870inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001871void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1872{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001873 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001874}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875
Marshall Clowb22274f2017-01-24 22:22:33 +00001876#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001878struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879{
1880 _Tp* __ptr_;
1881};
1882
1883template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001884class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885{
1886private:
1887 _Tp* __ptr_;
1888public:
1889 typedef _Tp element_type;
1890
Dimitry Andric47269ce2020-03-13 19:36:26 +01001891 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1892 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1893 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001895 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001897 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001899 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001901 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902
Dimitry Andric47269ce2020-03-13 19:36:26 +01001903 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001905 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1906 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1907 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 {
1909 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001910 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911 return __t;
1912 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001913 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 {
1915 if (__ptr_ != __p)
1916 delete __ptr_;
1917 __ptr_ = __p;
1918 }
1919
Dimitry Andric47269ce2020-03-13 19:36:26 +01001920 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1921 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001923 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924 {return auto_ptr<_Up>(release());}
1925};
1926
1927template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001928class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929{
1930public:
1931 typedef void element_type;
1932};
Marshall Clowb22274f2017-01-24 22:22:33 +00001933#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001935// Tag used to default initialize one or both of the pair's elements.
1936struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001937struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001938
Eric Fiselier9d355982017-04-12 23:45:53 +00001939template <class _Tp, int _Idx,
1940 bool _CanBeEmptyBase =
1941 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1942struct __compressed_pair_elem {
1943 typedef _Tp _ParamT;
1944 typedef _Tp& reference;
1945 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001947 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001948 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001949 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1950 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951
Eric Fiselier9d355982017-04-12 23:45:53 +00001952 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001953 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1954 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001955 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001956 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001957 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001958 : __value_(_VSTD::forward<_Up>(__u))
1959 {
1960 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001962
1963#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001964 template <class... _Args, size_t... _Indexes>
1965 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1966 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1967 __tuple_indices<_Indexes...>)
1968 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001971
Alex Lorenz76132112017-11-09 17:54:49 +00001972 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1973 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001974 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001977 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978};
1979
Eric Fiselier9d355982017-04-12 23:45:53 +00001980template <class _Tp, int _Idx>
1981struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1982 typedef _Tp _ParamT;
1983 typedef _Tp& reference;
1984 typedef const _Tp& const_reference;
1985 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001987 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1988 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1989 __compressed_pair_elem(__default_init_tag) {}
1990 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1991 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992
Eric Fiselier9d355982017-04-12 23:45:53 +00001993 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001994 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1995 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001996 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001997 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001998 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001999 : __value_type(_VSTD::forward<_Up>(__u))
2000 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002002#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002003 template <class... _Args, size_t... _Indexes>
2004 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2005 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2006 __tuple_indices<_Indexes...>)
2007 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002008#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009
Alex Lorenz76132112017-11-09 17:54:49 +00002010 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2011 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002012 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013};
2014
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002016class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2017 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002018 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2019 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002020
2021 // NOTE: This static assert should never fire because __compressed_pair
2022 // is *almost never* used in a scenario where it's possible for T1 == T2.
2023 // (The exception is std::function where it is possible that the function
2024 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002025 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05002026 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00002027 "The current implementation is NOT ABI-compatible with the previous "
2028 "implementation for this configuration");
2029
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002031 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002032 class = typename enable_if<
2033 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2034 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2035 >::type
2036 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002037 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002038 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039
Eric Fiselier9d355982017-04-12 23:45:53 +00002040 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002041 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002042 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002043 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002045#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002046 template <class... _Args1, class... _Args2>
2047 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2048 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2049 tuple<_Args2...> __second_args)
2050 : _Base1(__pc, _VSTD::move(__first_args),
2051 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2052 _Base2(__pc, _VSTD::move(__second_args),
2053 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002054#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055
Eric Fiselier9d355982017-04-12 23:45:53 +00002056 _LIBCPP_INLINE_VISIBILITY
2057 typename _Base1::reference first() _NOEXCEPT {
2058 return static_cast<_Base1&>(*this).__get();
2059 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060
Eric Fiselier9d355982017-04-12 23:45:53 +00002061 _LIBCPP_INLINE_VISIBILITY
2062 typename _Base1::const_reference first() const _NOEXCEPT {
2063 return static_cast<_Base1 const&>(*this).__get();
2064 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065
Eric Fiselier9d355982017-04-12 23:45:53 +00002066 _LIBCPP_INLINE_VISIBILITY
2067 typename _Base2::reference second() _NOEXCEPT {
2068 return static_cast<_Base2&>(*this).__get();
2069 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070
Eric Fiselier9d355982017-04-12 23:45:53 +00002071 _LIBCPP_INLINE_VISIBILITY
2072 typename _Base2::const_reference second() const _NOEXCEPT {
2073 return static_cast<_Base2 const&>(*this).__get();
2074 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075
Eric Fiselier9d355982017-04-12 23:45:53 +00002076 _LIBCPP_INLINE_VISIBILITY
2077 void swap(__compressed_pair& __x)
2078 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2079 __is_nothrow_swappable<_T2>::value)
2080 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002081 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00002082 swap(first(), __x.first());
2083 swap(second(), __x.second());
2084 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085};
2086
2087template <class _T1, class _T2>
2088inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002089void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2090 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2091 __is_nothrow_swappable<_T2>::value) {
2092 __x.swap(__y);
2093}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002095// default_delete
2096
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002098struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002099 static_assert(!is_function<_Tp>::value,
2100 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002101#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002102 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002103#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002104 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002105#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002106 template <class _Up>
2107 _LIBCPP_INLINE_VISIBILITY
2108 default_delete(const default_delete<_Up>&,
2109 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2110 0) _NOEXCEPT {}
2111
2112 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2113 static_assert(sizeof(_Tp) > 0,
2114 "default_delete can not delete incomplete type");
2115 static_assert(!is_void<_Tp>::value,
2116 "default_delete can not delete incomplete type");
2117 delete __ptr;
2118 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119};
2120
2121template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002122struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2123private:
2124 template <class _Up>
2125 struct _EnableIfConvertible
2126 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2127
Howard Hinnant4500ca52011-12-18 21:19:44 +00002128public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002129#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002130 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002131#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002132 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002133#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002134
2135 template <class _Up>
2136 _LIBCPP_INLINE_VISIBILITY
2137 default_delete(const default_delete<_Up[]>&,
2138 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2139
2140 template <class _Up>
2141 _LIBCPP_INLINE_VISIBILITY
2142 typename _EnableIfConvertible<_Up>::type
2143 operator()(_Up* __ptr) const _NOEXCEPT {
2144 static_assert(sizeof(_Tp) > 0,
2145 "default_delete can not delete incomplete type");
2146 static_assert(!is_void<_Tp>::value,
2147 "default_delete can not delete void type");
2148 delete[] __ptr;
2149 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150};
2151
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002152template <class _Deleter>
2153struct __unique_ptr_deleter_sfinae {
2154 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2155 typedef const _Deleter& __lval_ref_type;
2156 typedef _Deleter&& __good_rval_ref_type;
2157 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158};
2159
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002160template <class _Deleter>
2161struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2162 typedef const _Deleter& __lval_ref_type;
2163 typedef const _Deleter&& __bad_rval_ref_type;
2164 typedef false_type __enable_rval_overload;
2165};
2166
2167template <class _Deleter>
2168struct __unique_ptr_deleter_sfinae<_Deleter&> {
2169 typedef _Deleter& __lval_ref_type;
2170 typedef _Deleter&& __bad_rval_ref_type;
2171 typedef false_type __enable_rval_overload;
2172};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002173
Vy Nguyene369bd92020-07-13 12:34:37 -04002174#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2175# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2176#else
2177# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2178#endif
2179
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002180template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04002181class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002182public:
2183 typedef _Tp element_type;
2184 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002185 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002186
2187 static_assert(!is_rvalue_reference<deleter_type>::value,
2188 "the specified deleter type cannot be an rvalue reference");
2189
2190private:
2191 __compressed_pair<pointer, deleter_type> __ptr_;
2192
2193 struct __nat { int __for_bool_; };
2194
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002195 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002196
2197 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002198 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002199 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002200
2201 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002202 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002203 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002204
2205 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002206 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002207 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002208
2209 template <bool _Dummy, class _Deleter = typename __dependent_type<
2210 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002211 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002212 typename enable_if<is_default_constructible<_Deleter>::value &&
2213 !is_pointer<_Deleter>::value>::type;
2214
2215 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002216 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002217 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2218
2219 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002220 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002221 is_convertible<typename _UPtr::pointer, pointer>::value &&
2222 !is_array<_Up>::value
2223 >::type;
2224
2225 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002226 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002227 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2228 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2229 >::type;
2230
2231 template <class _UDel>
2232 using _EnableIfDeleterAssignable = typename enable_if<
2233 is_assignable<_Dp&, _UDel&&>::value
2234 >::type;
2235
2236public:
2237 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002238 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002239 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002240 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002241
2242 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002243 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002245 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002246
2247 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002248 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002249 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002250 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002251
2252 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002253 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002255 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002256 : __ptr_(__p, __d) {}
2257
2258 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002259 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002260 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002261 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002262 : __ptr_(__p, _VSTD::move(__d)) {
2263 static_assert(!is_reference<deleter_type>::value,
2264 "rvalue deleter bound to reference");
2265 }
2266
2267 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002268 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002269 _LIBCPP_INLINE_VISIBILITY
2270 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2271
2272 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002273 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002274 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2275 }
2276
2277 template <class _Up, class _Ep,
2278 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2279 class = _EnableIfDeleterConvertible<_Ep>
2280 >
2281 _LIBCPP_INLINE_VISIBILITY
2282 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2283 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2284
2285#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2286 template <class _Up>
2287 _LIBCPP_INLINE_VISIBILITY
2288 unique_ptr(auto_ptr<_Up>&& __p,
2289 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002290 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002291 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002292 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002293#endif
2294
2295 _LIBCPP_INLINE_VISIBILITY
2296 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2297 reset(__u.release());
2298 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2299 return *this;
2300 }
2301
2302 template <class _Up, class _Ep,
2303 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2304 class = _EnableIfDeleterAssignable<_Ep>
2305 >
2306 _LIBCPP_INLINE_VISIBILITY
2307 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2308 reset(__u.release());
2309 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2310 return *this;
2311 }
2312
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002313#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2314 template <class _Up>
2315 _LIBCPP_INLINE_VISIBILITY
2316 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2317 is_same<_Dp, default_delete<_Tp> >::value,
2318 unique_ptr&>::type
2319 operator=(auto_ptr<_Up> __p) {
2320 reset(__p.release());
2321 return *this;
2322 }
2323#endif
2324
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002325#ifdef _LIBCPP_CXX03_LANG
2326 unique_ptr(unique_ptr const&) = delete;
2327 unique_ptr& operator=(unique_ptr const&) = delete;
2328#endif
2329
2330
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002331 _LIBCPP_INLINE_VISIBILITY
2332 ~unique_ptr() { reset(); }
2333
2334 _LIBCPP_INLINE_VISIBILITY
2335 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2336 reset();
2337 return *this;
2338 }
2339
2340 _LIBCPP_INLINE_VISIBILITY
2341 typename add_lvalue_reference<_Tp>::type
2342 operator*() const {
2343 return *__ptr_.first();
2344 }
2345 _LIBCPP_INLINE_VISIBILITY
2346 pointer operator->() const _NOEXCEPT {
2347 return __ptr_.first();
2348 }
2349 _LIBCPP_INLINE_VISIBILITY
2350 pointer get() const _NOEXCEPT {
2351 return __ptr_.first();
2352 }
2353 _LIBCPP_INLINE_VISIBILITY
2354 deleter_type& get_deleter() _NOEXCEPT {
2355 return __ptr_.second();
2356 }
2357 _LIBCPP_INLINE_VISIBILITY
2358 const deleter_type& get_deleter() const _NOEXCEPT {
2359 return __ptr_.second();
2360 }
2361 _LIBCPP_INLINE_VISIBILITY
2362 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2363 return __ptr_.first() != nullptr;
2364 }
2365
2366 _LIBCPP_INLINE_VISIBILITY
2367 pointer release() _NOEXCEPT {
2368 pointer __t = __ptr_.first();
2369 __ptr_.first() = pointer();
2370 return __t;
2371 }
2372
2373 _LIBCPP_INLINE_VISIBILITY
2374 void reset(pointer __p = pointer()) _NOEXCEPT {
2375 pointer __tmp = __ptr_.first();
2376 __ptr_.first() = __p;
2377 if (__tmp)
2378 __ptr_.second()(__tmp);
2379 }
2380
2381 _LIBCPP_INLINE_VISIBILITY
2382 void swap(unique_ptr& __u) _NOEXCEPT {
2383 __ptr_.swap(__u.__ptr_);
2384 }
2385};
2386
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002387
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002389class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002391 typedef _Tp element_type;
2392 typedef _Dp deleter_type;
2393 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2394
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002396 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397
Eric Fiselier31127cd2017-04-16 02:14:31 +00002398 template <class _From>
2399 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400
Eric Fiselier31127cd2017-04-16 02:14:31 +00002401 template <class _FromElem>
2402 struct _CheckArrayPointerConversion<_FromElem*>
2403 : integral_constant<bool,
2404 is_same<_FromElem*, pointer>::value ||
2405 (is_same<pointer, element_type*>::value &&
2406 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2407 >
2408 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409
Eric Fiselier31127cd2017-04-16 02:14:31 +00002410 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002411
2412 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002413 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002414 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002415
2416 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002417 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002418 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002419
2420 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002421 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002422 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002423
2424 template <bool _Dummy, class _Deleter = typename __dependent_type<
2425 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002426 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427 typename enable_if<is_default_constructible<_Deleter>::value &&
2428 !is_pointer<_Deleter>::value>::type;
2429
2430 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002431 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002432 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2433
2434 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002435 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002436 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002437 >::type;
2438
2439 template <class _UPtr, class _Up,
2440 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002441 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002442 is_array<_Up>::value &&
2443 is_same<pointer, element_type*>::value &&
2444 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2445 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2446 >::type;
2447
2448 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002449 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002450 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2451 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2452 >::type;
2453
2454 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002455 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002456 is_assignable<_Dp&, _UDel&&>::value
2457 >::type;
2458
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002460 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002461 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002462 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002463 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002465 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002466 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002467 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002468 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470 template <class _Pp, bool _Dummy = true,
2471 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002472 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002473 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002474 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002475 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002478 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2479 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002480 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002481 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002482 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002484 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002485 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002486 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002487 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002488 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002490 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002491 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2492 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002493 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002494 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002495 : __ptr_(__p, _VSTD::move(__d)) {
2496 static_assert(!is_reference<deleter_type>::value,
2497 "rvalue deleter bound to reference");
2498 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002501 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002502 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002503 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002504 : __ptr_(nullptr, _VSTD::move(__d)) {
2505 static_assert(!is_reference<deleter_type>::value,
2506 "rvalue deleter bound to reference");
2507 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002508
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002509 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002510 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2511 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512 _LIBCPP_INLINE_VISIBILITY
2513 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002514
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002515 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002516 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002517 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2518 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002519
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002520 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002521 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002522 reset(__u.release());
2523 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2524 return *this;
2525 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002527 template <class _Up, class _Ep,
2528 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2529 class = _EnableIfDeleterConvertible<_Ep>
2530 >
2531 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002532 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002533 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002534 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002536 template <class _Up, class _Ep,
2537 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2538 class = _EnableIfDeleterAssignable<_Ep>
2539 >
2540 _LIBCPP_INLINE_VISIBILITY
2541 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002542 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002543 reset(__u.release());
2544 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2545 return *this;
2546 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002548#ifdef _LIBCPP_CXX03_LANG
2549 unique_ptr(unique_ptr const&) = delete;
2550 unique_ptr& operator=(unique_ptr const&) = delete;
2551#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002552
2553public:
2554 _LIBCPP_INLINE_VISIBILITY
2555 ~unique_ptr() { reset(); }
2556
2557 _LIBCPP_INLINE_VISIBILITY
2558 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2559 reset();
2560 return *this;
2561 }
2562
2563 _LIBCPP_INLINE_VISIBILITY
2564 typename add_lvalue_reference<_Tp>::type
2565 operator[](size_t __i) const {
2566 return __ptr_.first()[__i];
2567 }
2568 _LIBCPP_INLINE_VISIBILITY
2569 pointer get() const _NOEXCEPT {
2570 return __ptr_.first();
2571 }
2572
2573 _LIBCPP_INLINE_VISIBILITY
2574 deleter_type& get_deleter() _NOEXCEPT {
2575 return __ptr_.second();
2576 }
2577
2578 _LIBCPP_INLINE_VISIBILITY
2579 const deleter_type& get_deleter() const _NOEXCEPT {
2580 return __ptr_.second();
2581 }
2582 _LIBCPP_INLINE_VISIBILITY
2583 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2584 return __ptr_.first() != nullptr;
2585 }
2586
2587 _LIBCPP_INLINE_VISIBILITY
2588 pointer release() _NOEXCEPT {
2589 pointer __t = __ptr_.first();
2590 __ptr_.first() = pointer();
2591 return __t;
2592 }
2593
2594 template <class _Pp>
2595 _LIBCPP_INLINE_VISIBILITY
2596 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002597 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002598 >::type
2599 reset(_Pp __p) _NOEXCEPT {
2600 pointer __tmp = __ptr_.first();
2601 __ptr_.first() = __p;
2602 if (__tmp)
2603 __ptr_.second()(__tmp);
2604 }
2605
2606 _LIBCPP_INLINE_VISIBILITY
2607 void reset(nullptr_t = nullptr) _NOEXCEPT {
2608 pointer __tmp = __ptr_.first();
2609 __ptr_.first() = nullptr;
2610 if (__tmp)
2611 __ptr_.second()(__tmp);
2612 }
2613
2614 _LIBCPP_INLINE_VISIBILITY
2615 void swap(unique_ptr& __u) _NOEXCEPT {
2616 __ptr_.swap(__u.__ptr_);
2617 }
2618
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619};
2620
2621template <class _Tp, class _Dp>
2622inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002623typename enable_if<
2624 __is_swappable<_Dp>::value,
2625 void
2626>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002627swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628
2629template <class _T1, class _D1, class _T2, class _D2>
2630inline _LIBCPP_INLINE_VISIBILITY
2631bool
2632operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2633
2634template <class _T1, class _D1, class _T2, class _D2>
2635inline _LIBCPP_INLINE_VISIBILITY
2636bool
2637operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2638
2639template <class _T1, class _D1, class _T2, class _D2>
2640inline _LIBCPP_INLINE_VISIBILITY
2641bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002642operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2643{
2644 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2645 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002646 typedef typename common_type<_P1, _P2>::type _Vp;
2647 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002648}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649
2650template <class _T1, class _D1, class _T2, class _D2>
2651inline _LIBCPP_INLINE_VISIBILITY
2652bool
2653operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2654
2655template <class _T1, class _D1, class _T2, class _D2>
2656inline _LIBCPP_INLINE_VISIBILITY
2657bool
2658operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2659
2660template <class _T1, class _D1, class _T2, class _D2>
2661inline _LIBCPP_INLINE_VISIBILITY
2662bool
2663operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2664
Howard Hinnantb17caf92012-02-21 21:02:58 +00002665template <class _T1, class _D1>
2666inline _LIBCPP_INLINE_VISIBILITY
2667bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002668operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002669{
2670 return !__x;
2671}
2672
2673template <class _T1, class _D1>
2674inline _LIBCPP_INLINE_VISIBILITY
2675bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002676operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002677{
2678 return !__x;
2679}
2680
2681template <class _T1, class _D1>
2682inline _LIBCPP_INLINE_VISIBILITY
2683bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002684operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002685{
2686 return static_cast<bool>(__x);
2687}
2688
2689template <class _T1, class _D1>
2690inline _LIBCPP_INLINE_VISIBILITY
2691bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002692operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002693{
2694 return static_cast<bool>(__x);
2695}
2696
2697template <class _T1, class _D1>
2698inline _LIBCPP_INLINE_VISIBILITY
2699bool
2700operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2701{
2702 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2703 return less<_P1>()(__x.get(), nullptr);
2704}
2705
2706template <class _T1, class _D1>
2707inline _LIBCPP_INLINE_VISIBILITY
2708bool
2709operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2710{
2711 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2712 return less<_P1>()(nullptr, __x.get());
2713}
2714
2715template <class _T1, class _D1>
2716inline _LIBCPP_INLINE_VISIBILITY
2717bool
2718operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2719{
2720 return nullptr < __x;
2721}
2722
2723template <class _T1, class _D1>
2724inline _LIBCPP_INLINE_VISIBILITY
2725bool
2726operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2727{
2728 return __x < nullptr;
2729}
2730
2731template <class _T1, class _D1>
2732inline _LIBCPP_INLINE_VISIBILITY
2733bool
2734operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2735{
2736 return !(nullptr < __x);
2737}
2738
2739template <class _T1, class _D1>
2740inline _LIBCPP_INLINE_VISIBILITY
2741bool
2742operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2743{
2744 return !(__x < nullptr);
2745}
2746
2747template <class _T1, class _D1>
2748inline _LIBCPP_INLINE_VISIBILITY
2749bool
2750operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2751{
2752 return !(__x < nullptr);
2753}
2754
2755template <class _T1, class _D1>
2756inline _LIBCPP_INLINE_VISIBILITY
2757bool
2758operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2759{
2760 return !(nullptr < __x);
2761}
2762
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002763#if _LIBCPP_STD_VER > 11
2764
2765template<class _Tp>
2766struct __unique_if
2767{
2768 typedef unique_ptr<_Tp> __unique_single;
2769};
2770
2771template<class _Tp>
2772struct __unique_if<_Tp[]>
2773{
2774 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2775};
2776
2777template<class _Tp, size_t _Np>
2778struct __unique_if<_Tp[_Np]>
2779{
2780 typedef void __unique_array_known_bound;
2781};
2782
2783template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002784inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002785typename __unique_if<_Tp>::__unique_single
2786make_unique(_Args&&... __args)
2787{
2788 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2789}
2790
2791template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002792inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002793typename __unique_if<_Tp>::__unique_array_unknown_bound
2794make_unique(size_t __n)
2795{
2796 typedef typename remove_extent<_Tp>::type _Up;
2797 return unique_ptr<_Tp>(new _Up[__n]());
2798}
2799
2800template<class _Tp, class... _Args>
2801 typename __unique_if<_Tp>::__unique_array_known_bound
2802 make_unique(_Args&&...) = delete;
2803
2804#endif // _LIBCPP_STD_VER > 11
2805
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002806template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002807#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002808struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002809#else
2810struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002811 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002812#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002813{
2814 typedef unique_ptr<_Tp, _Dp> argument_type;
2815 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002816 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002817 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002818 {
2819 typedef typename argument_type::pointer pointer;
2820 return hash<pointer>()(__ptr.get());
2821 }
2822};
2823
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824struct __destruct_n
2825{
2826private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002827 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828
2829 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002830 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002831 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832
2833 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002834 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 {}
2836
Howard Hinnant719bda32011-05-28 14:41:13 +00002837 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002838 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002839 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 {}
2841
Howard Hinnant719bda32011-05-28 14:41:13 +00002842 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002843 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002844 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 {}
2846public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002847 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002848 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849
2850 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002851 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002852 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853
2854 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002855 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002856 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857
2858 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002859 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002860 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861};
2862
2863template <class _Alloc>
2864class __allocator_destructor
2865{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002866 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002868 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2869 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870private:
2871 _Alloc& __alloc_;
2872 size_type __s_;
2873public:
2874 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002875 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002878 void operator()(pointer __p) _NOEXCEPT
2879 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880};
2881
2882template <class _InputIterator, class _ForwardIterator>
2883_ForwardIterator
2884uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2885{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002887#ifndef _LIBCPP_NO_EXCEPTIONS
2888 _ForwardIterator __s = __r;
2889 try
2890 {
2891#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002892 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002893 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002894#ifndef _LIBCPP_NO_EXCEPTIONS
2895 }
2896 catch (...)
2897 {
2898 for (; __s != __r; ++__s)
2899 __s->~value_type();
2900 throw;
2901 }
2902#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903 return __r;
2904}
2905
2906template <class _InputIterator, class _Size, class _ForwardIterator>
2907_ForwardIterator
2908uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2909{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002911#ifndef _LIBCPP_NO_EXCEPTIONS
2912 _ForwardIterator __s = __r;
2913 try
2914 {
2915#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002916 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002917 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002918#ifndef _LIBCPP_NO_EXCEPTIONS
2919 }
2920 catch (...)
2921 {
2922 for (; __s != __r; ++__s)
2923 __s->~value_type();
2924 throw;
2925 }
2926#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 return __r;
2928}
2929
2930template <class _ForwardIterator, class _Tp>
2931void
2932uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2933{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002935#ifndef _LIBCPP_NO_EXCEPTIONS
2936 _ForwardIterator __s = __f;
2937 try
2938 {
2939#endif
2940 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002941 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002942#ifndef _LIBCPP_NO_EXCEPTIONS
2943 }
2944 catch (...)
2945 {
2946 for (; __s != __f; ++__s)
2947 __s->~value_type();
2948 throw;
2949 }
2950#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951}
2952
2953template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002954_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2956{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002958#ifndef _LIBCPP_NO_EXCEPTIONS
2959 _ForwardIterator __s = __f;
2960 try
2961 {
2962#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002963 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002964 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002965#ifndef _LIBCPP_NO_EXCEPTIONS
2966 }
2967 catch (...)
2968 {
2969 for (; __s != __f; ++__s)
2970 __s->~value_type();
2971 throw;
2972 }
2973#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002974 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975}
2976
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002977#if _LIBCPP_STD_VER > 14
2978
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002979template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002980inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002981void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2982 for (; __first != __last; ++__first)
2983 _VSTD::destroy_at(_VSTD::addressof(*__first));
2984}
2985
2986template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002987inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002988_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2989 for (; __n > 0; (void)++__first, --__n)
2990 _VSTD::destroy_at(_VSTD::addressof(*__first));
2991 return __first;
2992}
2993
Eric Fiselier290c07c2016-10-11 21:13:44 +00002994template <class _ForwardIterator>
2995inline _LIBCPP_INLINE_VISIBILITY
2996void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2997 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2998 auto __idx = __first;
2999#ifndef _LIBCPP_NO_EXCEPTIONS
3000 try {
3001#endif
3002 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003003 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00003004#ifndef _LIBCPP_NO_EXCEPTIONS
3005 } catch (...) {
3006 _VSTD::destroy(__first, __idx);
3007 throw;
3008 }
3009#endif
3010}
3011
3012template <class _ForwardIterator, class _Size>
3013inline _LIBCPP_INLINE_VISIBILITY
3014_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3015 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3016 auto __idx = __first;
3017#ifndef _LIBCPP_NO_EXCEPTIONS
3018 try {
3019#endif
3020 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003021 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00003022 return __idx;
3023#ifndef _LIBCPP_NO_EXCEPTIONS
3024 } catch (...) {
3025 _VSTD::destroy(__first, __idx);
3026 throw;
3027 }
3028#endif
3029}
3030
3031
3032template <class _ForwardIterator>
3033inline _LIBCPP_INLINE_VISIBILITY
3034void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3035 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3036 auto __idx = __first;
3037#ifndef _LIBCPP_NO_EXCEPTIONS
3038 try {
3039#endif
3040 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003041 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00003042#ifndef _LIBCPP_NO_EXCEPTIONS
3043 } catch (...) {
3044 _VSTD::destroy(__first, __idx);
3045 throw;
3046 }
3047#endif
3048}
3049
3050template <class _ForwardIterator, class _Size>
3051inline _LIBCPP_INLINE_VISIBILITY
3052_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3053 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3054 auto __idx = __first;
3055#ifndef _LIBCPP_NO_EXCEPTIONS
3056 try {
3057#endif
3058 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003059 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00003060 return __idx;
3061#ifndef _LIBCPP_NO_EXCEPTIONS
3062 } catch (...) {
3063 _VSTD::destroy(__first, __idx);
3064 throw;
3065 }
3066#endif
3067}
3068
3069
3070template <class _InputIt, class _ForwardIt>
3071inline _LIBCPP_INLINE_VISIBILITY
3072_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3073 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3074 auto __idx = __first_res;
3075#ifndef _LIBCPP_NO_EXCEPTIONS
3076 try {
3077#endif
3078 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003079 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00003080 return __idx;
3081#ifndef _LIBCPP_NO_EXCEPTIONS
3082 } catch (...) {
3083 _VSTD::destroy(__first_res, __idx);
3084 throw;
3085 }
3086#endif
3087}
3088
3089template <class _InputIt, class _Size, class _ForwardIt>
3090inline _LIBCPP_INLINE_VISIBILITY
3091pair<_InputIt, _ForwardIt>
3092uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3093 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3094 auto __idx = __first_res;
3095#ifndef _LIBCPP_NO_EXCEPTIONS
3096 try {
3097#endif
3098 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003099 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00003100 return {__first, __idx};
3101#ifndef _LIBCPP_NO_EXCEPTIONS
3102 } catch (...) {
3103 _VSTD::destroy(__first_res, __idx);
3104 throw;
3105 }
3106#endif
3107}
3108
3109
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003110#endif // _LIBCPP_STD_VER > 14
3111
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003112// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3113// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003114// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003115#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3116 && defined(__ATOMIC_RELAXED) \
3117 && defined(__ATOMIC_ACQ_REL)
3118# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003119#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003120# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3121#endif
3122
3123template <class _Tp>
3124inline _LIBCPP_INLINE_VISIBILITY _Tp
3125__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3126{
3127#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3128 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3129#else
3130 return __t += 1;
3131#endif
3132}
3133
3134template <class _Tp>
3135inline _LIBCPP_INLINE_VISIBILITY _Tp
3136__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3137{
3138#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3139 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3140#else
3141 return __t -= 1;
3142#endif
3143}
3144
Howard Hinnant756c69b2010-09-22 16:48:34 +00003145class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146 : public std::exception
3147{
3148public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003149 bad_weak_ptr() _NOEXCEPT = default;
3150 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 virtual ~bad_weak_ptr() _NOEXCEPT;
3152 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153};
3154
Louis Dionne16fe2952018-07-11 23:14:33 +00003155_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003156void __throw_bad_weak_ptr()
3157{
3158#ifndef _LIBCPP_NO_EXCEPTIONS
3159 throw bad_weak_ptr();
3160#else
3161 _VSTD::abort();
3162#endif
3163}
3164
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003165template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003167class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168{
3169 __shared_count(const __shared_count&);
3170 __shared_count& operator=(const __shared_count&);
3171
3172protected:
3173 long __shared_owners_;
3174 virtual ~__shared_count();
3175private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003176 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177
3178public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003180 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 : __shared_owners_(__refs) {}
3182
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003183#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003184 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003185 void __add_shared() _NOEXCEPT;
3186 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003187#else
3188 _LIBCPP_INLINE_VISIBILITY
3189 void __add_shared() _NOEXCEPT {
3190 __libcpp_atomic_refcount_increment(__shared_owners_);
3191 }
3192 _LIBCPP_INLINE_VISIBILITY
3193 bool __release_shared() _NOEXCEPT {
3194 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3195 __on_zero_shared();
3196 return true;
3197 }
3198 return false;
3199 }
3200#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003201 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003202 long use_count() const _NOEXCEPT {
3203 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3204 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205};
3206
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003207class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208 : private __shared_count
3209{
3210 long __shared_weak_owners_;
3211
3212public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003214 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215 : __shared_count(__refs),
3216 __shared_weak_owners_(__refs) {}
3217protected:
3218 virtual ~__shared_weak_count();
3219
3220public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003221#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003222 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003223 void __add_shared() _NOEXCEPT;
3224 void __add_weak() _NOEXCEPT;
3225 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003226#else
3227 _LIBCPP_INLINE_VISIBILITY
3228 void __add_shared() _NOEXCEPT {
3229 __shared_count::__add_shared();
3230 }
3231 _LIBCPP_INLINE_VISIBILITY
3232 void __add_weak() _NOEXCEPT {
3233 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3234 }
3235 _LIBCPP_INLINE_VISIBILITY
3236 void __release_shared() _NOEXCEPT {
3237 if (__shared_count::__release_shared())
3238 __release_weak();
3239 }
3240#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003241 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003243 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3244 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245
Howard Hinnant719bda32011-05-28 14:41:13 +00003246 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003248 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249};
3250
3251template <class _Tp, class _Dp, class _Alloc>
3252class __shared_ptr_pointer
3253 : public __shared_weak_count
3254{
3255 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3256public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003259 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260
Howard Hinnant72f73582010-08-11 17:04:31 +00003261#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003262 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003263#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003264
3265private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003266 virtual void __on_zero_shared() _NOEXCEPT;
3267 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268};
3269
Howard Hinnant72f73582010-08-11 17:04:31 +00003270#ifndef _LIBCPP_NO_RTTI
3271
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272template <class _Tp, class _Dp, class _Alloc>
3273const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003274__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003276 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277}
3278
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003279#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003280
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281template <class _Tp, class _Dp, class _Alloc>
3282void
Howard Hinnant719bda32011-05-28 14:41:13 +00003283__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003284{
3285 __data_.first().second()(__data_.first().first());
3286 __data_.first().second().~_Dp();
3287}
3288
3289template <class _Tp, class _Dp, class _Alloc>
3290void
Howard Hinnant719bda32011-05-28 14:41:13 +00003291__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003292{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003293 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3294 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003295 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3296
Eric Fiselierf8898c82015-02-05 23:01:40 +00003297 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003299 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300}
3301
3302template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05003303struct __shared_ptr_emplace
3304 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305{
Louis Dionne86549d72020-12-09 16:22:17 -05003306 _LIBCPP_HIDE_FROM_ABI
3307 explicit __shared_ptr_emplace(_Alloc __a)
3308 : __data_(_VSTD::move(__a), __value_init_tag())
3309 { }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310
Louis Dionne86549d72020-12-09 16:22:17 -05003311 template <class ..._Args>
3312 _LIBCPP_HIDE_FROM_ABI
3313 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003314#ifndef _LIBCPP_CXX03_LANG
Louis Dionne86549d72020-12-09 16:22:17 -05003315 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3316 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003317#else
Louis Dionne86549d72020-12-09 16:22:17 -05003318 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003319#endif
Louis Dionne86549d72020-12-09 16:22:17 -05003320 { }
3321
3322 _LIBCPP_HIDE_FROM_ABI
3323 _Tp* __get_elem() _NOEXCEPT { return _VSTD::addressof(__data_.second()); }
3324
3325 _LIBCPP_HIDE_FROM_ABI
3326 _Alloc& __get_alloc() _NOEXCEPT { return __data_.first(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327
3328private:
Louis Dionne86549d72020-12-09 16:22:17 -05003329 virtual void __on_zero_shared() _NOEXCEPT {
3330 __get_elem()->~_Tp();
3331 }
3332
3333 virtual void __on_zero_shared_weak() _NOEXCEPT {
3334 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
3335 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
3336 _ControlBlockAlloc __tmp(__get_alloc());
3337 __get_alloc().~_Alloc();
3338 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
3339 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
3340 }
3341
3342 __compressed_pair<_Alloc, _Tp> __data_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343};
3344
Erik Pilkington2a398762017-05-25 15:43:31 +00003345struct __shared_ptr_dummy_rebind_allocator_type;
3346template <>
3347class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3348{
3349public:
3350 template <class _Other>
3351 struct rebind
3352 {
3353 typedef allocator<_Other> other;
3354 };
3355};
3356
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003357template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358
zoecarverd73f42a2020-05-11 18:42:50 -07003359template<class _Tp, class _Up>
3360struct __compatible_with
3361#if _LIBCPP_STD_VER > 14
3362 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3363#else
3364 : is_convertible<_Tp*, _Up*> {};
3365#endif // _LIBCPP_STD_VER > 14
3366
Vy Nguyene369bd92020-07-13 12:34:37 -04003367#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3368# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3369#else
3370# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3371#endif
3372
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003374class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003376public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003377#if _LIBCPP_STD_VER > 14
3378 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003379 typedef remove_extent_t<_Tp> element_type;
3380#else
3381 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003382#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003383
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384private:
3385 element_type* __ptr_;
3386 __shared_weak_count* __cntrl_;
3387
3388 struct __nat {int __for_bool_;};
3389public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003391 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003393 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003394 template<class _Yp>
3395 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003396 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003397 template<class _Yp, class _Dp>
3398 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003399 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003400 template<class _Yp, class _Dp, class _Alloc>
3401 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003402 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3404 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003405 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003407 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003411 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003412 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003414 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003415 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003416 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003417 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003419 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003420#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00003421 template<class _Yp>
3422 shared_ptr(auto_ptr<_Yp>&& __r,
3423 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003424#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003425 template <class _Yp, class _Dp>
3426 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3427 typename enable_if
3428 <
3429 !is_lvalue_reference<_Dp>::value &&
3430 !is_array<_Yp>::value &&
3431 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3432 __nat
3433 >::type = __nat());
3434 template <class _Yp, class _Dp>
3435 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3436 typename enable_if
3437 <
3438 is_lvalue_reference<_Dp>::value &&
3439 !is_array<_Yp>::value &&
3440 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3441 __nat
3442 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443
3444 ~shared_ptr();
3445
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003447 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003448 template<class _Yp>
3449 typename enable_if
3450 <
zoecarverd73f42a2020-05-11 18:42:50 -07003451 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003452 shared_ptr&
3453 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003455 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003457 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003458 template<class _Yp>
3459 typename enable_if
3460 <
zoecarverd73f42a2020-05-11 18:42:50 -07003461 __compatible_with<_Yp, element_type>::value,
3462 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003463 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003465 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003466#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003467 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003469 typename enable_if
3470 <
3471 !is_array<_Yp>::value &&
3472 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003473 shared_ptr
3474 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003475 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003476#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003477 template <class _Yp, class _Dp>
3478 typename enable_if
3479 <
3480 !is_array<_Yp>::value &&
3481 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3482 shared_ptr&
3483 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003485 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003488 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003490 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003491 template<class _Yp>
3492 typename enable_if
3493 <
zoecarverd73f42a2020-05-11 18:42:50 -07003494 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003495 void
3496 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003498 reset(_Yp* __p);
3499 template<class _Yp, class _Dp>
3500 typename enable_if
3501 <
zoecarverd73f42a2020-05-11 18:42:50 -07003502 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003503 void
3504 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003506 reset(_Yp* __p, _Dp __d);
3507 template<class _Yp, class _Dp, class _Alloc>
3508 typename enable_if
3509 <
zoecarverd73f42a2020-05-11 18:42:50 -07003510 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003511 void
3512 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003514 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515
Howard Hinnant756c69b2010-09-22 16:48:34 +00003516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003517 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003519 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3520 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003521 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003522 element_type* operator->() const _NOEXCEPT
3523 {
3524 static_assert(!_VSTD::is_array<_Tp>::value,
3525 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3526 return __ptr_;
3527 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003529 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003531 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003532 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05003533 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003534 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003535 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003536 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003538 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003539 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003540 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003542 _LIBCPP_INLINE_VISIBILITY
3543 bool
3544 __owner_equivalent(const shared_ptr& __p) const
3545 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546
zoecarverd73f42a2020-05-11 18:42:50 -07003547#if _LIBCPP_STD_VER > 14
3548 typename add_lvalue_reference<element_type>::type
3549 _LIBCPP_INLINE_VISIBILITY
3550 operator[](ptrdiff_t __i) const
3551 {
3552 static_assert(_VSTD::is_array<_Tp>::value,
3553 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3554 return __ptr_[__i];
3555 }
3556#endif
3557
Howard Hinnant72f73582010-08-11 17:04:31 +00003558#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003561 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003562 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003563 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003564 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003565#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566
Zoe Carverd9040c72019-10-22 15:16:49 +00003567 template<class _Yp, class _CntrlBlk>
3568 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003569 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003570 {
3571 shared_ptr<_Tp> __r;
3572 __r.__ptr_ = __p;
3573 __r.__cntrl_ = __cntrl;
3574 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3575 return __r;
3576 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003577
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003579 template <class _Yp, bool = is_function<_Yp>::value>
3580 struct __shared_ptr_default_allocator
3581 {
3582 typedef allocator<_Yp> type;
3583 };
3584
3585 template <class _Yp>
3586 struct __shared_ptr_default_allocator<_Yp, true>
3587 {
3588 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3589 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003591 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003592 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003593 typename enable_if<is_convertible<_OrigPtr*,
3594 const enable_shared_from_this<_Yp>*
3595 >::value,
3596 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003597 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3598 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003600 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003601 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003602 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003603 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3604 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003605 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606 }
3607
Erik Pilkington2a398762017-05-25 15:43:31 +00003608 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609
zoecarverd73f42a2020-05-11 18:42:50 -07003610 template <class, class _Yp>
3611 struct __shared_ptr_default_delete
3612 : default_delete<_Yp> {};
3613
3614 template <class _Yp, class _Un, size_t _Sz>
3615 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3616 : default_delete<_Yp[]> {};
3617
3618 template <class _Yp, class _Un>
3619 struct __shared_ptr_default_delete<_Yp[], _Un>
3620 : default_delete<_Yp[]> {};
3621
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003622 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3623 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624};
3625
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003626#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3627template<class _Tp>
3628shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3629template<class _Tp, class _Dp>
3630shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3631#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003632
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003634inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003635_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003636shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003637 : __ptr_(nullptr),
3638 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639{
3640}
3641
3642template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003643inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003644_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003645shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003646 : __ptr_(nullptr),
3647 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648{
3649}
3650
3651template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003652template<class _Yp>
3653shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003654 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655 : __ptr_(__p)
3656{
3657 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003658 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003659 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3660 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003662 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663}
3664
3665template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003666template<class _Yp, class _Dp>
3667shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003668 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669 : __ptr_(__p)
3670{
3671#ifndef _LIBCPP_NO_EXCEPTIONS
3672 try
3673 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003674#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003675 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3676 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3677 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003678 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679#ifndef _LIBCPP_NO_EXCEPTIONS
3680 }
3681 catch (...)
3682 {
3683 __d(__p);
3684 throw;
3685 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003686#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687}
3688
3689template<class _Tp>
3690template<class _Dp>
3691shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003692 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
3694#ifndef _LIBCPP_NO_EXCEPTIONS
3695 try
3696 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003697#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003698 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3699 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3700 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701#ifndef _LIBCPP_NO_EXCEPTIONS
3702 }
3703 catch (...)
3704 {
3705 __d(__p);
3706 throw;
3707 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003708#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709}
3710
3711template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003712template<class _Yp, class _Dp, class _Alloc>
3713shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003714 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715 : __ptr_(__p)
3716{
3717#ifndef _LIBCPP_NO_EXCEPTIONS
3718 try
3719 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003720#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003722 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723 typedef __allocator_destructor<_A2> _D2;
3724 _A2 __a2(__a);
3725 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003726 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003727 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003728 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729#ifndef _LIBCPP_NO_EXCEPTIONS
3730 }
3731 catch (...)
3732 {
3733 __d(__p);
3734 throw;
3735 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003736#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739template<class _Tp>
3740template<class _Dp, class _Alloc>
3741shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003742 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743{
3744#ifndef _LIBCPP_NO_EXCEPTIONS
3745 try
3746 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003747#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003749 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750 typedef __allocator_destructor<_A2> _D2;
3751 _A2 __a2(__a);
3752 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05003753 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__p, __d, __a);
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003754 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755#ifndef _LIBCPP_NO_EXCEPTIONS
3756 }
3757 catch (...)
3758 {
3759 __d(__p);
3760 throw;
3761 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003762#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763}
3764
3765template<class _Tp>
3766template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003767inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003768shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769 : __ptr_(__p),
3770 __cntrl_(__r.__cntrl_)
3771{
3772 if (__cntrl_)
3773 __cntrl_->__add_shared();
3774}
3775
3776template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003777inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003778shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779 : __ptr_(__r.__ptr_),
3780 __cntrl_(__r.__cntrl_)
3781{
3782 if (__cntrl_)
3783 __cntrl_->__add_shared();
3784}
3785
3786template<class _Tp>
3787template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003788inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003790 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003791 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792 : __ptr_(__r.__ptr_),
3793 __cntrl_(__r.__cntrl_)
3794{
3795 if (__cntrl_)
3796 __cntrl_->__add_shared();
3797}
3798
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003800inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003801shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802 : __ptr_(__r.__ptr_),
3803 __cntrl_(__r.__cntrl_)
3804{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003805 __r.__ptr_ = nullptr;
3806 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807}
3808
3809template<class _Tp>
3810template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003811inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003813 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003814 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815 : __ptr_(__r.__ptr_),
3816 __cntrl_(__r.__cntrl_)
3817{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003818 __r.__ptr_ = nullptr;
3819 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820}
3821
Marshall Clowb22274f2017-01-24 22:22:33 +00003822#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003824template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003825shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003826 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 : __ptr_(__r.get())
3828{
3829 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3830 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003831 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 __r.release();
3833}
Marshall Clowb22274f2017-01-24 22:22:33 +00003834#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835
3836template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003837template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003839 typename enable_if
3840 <
3841 !is_lvalue_reference<_Dp>::value &&
3842 !is_array<_Yp>::value &&
3843 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3844 __nat
3845 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846 : __ptr_(__r.get())
3847{
Marshall Clow35cde742015-05-10 13:59:45 +00003848#if _LIBCPP_STD_VER > 11
3849 if (__ptr_ == nullptr)
3850 __cntrl_ = nullptr;
3851 else
3852#endif
3853 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003854 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3855 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3856 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003857 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003858 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859 __r.release();
3860}
3861
3862template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003863template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003865 typename enable_if
3866 <
3867 is_lvalue_reference<_Dp>::value &&
3868 !is_array<_Yp>::value &&
3869 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3870 __nat
3871 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872 : __ptr_(__r.get())
3873{
Marshall Clow35cde742015-05-10 13:59:45 +00003874#if _LIBCPP_STD_VER > 11
3875 if (__ptr_ == nullptr)
3876 __cntrl_ = nullptr;
3877 else
3878#endif
3879 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003880 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003881 typedef __shared_ptr_pointer<_Yp*,
3882 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003883 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003884 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003885 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003886 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887 __r.release();
3888}
3889
Zoe Carver6cd05c32019-08-19 15:47:16 +00003890template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891shared_ptr<_Tp>::~shared_ptr()
3892{
3893 if (__cntrl_)
3894 __cntrl_->__release_shared();
3895}
3896
3897template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003898inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003899shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003900shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901{
3902 shared_ptr(__r).swap(*this);
3903 return *this;
3904}
3905
3906template<class _Tp>
3907template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003908inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003909typename enable_if
3910<
zoecarverd73f42a2020-05-11 18:42:50 -07003911 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003912 shared_ptr<_Tp>&
3913>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003914shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915{
3916 shared_ptr(__r).swap(*this);
3917 return *this;
3918}
3919
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003921inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003923shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003925 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 return *this;
3927}
3928
3929template<class _Tp>
3930template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003931inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003932typename enable_if
3933<
zoecarverd73f42a2020-05-11 18:42:50 -07003934 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003935 shared_ptr<_Tp>&
3936>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3938{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003939 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940 return *this;
3941}
3942
Marshall Clowb22274f2017-01-24 22:22:33 +00003943#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944template<class _Tp>
3945template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003946inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003947typename enable_if
3948<
3949 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003950 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003951 shared_ptr<_Tp>
3952>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3954{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003955 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956 return *this;
3957}
Marshall Clowb22274f2017-01-24 22:22:33 +00003958#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959
3960template<class _Tp>
3961template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003962inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003963typename enable_if
3964<
3965 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003966 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003967 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003968 shared_ptr<_Tp>&
3969>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3971{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003972 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 return *this;
3974}
3975
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003977inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978void
Howard Hinnant719bda32011-05-28 14:41:13 +00003979shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003981 _VSTD::swap(__ptr_, __r.__ptr_);
3982 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983}
3984
3985template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003986inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987void
Howard Hinnant719bda32011-05-28 14:41:13 +00003988shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989{
3990 shared_ptr().swap(*this);
3991}
3992
3993template<class _Tp>
3994template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003995inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003996typename enable_if
3997<
zoecarverd73f42a2020-05-11 18:42:50 -07003998 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003999 void
4000>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001shared_ptr<_Tp>::reset(_Yp* __p)
4002{
4003 shared_ptr(__p).swap(*this);
4004}
4005
4006template<class _Tp>
4007template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004008inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004009typename enable_if
4010<
zoecarverd73f42a2020-05-11 18:42:50 -07004011 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004012 void
4013>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4015{
4016 shared_ptr(__p, __d).swap(*this);
4017}
4018
4019template<class _Tp>
4020template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004021inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004022typename enable_if
4023<
zoecarverd73f42a2020-05-11 18:42:50 -07004024 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004025 void
4026>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4028{
4029 shared_ptr(__p, __d, __a).swap(*this);
4030}
4031
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004032template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004034typename enable_if
4035<
4036 !is_array<_Tp>::value,
4037 shared_ptr<_Tp>
4038>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039allocate_shared(const _Alloc& __a, _Args&& ...__args)
4040{
Louis Dionne43c9f8f2020-12-09 16:57:28 -05004041 static_assert(is_constructible<_Tp, _Args...>::value,
4042 "allocate_shared/make_shared: the type is not constructible from the provided arguments");
zoecarver505730a2020-02-25 16:50:57 -08004043
4044 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4045 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4046 typedef __allocator_destructor<_A2> _D2;
4047
4048 _A2 __a2(__a);
4049 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05004050 ::new ((void*)_VSTD::addressof(*__hold2.get())) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
zoecarver505730a2020-02-25 16:50:57 -08004051
Louis Dionnef34e5c82020-11-10 12:47:10 -05004052 typename shared_ptr<_Tp>::element_type *__p = __hold2->__get_elem();
zoecarver505730a2020-02-25 16:50:57 -08004053 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054}
4055
Louis Dionne43c9f8f2020-12-09 16:57:28 -05004056template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
4057_LIBCPP_HIDE_FROM_ABI
4058shared_ptr<_Tp> make_shared(_Args&& ...__args)
4059{
4060 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
4061}
4062
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063template<class _Tp, class _Up>
4064inline _LIBCPP_INLINE_VISIBILITY
4065bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004066operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067{
4068 return __x.get() == __y.get();
4069}
4070
4071template<class _Tp, class _Up>
4072inline _LIBCPP_INLINE_VISIBILITY
4073bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004074operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075{
4076 return !(__x == __y);
4077}
4078
4079template<class _Tp, class _Up>
4080inline _LIBCPP_INLINE_VISIBILITY
4081bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004082operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004084#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004085 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4086 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004087#else
4088 return less<>()(__x.get(), __y.get());
4089#endif
4090
Howard Hinnantb17caf92012-02-21 21:02:58 +00004091}
4092
4093template<class _Tp, class _Up>
4094inline _LIBCPP_INLINE_VISIBILITY
4095bool
4096operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4097{
4098 return __y < __x;
4099}
4100
4101template<class _Tp, class _Up>
4102inline _LIBCPP_INLINE_VISIBILITY
4103bool
4104operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4105{
4106 return !(__y < __x);
4107}
4108
4109template<class _Tp, class _Up>
4110inline _LIBCPP_INLINE_VISIBILITY
4111bool
4112operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4113{
4114 return !(__x < __y);
4115}
4116
4117template<class _Tp>
4118inline _LIBCPP_INLINE_VISIBILITY
4119bool
4120operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4121{
4122 return !__x;
4123}
4124
4125template<class _Tp>
4126inline _LIBCPP_INLINE_VISIBILITY
4127bool
4128operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4129{
4130 return !__x;
4131}
4132
4133template<class _Tp>
4134inline _LIBCPP_INLINE_VISIBILITY
4135bool
4136operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4137{
4138 return static_cast<bool>(__x);
4139}
4140
4141template<class _Tp>
4142inline _LIBCPP_INLINE_VISIBILITY
4143bool
4144operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4145{
4146 return static_cast<bool>(__x);
4147}
4148
4149template<class _Tp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151bool
4152operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4153{
4154 return less<_Tp*>()(__x.get(), nullptr);
4155}
4156
4157template<class _Tp>
4158inline _LIBCPP_INLINE_VISIBILITY
4159bool
4160operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4161{
4162 return less<_Tp*>()(nullptr, __x.get());
4163}
4164
4165template<class _Tp>
4166inline _LIBCPP_INLINE_VISIBILITY
4167bool
4168operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4169{
4170 return nullptr < __x;
4171}
4172
4173template<class _Tp>
4174inline _LIBCPP_INLINE_VISIBILITY
4175bool
4176operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4177{
4178 return __x < nullptr;
4179}
4180
4181template<class _Tp>
4182inline _LIBCPP_INLINE_VISIBILITY
4183bool
4184operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4185{
4186 return !(nullptr < __x);
4187}
4188
4189template<class _Tp>
4190inline _LIBCPP_INLINE_VISIBILITY
4191bool
4192operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4193{
4194 return !(__x < nullptr);
4195}
4196
4197template<class _Tp>
4198inline _LIBCPP_INLINE_VISIBILITY
4199bool
4200operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4201{
4202 return !(__x < nullptr);
4203}
4204
4205template<class _Tp>
4206inline _LIBCPP_INLINE_VISIBILITY
4207bool
4208operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4209{
4210 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211}
4212
4213template<class _Tp>
4214inline _LIBCPP_INLINE_VISIBILITY
4215void
Howard Hinnant719bda32011-05-28 14:41:13 +00004216swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217{
4218 __x.swap(__y);
4219}
4220
4221template<class _Tp, class _Up>
4222inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004223shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004224static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225{
zoecarverd73f42a2020-05-11 18:42:50 -07004226 return shared_ptr<_Tp>(__r,
4227 static_cast<
4228 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229}
4230
4231template<class _Tp, class _Up>
4232inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004233shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004234dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235{
zoecarverd73f42a2020-05-11 18:42:50 -07004236 typedef typename shared_ptr<_Tp>::element_type _ET;
4237 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4239}
4240
4241template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004242shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004243const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244{
zoecarverd73f42a2020-05-11 18:42:50 -07004245 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004246 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247}
4248
zoecarverd73f42a2020-05-11 18:42:50 -07004249template<class _Tp, class _Up>
4250shared_ptr<_Tp>
4251reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4252{
4253 return shared_ptr<_Tp>(__r,
4254 reinterpret_cast<
4255 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4256}
4257
Howard Hinnant72f73582010-08-11 17:04:31 +00004258#ifndef _LIBCPP_NO_RTTI
4259
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260template<class _Dp, class _Tp>
4261inline _LIBCPP_INLINE_VISIBILITY
4262_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004263get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264{
4265 return __p.template __get_deleter<_Dp>();
4266}
4267
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004268#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004269
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04004271class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004273public:
4274 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275private:
4276 element_type* __ptr_;
4277 __shared_weak_count* __cntrl_;
4278
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004279public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004281 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004282 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004283 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4284 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004286 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004287 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004288 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4289 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004290
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004292 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004293 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004294 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4295 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004296 ~weak_ptr();
4297
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004299 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004300 template<class _Yp>
4301 typename enable_if
4302 <
4303 is_convertible<_Yp*, element_type*>::value,
4304 weak_ptr&
4305 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004307 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4308
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004310 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4311 template<class _Yp>
4312 typename enable_if
4313 <
4314 is_convertible<_Yp*, element_type*>::value,
4315 weak_ptr&
4316 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004318 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4319
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004320 template<class _Yp>
4321 typename enable_if
4322 <
4323 is_convertible<_Yp*, element_type*>::value,
4324 weak_ptr&
4325 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004327 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004328
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004330 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004332 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004333
Howard Hinnant756c69b2010-09-22 16:48:34 +00004334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004335 long use_count() const _NOEXCEPT
4336 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004338 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05004339 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00004340 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004341 template<class _Up>
4342 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004343 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004345 template<class _Up>
4346 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004347 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 {return __cntrl_ < __r.__cntrl_;}
4349
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004350 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4351 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352};
4353
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004354#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4355template<class _Tp>
4356weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4357#endif
4358
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004360inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004361_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004362weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05004363 : __ptr_(nullptr),
4364 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365{
4366}
4367
4368template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004369inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004370weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371 : __ptr_(__r.__ptr_),
4372 __cntrl_(__r.__cntrl_)
4373{
4374 if (__cntrl_)
4375 __cntrl_->__add_weak();
4376}
4377
4378template<class _Tp>
4379template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004380inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004381weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004382 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004383 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384 : __ptr_(__r.__ptr_),
4385 __cntrl_(__r.__cntrl_)
4386{
4387 if (__cntrl_)
4388 __cntrl_->__add_weak();
4389}
4390
4391template<class _Tp>
4392template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004393inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004395 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004396 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397 : __ptr_(__r.__ptr_),
4398 __cntrl_(__r.__cntrl_)
4399{
4400 if (__cntrl_)
4401 __cntrl_->__add_weak();
4402}
4403
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004404template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004405inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004406weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4407 : __ptr_(__r.__ptr_),
4408 __cntrl_(__r.__cntrl_)
4409{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004410 __r.__ptr_ = nullptr;
4411 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004412}
4413
4414template<class _Tp>
4415template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004416inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004417weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4418 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4419 _NOEXCEPT
4420 : __ptr_(__r.__ptr_),
4421 __cntrl_(__r.__cntrl_)
4422{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004423 __r.__ptr_ = nullptr;
4424 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004425}
4426
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427template<class _Tp>
4428weak_ptr<_Tp>::~weak_ptr()
4429{
4430 if (__cntrl_)
4431 __cntrl_->__release_weak();
4432}
4433
4434template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004435inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004437weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438{
4439 weak_ptr(__r).swap(*this);
4440 return *this;
4441}
4442
4443template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004444template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004445inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004446typename enable_if
4447<
4448 is_convertible<_Yp*, _Tp*>::value,
4449 weak_ptr<_Tp>&
4450>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004451weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452{
4453 weak_ptr(__r).swap(*this);
4454 return *this;
4455}
4456
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004457template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004458inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004459weak_ptr<_Tp>&
4460weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4461{
4462 weak_ptr(_VSTD::move(__r)).swap(*this);
4463 return *this;
4464}
4465
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004467template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004468inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004469typename enable_if
4470<
4471 is_convertible<_Yp*, _Tp*>::value,
4472 weak_ptr<_Tp>&
4473>::type
4474weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4475{
4476 weak_ptr(_VSTD::move(__r)).swap(*this);
4477 return *this;
4478}
4479
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004480template<class _Tp>
4481template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004482inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004483typename enable_if
4484<
4485 is_convertible<_Yp*, _Tp*>::value,
4486 weak_ptr<_Tp>&
4487>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004488weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489{
4490 weak_ptr(__r).swap(*this);
4491 return *this;
4492}
4493
4494template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004495inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496void
Howard Hinnant719bda32011-05-28 14:41:13 +00004497weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004499 _VSTD::swap(__ptr_, __r.__ptr_);
4500 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501}
4502
4503template<class _Tp>
4504inline _LIBCPP_INLINE_VISIBILITY
4505void
Howard Hinnant719bda32011-05-28 14:41:13 +00004506swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507{
4508 __x.swap(__y);
4509}
4510
4511template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004512inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004513void
Howard Hinnant719bda32011-05-28 14:41:13 +00004514weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515{
4516 weak_ptr().swap(*this);
4517}
4518
4519template<class _Tp>
4520template<class _Yp>
4521shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004522 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523 : __ptr_(__r.__ptr_),
4524 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4525{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004526 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00004527 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528}
4529
4530template<class _Tp>
4531shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004532weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533{
4534 shared_ptr<_Tp> __r;
4535 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4536 if (__r.__cntrl_)
4537 __r.__ptr_ = __ptr_;
4538 return __r;
4539}
4540
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004541#if _LIBCPP_STD_VER > 14
4542template <class _Tp = void> struct owner_less;
4543#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004544template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004545#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546
4547template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004548struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004550{
4551 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004552 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004553 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004555 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004556 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004558 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004559 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004560 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004561};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004562
4563template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004564struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4566{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004567 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004568 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004569 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004570 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004571 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004572 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004574 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004575 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004576 {return __x.owner_before(__y);}
4577};
4578
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004579#if _LIBCPP_STD_VER > 14
4580template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004581struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004582{
4583 template <class _Tp, class _Up>
4584 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004585 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004586 {return __x.owner_before(__y);}
4587 template <class _Tp, class _Up>
4588 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004589 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004590 {return __x.owner_before(__y);}
4591 template <class _Tp, class _Up>
4592 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004593 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004594 {return __x.owner_before(__y);}
4595 template <class _Tp, class _Up>
4596 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004597 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004598 {return __x.owner_before(__y);}
4599 typedef void is_transparent;
4600};
4601#endif
4602
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004604class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605{
4606 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004607protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004608 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004609 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004611 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004613 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4614 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004616 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004617public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004619 shared_ptr<_Tp> shared_from_this()
4620 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004622 shared_ptr<_Tp const> shared_from_this() const
4623 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004624
Eric Fiselier84006862016-06-02 00:15:35 +00004625#if _LIBCPP_STD_VER > 14
4626 _LIBCPP_INLINE_VISIBILITY
4627 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4628 { return __weak_this_; }
4629
4630 _LIBCPP_INLINE_VISIBILITY
4631 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4632 { return __weak_this_; }
4633#endif // _LIBCPP_STD_VER > 14
4634
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635 template <class _Up> friend class shared_ptr;
4636};
4637
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004638template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004639struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004640{
4641 typedef shared_ptr<_Tp> argument_type;
4642 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004643
Howard Hinnant756c69b2010-09-22 16:48:34 +00004644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004645 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004646 {
zoecarverd73f42a2020-05-11 18:42:50 -07004647 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004648 }
4649};
4650
Howard Hinnantc834c512011-11-29 18:15:50 +00004651template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004652inline _LIBCPP_INLINE_VISIBILITY
4653basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004654operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004655
Eric Fiselier9b492672016-06-18 02:12:53 +00004656
4657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004658
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004659class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004660{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004661 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004662public:
4663 void lock() _NOEXCEPT;
4664 void unlock() _NOEXCEPT;
4665
4666private:
4667 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4668 __sp_mut(const __sp_mut&);
4669 __sp_mut& operator=(const __sp_mut&);
4670
Howard Hinnant8331b762013-03-06 23:30:19 +00004671 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004672};
4673
Mehdi Amini228053d2017-05-04 17:08:54 +00004674_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4675__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004676
4677template <class _Tp>
4678inline _LIBCPP_INLINE_VISIBILITY
4679bool
4680atomic_is_lock_free(const shared_ptr<_Tp>*)
4681{
4682 return false;
4683}
4684
4685template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004686_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004687shared_ptr<_Tp>
4688atomic_load(const shared_ptr<_Tp>* __p)
4689{
4690 __sp_mut& __m = __get_sp_mut(__p);
4691 __m.lock();
4692 shared_ptr<_Tp> __q = *__p;
4693 __m.unlock();
4694 return __q;
4695}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004696
Howard Hinnant9fa30202012-07-30 01:40:57 +00004697template <class _Tp>
4698inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004699_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004700shared_ptr<_Tp>
4701atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4702{
4703 return atomic_load(__p);
4704}
4705
4706template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004707_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004708void
4709atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4710{
4711 __sp_mut& __m = __get_sp_mut(__p);
4712 __m.lock();
4713 __p->swap(__r);
4714 __m.unlock();
4715}
4716
4717template <class _Tp>
4718inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004719_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004720void
4721atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4722{
4723 atomic_store(__p, __r);
4724}
4725
4726template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004727_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004728shared_ptr<_Tp>
4729atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4730{
4731 __sp_mut& __m = __get_sp_mut(__p);
4732 __m.lock();
4733 __p->swap(__r);
4734 __m.unlock();
4735 return __r;
4736}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004737
Howard Hinnant9fa30202012-07-30 01:40:57 +00004738template <class _Tp>
4739inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004740_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004741shared_ptr<_Tp>
4742atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4743{
4744 return atomic_exchange(__p, __r);
4745}
4746
4747template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004748_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004749bool
4750atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4751{
Marshall Clow4201ee82016-05-18 17:50:13 +00004752 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004753 __sp_mut& __m = __get_sp_mut(__p);
4754 __m.lock();
4755 if (__p->__owner_equivalent(*__v))
4756 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004757 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004758 *__p = __w;
4759 __m.unlock();
4760 return true;
4761 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004762 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004763 *__v = *__p;
4764 __m.unlock();
4765 return false;
4766}
4767
4768template <class _Tp>
4769inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004770_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004771bool
4772atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4773{
4774 return atomic_compare_exchange_strong(__p, __v, __w);
4775}
4776
4777template <class _Tp>
4778inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004779_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004780bool
4781atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4782 shared_ptr<_Tp> __w, memory_order, memory_order)
4783{
4784 return atomic_compare_exchange_strong(__p, __v, __w);
4785}
4786
4787template <class _Tp>
4788inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004789_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004790bool
4791atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4792 shared_ptr<_Tp> __w, memory_order, memory_order)
4793{
4794 return atomic_compare_exchange_weak(__p, __v, __w);
4795}
4796
Eric Fiselier9b492672016-06-18 02:12:53 +00004797#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004798
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004799//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004800#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4801# ifndef _LIBCPP_CXX03_LANG
4802enum class pointer_safety : unsigned char {
4803 relaxed,
4804 preferred,
4805 strict
4806};
4807# endif
4808#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004809struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004810{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004811 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004812 {
4813 relaxed,
4814 preferred,
4815 strict
4816 };
4817
Howard Hinnant49e145e2012-10-30 19:06:59 +00004818 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004819
Howard Hinnant756c69b2010-09-22 16:48:34 +00004820 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004821 pointer_safety() : __v_() {}
4822
4823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004824 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004826 operator int() const {return __v_;}
4827};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004828#endif
4829
4830#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004831 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004832_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4833#else
4834// This function is only offered in C++03 under ABI v1.
4835# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4836inline _LIBCPP_INLINE_VISIBILITY
4837pointer_safety get_pointer_safety() _NOEXCEPT {
4838 return pointer_safety::relaxed;
4839}
4840# endif
4841#endif
4842
Howard Hinnantc51e1022010-05-11 19:42:16 +00004843
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004844_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4845_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4846_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004847_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004848
4849template <class _Tp>
4850inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004851_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004852undeclare_reachable(_Tp* __p)
4853{
4854 return static_cast<_Tp*>(__undeclare_reachable(__p));
4855}
4856
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004857_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004858
Marshall Clow8982dcd2015-07-13 20:04:56 +00004859// --- Helper for container swap --
4860template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004861_LIBCPP_INLINE_VISIBILITY
4862void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4863#if _LIBCPP_STD_VER >= 14
4864 _NOEXCEPT
4865#else
4866 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4867#endif
4868{
4869 using _VSTD::swap;
4870 swap(__a1, __a2);
4871}
4872
4873template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004874inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004875void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4876
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004877template <typename _Alloc>
4878inline _LIBCPP_INLINE_VISIBILITY
4879void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4880#if _LIBCPP_STD_VER >= 14
4881 _NOEXCEPT
4882#else
4883 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4884#endif
4885{
4886 _VSTD::__swap_allocator(__a1, __a2,
4887 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4888}
4889
Marshall Clowff91de82015-08-18 19:51:37 +00004890template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004891struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004892 _Traits::propagate_on_container_move_assignment::value
4893#if _LIBCPP_STD_VER > 14
4894 || _Traits::is_always_equal::value
4895#else
4896 && is_nothrow_move_assignable<_Alloc>::value
4897#endif
4898 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004899
Marshall Clowa591b9a2016-07-11 21:38:08 +00004900
Marshall Clowa591b9a2016-07-11 21:38:08 +00004901template <class _Tp, class _Alloc>
4902struct __temp_value {
4903 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004904
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004905 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004906 _Alloc &__a;
4907
4908 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4909 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004910
Marshall Clowa591b9a2016-07-11 21:38:08 +00004911 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004912 _LIBCPP_NO_CFI
4913 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4914 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4915 _VSTD::forward<_Args>(__args)...);
4916 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004917
Marshall Clowa591b9a2016-07-11 21:38:08 +00004918 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4919 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004920
Marshall Clowe46031a2018-07-02 18:41:15 +00004921template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004922struct __is_allocator : false_type {};
4923
4924template<typename _Alloc>
4925struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004926 typename __void_t<typename _Alloc::value_type>::type,
4927 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4928 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004929 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004930
Eric Fiselier74ebee62019-06-08 01:31:19 +00004931// __builtin_new_allocator -- A non-templated helper for allocating and
4932// deallocating memory using __builtin_operator_new and
4933// __builtin_operator_delete. It should be used in preference to
4934// `std::allocator<T>` to avoid additional instantiations.
4935struct __builtin_new_allocator {
4936 struct __builtin_new_deleter {
4937 typedef void* pointer_type;
4938
4939 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4940 : __size_(__size), __align_(__align) {}
4941
4942 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004943 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004944 }
4945
4946 private:
4947 size_t __size_;
4948 size_t __align_;
4949 };
4950
4951 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4952
4953 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004954 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004955 __builtin_new_deleter(__s, __align));
4956 }
4957
4958 static void __deallocate_bytes(void* __p, size_t __s,
4959 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004960 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004961 }
4962
4963 template <class _Tp>
4964 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4965 static __holder_t __allocate_type(size_t __n) {
4966 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4967 }
4968
4969 template <class _Tp>
4970 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4971 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4972 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4973 }
4974};
4975
4976
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977_LIBCPP_END_NAMESPACE_STD
4978
Eric Fiselierf4433a32017-05-31 22:07:49 +00004979_LIBCPP_POP_MACROS
4980
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004981#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004982# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004983#endif
4984
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985#endif // _LIBCPP_MEMORY