blob: b5193396673fbdee3f8a3123008869b5befb7365 [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
895template<class _Tp>
896_LIBCPP_CONSTEXPR_AFTER_CXX17 void* __voidify(_Tp& __ptr) noexcept {
897 return const_cast<void*>(static_cast<const volatile void*>(_VSTD::addressof(__ptr)));
898}
899
900template<class _Tp, class ..._Args, class = decltype(
901 ::new (_VSTD::declval<void*>()) _Tp(_VSTD::declval<_Args>()...)
902)>
903_LIBCPP_INLINE_VISIBILITY
904constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
905 _LIBCPP_ASSERT(__location, "null pointer given to construct_at");
906 return ::new (_VSTD::__voidify(*__location)) _Tp(_VSTD::forward<_Args>(__args)...);
907}
908
909#endif
910
911// destroy_at
912
913#if _LIBCPP_STD_VER > 14
914
915template <class _Tp>
916inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
917void destroy_at(_Tp* __loc) {
918 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
919 __loc->~_Tp();
920}
921
922#endif
923
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924// allocator_traits
925
Marshall Clow0be70c32017-06-14 21:23:57 +0000926template <class _Tp, class = void>
927struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928
929template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000930struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000931 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932
933namespace __pointer_type_imp
934{
935
936template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
937struct __pointer_type
938{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000939 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940};
941
942template <class _Tp, class _Dp>
943struct __pointer_type<_Tp, _Dp, false>
944{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000945 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946};
947
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000948} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
950template <class _Tp, class _Dp>
951struct __pointer_type
952{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000953 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 +0000954};
955
Marshall Clow0be70c32017-06-14 21:23:57 +0000956template <class _Tp, class = void>
957struct __has_const_pointer : false_type {};
958
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000960struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000961 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
963template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
964struct __const_pointer
965{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000966 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967};
968
969template <class _Tp, class _Ptr, class _Alloc>
970struct __const_pointer<_Tp, _Ptr, _Alloc, false>
971{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000972#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000973 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974#else
975 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
976#endif
977};
978
Marshall Clow0be70c32017-06-14 21:23:57 +0000979template <class _Tp, class = void>
980struct __has_void_pointer : false_type {};
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000983struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000984 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
986template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
987struct __void_pointer
988{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000989 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990};
991
992template <class _Ptr, class _Alloc>
993struct __void_pointer<_Ptr, _Alloc, false>
994{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000995#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000996 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000998 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999#endif
1000};
1001
Marshall Clow0be70c32017-06-14 21:23:57 +00001002template <class _Tp, class = void>
1003struct __has_const_void_pointer : false_type {};
1004
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001006struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001007 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
1009template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1010struct __const_void_pointer
1011{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001012 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013};
1014
1015template <class _Ptr, class _Alloc>
1016struct __const_void_pointer<_Ptr, _Alloc, false>
1017{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001018#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001019 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001021 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022#endif
1023};
1024
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001025
1026template <bool _UsePointerTraits> struct __to_address_helper;
1027
1028template <> struct __to_address_helper<true> {
1029 template <class _Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001030 using __return_type = decltype(pointer_traits<_Pointer>::to_address(_VSTD::declval<const _Pointer&>()));
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001031
1032 template <class _Pointer>
1033 _LIBCPP_CONSTEXPR
1034 static __return_type<_Pointer>
1035 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1036};
1037
1038template <class _Pointer, bool _Dummy = true>
1039using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1040
1041
Howard Hinnantc834c512011-11-29 18:15:50 +00001042template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001043inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001044_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001045__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001047 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 return __p;
1049}
1050
1051template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001052inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1053typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1054__to_address(const _Pointer& __p) _NOEXCEPT {
1055 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001056}
1057
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001058template <> struct __to_address_helper<false> {
1059 template <class _Pointer>
1060 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001061
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001062 template <class _Pointer>
1063 _LIBCPP_CONSTEXPR
1064 static __return_type<_Pointer>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001065 __do_it(const _Pointer &__p) _NOEXCEPT { return _VSTD::__to_address(__p.operator->()); }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001066};
1067
1068
1069#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001070template <class _Tp>
1071inline _LIBCPP_INLINE_VISIBILITY constexpr
1072_Tp*
1073to_address(_Tp* __p) _NOEXCEPT
1074{
1075 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1076 return __p;
1077}
1078
1079template <class _Pointer>
Marek Kurdej1f0cde92020-12-06 15:23:46 +01001080inline _LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001081auto
1082to_address(const _Pointer& __p) _NOEXCEPT
1083{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001084 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001085}
1086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087
Marshall Clow0be70c32017-06-14 21:23:57 +00001088template <class _Tp, class = void>
1089struct __has_size_type : false_type {};
1090
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001092struct __has_size_type<_Tp,
1093 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001095template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096struct __size_type
1097{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001098 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099};
1100
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001101template <class _Alloc, class _DiffType>
1102struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001104 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105};
1106
Marshall Clow0be70c32017-06-14 21:23:57 +00001107template <class _Tp, class = void>
1108struct __has_propagate_on_container_copy_assignment : false_type {};
1109
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001111struct __has_propagate_on_container_copy_assignment<_Tp,
1112 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1113 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114
1115template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1116struct __propagate_on_container_copy_assignment
1117{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001118 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119};
1120
1121template <class _Alloc>
1122struct __propagate_on_container_copy_assignment<_Alloc, true>
1123{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001124 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125};
1126
Marshall Clow0be70c32017-06-14 21:23:57 +00001127template <class _Tp, class = void>
1128struct __has_propagate_on_container_move_assignment : false_type {};
1129
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001131struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001132 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1133 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001144 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145};
1146
Marshall Clow0be70c32017-06-14 21:23:57 +00001147template <class _Tp, class = void>
1148struct __has_propagate_on_container_swap : false_type {};
1149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001151struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001152 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1153 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154
1155template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1156struct __propagate_on_container_swap
1157{
1158 typedef false_type type;
1159};
1160
1161template <class _Alloc>
1162struct __propagate_on_container_swap<_Alloc, true>
1163{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001164 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165};
1166
Marshall Clow0be70c32017-06-14 21:23:57 +00001167template <class _Tp, class = void>
1168struct __has_is_always_equal : false_type {};
1169
Marshall Clow0b587562015-06-02 16:34:03 +00001170template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001171struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001172 typename __void_t<typename _Tp::is_always_equal>::type>
1173 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001174
1175template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1176struct __is_always_equal
1177{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001178 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001179};
1180
1181template <class _Alloc>
1182struct __is_always_equal<_Alloc, true>
1183{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001184 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001185};
1186
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1188struct __has_rebind_other
1189{
1190private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001191 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001193 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001195 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196public:
1197 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1198};
1199
1200template <class _Tp, class _Up>
1201struct __has_rebind_other<_Tp, _Up, false>
1202{
1203 static const bool value = false;
1204};
1205
1206template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1207struct __allocator_traits_rebind
1208{
Michael Park99f9e912020-03-04 11:27:14 -05001209 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001210 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001211 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212};
1213
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1215struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1216{
Michael Park99f9e912020-03-04 11:27:14 -05001217 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001218 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001219 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220};
1221
1222template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1223struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1224{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001225 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226};
1227
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001228#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
Michael Park99f9e912020-03-04 11:27:14 -05001230_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1232auto
1233__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001234 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001235_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236
1237template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1238auto
1239__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1240 -> false_type;
1241
1242template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1243struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001244 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1245 declval<_SizeType>(),
1246 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247{
1248};
1249
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001250#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251
1252template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1253struct __has_allocate_hint
1254 : true_type
1255{
1256};
1257
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001258#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
zoecarver75816d42020-05-23 14:32:12 -07001260_LIBCPP_SUPPRESS_DEPRECATED_PUSH
zoecarver20590dd2020-05-23 14:03:04 -07001261template <class _Alloc, class ..._Args,
1262 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1263static true_type __test_has_construct(int);
zoecarver75816d42020-05-23 14:32:12 -07001264_LIBCPP_SUPPRESS_DEPRECATED_POP
1265
zoecarver20590dd2020-05-23 14:03:04 -07001266template <class _Alloc, class...>
1267static false_type __test_has_construct(...);
1268
1269template <class _Alloc, class ..._Args>
1270struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1271
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001272#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273
Michael Park99f9e912020-03-04 11:27:14 -05001274_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275template <class _Alloc, class _Pointer>
1276auto
1277__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1278 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001279_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
1281template <class _Alloc, class _Pointer>
1282auto
1283__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1284 -> false_type;
1285
1286template <class _Alloc, class _Pointer>
1287struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001288 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1289 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290{
1291};
1292
Michael Park99f9e912020-03-04 11:27:14 -05001293_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294template <class _Alloc>
1295auto
1296__has_max_size_test(_Alloc&& __a)
1297 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001298_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299
1300template <class _Alloc>
1301auto
1302__has_max_size_test(const volatile _Alloc& __a)
1303 -> false_type;
1304
1305template <class _Alloc>
1306struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001307 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308{
1309};
1310
1311template <class _Alloc>
1312auto
1313__has_select_on_container_copy_construction_test(_Alloc&& __a)
1314 -> decltype(__a.select_on_container_copy_construction(), true_type());
1315
1316template <class _Alloc>
1317auto
1318__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1319 -> false_type;
1320
1321template <class _Alloc>
1322struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001323 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324{
1325};
1326
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001327#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001329template <class _Alloc, class _Pointer, class = void>
1330struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
1332template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001333struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1334 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001335>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336
1337template <class _Alloc>
1338struct __has_max_size
1339 : true_type
1340{
1341};
1342
1343template <class _Alloc>
1344struct __has_select_on_container_copy_construction
1345 : false_type
1346{
1347};
1348
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001349#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001351template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1352struct __alloc_traits_difference_type
1353{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001354 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001355};
1356
1357template <class _Alloc, class _Ptr>
1358struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1359{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001360 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001361};
1362
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001363template <class _Tp>
1364struct __is_default_allocator : false_type {};
1365
1366template <class _Tp>
1367struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1368
Eric Fiselier909fe962019-09-13 16:09:33 +00001369
1370
1371template <class _Alloc,
1372 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1373 >
1374struct __is_cpp17_move_insertable;
1375template <class _Alloc>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001376struct __is_cpp17_move_insertable<_Alloc, true> : true_type {};
Eric Fiselier909fe962019-09-13 16:09:33 +00001377template <class _Alloc>
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001378struct __is_cpp17_move_insertable<_Alloc, false> : is_move_constructible<typename _Alloc::value_type> {};
Eric Fiselier909fe962019-09-13 16:09:33 +00001379
1380template <class _Alloc,
1381 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1382 >
1383struct __is_cpp17_copy_insertable;
1384template <class _Alloc>
1385struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1386template <class _Alloc>
1387struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001388 is_copy_constructible<typename _Alloc::value_type>::value &&
Eric Fiselier909fe962019-09-13 16:09:33 +00001389 __is_cpp17_move_insertable<_Alloc>::value>
1390 {};
1391
1392
1393
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001395struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396{
1397 typedef _Alloc allocator_type;
1398 typedef typename allocator_type::value_type value_type;
1399
1400 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1401 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1402 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1403 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1404
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001405 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1406 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407
1408 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1409 propagate_on_container_copy_assignment;
1410 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1411 propagate_on_container_move_assignment;
1412 typedef typename __propagate_on_container_swap<allocator_type>::type
1413 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001414 typedef typename __is_always_equal<allocator_type>::type
1415 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001417#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001419 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001420 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001421#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 template <class _Tp> struct rebind_alloc
1423 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1424 template <class _Tp> struct rebind_traits
1425 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001426#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427
Louis Dionne99f59432020-09-17 12:06:13 -04001428 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 static pointer allocate(allocator_type& __a, size_type __n)
1430 {return __a.allocate(__n);}
Louis Dionne99f59432020-09-17 12:06:13 -04001431 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001433 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1435
Louis Dionne99f59432020-09-17 12:06:13 -04001436 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001437 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 {__a.deallocate(__p, __n);}
1439
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001441 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001443 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001444 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
1446 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001447 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 static void destroy(allocator_type& __a, _Tp* __p)
1449 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1450
Louis Dionne99f59432020-09-17 12:06:13 -04001451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow4f834b52013-08-27 20:22:15 +00001452 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1454
Louis Dionne99f59432020-09-17 12:06:13 -04001455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 static allocator_type
1457 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001458 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 __has_select_on_container_copy_construction<const allocator_type>(),
1460 __a);}
1461
1462private:
Louis Dionne99f59432020-09-17 12:06:13 -04001463 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001464 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001466 {
1467 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1468 return __a.allocate(__n, __hint);
1469 _LIBCPP_SUPPRESS_DEPRECATED_POP
1470 }
Louis Dionne99f59432020-09-17 12:06:13 -04001471 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001472 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001473 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 {return __a.allocate(__n);}
1475
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001477 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001479 {
1480 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1481 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1482 _LIBCPP_SUPPRESS_DEPRECATED_POP
1483 }
1484
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001486 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1488 {
Louis Dionne99f59432020-09-17 12:06:13 -04001489#if _LIBCPP_STD_VER > 17
1490 _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
1491#else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001492 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne99f59432020-09-17 12:06:13 -04001493#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495
1496 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001497 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001499 {
1500 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1501 __a.destroy(__p);
1502 _LIBCPP_SUPPRESS_DEPRECATED_POP
1503 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001505 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 static void __destroy(false_type, allocator_type&, _Tp* __p)
1507 {
Louis Dionne99f59432020-09-17 12:06:13 -04001508#if _LIBCPP_STD_VER > 17
1509 _VSTD::destroy_at(__p);
1510#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511 __p->~_Tp();
Louis Dionne99f59432020-09-17 12:06:13 -04001512#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 }
1514
Louis Dionne99f59432020-09-17 12:06:13 -04001515 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001516 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001517 {
1518 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1519 return __a.max_size();
1520 _LIBCPP_SUPPRESS_DEPRECATED_POP
1521 }
1522
Louis Dionne99f59432020-09-17 12:06:13 -04001523 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001524 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001525 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526
Louis Dionne99f59432020-09-17 12:06:13 -04001527 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001529 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 {return __a.select_on_container_copy_construction();}
Louis Dionne99f59432020-09-17 12:06:13 -04001531 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001533 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 {return __a;}
1535};
1536
Marshall Clow940e01c2015-04-07 05:21:38 +00001537template <class _Traits, class _Tp>
1538struct __rebind_alloc_helper
1539{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001540#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001541 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001542#else
1543 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1544#endif
1545};
1546
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547// allocator
1548
1549template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001550class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551{
1552public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001553 typedef size_t size_type;
1554 typedef ptrdiff_t difference_type;
1555 typedef _Tp value_type;
1556 typedef true_type propagate_on_container_move_assignment;
1557 typedef true_type is_always_equal;
1558
1559 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1560 allocator() _NOEXCEPT { }
1561
1562 template <class _Up>
1563 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1564 allocator(const allocator<_Up>&) _NOEXCEPT { }
1565
1566 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1567 _Tp* allocate(size_t __n) {
1568 if (__n > allocator_traits<allocator>::max_size(*this))
1569 __throw_length_error("allocator<T>::allocate(size_t n)"
1570 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -04001571 if (__libcpp_is_constant_evaluated()) {
1572 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
1573 } else {
1574 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1575 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001576 }
1577
1578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1579 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -04001580 if (__libcpp_is_constant_evaluated()) {
1581 ::operator delete(__p);
1582 } else {
1583 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1584 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001585 }
1586
1587 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001588#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001589 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1590 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1591 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1592 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1593
Louis Dionne481a2662018-09-23 18:35:00 +00001594 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001595 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1596 typedef allocator<_Up> other;
1597 };
Marshall Clowbc759762018-03-20 23:02:53 +00001598
Michael Park99f9e912020-03-04 11:27:14 -05001599 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001600 pointer address(reference __x) const _NOEXCEPT {
1601 return _VSTD::addressof(__x);
1602 }
Michael Park99f9e912020-03-04 11:27:14 -05001603 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001604 const_pointer address(const_reference __x) const _NOEXCEPT {
1605 return _VSTD::addressof(__x);
1606 }
Michael Park99f9e912020-03-04 11:27:14 -05001607
Michael Park99f9e912020-03-04 11:27:14 -05001608 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001609 _Tp* allocate(size_t __n, const void*) {
1610 return allocate(__n);
1611 }
Michael Park99f9e912020-03-04 11:27:14 -05001612
Louis Dionnef8b6c022020-09-21 10:36:37 -04001613 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1614 return size_type(~0) / sizeof(_Tp);
1615 }
Michael Park99f9e912020-03-04 11:27:14 -05001616
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001618 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1619 void construct(_Up* __p, _Args&&... __args) {
1620 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1621 }
1622
1623 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1624 void destroy(pointer __p) {
1625 __p->~_Tp();
1626 }
Michael Park99f9e912020-03-04 11:27:14 -05001627#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628};
1629
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001630template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001631class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001632{
1633public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001634 typedef size_t size_type;
1635 typedef ptrdiff_t difference_type;
1636 typedef const _Tp value_type;
1637 typedef true_type propagate_on_container_move_assignment;
1638 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001639
Marshall Clowbc759762018-03-20 23:02:53 +00001640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001641 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +00001642
1643 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001645 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -05001646
Louis Dionne99f59432020-09-17 12:06:13 -04001647 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001648 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -04001649 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +00001650 __throw_length_error("allocator<const T>::allocate(size_t n)"
1651 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -04001652 if (__libcpp_is_constant_evaluated()) {
1653 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
1654 } else {
1655 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1656 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001657 }
Michael Park99f9e912020-03-04 11:27:14 -05001658
Louis Dionne99f59432020-09-17 12:06:13 -04001659 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001660 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -04001661 if (__libcpp_is_constant_evaluated()) {
1662 ::operator delete(const_cast<_Tp*>(__p));
1663 } else {
1664 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1665 }
Louis Dionnef8b6c022020-09-21 10:36:37 -04001666 }
Michael Park99f9e912020-03-04 11:27:14 -05001667
Louis Dionnef8b6c022020-09-21 10:36:37 -04001668 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001669#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -04001670 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1671 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1672 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1673 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1674
1675 template <class _Up>
1676 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1677 typedef allocator<_Up> other;
1678 };
1679
1680 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1681 const_pointer address(const_reference __x) const _NOEXCEPT {
1682 return _VSTD::addressof(__x);
1683 }
1684
1685 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1686 const _Tp* allocate(size_t __n, const void*) {
1687 return allocate(__n);
1688 }
1689
1690 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1691 return size_type(~0) / sizeof(_Tp);
1692 }
Michael Park99f9e912020-03-04 11:27:14 -05001693
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001694 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001695 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1696 void construct(_Up* __p, _Args&&... __args) {
1697 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1698 }
Howard Hinnant9e028f12012-05-01 15:37:54 +00001699
Louis Dionnef8b6c022020-09-21 10:36:37 -04001700 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1701 void destroy(pointer __p) {
1702 __p->~_Tp();
1703 }
Michael Park99f9e912020-03-04 11:27:14 -05001704#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001705};
1706
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001708inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001709bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710
1711template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001712inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001713bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714
Louis Dionned6651542020-11-03 12:05:55 -05001715template <class _Alloc, class _Ptr>
1716_LIBCPP_INLINE_VISIBILITY
1717void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
1718 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -05001719 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -05001720 typedef allocator_traits<_Alloc> _Traits;
1721 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
1722 _Traits::construct(__a, _VSTD::__to_address(__begin2),
1723#ifdef _LIBCPP_NO_EXCEPTIONS
1724 _VSTD::move(*__begin1)
1725#else
1726 _VSTD::move_if_noexcept(*__begin1)
1727#endif
1728 );
1729 }
1730}
1731
1732template <class _Alloc, class _Tp, typename enable_if<
1733 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1734 is_trivially_move_constructible<_Tp>::value
1735>::type>
1736_LIBCPP_INLINE_VISIBILITY
1737void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1738 ptrdiff_t _Np = __end1 - __begin1;
1739 if (_Np > 0) {
1740 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1741 __begin2 += _Np;
1742 }
1743}
1744
1745template <class _Alloc, class _Iter, class _Ptr>
1746_LIBCPP_INLINE_VISIBILITY
1747void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1748 typedef allocator_traits<_Alloc> _Traits;
1749 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1750 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1751 }
1752}
1753
1754template <class _Alloc, class _Source, class _Dest,
1755 class _RawSource = typename remove_const<_Source>::type,
1756 class _RawDest = typename remove_const<_Dest>::type,
1757 class =
1758 typename enable_if<
1759 is_trivially_copy_constructible<_Dest>::value &&
1760 is_same<_RawSource, _RawDest>::value &&
1761 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1762 >::type>
1763_LIBCPP_INLINE_VISIBILITY
1764void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1765 ptrdiff_t _Np = __end1 - __begin1;
1766 if (_Np > 0) {
1767 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1768 __begin2 += _Np;
1769 }
1770}
1771
1772template <class _Alloc, class _Ptr>
1773_LIBCPP_INLINE_VISIBILITY
1774void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1775 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1776 "The specified type does not meet the requirements of Cpp17MoveInsertable");
1777 typedef allocator_traits<_Alloc> _Traits;
1778 while (__end1 != __begin1) {
1779 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1780#ifdef _LIBCPP_NO_EXCEPTIONS
1781 _VSTD::move(*--__end1)
1782#else
1783 _VSTD::move_if_noexcept(*--__end1)
1784#endif
1785 );
1786 --__end2;
1787 }
1788}
1789
1790template <class _Alloc, class _Tp, class = typename enable_if<
1791 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1792 is_trivially_move_constructible<_Tp>::value
1793>::type>
1794_LIBCPP_INLINE_VISIBILITY
1795void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1796 ptrdiff_t _Np = __end1 - __begin1;
1797 __end2 -= _Np;
1798 if (_Np > 0)
1799 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1800}
1801
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001803class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 : public iterator<output_iterator_tag,
1805 _Tp, // purposefully not C++03
1806 ptrdiff_t, // purposefully not C++03
1807 _Tp*, // purposefully not C++03
1808 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1809{
1810private:
1811 _OutputIterator __x_;
1812public:
1813 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1814 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1815 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001816 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001817#if _LIBCPP_STD_VER >= 14
1818 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001819 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001820#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1822 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1823 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001824#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001825 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001826#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827};
1828
1829template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001830_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001832get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833{
1834 pair<_Tp*, ptrdiff_t> __r(0, 0);
1835 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1836 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1837 / sizeof(_Tp);
1838 if (__n > __m)
1839 __n = __m;
1840 while (__n > 0)
1841 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001842#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001843 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001844 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001845 align_val_t __al =
1846 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001847 __r.first = static_cast<_Tp*>(::operator new(
1848 __n * sizeof(_Tp), __al, nothrow));
1849 } else {
1850 __r.first = static_cast<_Tp*>(::operator new(
1851 __n * sizeof(_Tp), nothrow));
1852 }
1853#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001854 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001855 {
1856 // Since aligned operator new is unavailable, return an empty
1857 // buffer rather than one with invalid alignment.
1858 return __r;
1859 }
1860
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001862#endif
1863
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864 if (__r.first)
1865 {
1866 __r.second = __n;
1867 break;
1868 }
1869 __n /= 2;
1870 }
1871 return __r;
1872}
1873
1874template <class _Tp>
1875inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001876void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1877{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001878 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001879}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880
Marshall Clowb22274f2017-01-24 22:22:33 +00001881#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001883struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884{
1885 _Tp* __ptr_;
1886};
1887
1888template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001889class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
1891private:
1892 _Tp* __ptr_;
1893public:
1894 typedef _Tp element_type;
1895
Dimitry Andric47269ce2020-03-13 19:36:26 +01001896 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1897 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1898 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001900 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001902 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001904 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001906 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907
Dimitry Andric47269ce2020-03-13 19:36:26 +01001908 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001910 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1911 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1912 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913 {
1914 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001915 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 return __t;
1917 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001918 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 {
1920 if (__ptr_ != __p)
1921 delete __ptr_;
1922 __ptr_ = __p;
1923 }
1924
Dimitry Andric47269ce2020-03-13 19:36:26 +01001925 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1926 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001928 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 {return auto_ptr<_Up>(release());}
1930};
1931
1932template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001933class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934{
1935public:
1936 typedef void element_type;
1937};
Marshall Clowb22274f2017-01-24 22:22:33 +00001938#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001940// Tag used to default initialize one or both of the pair's elements.
1941struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001942struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001943
Eric Fiselier9d355982017-04-12 23:45:53 +00001944template <class _Tp, int _Idx,
1945 bool _CanBeEmptyBase =
1946 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1947struct __compressed_pair_elem {
1948 typedef _Tp _ParamT;
1949 typedef _Tp& reference;
1950 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001952 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001953 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001954 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1955 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956
Eric Fiselier9d355982017-04-12 23:45:53 +00001957 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001958 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1959 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001960 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001961 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001962 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001963 : __value_(_VSTD::forward<_Up>(__u))
1964 {
1965 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001967
1968#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001969 template <class... _Args, size_t... _Indexes>
1970 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1971 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1972 __tuple_indices<_Indexes...>)
1973 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001974#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001976
Alex Lorenz76132112017-11-09 17:54:49 +00001977 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1978 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001979 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001982 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983};
1984
Eric Fiselier9d355982017-04-12 23:45:53 +00001985template <class _Tp, int _Idx>
1986struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1987 typedef _Tp _ParamT;
1988 typedef _Tp& reference;
1989 typedef const _Tp& const_reference;
1990 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001992 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1993 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1994 __compressed_pair_elem(__default_init_tag) {}
1995 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1996 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997
Eric Fiselier9d355982017-04-12 23:45:53 +00001998 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001999 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2000 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002001 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002002 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002003 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002004 : __value_type(_VSTD::forward<_Up>(__u))
2005 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002007#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002008 template <class... _Args, size_t... _Indexes>
2009 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2010 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2011 __tuple_indices<_Indexes...>)
2012 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002013#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014
Alex Lorenz76132112017-11-09 17:54:49 +00002015 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2016 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002017 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018};
2019
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002021class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2022 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002023 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2024 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002025
2026 // NOTE: This static assert should never fire because __compressed_pair
2027 // is *almost never* used in a scenario where it's possible for T1 == T2.
2028 // (The exception is std::function where it is possible that the function
2029 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002030 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05002031 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00002032 "The current implementation is NOT ABI-compatible with the previous "
2033 "implementation for this configuration");
2034
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002036 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002037 class = typename enable_if<
2038 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2039 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2040 >::type
2041 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002042 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002043 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044
Eric Fiselier9d355982017-04-12 23:45:53 +00002045 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002046 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002047 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002048 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002050#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002051 template <class... _Args1, class... _Args2>
2052 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2053 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2054 tuple<_Args2...> __second_args)
2055 : _Base1(__pc, _VSTD::move(__first_args),
2056 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2057 _Base2(__pc, _VSTD::move(__second_args),
2058 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002059#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060
Eric Fiselier9d355982017-04-12 23:45:53 +00002061 _LIBCPP_INLINE_VISIBILITY
2062 typename _Base1::reference first() _NOEXCEPT {
2063 return static_cast<_Base1&>(*this).__get();
2064 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065
Eric Fiselier9d355982017-04-12 23:45:53 +00002066 _LIBCPP_INLINE_VISIBILITY
2067 typename _Base1::const_reference first() const _NOEXCEPT {
2068 return static_cast<_Base1 const&>(*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::reference second() _NOEXCEPT {
2073 return static_cast<_Base2&>(*this).__get();
2074 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075
Eric Fiselier9d355982017-04-12 23:45:53 +00002076 _LIBCPP_INLINE_VISIBILITY
2077 typename _Base2::const_reference second() const _NOEXCEPT {
2078 return static_cast<_Base2 const&>(*this).__get();
2079 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081 _LIBCPP_INLINE_VISIBILITY
2082 void swap(__compressed_pair& __x)
2083 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2084 __is_nothrow_swappable<_T2>::value)
2085 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002086 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00002087 swap(first(), __x.first());
2088 swap(second(), __x.second());
2089 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090};
2091
2092template <class _T1, class _T2>
2093inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002094void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2095 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2096 __is_nothrow_swappable<_T2>::value) {
2097 __x.swap(__y);
2098}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002100// default_delete
2101
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002103struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002104 static_assert(!is_function<_Tp>::value,
2105 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002106#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002107 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002108#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002109 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002110#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002111 template <class _Up>
2112 _LIBCPP_INLINE_VISIBILITY
2113 default_delete(const default_delete<_Up>&,
2114 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2115 0) _NOEXCEPT {}
2116
2117 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2118 static_assert(sizeof(_Tp) > 0,
2119 "default_delete can not delete incomplete type");
2120 static_assert(!is_void<_Tp>::value,
2121 "default_delete can not delete incomplete type");
2122 delete __ptr;
2123 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124};
2125
2126template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002127struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2128private:
2129 template <class _Up>
2130 struct _EnableIfConvertible
2131 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2132
Howard Hinnant4500ca52011-12-18 21:19:44 +00002133public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002134#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002135 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002136#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002137 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002138#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002139
2140 template <class _Up>
2141 _LIBCPP_INLINE_VISIBILITY
2142 default_delete(const default_delete<_Up[]>&,
2143 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2144
2145 template <class _Up>
2146 _LIBCPP_INLINE_VISIBILITY
2147 typename _EnableIfConvertible<_Up>::type
2148 operator()(_Up* __ptr) const _NOEXCEPT {
2149 static_assert(sizeof(_Tp) > 0,
2150 "default_delete can not delete incomplete type");
2151 static_assert(!is_void<_Tp>::value,
2152 "default_delete can not delete void type");
2153 delete[] __ptr;
2154 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155};
2156
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002157template <class _Deleter>
2158struct __unique_ptr_deleter_sfinae {
2159 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2160 typedef const _Deleter& __lval_ref_type;
2161 typedef _Deleter&& __good_rval_ref_type;
2162 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163};
2164
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002165template <class _Deleter>
2166struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2167 typedef const _Deleter& __lval_ref_type;
2168 typedef const _Deleter&& __bad_rval_ref_type;
2169 typedef false_type __enable_rval_overload;
2170};
2171
2172template <class _Deleter>
2173struct __unique_ptr_deleter_sfinae<_Deleter&> {
2174 typedef _Deleter& __lval_ref_type;
2175 typedef _Deleter&& __bad_rval_ref_type;
2176 typedef false_type __enable_rval_overload;
2177};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002178
Vy Nguyene369bd92020-07-13 12:34:37 -04002179#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2180# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2181#else
2182# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2183#endif
2184
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002185template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04002186class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002187public:
2188 typedef _Tp element_type;
2189 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002190 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002191
2192 static_assert(!is_rvalue_reference<deleter_type>::value,
2193 "the specified deleter type cannot be an rvalue reference");
2194
2195private:
2196 __compressed_pair<pointer, deleter_type> __ptr_;
2197
2198 struct __nat { int __for_bool_; };
2199
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002200 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002201
2202 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002203 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002204 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002205
2206 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002207 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002208 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002209
2210 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002211 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002212 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002213
2214 template <bool _Dummy, class _Deleter = typename __dependent_type<
2215 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002216 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002217 typename enable_if<is_default_constructible<_Deleter>::value &&
2218 !is_pointer<_Deleter>::value>::type;
2219
2220 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002221 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002222 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2223
2224 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002225 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002226 is_convertible<typename _UPtr::pointer, pointer>::value &&
2227 !is_array<_Up>::value
2228 >::type;
2229
2230 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002231 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002232 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2233 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2234 >::type;
2235
2236 template <class _UDel>
2237 using _EnableIfDeleterAssignable = typename enable_if<
2238 is_assignable<_Dp&, _UDel&&>::value
2239 >::type;
2240
2241public:
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() _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 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __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 = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002255 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002256
2257 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002258 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002259 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002260 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002261 : __ptr_(__p, __d) {}
2262
2263 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002264 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002266 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002267 : __ptr_(__p, _VSTD::move(__d)) {
2268 static_assert(!is_reference<deleter_type>::value,
2269 "rvalue deleter bound to reference");
2270 }
2271
2272 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002273 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002274 _LIBCPP_INLINE_VISIBILITY
2275 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2276
2277 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002278 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002279 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2280 }
2281
2282 template <class _Up, class _Ep,
2283 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2284 class = _EnableIfDeleterConvertible<_Ep>
2285 >
2286 _LIBCPP_INLINE_VISIBILITY
2287 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2288 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2289
2290#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2291 template <class _Up>
2292 _LIBCPP_INLINE_VISIBILITY
2293 unique_ptr(auto_ptr<_Up>&& __p,
2294 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002295 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002296 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002297 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002298#endif
2299
2300 _LIBCPP_INLINE_VISIBILITY
2301 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2302 reset(__u.release());
2303 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2304 return *this;
2305 }
2306
2307 template <class _Up, class _Ep,
2308 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2309 class = _EnableIfDeleterAssignable<_Ep>
2310 >
2311 _LIBCPP_INLINE_VISIBILITY
2312 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2313 reset(__u.release());
2314 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2315 return *this;
2316 }
2317
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002318#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2319 template <class _Up>
2320 _LIBCPP_INLINE_VISIBILITY
2321 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2322 is_same<_Dp, default_delete<_Tp> >::value,
2323 unique_ptr&>::type
2324 operator=(auto_ptr<_Up> __p) {
2325 reset(__p.release());
2326 return *this;
2327 }
2328#endif
2329
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002330#ifdef _LIBCPP_CXX03_LANG
2331 unique_ptr(unique_ptr const&) = delete;
2332 unique_ptr& operator=(unique_ptr const&) = delete;
2333#endif
2334
2335
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002336 _LIBCPP_INLINE_VISIBILITY
2337 ~unique_ptr() { reset(); }
2338
2339 _LIBCPP_INLINE_VISIBILITY
2340 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2341 reset();
2342 return *this;
2343 }
2344
2345 _LIBCPP_INLINE_VISIBILITY
2346 typename add_lvalue_reference<_Tp>::type
2347 operator*() const {
2348 return *__ptr_.first();
2349 }
2350 _LIBCPP_INLINE_VISIBILITY
2351 pointer operator->() const _NOEXCEPT {
2352 return __ptr_.first();
2353 }
2354 _LIBCPP_INLINE_VISIBILITY
2355 pointer get() const _NOEXCEPT {
2356 return __ptr_.first();
2357 }
2358 _LIBCPP_INLINE_VISIBILITY
2359 deleter_type& get_deleter() _NOEXCEPT {
2360 return __ptr_.second();
2361 }
2362 _LIBCPP_INLINE_VISIBILITY
2363 const deleter_type& get_deleter() const _NOEXCEPT {
2364 return __ptr_.second();
2365 }
2366 _LIBCPP_INLINE_VISIBILITY
2367 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2368 return __ptr_.first() != nullptr;
2369 }
2370
2371 _LIBCPP_INLINE_VISIBILITY
2372 pointer release() _NOEXCEPT {
2373 pointer __t = __ptr_.first();
2374 __ptr_.first() = pointer();
2375 return __t;
2376 }
2377
2378 _LIBCPP_INLINE_VISIBILITY
2379 void reset(pointer __p = pointer()) _NOEXCEPT {
2380 pointer __tmp = __ptr_.first();
2381 __ptr_.first() = __p;
2382 if (__tmp)
2383 __ptr_.second()(__tmp);
2384 }
2385
2386 _LIBCPP_INLINE_VISIBILITY
2387 void swap(unique_ptr& __u) _NOEXCEPT {
2388 __ptr_.swap(__u.__ptr_);
2389 }
2390};
2391
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002392
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002394class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002396 typedef _Tp element_type;
2397 typedef _Dp deleter_type;
2398 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2399
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002401 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402
Eric Fiselier31127cd2017-04-16 02:14:31 +00002403 template <class _From>
2404 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405
Eric Fiselier31127cd2017-04-16 02:14:31 +00002406 template <class _FromElem>
2407 struct _CheckArrayPointerConversion<_FromElem*>
2408 : integral_constant<bool,
2409 is_same<_FromElem*, pointer>::value ||
2410 (is_same<pointer, element_type*>::value &&
2411 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2412 >
2413 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414
Eric Fiselier31127cd2017-04-16 02:14:31 +00002415 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002416
2417 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002418 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002419 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002420
2421 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002422 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002423 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002424
2425 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002426 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002427 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002428
2429 template <bool _Dummy, class _Deleter = typename __dependent_type<
2430 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002431 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002432 typename enable_if<is_default_constructible<_Deleter>::value &&
2433 !is_pointer<_Deleter>::value>::type;
2434
2435 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002436 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002437 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2438
2439 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002440 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002441 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002442 >::type;
2443
2444 template <class _UPtr, class _Up,
2445 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002446 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002447 is_array<_Up>::value &&
2448 is_same<pointer, element_type*>::value &&
2449 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2450 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2451 >::type;
2452
2453 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002454 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002455 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2456 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2457 >::type;
2458
2459 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002460 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002461 is_assignable<_Dp&, _UDel&&>::value
2462 >::type;
2463
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464public:
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() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002470 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002471 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002472 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002473 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002475 template <class _Pp, bool _Dummy = true,
2476 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002477 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002478 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002479 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002480 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002482 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002483 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2484 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002485 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002486 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002487 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002489 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002490 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002491 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002492 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002493 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002495 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002496 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2497 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002499 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500 : __ptr_(__p, _VSTD::move(__d)) {
2501 static_assert(!is_reference<deleter_type>::value,
2502 "rvalue deleter bound to reference");
2503 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002505 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002506 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002507 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002508 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002509 : __ptr_(nullptr, _VSTD::move(__d)) {
2510 static_assert(!is_reference<deleter_type>::value,
2511 "rvalue deleter bound to reference");
2512 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002513
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002514 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002515 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2516 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002517 _LIBCPP_INLINE_VISIBILITY
2518 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
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(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002522 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2523 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002524
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002525 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002526 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002527 reset(__u.release());
2528 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2529 return *this;
2530 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002532 template <class _Up, class _Ep,
2533 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2534 class = _EnableIfDeleterConvertible<_Ep>
2535 >
2536 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002537 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002538 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002539 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002541 template <class _Up, class _Ep,
2542 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2543 class = _EnableIfDeleterAssignable<_Ep>
2544 >
2545 _LIBCPP_INLINE_VISIBILITY
2546 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002547 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548 reset(__u.release());
2549 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2550 return *this;
2551 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002553#ifdef _LIBCPP_CXX03_LANG
2554 unique_ptr(unique_ptr const&) = delete;
2555 unique_ptr& operator=(unique_ptr const&) = delete;
2556#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002557
2558public:
2559 _LIBCPP_INLINE_VISIBILITY
2560 ~unique_ptr() { reset(); }
2561
2562 _LIBCPP_INLINE_VISIBILITY
2563 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2564 reset();
2565 return *this;
2566 }
2567
2568 _LIBCPP_INLINE_VISIBILITY
2569 typename add_lvalue_reference<_Tp>::type
2570 operator[](size_t __i) const {
2571 return __ptr_.first()[__i];
2572 }
2573 _LIBCPP_INLINE_VISIBILITY
2574 pointer get() const _NOEXCEPT {
2575 return __ptr_.first();
2576 }
2577
2578 _LIBCPP_INLINE_VISIBILITY
2579 deleter_type& get_deleter() _NOEXCEPT {
2580 return __ptr_.second();
2581 }
2582
2583 _LIBCPP_INLINE_VISIBILITY
2584 const deleter_type& get_deleter() const _NOEXCEPT {
2585 return __ptr_.second();
2586 }
2587 _LIBCPP_INLINE_VISIBILITY
2588 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2589 return __ptr_.first() != nullptr;
2590 }
2591
2592 _LIBCPP_INLINE_VISIBILITY
2593 pointer release() _NOEXCEPT {
2594 pointer __t = __ptr_.first();
2595 __ptr_.first() = pointer();
2596 return __t;
2597 }
2598
2599 template <class _Pp>
2600 _LIBCPP_INLINE_VISIBILITY
2601 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002602 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603 >::type
2604 reset(_Pp __p) _NOEXCEPT {
2605 pointer __tmp = __ptr_.first();
2606 __ptr_.first() = __p;
2607 if (__tmp)
2608 __ptr_.second()(__tmp);
2609 }
2610
2611 _LIBCPP_INLINE_VISIBILITY
2612 void reset(nullptr_t = nullptr) _NOEXCEPT {
2613 pointer __tmp = __ptr_.first();
2614 __ptr_.first() = nullptr;
2615 if (__tmp)
2616 __ptr_.second()(__tmp);
2617 }
2618
2619 _LIBCPP_INLINE_VISIBILITY
2620 void swap(unique_ptr& __u) _NOEXCEPT {
2621 __ptr_.swap(__u.__ptr_);
2622 }
2623
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624};
2625
2626template <class _Tp, class _Dp>
2627inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002628typename enable_if<
2629 __is_swappable<_Dp>::value,
2630 void
2631>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002632swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633
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.get() == __y.get();}
2638
2639template <class _T1, class _D1, class _T2, class _D2>
2640inline _LIBCPP_INLINE_VISIBILITY
2641bool
2642operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2643
2644template <class _T1, class _D1, class _T2, class _D2>
2645inline _LIBCPP_INLINE_VISIBILITY
2646bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002647operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2648{
2649 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2650 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002651 typedef typename common_type<_P1, _P2>::type _Vp;
2652 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002653}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654
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 !(__y < __x);}
2664
2665template <class _T1, class _D1, class _T2, class _D2>
2666inline _LIBCPP_INLINE_VISIBILITY
2667bool
2668operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2669
Howard Hinnantb17caf92012-02-21 21:02:58 +00002670template <class _T1, class _D1>
2671inline _LIBCPP_INLINE_VISIBILITY
2672bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002673operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002674{
2675 return !__x;
2676}
2677
2678template <class _T1, class _D1>
2679inline _LIBCPP_INLINE_VISIBILITY
2680bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002681operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002682{
2683 return !__x;
2684}
2685
2686template <class _T1, class _D1>
2687inline _LIBCPP_INLINE_VISIBILITY
2688bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002689operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002690{
2691 return static_cast<bool>(__x);
2692}
2693
2694template <class _T1, class _D1>
2695inline _LIBCPP_INLINE_VISIBILITY
2696bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002697operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002698{
2699 return static_cast<bool>(__x);
2700}
2701
2702template <class _T1, class _D1>
2703inline _LIBCPP_INLINE_VISIBILITY
2704bool
2705operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2706{
2707 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2708 return less<_P1>()(__x.get(), nullptr);
2709}
2710
2711template <class _T1, class _D1>
2712inline _LIBCPP_INLINE_VISIBILITY
2713bool
2714operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2715{
2716 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2717 return less<_P1>()(nullptr, __x.get());
2718}
2719
2720template <class _T1, class _D1>
2721inline _LIBCPP_INLINE_VISIBILITY
2722bool
2723operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2724{
2725 return nullptr < __x;
2726}
2727
2728template <class _T1, class _D1>
2729inline _LIBCPP_INLINE_VISIBILITY
2730bool
2731operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2732{
2733 return __x < nullptr;
2734}
2735
2736template <class _T1, class _D1>
2737inline _LIBCPP_INLINE_VISIBILITY
2738bool
2739operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2740{
2741 return !(nullptr < __x);
2742}
2743
2744template <class _T1, class _D1>
2745inline _LIBCPP_INLINE_VISIBILITY
2746bool
2747operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2748{
2749 return !(__x < nullptr);
2750}
2751
2752template <class _T1, class _D1>
2753inline _LIBCPP_INLINE_VISIBILITY
2754bool
2755operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2756{
2757 return !(__x < nullptr);
2758}
2759
2760template <class _T1, class _D1>
2761inline _LIBCPP_INLINE_VISIBILITY
2762bool
2763operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2764{
2765 return !(nullptr < __x);
2766}
2767
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002768#if _LIBCPP_STD_VER > 11
2769
2770template<class _Tp>
2771struct __unique_if
2772{
2773 typedef unique_ptr<_Tp> __unique_single;
2774};
2775
2776template<class _Tp>
2777struct __unique_if<_Tp[]>
2778{
2779 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2780};
2781
2782template<class _Tp, size_t _Np>
2783struct __unique_if<_Tp[_Np]>
2784{
2785 typedef void __unique_array_known_bound;
2786};
2787
2788template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002789inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002790typename __unique_if<_Tp>::__unique_single
2791make_unique(_Args&&... __args)
2792{
2793 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2794}
2795
2796template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002797inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002798typename __unique_if<_Tp>::__unique_array_unknown_bound
2799make_unique(size_t __n)
2800{
2801 typedef typename remove_extent<_Tp>::type _Up;
2802 return unique_ptr<_Tp>(new _Up[__n]());
2803}
2804
2805template<class _Tp, class... _Args>
2806 typename __unique_if<_Tp>::__unique_array_known_bound
2807 make_unique(_Args&&...) = delete;
2808
2809#endif // _LIBCPP_STD_VER > 11
2810
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002811template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002812#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002813struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002814#else
2815struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002816 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002817#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002818{
2819 typedef unique_ptr<_Tp, _Dp> argument_type;
2820 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002821 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002822 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002823 {
2824 typedef typename argument_type::pointer pointer;
2825 return hash<pointer>()(__ptr.get());
2826 }
2827};
2828
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829struct __destruct_n
2830{
2831private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002832 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833
2834 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002835 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002836 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837
2838 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002839 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840 {}
2841
Howard Hinnant719bda32011-05-28 14:41:13 +00002842 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002843 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002844 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845 {}
2846
Howard Hinnant719bda32011-05-28 14:41:13 +00002847 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002848 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002849 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 {}
2851public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002852 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002853 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854
2855 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002856 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002857 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858
2859 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002860 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002861 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862
2863 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002864 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002865 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866};
2867
2868template <class _Alloc>
2869class __allocator_destructor
2870{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002871 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002873 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2874 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875private:
2876 _Alloc& __alloc_;
2877 size_type __s_;
2878public:
2879 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002880 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002883 void operator()(pointer __p) _NOEXCEPT
2884 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885};
2886
2887template <class _InputIterator, class _ForwardIterator>
2888_ForwardIterator
2889uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2890{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002892#ifndef _LIBCPP_NO_EXCEPTIONS
2893 _ForwardIterator __s = __r;
2894 try
2895 {
2896#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002897 for (; __f != __l; ++__f, (void) ++__r)
2898 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002899#ifndef _LIBCPP_NO_EXCEPTIONS
2900 }
2901 catch (...)
2902 {
2903 for (; __s != __r; ++__s)
2904 __s->~value_type();
2905 throw;
2906 }
2907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 return __r;
2909}
2910
2911template <class _InputIterator, class _Size, class _ForwardIterator>
2912_ForwardIterator
2913uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2914{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002916#ifndef _LIBCPP_NO_EXCEPTIONS
2917 _ForwardIterator __s = __r;
2918 try
2919 {
2920#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002921 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2922 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002923#ifndef _LIBCPP_NO_EXCEPTIONS
2924 }
2925 catch (...)
2926 {
2927 for (; __s != __r; ++__s)
2928 __s->~value_type();
2929 throw;
2930 }
2931#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 return __r;
2933}
2934
2935template <class _ForwardIterator, class _Tp>
2936void
2937uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2938{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002940#ifndef _LIBCPP_NO_EXCEPTIONS
2941 _ForwardIterator __s = __f;
2942 try
2943 {
2944#endif
2945 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002946 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002947#ifndef _LIBCPP_NO_EXCEPTIONS
2948 }
2949 catch (...)
2950 {
2951 for (; __s != __f; ++__s)
2952 __s->~value_type();
2953 throw;
2954 }
2955#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956}
2957
2958template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002959_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2961{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002963#ifndef _LIBCPP_NO_EXCEPTIONS
2964 _ForwardIterator __s = __f;
2965 try
2966 {
2967#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002968 for (; __n > 0; ++__f, (void) --__n)
2969 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002970#ifndef _LIBCPP_NO_EXCEPTIONS
2971 }
2972 catch (...)
2973 {
2974 for (; __s != __f; ++__s)
2975 __s->~value_type();
2976 throw;
2977 }
2978#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002979 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980}
2981
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002982#if _LIBCPP_STD_VER > 14
2983
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002984template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002985inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002986void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2987 for (; __first != __last; ++__first)
2988 _VSTD::destroy_at(_VSTD::addressof(*__first));
2989}
2990
2991template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002992inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002993_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2994 for (; __n > 0; (void)++__first, --__n)
2995 _VSTD::destroy_at(_VSTD::addressof(*__first));
2996 return __first;
2997}
2998
Eric Fiselier290c07c2016-10-11 21:13:44 +00002999template <class _ForwardIterator>
3000inline _LIBCPP_INLINE_VISIBILITY
3001void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3002 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3003 auto __idx = __first;
3004#ifndef _LIBCPP_NO_EXCEPTIONS
3005 try {
3006#endif
3007 for (; __idx != __last; ++__idx)
3008 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3009#ifndef _LIBCPP_NO_EXCEPTIONS
3010 } catch (...) {
3011 _VSTD::destroy(__first, __idx);
3012 throw;
3013 }
3014#endif
3015}
3016
3017template <class _ForwardIterator, class _Size>
3018inline _LIBCPP_INLINE_VISIBILITY
3019_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3020 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3021 auto __idx = __first;
3022#ifndef _LIBCPP_NO_EXCEPTIONS
3023 try {
3024#endif
3025 for (; __n > 0; (void)++__idx, --__n)
3026 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3027 return __idx;
3028#ifndef _LIBCPP_NO_EXCEPTIONS
3029 } catch (...) {
3030 _VSTD::destroy(__first, __idx);
3031 throw;
3032 }
3033#endif
3034}
3035
3036
3037template <class _ForwardIterator>
3038inline _LIBCPP_INLINE_VISIBILITY
3039void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3040 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3041 auto __idx = __first;
3042#ifndef _LIBCPP_NO_EXCEPTIONS
3043 try {
3044#endif
3045 for (; __idx != __last; ++__idx)
3046 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3047#ifndef _LIBCPP_NO_EXCEPTIONS
3048 } catch (...) {
3049 _VSTD::destroy(__first, __idx);
3050 throw;
3051 }
3052#endif
3053}
3054
3055template <class _ForwardIterator, class _Size>
3056inline _LIBCPP_INLINE_VISIBILITY
3057_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3058 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3059 auto __idx = __first;
3060#ifndef _LIBCPP_NO_EXCEPTIONS
3061 try {
3062#endif
3063 for (; __n > 0; (void)++__idx, --__n)
3064 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3065 return __idx;
3066#ifndef _LIBCPP_NO_EXCEPTIONS
3067 } catch (...) {
3068 _VSTD::destroy(__first, __idx);
3069 throw;
3070 }
3071#endif
3072}
3073
3074
3075template <class _InputIt, class _ForwardIt>
3076inline _LIBCPP_INLINE_VISIBILITY
3077_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3078 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3079 auto __idx = __first_res;
3080#ifndef _LIBCPP_NO_EXCEPTIONS
3081 try {
3082#endif
3083 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003084 ::new((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00003085 return __idx;
3086#ifndef _LIBCPP_NO_EXCEPTIONS
3087 } catch (...) {
3088 _VSTD::destroy(__first_res, __idx);
3089 throw;
3090 }
3091#endif
3092}
3093
3094template <class _InputIt, class _Size, class _ForwardIt>
3095inline _LIBCPP_INLINE_VISIBILITY
3096pair<_InputIt, _ForwardIt>
3097uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3098 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3099 auto __idx = __first_res;
3100#ifndef _LIBCPP_NO_EXCEPTIONS
3101 try {
3102#endif
3103 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05003104 ::new((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00003105 return {__first, __idx};
3106#ifndef _LIBCPP_NO_EXCEPTIONS
3107 } catch (...) {
3108 _VSTD::destroy(__first_res, __idx);
3109 throw;
3110 }
3111#endif
3112}
3113
3114
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003115#endif // _LIBCPP_STD_VER > 14
3116
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003117// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3118// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003119// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003120#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3121 && defined(__ATOMIC_RELAXED) \
3122 && defined(__ATOMIC_ACQ_REL)
3123# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003124#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003125# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3126#endif
3127
3128template <class _Tp>
3129inline _LIBCPP_INLINE_VISIBILITY _Tp
3130__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3131{
3132#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3133 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3134#else
3135 return __t += 1;
3136#endif
3137}
3138
3139template <class _Tp>
3140inline _LIBCPP_INLINE_VISIBILITY _Tp
3141__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3142{
3143#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3144 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3145#else
3146 return __t -= 1;
3147#endif
3148}
3149
Howard Hinnant756c69b2010-09-22 16:48:34 +00003150class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 : public std::exception
3152{
3153public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003154 bad_weak_ptr() _NOEXCEPT = default;
3155 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003156 virtual ~bad_weak_ptr() _NOEXCEPT;
3157 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158};
3159
Louis Dionne16fe2952018-07-11 23:14:33 +00003160_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003161void __throw_bad_weak_ptr()
3162{
3163#ifndef _LIBCPP_NO_EXCEPTIONS
3164 throw bad_weak_ptr();
3165#else
3166 _VSTD::abort();
3167#endif
3168}
3169
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003170template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003172class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173{
3174 __shared_count(const __shared_count&);
3175 __shared_count& operator=(const __shared_count&);
3176
3177protected:
3178 long __shared_owners_;
3179 virtual ~__shared_count();
3180private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003181 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182
3183public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003185 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186 : __shared_owners_(__refs) {}
3187
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003188#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003189 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003190 void __add_shared() _NOEXCEPT;
3191 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003192#else
3193 _LIBCPP_INLINE_VISIBILITY
3194 void __add_shared() _NOEXCEPT {
3195 __libcpp_atomic_refcount_increment(__shared_owners_);
3196 }
3197 _LIBCPP_INLINE_VISIBILITY
3198 bool __release_shared() _NOEXCEPT {
3199 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3200 __on_zero_shared();
3201 return true;
3202 }
3203 return false;
3204 }
3205#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003206 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003207 long use_count() const _NOEXCEPT {
3208 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3209 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210};
3211
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003212class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213 : private __shared_count
3214{
3215 long __shared_weak_owners_;
3216
3217public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003219 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220 : __shared_count(__refs),
3221 __shared_weak_owners_(__refs) {}
3222protected:
3223 virtual ~__shared_weak_count();
3224
3225public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003226#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003227 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003228 void __add_shared() _NOEXCEPT;
3229 void __add_weak() _NOEXCEPT;
3230 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003231#else
3232 _LIBCPP_INLINE_VISIBILITY
3233 void __add_shared() _NOEXCEPT {
3234 __shared_count::__add_shared();
3235 }
3236 _LIBCPP_INLINE_VISIBILITY
3237 void __add_weak() _NOEXCEPT {
3238 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3239 }
3240 _LIBCPP_INLINE_VISIBILITY
3241 void __release_shared() _NOEXCEPT {
3242 if (__shared_count::__release_shared())
3243 __release_weak();
3244 }
3245#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003246 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003248 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3249 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250
Howard Hinnant719bda32011-05-28 14:41:13 +00003251 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003253 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254};
3255
3256template <class _Tp, class _Dp, class _Alloc>
3257class __shared_ptr_pointer
3258 : public __shared_weak_count
3259{
3260 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3261public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003264 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265
Howard Hinnant72f73582010-08-11 17:04:31 +00003266#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003267 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269
3270private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003271 virtual void __on_zero_shared() _NOEXCEPT;
3272 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273};
3274
Howard Hinnant72f73582010-08-11 17:04:31 +00003275#ifndef _LIBCPP_NO_RTTI
3276
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277template <class _Tp, class _Dp, class _Alloc>
3278const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003279__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003281 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282}
3283
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003284#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003285
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286template <class _Tp, class _Dp, class _Alloc>
3287void
Howard Hinnant719bda32011-05-28 14:41:13 +00003288__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003289{
3290 __data_.first().second()(__data_.first().first());
3291 __data_.first().second().~_Dp();
3292}
3293
3294template <class _Tp, class _Dp, class _Alloc>
3295void
Howard Hinnant719bda32011-05-28 14:41:13 +00003296__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003298 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3299 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003300 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3301
Eric Fiselierf8898c82015-02-05 23:01:40 +00003302 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003304 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305}
3306
3307template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05003308struct __shared_ptr_emplace
3309 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310{
Louis Dionne86549d72020-12-09 16:22:17 -05003311 _LIBCPP_HIDE_FROM_ABI
3312 explicit __shared_ptr_emplace(_Alloc __a)
3313 : __data_(_VSTD::move(__a), __value_init_tag())
3314 { }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315
Louis Dionne86549d72020-12-09 16:22:17 -05003316 template <class ..._Args>
3317 _LIBCPP_HIDE_FROM_ABI
3318 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003319#ifndef _LIBCPP_CXX03_LANG
Louis Dionne86549d72020-12-09 16:22:17 -05003320 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3321 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003322#else
Louis Dionne86549d72020-12-09 16:22:17 -05003323 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...))
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003324#endif
Louis Dionne86549d72020-12-09 16:22:17 -05003325 { }
3326
3327 _LIBCPP_HIDE_FROM_ABI
3328 _Tp* __get_elem() _NOEXCEPT { return _VSTD::addressof(__data_.second()); }
3329
3330 _LIBCPP_HIDE_FROM_ABI
3331 _Alloc& __get_alloc() _NOEXCEPT { return __data_.first(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332
3333private:
Louis Dionne86549d72020-12-09 16:22:17 -05003334 virtual void __on_zero_shared() _NOEXCEPT {
3335 __get_elem()->~_Tp();
3336 }
3337
3338 virtual void __on_zero_shared_weak() _NOEXCEPT {
3339 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
3340 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
3341 _ControlBlockAlloc __tmp(__get_alloc());
3342 __get_alloc().~_Alloc();
3343 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
3344 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
3345 }
3346
3347 __compressed_pair<_Alloc, _Tp> __data_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348};
3349
Erik Pilkington2a398762017-05-25 15:43:31 +00003350struct __shared_ptr_dummy_rebind_allocator_type;
3351template <>
3352class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3353{
3354public:
3355 template <class _Other>
3356 struct rebind
3357 {
3358 typedef allocator<_Other> other;
3359 };
3360};
3361
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003362template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363
zoecarverd73f42a2020-05-11 18:42:50 -07003364template<class _Tp, class _Up>
3365struct __compatible_with
3366#if _LIBCPP_STD_VER > 14
3367 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3368#else
3369 : is_convertible<_Tp*, _Up*> {};
3370#endif // _LIBCPP_STD_VER > 14
3371
Vy Nguyene369bd92020-07-13 12:34:37 -04003372#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3373# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3374#else
3375# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3376#endif
3377
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003379class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003381public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003382#if _LIBCPP_STD_VER > 14
3383 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003384 typedef remove_extent_t<_Tp> element_type;
3385#else
3386 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003387#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003388
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389private:
3390 element_type* __ptr_;
3391 __shared_weak_count* __cntrl_;
3392
3393 struct __nat {int __for_bool_;};
3394public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003396 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003398 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003399 template<class _Yp>
3400 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003401 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003402 template<class _Yp, class _Dp>
3403 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003404 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003405 template<class _Yp, class _Dp, class _Alloc>
3406 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003407 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3409 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003410 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003412 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415 shared_ptr(const 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;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003419 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003420 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003421 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003422 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003424 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003425#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00003426 template<class _Yp>
3427 shared_ptr(auto_ptr<_Yp>&& __r,
3428 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003429#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003430 template <class _Yp, class _Dp>
3431 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3432 typename enable_if
3433 <
3434 !is_lvalue_reference<_Dp>::value &&
3435 !is_array<_Yp>::value &&
3436 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3437 __nat
3438 >::type = __nat());
3439 template <class _Yp, class _Dp>
3440 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3441 typename enable_if
3442 <
3443 is_lvalue_reference<_Dp>::value &&
3444 !is_array<_Yp>::value &&
3445 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3446 __nat
3447 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448
3449 ~shared_ptr();
3450
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003452 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003453 template<class _Yp>
3454 typename enable_if
3455 <
zoecarverd73f42a2020-05-11 18:42:50 -07003456 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003457 shared_ptr&
3458 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003460 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003462 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003463 template<class _Yp>
3464 typename enable_if
3465 <
zoecarverd73f42a2020-05-11 18:42:50 -07003466 __compatible_with<_Yp, element_type>::value,
3467 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003468 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003470 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003471#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003472 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003474 typename enable_if
3475 <
3476 !is_array<_Yp>::value &&
3477 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003478 shared_ptr
3479 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003480 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003481#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003482 template <class _Yp, class _Dp>
3483 typename enable_if
3484 <
3485 !is_array<_Yp>::value &&
3486 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3487 shared_ptr&
3488 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003490 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003493 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003495 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003496 template<class _Yp>
3497 typename enable_if
3498 <
zoecarverd73f42a2020-05-11 18:42:50 -07003499 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003500 void
3501 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003503 reset(_Yp* __p);
3504 template<class _Yp, class _Dp>
3505 typename enable_if
3506 <
zoecarverd73f42a2020-05-11 18:42:50 -07003507 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003508 void
3509 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003511 reset(_Yp* __p, _Dp __d);
3512 template<class _Yp, class _Dp, class _Alloc>
3513 typename enable_if
3514 <
zoecarverd73f42a2020-05-11 18:42:50 -07003515 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003516 void
3517 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003519 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520
Howard Hinnant756c69b2010-09-22 16:48:34 +00003521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003522 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003524 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3525 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003526 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003527 element_type* operator->() const _NOEXCEPT
3528 {
3529 static_assert(!_VSTD::is_array<_Tp>::value,
3530 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3531 return __ptr_;
3532 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003534 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003536 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003537 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05003538 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003539 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003540 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003541 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003543 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003544 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003545 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003547 _LIBCPP_INLINE_VISIBILITY
3548 bool
3549 __owner_equivalent(const shared_ptr& __p) const
3550 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551
zoecarverd73f42a2020-05-11 18:42:50 -07003552#if _LIBCPP_STD_VER > 14
3553 typename add_lvalue_reference<element_type>::type
3554 _LIBCPP_INLINE_VISIBILITY
3555 operator[](ptrdiff_t __i) const
3556 {
3557 static_assert(_VSTD::is_array<_Tp>::value,
3558 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3559 return __ptr_[__i];
3560 }
3561#endif
3562
Howard Hinnant72f73582010-08-11 17:04:31 +00003563#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003566 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003567 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003568 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003569 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003570#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571
Zoe Carverd9040c72019-10-22 15:16:49 +00003572 template<class _Yp, class _CntrlBlk>
3573 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003574 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003575 {
3576 shared_ptr<_Tp> __r;
3577 __r.__ptr_ = __p;
3578 __r.__cntrl_ = __cntrl;
3579 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3580 return __r;
3581 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003582
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003584 template <class _Yp, bool = is_function<_Yp>::value>
3585 struct __shared_ptr_default_allocator
3586 {
3587 typedef allocator<_Yp> type;
3588 };
3589
3590 template <class _Yp>
3591 struct __shared_ptr_default_allocator<_Yp, true>
3592 {
3593 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3594 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003596 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003597 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003598 typename enable_if<is_convertible<_OrigPtr*,
3599 const enable_shared_from_this<_Yp>*
3600 >::value,
3601 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003602 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3603 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003605 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003606 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003607 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003608 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3609 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003610 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611 }
3612
Erik Pilkington2a398762017-05-25 15:43:31 +00003613 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614
zoecarverd73f42a2020-05-11 18:42:50 -07003615 template <class, class _Yp>
3616 struct __shared_ptr_default_delete
3617 : default_delete<_Yp> {};
3618
3619 template <class _Yp, class _Un, size_t _Sz>
3620 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3621 : default_delete<_Yp[]> {};
3622
3623 template <class _Yp, class _Un>
3624 struct __shared_ptr_default_delete<_Yp[], _Un>
3625 : default_delete<_Yp[]> {};
3626
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003627 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3628 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629};
3630
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003631#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3632template<class _Tp>
3633shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3634template<class _Tp, class _Dp>
3635shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3636#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003637
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003639inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003640_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003641shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003642 : __ptr_(nullptr),
3643 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644{
3645}
3646
3647template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003648inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003649_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003650shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003651 : __ptr_(nullptr),
3652 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653{
3654}
3655
3656template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003657template<class _Yp>
3658shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003659 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660 : __ptr_(__p)
3661{
3662 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003663 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003664 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3665 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003667 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668}
3669
3670template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003671template<class _Yp, class _Dp>
3672shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003673 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674 : __ptr_(__p)
3675{
3676#ifndef _LIBCPP_NO_EXCEPTIONS
3677 try
3678 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003679#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003680 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3681 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3682 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003683 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684#ifndef _LIBCPP_NO_EXCEPTIONS
3685 }
3686 catch (...)
3687 {
3688 __d(__p);
3689 throw;
3690 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692}
3693
3694template<class _Tp>
3695template<class _Dp>
3696shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003697 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698{
3699#ifndef _LIBCPP_NO_EXCEPTIONS
3700 try
3701 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003702#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003703 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3704 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3705 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706#ifndef _LIBCPP_NO_EXCEPTIONS
3707 }
3708 catch (...)
3709 {
3710 __d(__p);
3711 throw;
3712 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003713#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714}
3715
3716template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003717template<class _Yp, class _Dp, class _Alloc>
3718shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003719 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720 : __ptr_(__p)
3721{
3722#ifndef _LIBCPP_NO_EXCEPTIONS
3723 try
3724 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003725#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003727 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728 typedef __allocator_destructor<_A2> _D2;
3729 _A2 __a2(__a);
3730 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003731 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3732 _CntrlBlk(__p, __d, __a);
3733 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003734 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735#ifndef _LIBCPP_NO_EXCEPTIONS
3736 }
3737 catch (...)
3738 {
3739 __d(__p);
3740 throw;
3741 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003742#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743}
3744
3745template<class _Tp>
3746template<class _Dp, class _Alloc>
3747shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003748 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749{
3750#ifndef _LIBCPP_NO_EXCEPTIONS
3751 try
3752 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003753#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003755 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756 typedef __allocator_destructor<_A2> _D2;
3757 _A2 __a2(__a);
3758 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003759 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3760 _CntrlBlk(__p, __d, __a);
3761 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762#ifndef _LIBCPP_NO_EXCEPTIONS
3763 }
3764 catch (...)
3765 {
3766 __d(__p);
3767 throw;
3768 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003769#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770}
3771
3772template<class _Tp>
3773template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003774inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003775shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776 : __ptr_(__p),
3777 __cntrl_(__r.__cntrl_)
3778{
3779 if (__cntrl_)
3780 __cntrl_->__add_shared();
3781}
3782
3783template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003784inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003785shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786 : __ptr_(__r.__ptr_),
3787 __cntrl_(__r.__cntrl_)
3788{
3789 if (__cntrl_)
3790 __cntrl_->__add_shared();
3791}
3792
3793template<class _Tp>
3794template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003797 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003798 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799 : __ptr_(__r.__ptr_),
3800 __cntrl_(__r.__cntrl_)
3801{
3802 if (__cntrl_)
3803 __cntrl_->__add_shared();
3804}
3805
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003807inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003808shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809 : __ptr_(__r.__ptr_),
3810 __cntrl_(__r.__cntrl_)
3811{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003812 __r.__ptr_ = nullptr;
3813 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814}
3815
3816template<class _Tp>
3817template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003818inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003820 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003821 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822 : __ptr_(__r.__ptr_),
3823 __cntrl_(__r.__cntrl_)
3824{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003825 __r.__ptr_ = nullptr;
3826 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827}
3828
Marshall Clowb22274f2017-01-24 22:22:33 +00003829#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003831template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003832shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003833 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 : __ptr_(__r.get())
3835{
3836 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3837 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003838 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839 __r.release();
3840}
Marshall Clowb22274f2017-01-24 22:22:33 +00003841#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842
3843template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003844template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003846 typename enable_if
3847 <
3848 !is_lvalue_reference<_Dp>::value &&
3849 !is_array<_Yp>::value &&
3850 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3851 __nat
3852 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853 : __ptr_(__r.get())
3854{
Marshall Clow35cde742015-05-10 13:59:45 +00003855#if _LIBCPP_STD_VER > 11
3856 if (__ptr_ == nullptr)
3857 __cntrl_ = nullptr;
3858 else
3859#endif
3860 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003861 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3862 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3863 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003864 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003865 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866 __r.release();
3867}
3868
3869template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003870template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003871shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003872 typename enable_if
3873 <
3874 is_lvalue_reference<_Dp>::value &&
3875 !is_array<_Yp>::value &&
3876 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3877 __nat
3878 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879 : __ptr_(__r.get())
3880{
Marshall Clow35cde742015-05-10 13:59:45 +00003881#if _LIBCPP_STD_VER > 11
3882 if (__ptr_ == nullptr)
3883 __cntrl_ = nullptr;
3884 else
3885#endif
3886 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003887 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003888 typedef __shared_ptr_pointer<_Yp*,
3889 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003890 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003891 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003892 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003893 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 __r.release();
3895}
3896
Zoe Carver6cd05c32019-08-19 15:47:16 +00003897template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898shared_ptr<_Tp>::~shared_ptr()
3899{
3900 if (__cntrl_)
3901 __cntrl_->__release_shared();
3902}
3903
3904template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003905inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003907shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908{
3909 shared_ptr(__r).swap(*this);
3910 return *this;
3911}
3912
3913template<class _Tp>
3914template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003915inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003916typename enable_if
3917<
zoecarverd73f42a2020-05-11 18:42:50 -07003918 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003919 shared_ptr<_Tp>&
3920>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003921shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003922{
3923 shared_ptr(__r).swap(*this);
3924 return *this;
3925}
3926
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003928inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003930shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003932 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933 return *this;
3934}
3935
3936template<class _Tp>
3937template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003938inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003939typename enable_if
3940<
zoecarverd73f42a2020-05-11 18:42:50 -07003941 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003942 shared_ptr<_Tp>&
3943>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3945{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003946 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 return *this;
3948}
3949
Marshall Clowb22274f2017-01-24 22:22:33 +00003950#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951template<class _Tp>
3952template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003953inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003954typename enable_if
3955<
3956 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003957 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003958 shared_ptr<_Tp>
3959>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3961{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003962 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963 return *this;
3964}
Marshall Clowb22274f2017-01-24 22:22:33 +00003965#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966
3967template<class _Tp>
3968template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003969inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003970typename enable_if
3971<
3972 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003973 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003974 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003975 shared_ptr<_Tp>&
3976>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3978{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003979 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980 return *this;
3981}
3982
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003984inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985void
Howard Hinnant719bda32011-05-28 14:41:13 +00003986shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003988 _VSTD::swap(__ptr_, __r.__ptr_);
3989 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990}
3991
3992template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003993inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994void
Howard Hinnant719bda32011-05-28 14:41:13 +00003995shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996{
3997 shared_ptr().swap(*this);
3998}
3999
4000template<class _Tp>
4001template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004002inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004003typename enable_if
4004<
zoecarverd73f42a2020-05-11 18:42:50 -07004005 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004006 void
4007>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008shared_ptr<_Tp>::reset(_Yp* __p)
4009{
4010 shared_ptr(__p).swap(*this);
4011}
4012
4013template<class _Tp>
4014template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004015inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004016typename enable_if
4017<
zoecarverd73f42a2020-05-11 18:42:50 -07004018 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004019 void
4020>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4022{
4023 shared_ptr(__p, __d).swap(*this);
4024}
4025
4026template<class _Tp>
4027template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004028inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004029typename enable_if
4030<
zoecarverd73f42a2020-05-11 18:42:50 -07004031 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004032 void
4033>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4035{
4036 shared_ptr(__p, __d, __a).swap(*this);
4037}
4038
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004039template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004041typename enable_if
4042<
4043 !is_array<_Tp>::value,
4044 shared_ptr<_Tp>
4045>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046allocate_shared(const _Alloc& __a, _Args&& ...__args)
4047{
Louis Dionne43c9f8f2020-12-09 16:57:28 -05004048 static_assert(is_constructible<_Tp, _Args...>::value,
4049 "allocate_shared/make_shared: the type is not constructible from the provided arguments");
zoecarver505730a2020-02-25 16:50:57 -08004050
4051 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4052 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4053 typedef __allocator_destructor<_A2> _D2;
4054
4055 _A2 __a2(__a);
4056 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4057 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4058 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4059
Louis Dionnef34e5c82020-11-10 12:47:10 -05004060 typename shared_ptr<_Tp>::element_type *__p = __hold2->__get_elem();
zoecarver505730a2020-02-25 16:50:57 -08004061 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062}
4063
Louis Dionne43c9f8f2020-12-09 16:57:28 -05004064template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
4065_LIBCPP_HIDE_FROM_ABI
4066shared_ptr<_Tp> make_shared(_Args&& ...__args)
4067{
4068 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
4069}
4070
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071template<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.get() == __y.get();
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{
4084 return !(__x == __y);
4085}
4086
4087template<class _Tp, class _Up>
4088inline _LIBCPP_INLINE_VISIBILITY
4089bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004090operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004092#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004093 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4094 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004095#else
4096 return less<>()(__x.get(), __y.get());
4097#endif
4098
Howard Hinnantb17caf92012-02-21 21:02:58 +00004099}
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 !(__y < __x);
4115}
4116
4117template<class _Tp, class _Up>
4118inline _LIBCPP_INLINE_VISIBILITY
4119bool
4120operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4121{
4122 return !(__x < __y);
4123}
4124
4125template<class _Tp>
4126inline _LIBCPP_INLINE_VISIBILITY
4127bool
4128operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4129{
4130 return !__x;
4131}
4132
4133template<class _Tp>
4134inline _LIBCPP_INLINE_VISIBILITY
4135bool
4136operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4137{
4138 return !__x;
4139}
4140
4141template<class _Tp>
4142inline _LIBCPP_INLINE_VISIBILITY
4143bool
4144operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4145{
4146 return static_cast<bool>(__x);
4147}
4148
4149template<class _Tp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151bool
4152operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4153{
4154 return static_cast<bool>(__x);
4155}
4156
4157template<class _Tp>
4158inline _LIBCPP_INLINE_VISIBILITY
4159bool
4160operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4161{
4162 return less<_Tp*>()(__x.get(), nullptr);
4163}
4164
4165template<class _Tp>
4166inline _LIBCPP_INLINE_VISIBILITY
4167bool
4168operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4169{
4170 return less<_Tp*>()(nullptr, __x.get());
4171}
4172
4173template<class _Tp>
4174inline _LIBCPP_INLINE_VISIBILITY
4175bool
4176operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4177{
4178 return nullptr < __x;
4179}
4180
4181template<class _Tp>
4182inline _LIBCPP_INLINE_VISIBILITY
4183bool
4184operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4185{
4186 return __x < nullptr;
4187}
4188
4189template<class _Tp>
4190inline _LIBCPP_INLINE_VISIBILITY
4191bool
4192operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4193{
4194 return !(nullptr < __x);
4195}
4196
4197template<class _Tp>
4198inline _LIBCPP_INLINE_VISIBILITY
4199bool
4200operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4201{
4202 return !(__x < nullptr);
4203}
4204
4205template<class _Tp>
4206inline _LIBCPP_INLINE_VISIBILITY
4207bool
4208operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4209{
4210 return !(__x < nullptr);
4211}
4212
4213template<class _Tp>
4214inline _LIBCPP_INLINE_VISIBILITY
4215bool
4216operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4217{
4218 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219}
4220
4221template<class _Tp>
4222inline _LIBCPP_INLINE_VISIBILITY
4223void
Howard Hinnant719bda32011-05-28 14:41:13 +00004224swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225{
4226 __x.swap(__y);
4227}
4228
4229template<class _Tp, class _Up>
4230inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004231shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004232static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233{
zoecarverd73f42a2020-05-11 18:42:50 -07004234 return shared_ptr<_Tp>(__r,
4235 static_cast<
4236 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237}
4238
4239template<class _Tp, class _Up>
4240inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004241shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004242dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243{
zoecarverd73f42a2020-05-11 18:42:50 -07004244 typedef typename shared_ptr<_Tp>::element_type _ET;
4245 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4247}
4248
4249template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004250shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004251const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252{
zoecarverd73f42a2020-05-11 18:42:50 -07004253 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004254 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255}
4256
zoecarverd73f42a2020-05-11 18:42:50 -07004257template<class _Tp, class _Up>
4258shared_ptr<_Tp>
4259reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4260{
4261 return shared_ptr<_Tp>(__r,
4262 reinterpret_cast<
4263 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4264}
4265
Howard Hinnant72f73582010-08-11 17:04:31 +00004266#ifndef _LIBCPP_NO_RTTI
4267
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268template<class _Dp, class _Tp>
4269inline _LIBCPP_INLINE_VISIBILITY
4270_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004271get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272{
4273 return __p.template __get_deleter<_Dp>();
4274}
4275
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004276#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004277
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04004279class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004281public:
4282 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283private:
4284 element_type* __ptr_;
4285 __shared_weak_count* __cntrl_;
4286
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004287public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004289 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004290 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004291 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4292 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004294 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004295 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004296 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4297 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004298
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004300 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004301 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004302 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4303 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004304 ~weak_ptr();
4305
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004307 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004308 template<class _Yp>
4309 typename enable_if
4310 <
4311 is_convertible<_Yp*, element_type*>::value,
4312 weak_ptr&
4313 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004315 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4316
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004318 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4319 template<class _Yp>
4320 typename enable_if
4321 <
4322 is_convertible<_Yp*, element_type*>::value,
4323 weak_ptr&
4324 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004326 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4327
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004328 template<class _Yp>
4329 typename enable_if
4330 <
4331 is_convertible<_Yp*, element_type*>::value,
4332 weak_ptr&
4333 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004335 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004336
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004338 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004340 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004341
Howard Hinnant756c69b2010-09-22 16:48:34 +00004342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004343 long use_count() const _NOEXCEPT
4344 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004346 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05004347 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00004348 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004349 template<class _Up>
4350 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004351 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004353 template<class _Up>
4354 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004355 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356 {return __cntrl_ < __r.__cntrl_;}
4357
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004358 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4359 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360};
4361
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004362#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4363template<class _Tp>
4364weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4365#endif
4366
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004368inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004369_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004370weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05004371 : __ptr_(nullptr),
4372 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373{
4374}
4375
4376template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004377inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004378weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379 : __ptr_(__r.__ptr_),
4380 __cntrl_(__r.__cntrl_)
4381{
4382 if (__cntrl_)
4383 __cntrl_->__add_weak();
4384}
4385
4386template<class _Tp>
4387template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004388inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004390 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004391 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392 : __ptr_(__r.__ptr_),
4393 __cntrl_(__r.__cntrl_)
4394{
4395 if (__cntrl_)
4396 __cntrl_->__add_weak();
4397}
4398
4399template<class _Tp>
4400template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004401inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004402weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004403 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004404 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405 : __ptr_(__r.__ptr_),
4406 __cntrl_(__r.__cntrl_)
4407{
4408 if (__cntrl_)
4409 __cntrl_->__add_weak();
4410}
4411
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004412template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004413inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004414weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4415 : __ptr_(__r.__ptr_),
4416 __cntrl_(__r.__cntrl_)
4417{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004418 __r.__ptr_ = nullptr;
4419 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004420}
4421
4422template<class _Tp>
4423template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004424inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004425weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4426 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4427 _NOEXCEPT
4428 : __ptr_(__r.__ptr_),
4429 __cntrl_(__r.__cntrl_)
4430{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004431 __r.__ptr_ = nullptr;
4432 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004433}
4434
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435template<class _Tp>
4436weak_ptr<_Tp>::~weak_ptr()
4437{
4438 if (__cntrl_)
4439 __cntrl_->__release_weak();
4440}
4441
4442template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004443inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004445weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446{
4447 weak_ptr(__r).swap(*this);
4448 return *this;
4449}
4450
4451template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004452template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004453inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004454typename enable_if
4455<
4456 is_convertible<_Yp*, _Tp*>::value,
4457 weak_ptr<_Tp>&
4458>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004459weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004460{
4461 weak_ptr(__r).swap(*this);
4462 return *this;
4463}
4464
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004465template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004466inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004467weak_ptr<_Tp>&
4468weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4469{
4470 weak_ptr(_VSTD::move(__r)).swap(*this);
4471 return *this;
4472}
4473
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004475template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004476inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004477typename enable_if
4478<
4479 is_convertible<_Yp*, _Tp*>::value,
4480 weak_ptr<_Tp>&
4481>::type
4482weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4483{
4484 weak_ptr(_VSTD::move(__r)).swap(*this);
4485 return *this;
4486}
4487
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004488template<class _Tp>
4489template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004490inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004491typename enable_if
4492<
4493 is_convertible<_Yp*, _Tp*>::value,
4494 weak_ptr<_Tp>&
4495>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004496weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497{
4498 weak_ptr(__r).swap(*this);
4499 return *this;
4500}
4501
4502template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004503inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504void
Howard Hinnant719bda32011-05-28 14:41:13 +00004505weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004506{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004507 _VSTD::swap(__ptr_, __r.__ptr_);
4508 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509}
4510
4511template<class _Tp>
4512inline _LIBCPP_INLINE_VISIBILITY
4513void
Howard Hinnant719bda32011-05-28 14:41:13 +00004514swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515{
4516 __x.swap(__y);
4517}
4518
4519template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004520inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521void
Howard Hinnant719bda32011-05-28 14:41:13 +00004522weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523{
4524 weak_ptr().swap(*this);
4525}
4526
4527template<class _Tp>
4528template<class _Yp>
4529shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004530 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531 : __ptr_(__r.__ptr_),
4532 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4533{
Bruce Mitchener170d8972020-11-24 12:53:53 -05004534 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00004535 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536}
4537
4538template<class _Tp>
4539shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004540weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004541{
4542 shared_ptr<_Tp> __r;
4543 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4544 if (__r.__cntrl_)
4545 __r.__ptr_ = __ptr_;
4546 return __r;
4547}
4548
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004549#if _LIBCPP_STD_VER > 14
4550template <class _Tp = void> struct owner_less;
4551#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004552template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004553#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554
4555template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004556struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004558{
4559 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004560 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004561 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004562 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004563 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004564 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004566 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004567 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004569};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004570
4571template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004572struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4574{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004575 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004576 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004577 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004579 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004580 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004581 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004582 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004583 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584 {return __x.owner_before(__y);}
4585};
4586
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004587#if _LIBCPP_STD_VER > 14
4588template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004589struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004590{
4591 template <class _Tp, class _Up>
4592 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004593 bool operator()( shared_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()( shared_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 template <class _Tp, class _Up>
4600 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004601 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004602 {return __x.owner_before(__y);}
4603 template <class _Tp, class _Up>
4604 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004605 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004606 {return __x.owner_before(__y);}
4607 typedef void is_transparent;
4608};
4609#endif
4610
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004612class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004613{
4614 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004615protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004616 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004617 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004619 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004621 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4622 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004624 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004625public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004627 shared_ptr<_Tp> shared_from_this()
4628 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004630 shared_ptr<_Tp const> shared_from_this() const
4631 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632
Eric Fiselier84006862016-06-02 00:15:35 +00004633#if _LIBCPP_STD_VER > 14
4634 _LIBCPP_INLINE_VISIBILITY
4635 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4636 { return __weak_this_; }
4637
4638 _LIBCPP_INLINE_VISIBILITY
4639 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4640 { return __weak_this_; }
4641#endif // _LIBCPP_STD_VER > 14
4642
Howard Hinnantc51e1022010-05-11 19:42:16 +00004643 template <class _Up> friend class shared_ptr;
4644};
4645
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004646template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004647struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004648{
4649 typedef shared_ptr<_Tp> argument_type;
4650 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004651
Howard Hinnant756c69b2010-09-22 16:48:34 +00004652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004653 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004654 {
zoecarverd73f42a2020-05-11 18:42:50 -07004655 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004656 }
4657};
4658
Howard Hinnantc834c512011-11-29 18:15:50 +00004659template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004660inline _LIBCPP_INLINE_VISIBILITY
4661basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004662operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004663
Eric Fiselier9b492672016-06-18 02:12:53 +00004664
4665#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004666
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004667class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004668{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004669 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004670public:
4671 void lock() _NOEXCEPT;
4672 void unlock() _NOEXCEPT;
4673
4674private:
4675 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4676 __sp_mut(const __sp_mut&);
4677 __sp_mut& operator=(const __sp_mut&);
4678
Howard Hinnant8331b762013-03-06 23:30:19 +00004679 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004680};
4681
Mehdi Amini228053d2017-05-04 17:08:54 +00004682_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4683__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004684
4685template <class _Tp>
4686inline _LIBCPP_INLINE_VISIBILITY
4687bool
4688atomic_is_lock_free(const shared_ptr<_Tp>*)
4689{
4690 return false;
4691}
4692
4693template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004694_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004695shared_ptr<_Tp>
4696atomic_load(const shared_ptr<_Tp>* __p)
4697{
4698 __sp_mut& __m = __get_sp_mut(__p);
4699 __m.lock();
4700 shared_ptr<_Tp> __q = *__p;
4701 __m.unlock();
4702 return __q;
4703}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004704
Howard Hinnant9fa30202012-07-30 01:40:57 +00004705template <class _Tp>
4706inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004707_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004708shared_ptr<_Tp>
4709atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4710{
4711 return atomic_load(__p);
4712}
4713
4714template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004715_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004716void
4717atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4718{
4719 __sp_mut& __m = __get_sp_mut(__p);
4720 __m.lock();
4721 __p->swap(__r);
4722 __m.unlock();
4723}
4724
4725template <class _Tp>
4726inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004727_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004728void
4729atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4730{
4731 atomic_store(__p, __r);
4732}
4733
4734template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004735_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004736shared_ptr<_Tp>
4737atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4738{
4739 __sp_mut& __m = __get_sp_mut(__p);
4740 __m.lock();
4741 __p->swap(__r);
4742 __m.unlock();
4743 return __r;
4744}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004745
Howard Hinnant9fa30202012-07-30 01:40:57 +00004746template <class _Tp>
4747inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004748_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004749shared_ptr<_Tp>
4750atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4751{
4752 return atomic_exchange(__p, __r);
4753}
4754
4755template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004756_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004757bool
4758atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4759{
Marshall Clow4201ee82016-05-18 17:50:13 +00004760 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004761 __sp_mut& __m = __get_sp_mut(__p);
4762 __m.lock();
4763 if (__p->__owner_equivalent(*__v))
4764 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004765 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004766 *__p = __w;
4767 __m.unlock();
4768 return true;
4769 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004770 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004771 *__v = *__p;
4772 __m.unlock();
4773 return false;
4774}
4775
4776template <class _Tp>
4777inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004778_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004779bool
4780atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4781{
4782 return atomic_compare_exchange_strong(__p, __v, __w);
4783}
4784
4785template <class _Tp>
4786inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004787_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004788bool
4789atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4790 shared_ptr<_Tp> __w, memory_order, memory_order)
4791{
4792 return atomic_compare_exchange_strong(__p, __v, __w);
4793}
4794
4795template <class _Tp>
4796inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004797_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004798bool
4799atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4800 shared_ptr<_Tp> __w, memory_order, memory_order)
4801{
4802 return atomic_compare_exchange_weak(__p, __v, __w);
4803}
4804
Eric Fiselier9b492672016-06-18 02:12:53 +00004805#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004806
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004807//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004808#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4809# ifndef _LIBCPP_CXX03_LANG
4810enum class pointer_safety : unsigned char {
4811 relaxed,
4812 preferred,
4813 strict
4814};
4815# endif
4816#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004817struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004818{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004819 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004820 {
4821 relaxed,
4822 preferred,
4823 strict
4824 };
4825
Howard Hinnant49e145e2012-10-30 19:06:59 +00004826 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004827
Howard Hinnant756c69b2010-09-22 16:48:34 +00004828 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004829 pointer_safety() : __v_() {}
4830
4831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004832 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004834 operator int() const {return __v_;}
4835};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004836#endif
4837
4838#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004839 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004840_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4841#else
4842// This function is only offered in C++03 under ABI v1.
4843# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4844inline _LIBCPP_INLINE_VISIBILITY
4845pointer_safety get_pointer_safety() _NOEXCEPT {
4846 return pointer_safety::relaxed;
4847}
4848# endif
4849#endif
4850
Howard Hinnantc51e1022010-05-11 19:42:16 +00004851
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004852_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4853_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4854_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004855_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004856
4857template <class _Tp>
4858inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004859_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004860undeclare_reachable(_Tp* __p)
4861{
4862 return static_cast<_Tp*>(__undeclare_reachable(__p));
4863}
4864
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004865_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004866
Marshall Clow8982dcd2015-07-13 20:04:56 +00004867// --- Helper for container swap --
4868template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004869_LIBCPP_INLINE_VISIBILITY
4870void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4871#if _LIBCPP_STD_VER >= 14
4872 _NOEXCEPT
4873#else
4874 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4875#endif
4876{
4877 using _VSTD::swap;
4878 swap(__a1, __a2);
4879}
4880
4881template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004882inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004883void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4884
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004885template <typename _Alloc>
4886inline _LIBCPP_INLINE_VISIBILITY
4887void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4888#if _LIBCPP_STD_VER >= 14
4889 _NOEXCEPT
4890#else
4891 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4892#endif
4893{
4894 _VSTD::__swap_allocator(__a1, __a2,
4895 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4896}
4897
Marshall Clowff91de82015-08-18 19:51:37 +00004898template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004899struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004900 _Traits::propagate_on_container_move_assignment::value
4901#if _LIBCPP_STD_VER > 14
4902 || _Traits::is_always_equal::value
4903#else
4904 && is_nothrow_move_assignable<_Alloc>::value
4905#endif
4906 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004907
Marshall Clowa591b9a2016-07-11 21:38:08 +00004908
Marshall Clowa591b9a2016-07-11 21:38:08 +00004909template <class _Tp, class _Alloc>
4910struct __temp_value {
4911 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004912
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004913 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004914 _Alloc &__a;
4915
4916 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4917 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004918
Marshall Clowa591b9a2016-07-11 21:38:08 +00004919 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004920 _LIBCPP_NO_CFI
4921 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4922 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4923 _VSTD::forward<_Args>(__args)...);
4924 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004925
Marshall Clowa591b9a2016-07-11 21:38:08 +00004926 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4927 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004928
Marshall Clowe46031a2018-07-02 18:41:15 +00004929template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004930struct __is_allocator : false_type {};
4931
4932template<typename _Alloc>
4933struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004934 typename __void_t<typename _Alloc::value_type>::type,
4935 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4936 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004937 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004938
Eric Fiselier74ebee62019-06-08 01:31:19 +00004939// __builtin_new_allocator -- A non-templated helper for allocating and
4940// deallocating memory using __builtin_operator_new and
4941// __builtin_operator_delete. It should be used in preference to
4942// `std::allocator<T>` to avoid additional instantiations.
4943struct __builtin_new_allocator {
4944 struct __builtin_new_deleter {
4945 typedef void* pointer_type;
4946
4947 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4948 : __size_(__size), __align_(__align) {}
4949
4950 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004951 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004952 }
4953
4954 private:
4955 size_t __size_;
4956 size_t __align_;
4957 };
4958
4959 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4960
4961 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004962 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004963 __builtin_new_deleter(__s, __align));
4964 }
4965
4966 static void __deallocate_bytes(void* __p, size_t __s,
4967 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004968 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004969 }
4970
4971 template <class _Tp>
4972 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4973 static __holder_t __allocate_type(size_t __n) {
4974 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4975 }
4976
4977 template <class _Tp>
4978 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4979 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4980 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4981 }
4982};
4983
4984
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985_LIBCPP_END_NAMESPACE_STD
4986
Eric Fiselierf4433a32017-05-31 22:07:49 +00004987_LIBCPP_POP_MACROS
4988
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004989#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004990# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004991#endif
4992
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993#endif // _LIBCPP_MEMORY