blob: 87fc379965e28415ac9f7de624427e34308e046e [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>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400671#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#include <cstddef>
673#include <cstdint>
674#include <new>
675#include <utility>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676#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>
Louis Dionnefa621d72020-12-10 18:28:13 -0500682#include <__memory/allocator_traits.h>
Louis Dionne26bc98e2021-04-09 12:44:26 -0400683#include <__memory/auto_ptr.h>
Louis Dionnefa621d72020-12-10 18:28:13 -0500684#include <__memory/base.h>
685#include <__memory/pointer_traits.h>
Louis Dionne74aef9f2020-12-11 12:20:06 -0500686#include <__memory/utilities.h>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000687#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000688# include <atomic>
689#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000690#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000691
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000692#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000694#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695
Eric Fiselierf4433a32017-05-31 22:07:49 +0000696_LIBCPP_PUSH_MACROS
697#include <__undef_macros>
698
699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700_LIBCPP_BEGIN_NAMESPACE_STD
701
Eric Fiselier89659d12015-07-07 00:27:16 +0000702template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000703inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000704_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
705#if !defined(_LIBCPP_HAS_NO_THREADS) && \
706 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000707 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000708 return __atomic_load_n(__value, __ATOMIC_RELAXED);
709#else
710 return *__value;
711#endif
712}
713
Kuba Breckade9d6792016-09-04 09:55:12 +0000714template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000715inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000716_ValueType __libcpp_acquire_load(_ValueType const* __value) {
717#if !defined(_LIBCPP_HAS_NO_THREADS) && \
718 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000719 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000720 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
721#else
722 return *__value;
723#endif
724}
725
Louis Dionnefa621d72020-12-10 18:28:13 -0500726template <class _Tp> class allocator;
Marshall Clow0be70c32017-06-14 21:23:57 +0000727
Louis Dionnefa621d72020-12-10 18:28:13 -0500728#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
729template <>
730class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732public:
Louis Dionnefa621d72020-12-10 18:28:13 -0500733 typedef void* pointer;
734 typedef const void* const_pointer;
735 typedef void value_type;
736
737 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738};
739
Louis Dionnefa621d72020-12-10 18:28:13 -0500740template <>
741class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742{
Louis Dionnefa621d72020-12-10 18:28:13 -0500743public:
744 typedef const void* pointer;
745 typedef const void* const_pointer;
746 typedef const void value_type;
747
748 template <class _Up> struct rebind {typedef allocator<_Up> other;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749};
Louis Dionne99f59432020-09-17 12:06:13 -0400750#endif
Marshall Clow940e01c2015-04-07 05:21:38 +0000751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752// allocator
753
754template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000755class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756{
757public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400758 typedef size_t size_type;
759 typedef ptrdiff_t difference_type;
760 typedef _Tp value_type;
761 typedef true_type propagate_on_container_move_assignment;
762 typedef true_type is_always_equal;
763
764 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
765 allocator() _NOEXCEPT { }
766
767 template <class _Up>
768 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
769 allocator(const allocator<_Up>&) _NOEXCEPT { }
770
771 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
772 _Tp* allocate(size_t __n) {
773 if (__n > allocator_traits<allocator>::max_size(*this))
774 __throw_length_error("allocator<T>::allocate(size_t n)"
775 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400776 if (__libcpp_is_constant_evaluated()) {
777 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
778 } else {
779 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
780 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400781 }
782
783 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
784 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400785 if (__libcpp_is_constant_evaluated()) {
786 ::operator delete(__p);
787 } else {
788 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
789 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400790 }
791
792 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500793#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -0500794 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
795 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
796 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
797 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
798
Louis Dionne481a2662018-09-23 18:35:00 +0000799 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400800 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
801 typedef allocator<_Up> other;
802 };
Marshall Clowbc759762018-03-20 23:02:53 +0000803
Michael Park99f9e912020-03-04 11:27:14 -0500804 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400805 pointer address(reference __x) const _NOEXCEPT {
806 return _VSTD::addressof(__x);
807 }
Michael Park99f9e912020-03-04 11:27:14 -0500808 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -0400809 const_pointer address(const_reference __x) const _NOEXCEPT {
810 return _VSTD::addressof(__x);
811 }
Michael Park99f9e912020-03-04 11:27:14 -0500812
Michael Park99f9e912020-03-04 11:27:14 -0500813 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400814 _Tp* allocate(size_t __n, const void*) {
815 return allocate(__n);
816 }
Michael Park99f9e912020-03-04 11:27:14 -0500817
Louis Dionnef8b6c022020-09-21 10:36:37 -0400818 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
819 return size_type(~0) / sizeof(_Tp);
820 }
Michael Park99f9e912020-03-04 11:27:14 -0500821
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400823 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
824 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500825 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400826 }
827
828 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
829 void destroy(pointer __p) {
830 __p->~_Tp();
831 }
Michael Park99f9e912020-03-04 11:27:14 -0500832#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833};
834
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000835template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000836class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000837{
838public:
Louis Dionnef8b6c022020-09-21 10:36:37 -0400839 typedef size_t size_type;
840 typedef ptrdiff_t difference_type;
841 typedef const _Tp value_type;
842 typedef true_type propagate_on_container_move_assignment;
843 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000844
Marshall Clowbc759762018-03-20 23:02:53 +0000845 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400846 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +0000847
848 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +0000849 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400850 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -0500851
Louis Dionne99f59432020-09-17 12:06:13 -0400852 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400853 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -0400854 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +0000855 __throw_length_error("allocator<const T>::allocate(size_t n)"
856 " 'n' exceeds maximum supported size");
Louis Dionne3c989bc2020-09-28 17:29:52 -0400857 if (__libcpp_is_constant_evaluated()) {
858 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
859 } else {
860 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
861 }
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000862 }
Michael Park99f9e912020-03-04 11:27:14 -0500863
Louis Dionne99f59432020-09-17 12:06:13 -0400864 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -0400865 void deallocate(const _Tp* __p, size_t __n) {
Louis Dionne3c989bc2020-09-28 17:29:52 -0400866 if (__libcpp_is_constant_evaluated()) {
867 ::operator delete(const_cast<_Tp*>(__p));
868 } else {
869 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
870 }
Louis Dionnef8b6c022020-09-21 10:36:37 -0400871 }
Michael Park99f9e912020-03-04 11:27:14 -0500872
Louis Dionnef8b6c022020-09-21 10:36:37 -0400873 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -0500874#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -0400875 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
876 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
877 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
878 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
879
880 template <class _Up>
881 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
882 typedef allocator<_Up> other;
883 };
884
885 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
886 const_pointer address(const_reference __x) const _NOEXCEPT {
887 return _VSTD::addressof(__x);
888 }
889
890 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
891 const _Tp* allocate(size_t __n, const void*) {
892 return allocate(__n);
893 }
894
895 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
896 return size_type(~0) / sizeof(_Tp);
897 }
Michael Park99f9e912020-03-04 11:27:14 -0500898
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000899 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -0400900 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
901 void construct(_Up* __p, _Args&&... __args) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500902 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Louis Dionnef8b6c022020-09-21 10:36:37 -0400903 }
Howard Hinnant9e028f12012-05-01 15:37:54 +0000904
Louis Dionnef8b6c022020-09-21 10:36:37 -0400905 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
906 void destroy(pointer __p) {
907 __p->~_Tp();
908 }
Michael Park99f9e912020-03-04 11:27:14 -0500909#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +0000910};
911
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400913inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000914bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
916template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -0400917inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +0000918bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919
Louis Dionned6651542020-11-03 12:05:55 -0500920template <class _Alloc, class _Ptr>
921_LIBCPP_INLINE_VISIBILITY
922void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
923 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
Arthur O'Dwyerf2016bb2020-12-12 11:43:15 -0500924 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Louis Dionned6651542020-11-03 12:05:55 -0500925 typedef allocator_traits<_Alloc> _Traits;
926 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
927 _Traits::construct(__a, _VSTD::__to_address(__begin2),
928#ifdef _LIBCPP_NO_EXCEPTIONS
929 _VSTD::move(*__begin1)
930#else
931 _VSTD::move_if_noexcept(*__begin1)
932#endif
933 );
934 }
935}
936
937template <class _Alloc, class _Tp, typename enable_if<
938 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
939 is_trivially_move_constructible<_Tp>::value
940>::type>
941_LIBCPP_INLINE_VISIBILITY
942void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
943 ptrdiff_t _Np = __end1 - __begin1;
944 if (_Np > 0) {
945 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
946 __begin2 += _Np;
947 }
948}
949
950template <class _Alloc, class _Iter, class _Ptr>
951_LIBCPP_INLINE_VISIBILITY
952void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
953 typedef allocator_traits<_Alloc> _Traits;
954 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
955 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
956 }
957}
958
959template <class _Alloc, class _Source, class _Dest,
960 class _RawSource = typename remove_const<_Source>::type,
961 class _RawDest = typename remove_const<_Dest>::type,
962 class =
963 typename enable_if<
964 is_trivially_copy_constructible<_Dest>::value &&
965 is_same<_RawSource, _RawDest>::value &&
966 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
967 >::type>
968_LIBCPP_INLINE_VISIBILITY
969void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
970 ptrdiff_t _Np = __end1 - __begin1;
971 if (_Np > 0) {
972 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
973 __begin2 += _Np;
974 }
975}
976
977template <class _Alloc, class _Ptr>
978_LIBCPP_INLINE_VISIBILITY
979void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
980 static_assert(__is_cpp17_move_insertable<_Alloc>::value,
981 "The specified type does not meet the requirements of Cpp17MoveInsertable");
982 typedef allocator_traits<_Alloc> _Traits;
983 while (__end1 != __begin1) {
984 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
985#ifdef _LIBCPP_NO_EXCEPTIONS
986 _VSTD::move(*--__end1)
987#else
988 _VSTD::move_if_noexcept(*--__end1)
989#endif
990 );
991 --__end2;
992 }
993}
994
995template <class _Alloc, class _Tp, class = typename enable_if<
996 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
997 is_trivially_move_constructible<_Tp>::value
998>::type>
999_LIBCPP_INLINE_VISIBILITY
1000void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1001 ptrdiff_t _Np = __end1 - __begin1;
1002 __end2 -= _Np;
1003 if (_Np > 0)
1004 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1005}
1006
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001008class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 : public iterator<output_iterator_tag,
1010 _Tp, // purposefully not C++03
1011 ptrdiff_t, // purposefully not C++03
1012 _Tp*, // purposefully not C++03
1013 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1014{
1015private:
1016 _OutputIterator __x_;
1017public:
1018 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1019 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1020 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001021 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001022#if _LIBCPP_STD_VER >= 14
1023 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001024 {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001025#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1027 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1028 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001029#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001030 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001031#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032};
1033
1034template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001035_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001037get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038{
1039 pair<_Tp*, ptrdiff_t> __r(0, 0);
1040 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1041 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1042 / sizeof(_Tp);
1043 if (__n > __m)
1044 __n = __m;
1045 while (__n > 0)
1046 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001047#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001048 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001049 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001050 align_val_t __al =
1051 align_val_t(alignment_of<_Tp>::value);
Richard Smith0657be12018-02-01 22:24:45 +00001052 __r.first = static_cast<_Tp*>(::operator new(
1053 __n * sizeof(_Tp), __al, nothrow));
1054 } else {
1055 __r.first = static_cast<_Tp*>(::operator new(
1056 __n * sizeof(_Tp), nothrow));
1057 }
1058#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001059 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001060 {
1061 // Since aligned operator new is unavailable, return an empty
1062 // buffer rather than one with invalid alignment.
1063 return __r;
1064 }
1065
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001067#endif
1068
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 if (__r.first)
1070 {
1071 __r.second = __n;
1072 break;
1073 }
1074 __n /= 2;
1075 }
1076 return __r;
1077}
1078
1079template <class _Tp>
1080inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001081void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1082{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001083 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001084}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001086// Tag used to default initialize one or both of the pair's elements.
1087struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001088struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001089
Eric Fiselier9d355982017-04-12 23:45:53 +00001090template <class _Tp, int _Idx,
1091 bool _CanBeEmptyBase =
1092 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1093struct __compressed_pair_elem {
1094 typedef _Tp _ParamT;
1095 typedef _Tp& reference;
1096 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001098 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001099 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001100 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1101 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102
Eric Fiselier9d355982017-04-12 23:45:53 +00001103 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001104 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1105 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001106 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001107 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001108 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001109 : __value_(_VSTD::forward<_Up>(__u))
1110 {
1111 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001113
1114#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001115 template <class... _Args, size_t... _Indexes>
1116 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1117 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1118 __tuple_indices<_Indexes...>)
1119 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001120#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001122
Alex Lorenz76132112017-11-09 17:54:49 +00001123 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1124 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001125 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001128 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129};
1130
Eric Fiselier9d355982017-04-12 23:45:53 +00001131template <class _Tp, int _Idx>
1132struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1133 typedef _Tp _ParamT;
1134 typedef _Tp& reference;
1135 typedef const _Tp& const_reference;
1136 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001138 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1139 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1140 __compressed_pair_elem(__default_init_tag) {}
1141 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1142 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143
Eric Fiselier9d355982017-04-12 23:45:53 +00001144 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001145 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1146 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001147 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001148 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001149 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001150 : __value_type(_VSTD::forward<_Up>(__u))
1151 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001153#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001154 template <class... _Args, size_t... _Indexes>
1155 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1156 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1157 __tuple_indices<_Indexes...>)
1158 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001159#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160
Alex Lorenz76132112017-11-09 17:54:49 +00001161 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1162 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001163 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164};
1165
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001167class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1168 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -05001169public:
Eric Fiselier9d355982017-04-12 23:45:53 +00001170 // NOTE: This static assert should never fire because __compressed_pair
1171 // is *almost never* used in a scenario where it's possible for T1 == T2.
1172 // (The exception is std::function where it is possible that the function
1173 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001174 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001175 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001176 "The current implementation is NOT ABI-compatible with the previous "
1177 "implementation for this configuration");
1178
Louis Dionneda463cb2020-12-11 12:22:16 -05001179 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1180 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1181
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001182 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001183 class = typename enable_if<
1184 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1185 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1186 >::type
1187 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001188 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001189 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190
Eric Fiselier9d355982017-04-12 23:45:53 +00001191 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001192 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001193 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001194 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001196#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001197 template <class... _Args1, class... _Args2>
1198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1199 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1200 tuple<_Args2...> __second_args)
1201 : _Base1(__pc, _VSTD::move(__first_args),
1202 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1203 _Base2(__pc, _VSTD::move(__second_args),
1204 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001205#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206
Eric Fiselier9d355982017-04-12 23:45:53 +00001207 _LIBCPP_INLINE_VISIBILITY
1208 typename _Base1::reference first() _NOEXCEPT {
1209 return static_cast<_Base1&>(*this).__get();
1210 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Eric Fiselier9d355982017-04-12 23:45:53 +00001212 _LIBCPP_INLINE_VISIBILITY
1213 typename _Base1::const_reference first() const _NOEXCEPT {
1214 return static_cast<_Base1 const&>(*this).__get();
1215 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216
Eric Fiselier9d355982017-04-12 23:45:53 +00001217 _LIBCPP_INLINE_VISIBILITY
1218 typename _Base2::reference second() _NOEXCEPT {
1219 return static_cast<_Base2&>(*this).__get();
1220 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
Eric Fiselier9d355982017-04-12 23:45:53 +00001222 _LIBCPP_INLINE_VISIBILITY
1223 typename _Base2::const_reference second() const _NOEXCEPT {
1224 return static_cast<_Base2 const&>(*this).__get();
1225 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226
Louis Dionneda463cb2020-12-11 12:22:16 -05001227 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1228 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1229 return static_cast<_Base1*>(__pair);
1230 }
1231 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1232 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1233 return static_cast<_Base2*>(__pair);
1234 }
1235
Eric Fiselier9d355982017-04-12 23:45:53 +00001236 _LIBCPP_INLINE_VISIBILITY
1237 void swap(__compressed_pair& __x)
1238 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1239 __is_nothrow_swappable<_T2>::value)
1240 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001241 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001242 swap(first(), __x.first());
1243 swap(second(), __x.second());
1244 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245};
1246
1247template <class _T1, class _T2>
1248inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001249void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1250 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1251 __is_nothrow_swappable<_T2>::value) {
1252 __x.swap(__y);
1253}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001255// default_delete
1256
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001258struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001259 static_assert(!is_function<_Tp>::value,
1260 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001261#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001262 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001263#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001264 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001265#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001266 template <class _Up>
1267 _LIBCPP_INLINE_VISIBILITY
1268 default_delete(const default_delete<_Up>&,
1269 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1270 0) _NOEXCEPT {}
1271
1272 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1273 static_assert(sizeof(_Tp) > 0,
1274 "default_delete can not delete incomplete type");
1275 static_assert(!is_void<_Tp>::value,
1276 "default_delete can not delete incomplete type");
1277 delete __ptr;
1278 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279};
1280
1281template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001282struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1283private:
1284 template <class _Up>
1285 struct _EnableIfConvertible
1286 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1287
Howard Hinnant4500ca52011-12-18 21:19:44 +00001288public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001289#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001290 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001291#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001292 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001293#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001294
1295 template <class _Up>
1296 _LIBCPP_INLINE_VISIBILITY
1297 default_delete(const default_delete<_Up[]>&,
1298 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1299
1300 template <class _Up>
1301 _LIBCPP_INLINE_VISIBILITY
1302 typename _EnableIfConvertible<_Up>::type
1303 operator()(_Up* __ptr) const _NOEXCEPT {
1304 static_assert(sizeof(_Tp) > 0,
1305 "default_delete can not delete incomplete type");
1306 static_assert(!is_void<_Tp>::value,
1307 "default_delete can not delete void type");
1308 delete[] __ptr;
1309 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310};
1311
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001312template <class _Deleter>
1313struct __unique_ptr_deleter_sfinae {
1314 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1315 typedef const _Deleter& __lval_ref_type;
1316 typedef _Deleter&& __good_rval_ref_type;
1317 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318};
1319
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001320template <class _Deleter>
1321struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1322 typedef const _Deleter& __lval_ref_type;
1323 typedef const _Deleter&& __bad_rval_ref_type;
1324 typedef false_type __enable_rval_overload;
1325};
1326
1327template <class _Deleter>
1328struct __unique_ptr_deleter_sfinae<_Deleter&> {
1329 typedef _Deleter& __lval_ref_type;
1330 typedef _Deleter&& __bad_rval_ref_type;
1331 typedef false_type __enable_rval_overload;
1332};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001333
Vy Nguyene369bd92020-07-13 12:34:37 -04001334#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1335# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1336#else
1337# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1338#endif
1339
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001340template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001341class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001342public:
1343 typedef _Tp element_type;
1344 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001345 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001346
1347 static_assert(!is_rvalue_reference<deleter_type>::value,
1348 "the specified deleter type cannot be an rvalue reference");
1349
1350private:
1351 __compressed_pair<pointer, deleter_type> __ptr_;
1352
1353 struct __nat { int __for_bool_; };
1354
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001355 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001356
1357 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001358 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001359 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001360
1361 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001362 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001363 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001364
1365 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001366 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001367 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001368
1369 template <bool _Dummy, class _Deleter = typename __dependent_type<
1370 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001371 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001372 typename enable_if<is_default_constructible<_Deleter>::value &&
1373 !is_pointer<_Deleter>::value>::type;
1374
1375 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001376 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001377 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1378
1379 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001380 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001381 is_convertible<typename _UPtr::pointer, pointer>::value &&
1382 !is_array<_Up>::value
1383 >::type;
1384
1385 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001386 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001387 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1388 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1389 >::type;
1390
1391 template <class _UDel>
1392 using _EnableIfDeleterAssignable = typename enable_if<
1393 is_assignable<_Dp&, _UDel&&>::value
1394 >::type;
1395
1396public:
1397 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001398 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001399 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001400 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001401
1402 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001403 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001404 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001405 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001406
1407 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001408 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001409 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001410 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001411
1412 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001413 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001414 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001415 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001416 : __ptr_(__p, __d) {}
1417
1418 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001419 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001420 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001421 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001422 : __ptr_(__p, _VSTD::move(__d)) {
1423 static_assert(!is_reference<deleter_type>::value,
1424 "rvalue deleter bound to reference");
1425 }
1426
1427 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001428 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001429 _LIBCPP_INLINE_VISIBILITY
1430 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1431
1432 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001433 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001434 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1435 }
1436
1437 template <class _Up, class _Ep,
1438 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1439 class = _EnableIfDeleterConvertible<_Ep>
1440 >
1441 _LIBCPP_INLINE_VISIBILITY
1442 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1443 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1444
1445#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1446 template <class _Up>
1447 _LIBCPP_INLINE_VISIBILITY
1448 unique_ptr(auto_ptr<_Up>&& __p,
1449 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001450 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001451 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001452 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001453#endif
1454
1455 _LIBCPP_INLINE_VISIBILITY
1456 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1457 reset(__u.release());
1458 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1459 return *this;
1460 }
1461
1462 template <class _Up, class _Ep,
1463 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1464 class = _EnableIfDeleterAssignable<_Ep>
1465 >
1466 _LIBCPP_INLINE_VISIBILITY
1467 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1468 reset(__u.release());
1469 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1470 return *this;
1471 }
1472
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001473#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1474 template <class _Up>
1475 _LIBCPP_INLINE_VISIBILITY
1476 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1477 is_same<_Dp, default_delete<_Tp> >::value,
1478 unique_ptr&>::type
1479 operator=(auto_ptr<_Up> __p) {
1480 reset(__p.release());
1481 return *this;
1482 }
1483#endif
1484
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001485#ifdef _LIBCPP_CXX03_LANG
1486 unique_ptr(unique_ptr const&) = delete;
1487 unique_ptr& operator=(unique_ptr const&) = delete;
1488#endif
1489
1490
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001491 _LIBCPP_INLINE_VISIBILITY
1492 ~unique_ptr() { reset(); }
1493
1494 _LIBCPP_INLINE_VISIBILITY
1495 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1496 reset();
1497 return *this;
1498 }
1499
1500 _LIBCPP_INLINE_VISIBILITY
1501 typename add_lvalue_reference<_Tp>::type
1502 operator*() const {
1503 return *__ptr_.first();
1504 }
1505 _LIBCPP_INLINE_VISIBILITY
1506 pointer operator->() const _NOEXCEPT {
1507 return __ptr_.first();
1508 }
1509 _LIBCPP_INLINE_VISIBILITY
1510 pointer get() const _NOEXCEPT {
1511 return __ptr_.first();
1512 }
1513 _LIBCPP_INLINE_VISIBILITY
1514 deleter_type& get_deleter() _NOEXCEPT {
1515 return __ptr_.second();
1516 }
1517 _LIBCPP_INLINE_VISIBILITY
1518 const deleter_type& get_deleter() const _NOEXCEPT {
1519 return __ptr_.second();
1520 }
1521 _LIBCPP_INLINE_VISIBILITY
1522 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1523 return __ptr_.first() != nullptr;
1524 }
1525
1526 _LIBCPP_INLINE_VISIBILITY
1527 pointer release() _NOEXCEPT {
1528 pointer __t = __ptr_.first();
1529 __ptr_.first() = pointer();
1530 return __t;
1531 }
1532
1533 _LIBCPP_INLINE_VISIBILITY
1534 void reset(pointer __p = pointer()) _NOEXCEPT {
1535 pointer __tmp = __ptr_.first();
1536 __ptr_.first() = __p;
1537 if (__tmp)
1538 __ptr_.second()(__tmp);
1539 }
1540
1541 _LIBCPP_INLINE_VISIBILITY
1542 void swap(unique_ptr& __u) _NOEXCEPT {
1543 __ptr_.swap(__u.__ptr_);
1544 }
1545};
1546
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001547
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001549class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001551 typedef _Tp element_type;
1552 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001553 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001554
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001556 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557
Eric Fiselier31127cd2017-04-16 02:14:31 +00001558 template <class _From>
1559 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560
Eric Fiselier31127cd2017-04-16 02:14:31 +00001561 template <class _FromElem>
1562 struct _CheckArrayPointerConversion<_FromElem*>
1563 : integral_constant<bool,
1564 is_same<_FromElem*, pointer>::value ||
1565 (is_same<pointer, element_type*>::value &&
1566 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1567 >
1568 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569
Eric Fiselier31127cd2017-04-16 02:14:31 +00001570 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001571
1572 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001573 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001574 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001575
1576 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001577 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001578 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001579
1580 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001581 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001582 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001583
1584 template <bool _Dummy, class _Deleter = typename __dependent_type<
1585 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001586 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001587 typename enable_if<is_default_constructible<_Deleter>::value &&
1588 !is_pointer<_Deleter>::value>::type;
1589
1590 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001591 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001592 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1593
1594 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001595 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001596 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001597 >::type;
1598
1599 template <class _UPtr, class _Up,
1600 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001601 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001602 is_array<_Up>::value &&
1603 is_same<pointer, element_type*>::value &&
1604 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1605 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1606 >::type;
1607
1608 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001609 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001610 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1611 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1612 >::type;
1613
1614 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001615 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001616 is_assignable<_Dp&, _UDel&&>::value
1617 >::type;
1618
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001620 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001621 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001622 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001623 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001625 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001626 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001627 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001628 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001630 template <class _Pp, bool _Dummy = true,
1631 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001632 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001633 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001634 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001635 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001637 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001638 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1639 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001640 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001641 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001642 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001644 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001645 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001646 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001647 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001648 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001650 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001651 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1652 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001653 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001654 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001655 : __ptr_(__p, _VSTD::move(__d)) {
1656 static_assert(!is_reference<deleter_type>::value,
1657 "rvalue deleter bound to reference");
1658 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001660 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001661 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001662 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001663 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001664 : __ptr_(nullptr, _VSTD::move(__d)) {
1665 static_assert(!is_reference<deleter_type>::value,
1666 "rvalue deleter bound to reference");
1667 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001668
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001669 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001670 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1671 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001672 _LIBCPP_INLINE_VISIBILITY
1673 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001674
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001675 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001676 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001677 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1678 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001679
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001680 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001681 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001682 reset(__u.release());
1683 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1684 return *this;
1685 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001687 template <class _Up, class _Ep,
1688 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1689 class = _EnableIfDeleterConvertible<_Ep>
1690 >
1691 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001692 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001693 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001694 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001696 template <class _Up, class _Ep,
1697 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1698 class = _EnableIfDeleterAssignable<_Ep>
1699 >
1700 _LIBCPP_INLINE_VISIBILITY
1701 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001702 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001703 reset(__u.release());
1704 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1705 return *this;
1706 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001708#ifdef _LIBCPP_CXX03_LANG
1709 unique_ptr(unique_ptr const&) = delete;
1710 unique_ptr& operator=(unique_ptr const&) = delete;
1711#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001712
1713public:
1714 _LIBCPP_INLINE_VISIBILITY
1715 ~unique_ptr() { reset(); }
1716
1717 _LIBCPP_INLINE_VISIBILITY
1718 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1719 reset();
1720 return *this;
1721 }
1722
1723 _LIBCPP_INLINE_VISIBILITY
1724 typename add_lvalue_reference<_Tp>::type
1725 operator[](size_t __i) const {
1726 return __ptr_.first()[__i];
1727 }
1728 _LIBCPP_INLINE_VISIBILITY
1729 pointer get() const _NOEXCEPT {
1730 return __ptr_.first();
1731 }
1732
1733 _LIBCPP_INLINE_VISIBILITY
1734 deleter_type& get_deleter() _NOEXCEPT {
1735 return __ptr_.second();
1736 }
1737
1738 _LIBCPP_INLINE_VISIBILITY
1739 const deleter_type& get_deleter() const _NOEXCEPT {
1740 return __ptr_.second();
1741 }
1742 _LIBCPP_INLINE_VISIBILITY
1743 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1744 return __ptr_.first() != nullptr;
1745 }
1746
1747 _LIBCPP_INLINE_VISIBILITY
1748 pointer release() _NOEXCEPT {
1749 pointer __t = __ptr_.first();
1750 __ptr_.first() = pointer();
1751 return __t;
1752 }
1753
1754 template <class _Pp>
1755 _LIBCPP_INLINE_VISIBILITY
1756 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001757 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001758 >::type
1759 reset(_Pp __p) _NOEXCEPT {
1760 pointer __tmp = __ptr_.first();
1761 __ptr_.first() = __p;
1762 if (__tmp)
1763 __ptr_.second()(__tmp);
1764 }
1765
1766 _LIBCPP_INLINE_VISIBILITY
1767 void reset(nullptr_t = nullptr) _NOEXCEPT {
1768 pointer __tmp = __ptr_.first();
1769 __ptr_.first() = nullptr;
1770 if (__tmp)
1771 __ptr_.second()(__tmp);
1772 }
1773
1774 _LIBCPP_INLINE_VISIBILITY
1775 void swap(unique_ptr& __u) _NOEXCEPT {
1776 __ptr_.swap(__u.__ptr_);
1777 }
1778
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779};
1780
1781template <class _Tp, class _Dp>
1782inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001783typename enable_if<
1784 __is_swappable<_Dp>::value,
1785 void
1786>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001787swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788
1789template <class _T1, class _D1, class _T2, class _D2>
1790inline _LIBCPP_INLINE_VISIBILITY
1791bool
1792operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1793
1794template <class _T1, class _D1, class _T2, class _D2>
1795inline _LIBCPP_INLINE_VISIBILITY
1796bool
1797operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1798
1799template <class _T1, class _D1, class _T2, class _D2>
1800inline _LIBCPP_INLINE_VISIBILITY
1801bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001802operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1803{
1804 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1805 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001806 typedef typename common_type<_P1, _P2>::type _Vp;
1807 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001808}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809
1810template <class _T1, class _D1, class _T2, class _D2>
1811inline _LIBCPP_INLINE_VISIBILITY
1812bool
1813operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1814
1815template <class _T1, class _D1, class _T2, class _D2>
1816inline _LIBCPP_INLINE_VISIBILITY
1817bool
1818operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1819
1820template <class _T1, class _D1, class _T2, class _D2>
1821inline _LIBCPP_INLINE_VISIBILITY
1822bool
1823operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1824
Howard Hinnantb17caf92012-02-21 21:02:58 +00001825template <class _T1, class _D1>
1826inline _LIBCPP_INLINE_VISIBILITY
1827bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001828operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001829{
1830 return !__x;
1831}
1832
1833template <class _T1, class _D1>
1834inline _LIBCPP_INLINE_VISIBILITY
1835bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001836operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001837{
1838 return !__x;
1839}
1840
1841template <class _T1, class _D1>
1842inline _LIBCPP_INLINE_VISIBILITY
1843bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001844operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001845{
1846 return static_cast<bool>(__x);
1847}
1848
1849template <class _T1, class _D1>
1850inline _LIBCPP_INLINE_VISIBILITY
1851bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001852operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001853{
1854 return static_cast<bool>(__x);
1855}
1856
1857template <class _T1, class _D1>
1858inline _LIBCPP_INLINE_VISIBILITY
1859bool
1860operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1861{
1862 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1863 return less<_P1>()(__x.get(), nullptr);
1864}
1865
1866template <class _T1, class _D1>
1867inline _LIBCPP_INLINE_VISIBILITY
1868bool
1869operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1870{
1871 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1872 return less<_P1>()(nullptr, __x.get());
1873}
1874
1875template <class _T1, class _D1>
1876inline _LIBCPP_INLINE_VISIBILITY
1877bool
1878operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1879{
1880 return nullptr < __x;
1881}
1882
1883template <class _T1, class _D1>
1884inline _LIBCPP_INLINE_VISIBILITY
1885bool
1886operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1887{
1888 return __x < nullptr;
1889}
1890
1891template <class _T1, class _D1>
1892inline _LIBCPP_INLINE_VISIBILITY
1893bool
1894operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1895{
1896 return !(nullptr < __x);
1897}
1898
1899template <class _T1, class _D1>
1900inline _LIBCPP_INLINE_VISIBILITY
1901bool
1902operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1903{
1904 return !(__x < nullptr);
1905}
1906
1907template <class _T1, class _D1>
1908inline _LIBCPP_INLINE_VISIBILITY
1909bool
1910operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1911{
1912 return !(__x < nullptr);
1913}
1914
1915template <class _T1, class _D1>
1916inline _LIBCPP_INLINE_VISIBILITY
1917bool
1918operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1919{
1920 return !(nullptr < __x);
1921}
1922
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001923#if _LIBCPP_STD_VER > 11
1924
1925template<class _Tp>
1926struct __unique_if
1927{
1928 typedef unique_ptr<_Tp> __unique_single;
1929};
1930
1931template<class _Tp>
1932struct __unique_if<_Tp[]>
1933{
1934 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1935};
1936
1937template<class _Tp, size_t _Np>
1938struct __unique_if<_Tp[_Np]>
1939{
1940 typedef void __unique_array_known_bound;
1941};
1942
1943template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00001944inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001945typename __unique_if<_Tp>::__unique_single
1946make_unique(_Args&&... __args)
1947{
1948 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
1949}
1950
1951template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00001952inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001953typename __unique_if<_Tp>::__unique_array_unknown_bound
1954make_unique(size_t __n)
1955{
1956 typedef typename remove_extent<_Tp>::type _Up;
1957 return unique_ptr<_Tp>(new _Up[__n]());
1958}
1959
1960template<class _Tp, class... _Args>
1961 typename __unique_if<_Tp>::__unique_array_known_bound
1962 make_unique(_Args&&...) = delete;
1963
1964#endif // _LIBCPP_STD_VER > 11
1965
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001966template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001967#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001968struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001969#else
1970struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001971 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00001972#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001973{
1974 typedef unique_ptr<_Tp, _Dp> argument_type;
1975 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00001976 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001977 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00001978 {
1979 typedef typename argument_type::pointer pointer;
1980 return hash<pointer>()(__ptr.get());
1981 }
1982};
1983
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984struct __destruct_n
1985{
1986private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001987 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988
1989 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001990 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001991 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992
1993 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00001994 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995 {}
1996
Howard Hinnant719bda32011-05-28 14:41:13 +00001997 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001998 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00001999 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 {}
2001
Howard Hinnant719bda32011-05-28 14:41:13 +00002002 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002003 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002004 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 {}
2006public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002007 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002008 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009
2010 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002011 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002012 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
2014 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002015 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002016 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017
2018 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002019 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002020 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021};
2022
2023template <class _Alloc>
2024class __allocator_destructor
2025{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002026 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002028 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2029 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030private:
2031 _Alloc& __alloc_;
2032 size_type __s_;
2033public:
2034 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002035 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002038 void operator()(pointer __p) _NOEXCEPT
2039 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040};
2041
2042template <class _InputIterator, class _ForwardIterator>
2043_ForwardIterator
2044uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2045{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002047#ifndef _LIBCPP_NO_EXCEPTIONS
2048 _ForwardIterator __s = __r;
2049 try
2050 {
2051#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002052 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002053 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002054#ifndef _LIBCPP_NO_EXCEPTIONS
2055 }
2056 catch (...)
2057 {
2058 for (; __s != __r; ++__s)
2059 __s->~value_type();
2060 throw;
2061 }
2062#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063 return __r;
2064}
2065
2066template <class _InputIterator, class _Size, class _ForwardIterator>
2067_ForwardIterator
2068uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2069{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002071#ifndef _LIBCPP_NO_EXCEPTIONS
2072 _ForwardIterator __s = __r;
2073 try
2074 {
2075#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002076 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002077 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002078#ifndef _LIBCPP_NO_EXCEPTIONS
2079 }
2080 catch (...)
2081 {
2082 for (; __s != __r; ++__s)
2083 __s->~value_type();
2084 throw;
2085 }
2086#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087 return __r;
2088}
2089
2090template <class _ForwardIterator, class _Tp>
2091void
2092uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2093{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002095#ifndef _LIBCPP_NO_EXCEPTIONS
2096 _ForwardIterator __s = __f;
2097 try
2098 {
2099#endif
2100 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002101 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002102#ifndef _LIBCPP_NO_EXCEPTIONS
2103 }
2104 catch (...)
2105 {
2106 for (; __s != __f; ++__s)
2107 __s->~value_type();
2108 throw;
2109 }
2110#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111}
2112
2113template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002114_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2116{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002118#ifndef _LIBCPP_NO_EXCEPTIONS
2119 _ForwardIterator __s = __f;
2120 try
2121 {
2122#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002123 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002124 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 }
2127 catch (...)
2128 {
2129 for (; __s != __f; ++__s)
2130 __s->~value_type();
2131 throw;
2132 }
2133#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002134 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135}
2136
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002137#if _LIBCPP_STD_VER > 14
2138
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002139template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002140inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002141void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2142 for (; __first != __last; ++__first)
2143 _VSTD::destroy_at(_VSTD::addressof(*__first));
2144}
2145
2146template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002147inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002148_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2149 for (; __n > 0; (void)++__first, --__n)
2150 _VSTD::destroy_at(_VSTD::addressof(*__first));
2151 return __first;
2152}
2153
Eric Fiselier290c07c2016-10-11 21:13:44 +00002154template <class _ForwardIterator>
2155inline _LIBCPP_INLINE_VISIBILITY
2156void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2157 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2158 auto __idx = __first;
2159#ifndef _LIBCPP_NO_EXCEPTIONS
2160 try {
2161#endif
2162 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002163 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002164#ifndef _LIBCPP_NO_EXCEPTIONS
2165 } catch (...) {
2166 _VSTD::destroy(__first, __idx);
2167 throw;
2168 }
2169#endif
2170}
2171
2172template <class _ForwardIterator, class _Size>
2173inline _LIBCPP_INLINE_VISIBILITY
2174_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2175 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2176 auto __idx = __first;
2177#ifndef _LIBCPP_NO_EXCEPTIONS
2178 try {
2179#endif
2180 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002181 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002182 return __idx;
2183#ifndef _LIBCPP_NO_EXCEPTIONS
2184 } catch (...) {
2185 _VSTD::destroy(__first, __idx);
2186 throw;
2187 }
2188#endif
2189}
2190
2191
2192template <class _ForwardIterator>
2193inline _LIBCPP_INLINE_VISIBILITY
2194void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2195 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2196 auto __idx = __first;
2197#ifndef _LIBCPP_NO_EXCEPTIONS
2198 try {
2199#endif
2200 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002201 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002202#ifndef _LIBCPP_NO_EXCEPTIONS
2203 } catch (...) {
2204 _VSTD::destroy(__first, __idx);
2205 throw;
2206 }
2207#endif
2208}
2209
2210template <class _ForwardIterator, class _Size>
2211inline _LIBCPP_INLINE_VISIBILITY
2212_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2213 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2214 auto __idx = __first;
2215#ifndef _LIBCPP_NO_EXCEPTIONS
2216 try {
2217#endif
2218 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002219 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002220 return __idx;
2221#ifndef _LIBCPP_NO_EXCEPTIONS
2222 } catch (...) {
2223 _VSTD::destroy(__first, __idx);
2224 throw;
2225 }
2226#endif
2227}
2228
2229
2230template <class _InputIt, class _ForwardIt>
2231inline _LIBCPP_INLINE_VISIBILITY
2232_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2233 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2234 auto __idx = __first_res;
2235#ifndef _LIBCPP_NO_EXCEPTIONS
2236 try {
2237#endif
2238 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002239 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002240 return __idx;
2241#ifndef _LIBCPP_NO_EXCEPTIONS
2242 } catch (...) {
2243 _VSTD::destroy(__first_res, __idx);
2244 throw;
2245 }
2246#endif
2247}
2248
2249template <class _InputIt, class _Size, class _ForwardIt>
2250inline _LIBCPP_INLINE_VISIBILITY
2251pair<_InputIt, _ForwardIt>
2252uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2253 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2254 auto __idx = __first_res;
2255#ifndef _LIBCPP_NO_EXCEPTIONS
2256 try {
2257#endif
2258 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002259 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002260 return {__first, __idx};
2261#ifndef _LIBCPP_NO_EXCEPTIONS
2262 } catch (...) {
2263 _VSTD::destroy(__first_res, __idx);
2264 throw;
2265 }
2266#endif
2267}
2268
2269
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002270#endif // _LIBCPP_STD_VER > 14
2271
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002272// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2273// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05002274// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002275#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2276 && defined(__ATOMIC_RELAXED) \
2277 && defined(__ATOMIC_ACQ_REL)
2278# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002279#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002280# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2281#endif
2282
2283template <class _Tp>
2284inline _LIBCPP_INLINE_VISIBILITY _Tp
2285__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2286{
2287#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2288 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2289#else
2290 return __t += 1;
2291#endif
2292}
2293
2294template <class _Tp>
2295inline _LIBCPP_INLINE_VISIBILITY _Tp
2296__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2297{
2298#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2299 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2300#else
2301 return __t -= 1;
2302#endif
2303}
2304
Howard Hinnant756c69b2010-09-22 16:48:34 +00002305class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306 : public std::exception
2307{
2308public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002309 bad_weak_ptr() _NOEXCEPT = default;
2310 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002311 virtual ~bad_weak_ptr() _NOEXCEPT;
2312 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313};
2314
Louis Dionne16fe2952018-07-11 23:14:33 +00002315_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002316void __throw_bad_weak_ptr()
2317{
2318#ifndef _LIBCPP_NO_EXCEPTIONS
2319 throw bad_weak_ptr();
2320#else
2321 _VSTD::abort();
2322#endif
2323}
2324
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002325template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002327class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328{
2329 __shared_count(const __shared_count&);
2330 __shared_count& operator=(const __shared_count&);
2331
2332protected:
2333 long __shared_owners_;
2334 virtual ~__shared_count();
2335private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002336 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337
2338public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002340 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341 : __shared_owners_(__refs) {}
2342
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002343#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002344 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002345 void __add_shared() _NOEXCEPT;
2346 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002347#else
2348 _LIBCPP_INLINE_VISIBILITY
2349 void __add_shared() _NOEXCEPT {
2350 __libcpp_atomic_refcount_increment(__shared_owners_);
2351 }
2352 _LIBCPP_INLINE_VISIBILITY
2353 bool __release_shared() _NOEXCEPT {
2354 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2355 __on_zero_shared();
2356 return true;
2357 }
2358 return false;
2359 }
2360#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002361 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002362 long use_count() const _NOEXCEPT {
2363 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2364 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365};
2366
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002367class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 : private __shared_count
2369{
2370 long __shared_weak_owners_;
2371
2372public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002374 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375 : __shared_count(__refs),
2376 __shared_weak_owners_(__refs) {}
2377protected:
2378 virtual ~__shared_weak_count();
2379
2380public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002381#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002382 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002383 void __add_shared() _NOEXCEPT;
2384 void __add_weak() _NOEXCEPT;
2385 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002386#else
2387 _LIBCPP_INLINE_VISIBILITY
2388 void __add_shared() _NOEXCEPT {
2389 __shared_count::__add_shared();
2390 }
2391 _LIBCPP_INLINE_VISIBILITY
2392 void __add_weak() _NOEXCEPT {
2393 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2394 }
2395 _LIBCPP_INLINE_VISIBILITY
2396 void __release_shared() _NOEXCEPT {
2397 if (__shared_count::__release_shared())
2398 __release_weak();
2399 }
2400#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002401 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002403 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2404 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002405
Howard Hinnant719bda32011-05-28 14:41:13 +00002406 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002408 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409};
2410
2411template <class _Tp, class _Dp, class _Alloc>
2412class __shared_ptr_pointer
2413 : public __shared_weak_count
2414{
2415 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2416public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002419 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420
Howard Hinnant72f73582010-08-11 17:04:31 +00002421#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002422 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002423#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424
2425private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002426 virtual void __on_zero_shared() _NOEXCEPT;
2427 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428};
2429
Howard Hinnant72f73582010-08-11 17:04:31 +00002430#ifndef _LIBCPP_NO_RTTI
2431
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432template <class _Tp, class _Dp, class _Alloc>
2433const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002434__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002436 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437}
2438
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002439#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002440
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441template <class _Tp, class _Dp, class _Alloc>
2442void
Howard Hinnant719bda32011-05-28 14:41:13 +00002443__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444{
2445 __data_.first().second()(__data_.first().first());
2446 __data_.first().second().~_Dp();
2447}
2448
2449template <class _Tp, class _Dp, class _Alloc>
2450void
Howard Hinnant719bda32011-05-28 14:41:13 +00002451__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002453 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2454 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002455 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2456
Eric Fiselierf8898c82015-02-05 23:01:40 +00002457 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002459 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460}
2461
2462template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002463struct __shared_ptr_emplace
2464 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465{
Louis Dionneda463cb2020-12-11 12:22:16 -05002466 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002467 _LIBCPP_HIDE_FROM_ABI
2468 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002469 : __storage_(_VSTD::move(__a))
2470 {
2471#if _LIBCPP_STD_VER > 17
2472 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2473 _TpAlloc __tmp(*__get_alloc());
2474 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002475#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002476 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002477#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002478 }
Louis Dionne86549d72020-12-09 16:22:17 -05002479
2480 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002481 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002482
2483 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002484 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485
2486private:
Louis Dionne86549d72020-12-09 16:22:17 -05002487 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002488#if _LIBCPP_STD_VER > 17
2489 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2490 _TpAlloc __tmp(*__get_alloc());
2491 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2492#else
Louis Dionne86549d72020-12-09 16:22:17 -05002493 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002494#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002495 }
2496
2497 virtual void __on_zero_shared_weak() _NOEXCEPT {
2498 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2499 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002500 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002501 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002502 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2503 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2504 }
2505
Louis Dionneda463cb2020-12-11 12:22:16 -05002506 // This class implements the control block for non-array shared pointers created
2507 // through `std::allocate_shared` and `std::make_shared`.
2508 //
2509 // In previous versions of the library, we used a compressed pair to store
2510 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2511 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2512 // we now use a properly aligned char buffer while making sure that we maintain
2513 // the same layout that we had when we used a compressed pair.
2514 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2515 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2516 char __blob_[sizeof(_CompressedPair)];
2517
2518 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2519 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2520 }
2521 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2522 __get_alloc()->~_Alloc();
2523 }
2524 _Alloc* __get_alloc() _NOEXCEPT {
2525 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2526 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2527 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2528 return __alloc;
2529 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002530 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002531 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2532 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2533 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2534 return __elem;
2535 }
2536 };
2537
2538 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2539 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2540 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541};
2542
Erik Pilkington2a398762017-05-25 15:43:31 +00002543struct __shared_ptr_dummy_rebind_allocator_type;
2544template <>
2545class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2546{
2547public:
2548 template <class _Other>
2549 struct rebind
2550 {
2551 typedef allocator<_Other> other;
2552 };
2553};
2554
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002555template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556
zoecarverd73f42a2020-05-11 18:42:50 -07002557template<class _Tp, class _Up>
2558struct __compatible_with
2559#if _LIBCPP_STD_VER > 14
2560 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2561#else
2562 : is_convertible<_Tp*, _Up*> {};
2563#endif // _LIBCPP_STD_VER > 14
2564
zoecarver9bc39102021-02-19 11:10:36 -08002565template <class _Dp, class _Pt,
2566 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2567static true_type __well_formed_deleter_test(int);
2568
2569template <class, class>
2570static false_type __well_formed_deleter_test(...);
2571
2572template <class _Dp, class _Pt>
2573struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2574
2575template<class _Dp, class _Tp, class _Yp>
2576struct __shared_ptr_deleter_ctor_reqs
2577{
2578 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2579 is_move_constructible<_Dp>::value &&
2580 __well_formed_deleter<_Dp, _Tp*>::value;
2581};
2582
Vy Nguyene369bd92020-07-13 12:34:37 -04002583#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2584# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2585#else
2586# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2587#endif
2588
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002590class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002592public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002593#if _LIBCPP_STD_VER > 14
2594 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002595 typedef remove_extent_t<_Tp> element_type;
2596#else
2597 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002598#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002599
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600private:
2601 element_type* __ptr_;
2602 __shared_weak_count* __cntrl_;
2603
2604 struct __nat {int __for_bool_;};
2605public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002607 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002609 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002610 template<class _Yp>
2611 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002612 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002613 template<class _Yp, class _Dp>
2614 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002615 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002616 template<class _Yp, class _Dp, class _Alloc>
2617 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002618 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2620 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002621 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002623 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002627 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002628 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002630 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002631 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002632 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002633 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002635 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002636#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002637 template<class _Yp>
2638 shared_ptr(auto_ptr<_Yp>&& __r,
2639 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002640#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002641 template <class _Yp, class _Dp>
2642 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2643 typename enable_if
2644 <
2645 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002646 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2647 __nat
2648 >::type = __nat());
2649 template <class _Yp, class _Dp>
2650 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2651 typename enable_if
2652 <
2653 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00002654 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2655 __nat
2656 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657
2658 ~shared_ptr();
2659
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002661 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002662 template<class _Yp>
2663 typename enable_if
2664 <
zoecarverd73f42a2020-05-11 18:42:50 -07002665 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002666 shared_ptr&
2667 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002669 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002671 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002672 template<class _Yp>
2673 typename enable_if
2674 <
zoecarverd73f42a2020-05-11 18:42:50 -07002675 __compatible_with<_Yp, element_type>::value,
2676 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002677 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002679 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002680#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002681 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002683 typename enable_if
2684 <
2685 !is_array<_Yp>::value &&
2686 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002687 shared_ptr
2688 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002689 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002690#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002691 template <class _Yp, class _Dp>
2692 typename enable_if
2693 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002694 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2695 shared_ptr&
2696 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002698 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002701 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002703 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002704 template<class _Yp>
2705 typename enable_if
2706 <
zoecarverd73f42a2020-05-11 18:42:50 -07002707 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002708 void
2709 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002711 reset(_Yp* __p);
2712 template<class _Yp, class _Dp>
2713 typename enable_if
2714 <
zoecarverd73f42a2020-05-11 18:42:50 -07002715 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002716 void
2717 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002719 reset(_Yp* __p, _Dp __d);
2720 template<class _Yp, class _Dp, class _Alloc>
2721 typename enable_if
2722 <
zoecarverd73f42a2020-05-11 18:42:50 -07002723 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002724 void
2725 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002727 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728
Howard Hinnant756c69b2010-09-22 16:48:34 +00002729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002730 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002732 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2733 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002734 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002735 element_type* operator->() const _NOEXCEPT
2736 {
2737 static_assert(!_VSTD::is_array<_Tp>::value,
2738 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2739 return __ptr_;
2740 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002742 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002744 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002745 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002746 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002747 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002748 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002749 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002751 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002752 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002753 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002755 _LIBCPP_INLINE_VISIBILITY
2756 bool
2757 __owner_equivalent(const shared_ptr& __p) const
2758 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759
zoecarverd73f42a2020-05-11 18:42:50 -07002760#if _LIBCPP_STD_VER > 14
2761 typename add_lvalue_reference<element_type>::type
2762 _LIBCPP_INLINE_VISIBILITY
2763 operator[](ptrdiff_t __i) const
2764 {
2765 static_assert(_VSTD::is_array<_Tp>::value,
2766 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2767 return __ptr_[__i];
2768 }
2769#endif
2770
Howard Hinnant72f73582010-08-11 17:04:31 +00002771#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002774 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002775 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002776 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002777 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002778#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779
Zoe Carverd9040c72019-10-22 15:16:49 +00002780 template<class _Yp, class _CntrlBlk>
2781 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002782 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002783 {
2784 shared_ptr<_Tp> __r;
2785 __r.__ptr_ = __p;
2786 __r.__cntrl_ = __cntrl;
2787 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2788 return __r;
2789 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002790
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002792 template <class _Yp, bool = is_function<_Yp>::value>
2793 struct __shared_ptr_default_allocator
2794 {
2795 typedef allocator<_Yp> type;
2796 };
2797
2798 template <class _Yp>
2799 struct __shared_ptr_default_allocator<_Yp, true>
2800 {
2801 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2802 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002804 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002805 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002806 typename enable_if<is_convertible<_OrigPtr*,
2807 const enable_shared_from_this<_Yp>*
2808 >::value,
2809 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002810 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2811 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002813 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002814 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002815 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002816 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2817 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002818 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 }
2820
Erik Pilkington2a398762017-05-25 15:43:31 +00002821 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822
zoecarverd73f42a2020-05-11 18:42:50 -07002823 template <class, class _Yp>
2824 struct __shared_ptr_default_delete
2825 : default_delete<_Yp> {};
2826
2827 template <class _Yp, class _Un, size_t _Sz>
2828 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2829 : default_delete<_Yp[]> {};
2830
2831 template <class _Yp, class _Un>
2832 struct __shared_ptr_default_delete<_Yp[], _Un>
2833 : default_delete<_Yp[]> {};
2834
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002835 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2836 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837};
2838
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002839#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2840template<class _Tp>
2841shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2842template<class _Tp, class _Dp>
2843shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2844#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002845
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002847inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002848_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002849shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002850 : __ptr_(nullptr),
2851 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852{
2853}
2854
2855template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002856inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002857_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002858shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002859 : __ptr_(nullptr),
2860 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861{
2862}
2863
2864template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002865template<class _Yp>
2866shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002867 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 : __ptr_(__p)
2869{
2870 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002871 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002872 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2873 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002875 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876}
2877
2878template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002879template<class _Yp, class _Dp>
2880shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002881 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 : __ptr_(__p)
2883{
2884#ifndef _LIBCPP_NO_EXCEPTIONS
2885 try
2886 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002887#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002888 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2889 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002890#ifndef _LIBCPP_CXX03_LANG
2891 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2892#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002893 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002894#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002895 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896#ifndef _LIBCPP_NO_EXCEPTIONS
2897 }
2898 catch (...)
2899 {
2900 __d(__p);
2901 throw;
2902 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002903#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904}
2905
2906template<class _Tp>
2907template<class _Dp>
2908shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002909 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910{
2911#ifndef _LIBCPP_NO_EXCEPTIONS
2912 try
2913 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002914#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002915 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2916 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002917#ifndef _LIBCPP_CXX03_LANG
2918 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2919#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002920 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002921#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922#ifndef _LIBCPP_NO_EXCEPTIONS
2923 }
2924 catch (...)
2925 {
2926 __d(__p);
2927 throw;
2928 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002929#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930}
2931
2932template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002933template<class _Yp, class _Dp, class _Alloc>
2934shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002935 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 : __ptr_(__p)
2937{
2938#ifndef _LIBCPP_NO_EXCEPTIONS
2939 try
2940 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002941#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002943 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 typedef __allocator_destructor<_A2> _D2;
2945 _A2 __a2(__a);
2946 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002947 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2948#ifndef _LIBCPP_CXX03_LANG
2949 _CntrlBlk(__p, _VSTD::move(__d), __a);
2950#else
2951 _CntrlBlk(__p, __d, __a);
2952#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002953 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002954 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955#ifndef _LIBCPP_NO_EXCEPTIONS
2956 }
2957 catch (...)
2958 {
2959 __d(__p);
2960 throw;
2961 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963}
2964
2965template<class _Tp>
2966template<class _Dp, class _Alloc>
2967shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002968 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969{
2970#ifndef _LIBCPP_NO_EXCEPTIONS
2971 try
2972 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002973#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002975 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 typedef __allocator_destructor<_A2> _D2;
2977 _A2 __a2(__a);
2978 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08002979 ::new ((void*)_VSTD::addressof(*__hold2.get()))
2980#ifndef _LIBCPP_CXX03_LANG
2981 _CntrlBlk(__p, _VSTD::move(__d), __a);
2982#else
2983 _CntrlBlk(__p, __d, __a);
2984#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002985 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986#ifndef _LIBCPP_NO_EXCEPTIONS
2987 }
2988 catch (...)
2989 {
2990 __d(__p);
2991 throw;
2992 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002993#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994}
2995
2996template<class _Tp>
2997template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002998inline
Howard Hinnant719bda32011-05-28 14:41:13 +00002999shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 : __ptr_(__p),
3001 __cntrl_(__r.__cntrl_)
3002{
3003 if (__cntrl_)
3004 __cntrl_->__add_shared();
3005}
3006
3007template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003008inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003009shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 : __ptr_(__r.__ptr_),
3011 __cntrl_(__r.__cntrl_)
3012{
3013 if (__cntrl_)
3014 __cntrl_->__add_shared();
3015}
3016
3017template<class _Tp>
3018template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003019inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003021 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003022 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 : __ptr_(__r.__ptr_),
3024 __cntrl_(__r.__cntrl_)
3025{
3026 if (__cntrl_)
3027 __cntrl_->__add_shared();
3028}
3029
Howard Hinnantc51e1022010-05-11 19:42:16 +00003030template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003031inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003032shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033 : __ptr_(__r.__ptr_),
3034 __cntrl_(__r.__cntrl_)
3035{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003036 __r.__ptr_ = nullptr;
3037 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038}
3039
3040template<class _Tp>
3041template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003042inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003044 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003045 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046 : __ptr_(__r.__ptr_),
3047 __cntrl_(__r.__cntrl_)
3048{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003049 __r.__ptr_ = nullptr;
3050 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051}
3052
Marshall Clowb22274f2017-01-24 22:22:33 +00003053#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003055template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003056shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003057 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058 : __ptr_(__r.get())
3059{
3060 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3061 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003062 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 __r.release();
3064}
Marshall Clowb22274f2017-01-24 22:22:33 +00003065#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066
3067template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003068template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003070 typename enable_if
3071 <
3072 !is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00003073 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3074 __nat
3075 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 : __ptr_(__r.get())
3077{
Marshall Clow35cde742015-05-10 13:59:45 +00003078#if _LIBCPP_STD_VER > 11
3079 if (__ptr_ == nullptr)
3080 __cntrl_ = nullptr;
3081 else
3082#endif
3083 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003084 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07003085 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
Erik Pilkington2a398762017-05-25 15:43:31 +00003086 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003087 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003088 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089 __r.release();
3090}
3091
3092template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003093template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003095 typename enable_if
3096 <
3097 is_lvalue_reference<_Dp>::value &&
Logan Chiend435f8b2014-01-31 09:30:46 +00003098 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3099 __nat
3100 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 : __ptr_(__r.get())
3102{
Marshall Clow35cde742015-05-10 13:59:45 +00003103#if _LIBCPP_STD_VER > 11
3104 if (__ptr_ == nullptr)
3105 __cntrl_ = nullptr;
3106 else
3107#endif
3108 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003109 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarvercaf89ec2021-04-08 22:01:35 -07003110 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow35cde742015-05-10 13:59:45 +00003111 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003112 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003113 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003114 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003115 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 __r.release();
3117}
3118
Zoe Carver6cd05c32019-08-19 15:47:16 +00003119template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120shared_ptr<_Tp>::~shared_ptr()
3121{
3122 if (__cntrl_)
3123 __cntrl_->__release_shared();
3124}
3125
3126template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003127inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003129shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130{
3131 shared_ptr(__r).swap(*this);
3132 return *this;
3133}
3134
3135template<class _Tp>
3136template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003137inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003138typename enable_if
3139<
zoecarverd73f42a2020-05-11 18:42:50 -07003140 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003141 shared_ptr<_Tp>&
3142>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003143shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144{
3145 shared_ptr(__r).swap(*this);
3146 return *this;
3147}
3148
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003150inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003152shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003154 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155 return *this;
3156}
3157
3158template<class _Tp>
3159template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003160inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003161typename enable_if
3162<
zoecarverd73f42a2020-05-11 18:42:50 -07003163 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003164 shared_ptr<_Tp>&
3165>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3167{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003168 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169 return *this;
3170}
3171
Marshall Clowb22274f2017-01-24 22:22:33 +00003172#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173template<class _Tp>
3174template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003175inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003176typename enable_if
3177<
3178 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003179 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003180 shared_ptr<_Tp>
3181>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3183{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003184 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 return *this;
3186}
Marshall Clowb22274f2017-01-24 22:22:33 +00003187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188
3189template<class _Tp>
3190template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003191inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003192typename enable_if
3193<
Aditya Kumar7c5db692017-08-20 10:38:55 +00003194 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003195 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003196 shared_ptr<_Tp>&
3197>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3199{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003200 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 return *this;
3202}
3203
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003205inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206void
Howard Hinnant719bda32011-05-28 14:41:13 +00003207shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003209 _VSTD::swap(__ptr_, __r.__ptr_);
3210 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211}
3212
3213template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215void
Howard Hinnant719bda32011-05-28 14:41:13 +00003216shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217{
3218 shared_ptr().swap(*this);
3219}
3220
3221template<class _Tp>
3222template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003223inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003224typename enable_if
3225<
zoecarverd73f42a2020-05-11 18:42:50 -07003226 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003227 void
3228>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229shared_ptr<_Tp>::reset(_Yp* __p)
3230{
3231 shared_ptr(__p).swap(*this);
3232}
3233
3234template<class _Tp>
3235template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003236inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003237typename enable_if
3238<
zoecarverd73f42a2020-05-11 18:42:50 -07003239 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003240 void
3241>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3243{
3244 shared_ptr(__p, __d).swap(*this);
3245}
3246
3247template<class _Tp>
3248template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003249inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003250typename enable_if
3251<
zoecarverd73f42a2020-05-11 18:42:50 -07003252 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003253 void
3254>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3256{
3257 shared_ptr(__p, __d, __a).swap(*this);
3258}
3259
Louis Dionne74aef9f2020-12-11 12:20:06 -05003260//
3261// std::allocate_shared and std::make_shared
3262//
3263template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3264_LIBCPP_HIDE_FROM_ABI
3265shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003267 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3268 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3269 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3270 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3271 auto __control_block = __guard.__release_ptr();
3272 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273}
3274
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003275template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3276_LIBCPP_HIDE_FROM_ABI
3277shared_ptr<_Tp> make_shared(_Args&& ...__args)
3278{
3279 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3280}
3281
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282template<class _Tp, class _Up>
3283inline _LIBCPP_INLINE_VISIBILITY
3284bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003285operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286{
3287 return __x.get() == __y.get();
3288}
3289
3290template<class _Tp, class _Up>
3291inline _LIBCPP_INLINE_VISIBILITY
3292bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003293operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294{
3295 return !(__x == __y);
3296}
3297
3298template<class _Tp, class _Up>
3299inline _LIBCPP_INLINE_VISIBILITY
3300bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003301operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003303#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003304 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3305 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003306#else
3307 return less<>()(__x.get(), __y.get());
3308#endif
3309
Howard Hinnantb17caf92012-02-21 21:02:58 +00003310}
3311
3312template<class _Tp, class _Up>
3313inline _LIBCPP_INLINE_VISIBILITY
3314bool
3315operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3316{
3317 return __y < __x;
3318}
3319
3320template<class _Tp, class _Up>
3321inline _LIBCPP_INLINE_VISIBILITY
3322bool
3323operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3324{
3325 return !(__y < __x);
3326}
3327
3328template<class _Tp, class _Up>
3329inline _LIBCPP_INLINE_VISIBILITY
3330bool
3331operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3332{
3333 return !(__x < __y);
3334}
3335
3336template<class _Tp>
3337inline _LIBCPP_INLINE_VISIBILITY
3338bool
3339operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3340{
3341 return !__x;
3342}
3343
3344template<class _Tp>
3345inline _LIBCPP_INLINE_VISIBILITY
3346bool
3347operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3348{
3349 return !__x;
3350}
3351
3352template<class _Tp>
3353inline _LIBCPP_INLINE_VISIBILITY
3354bool
3355operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3356{
3357 return static_cast<bool>(__x);
3358}
3359
3360template<class _Tp>
3361inline _LIBCPP_INLINE_VISIBILITY
3362bool
3363operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3364{
3365 return static_cast<bool>(__x);
3366}
3367
3368template<class _Tp>
3369inline _LIBCPP_INLINE_VISIBILITY
3370bool
3371operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3372{
3373 return less<_Tp*>()(__x.get(), nullptr);
3374}
3375
3376template<class _Tp>
3377inline _LIBCPP_INLINE_VISIBILITY
3378bool
3379operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3380{
3381 return less<_Tp*>()(nullptr, __x.get());
3382}
3383
3384template<class _Tp>
3385inline _LIBCPP_INLINE_VISIBILITY
3386bool
3387operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3388{
3389 return nullptr < __x;
3390}
3391
3392template<class _Tp>
3393inline _LIBCPP_INLINE_VISIBILITY
3394bool
3395operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3396{
3397 return __x < nullptr;
3398}
3399
3400template<class _Tp>
3401inline _LIBCPP_INLINE_VISIBILITY
3402bool
3403operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3404{
3405 return !(nullptr < __x);
3406}
3407
3408template<class _Tp>
3409inline _LIBCPP_INLINE_VISIBILITY
3410bool
3411operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3412{
3413 return !(__x < nullptr);
3414}
3415
3416template<class _Tp>
3417inline _LIBCPP_INLINE_VISIBILITY
3418bool
3419operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3420{
3421 return !(__x < nullptr);
3422}
3423
3424template<class _Tp>
3425inline _LIBCPP_INLINE_VISIBILITY
3426bool
3427operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3428{
3429 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430}
3431
3432template<class _Tp>
3433inline _LIBCPP_INLINE_VISIBILITY
3434void
Howard Hinnant719bda32011-05-28 14:41:13 +00003435swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436{
3437 __x.swap(__y);
3438}
3439
3440template<class _Tp, class _Up>
3441inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003442shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003443static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444{
zoecarverd73f42a2020-05-11 18:42:50 -07003445 return shared_ptr<_Tp>(__r,
3446 static_cast<
3447 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448}
3449
3450template<class _Tp, class _Up>
3451inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003452shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003453dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454{
zoecarverd73f42a2020-05-11 18:42:50 -07003455 typedef typename shared_ptr<_Tp>::element_type _ET;
3456 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3458}
3459
3460template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003461shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003462const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463{
zoecarverd73f42a2020-05-11 18:42:50 -07003464 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003465 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466}
3467
zoecarverd73f42a2020-05-11 18:42:50 -07003468template<class _Tp, class _Up>
3469shared_ptr<_Tp>
3470reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3471{
3472 return shared_ptr<_Tp>(__r,
3473 reinterpret_cast<
3474 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3475}
3476
Howard Hinnant72f73582010-08-11 17:04:31 +00003477#ifndef _LIBCPP_NO_RTTI
3478
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479template<class _Dp, class _Tp>
3480inline _LIBCPP_INLINE_VISIBILITY
3481_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003482get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483{
3484 return __p.template __get_deleter<_Dp>();
3485}
3486
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003487#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003488
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003490class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003492public:
3493 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494private:
3495 element_type* __ptr_;
3496 __shared_weak_count* __cntrl_;
3497
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003498public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003500 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003501 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003502 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3503 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003505 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003506 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003507 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3508 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003509
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003511 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003512 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003513 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3514 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003515 ~weak_ptr();
3516
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003518 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003519 template<class _Yp>
3520 typename enable_if
3521 <
3522 is_convertible<_Yp*, element_type*>::value,
3523 weak_ptr&
3524 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003526 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3527
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003529 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3530 template<class _Yp>
3531 typename enable_if
3532 <
3533 is_convertible<_Yp*, element_type*>::value,
3534 weak_ptr&
3535 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003537 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3538
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003539 template<class _Yp>
3540 typename enable_if
3541 <
3542 is_convertible<_Yp*, element_type*>::value,
3543 weak_ptr&
3544 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003546 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003547
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003549 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003551 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003552
Howard Hinnant756c69b2010-09-22 16:48:34 +00003553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003554 long use_count() const _NOEXCEPT
3555 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003557 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003558 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003559 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003560 template<class _Up>
3561 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003562 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003564 template<class _Up>
3565 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003566 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 {return __cntrl_ < __r.__cntrl_;}
3568
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003569 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3570 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571};
3572
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003573#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3574template<class _Tp>
3575weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3576#endif
3577
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003579inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003580_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003581weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003582 : __ptr_(nullptr),
3583 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584{
3585}
3586
3587template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003588inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003589weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590 : __ptr_(__r.__ptr_),
3591 __cntrl_(__r.__cntrl_)
3592{
3593 if (__cntrl_)
3594 __cntrl_->__add_weak();
3595}
3596
3597template<class _Tp>
3598template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003599inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003601 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003602 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603 : __ptr_(__r.__ptr_),
3604 __cntrl_(__r.__cntrl_)
3605{
3606 if (__cntrl_)
3607 __cntrl_->__add_weak();
3608}
3609
3610template<class _Tp>
3611template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003612inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003614 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003615 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616 : __ptr_(__r.__ptr_),
3617 __cntrl_(__r.__cntrl_)
3618{
3619 if (__cntrl_)
3620 __cntrl_->__add_weak();
3621}
3622
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003623template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003624inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003625weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3626 : __ptr_(__r.__ptr_),
3627 __cntrl_(__r.__cntrl_)
3628{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003629 __r.__ptr_ = nullptr;
3630 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003631}
3632
3633template<class _Tp>
3634template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003635inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003636weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3637 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3638 _NOEXCEPT
3639 : __ptr_(__r.__ptr_),
3640 __cntrl_(__r.__cntrl_)
3641{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003642 __r.__ptr_ = nullptr;
3643 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003644}
3645
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646template<class _Tp>
3647weak_ptr<_Tp>::~weak_ptr()
3648{
3649 if (__cntrl_)
3650 __cntrl_->__release_weak();
3651}
3652
3653template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003654inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003656weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657{
3658 weak_ptr(__r).swap(*this);
3659 return *this;
3660}
3661
3662template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003663template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003664inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003665typename enable_if
3666<
3667 is_convertible<_Yp*, _Tp*>::value,
3668 weak_ptr<_Tp>&
3669>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003670weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
3672 weak_ptr(__r).swap(*this);
3673 return *this;
3674}
3675
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003676template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003677inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003678weak_ptr<_Tp>&
3679weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3680{
3681 weak_ptr(_VSTD::move(__r)).swap(*this);
3682 return *this;
3683}
3684
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003686template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003687inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003688typename enable_if
3689<
3690 is_convertible<_Yp*, _Tp*>::value,
3691 weak_ptr<_Tp>&
3692>::type
3693weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3694{
3695 weak_ptr(_VSTD::move(__r)).swap(*this);
3696 return *this;
3697}
3698
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003699template<class _Tp>
3700template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003701inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003702typename enable_if
3703<
3704 is_convertible<_Yp*, _Tp*>::value,
3705 weak_ptr<_Tp>&
3706>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003707weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
3709 weak_ptr(__r).swap(*this);
3710 return *this;
3711}
3712
3713template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003714inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715void
Howard Hinnant719bda32011-05-28 14:41:13 +00003716weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003718 _VSTD::swap(__ptr_, __r.__ptr_);
3719 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720}
3721
3722template<class _Tp>
3723inline _LIBCPP_INLINE_VISIBILITY
3724void
Howard Hinnant719bda32011-05-28 14:41:13 +00003725swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726{
3727 __x.swap(__y);
3728}
3729
3730template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003731inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732void
Howard Hinnant719bda32011-05-28 14:41:13 +00003733weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734{
3735 weak_ptr().swap(*this);
3736}
3737
3738template<class _Tp>
3739template<class _Yp>
3740shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003741 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742 : __ptr_(__r.__ptr_),
3743 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3744{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003745 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003746 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747}
3748
3749template<class _Tp>
3750shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003751weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003752{
3753 shared_ptr<_Tp> __r;
3754 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3755 if (__r.__cntrl_)
3756 __r.__ptr_ = __ptr_;
3757 return __r;
3758}
3759
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003760#if _LIBCPP_STD_VER > 14
3761template <class _Tp = void> struct owner_less;
3762#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003763template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003764#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765
3766template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003767struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003769{
3770 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003771 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003772 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003775 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003777 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003778 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003780};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781
3782template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003783struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3785{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003786 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003787 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003788 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003790 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003791 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003793 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003794 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795 {return __x.owner_before(__y);}
3796};
3797
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003798#if _LIBCPP_STD_VER > 14
3799template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003800struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003801{
3802 template <class _Tp, class _Up>
3803 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003804 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003805 {return __x.owner_before(__y);}
3806 template <class _Tp, class _Up>
3807 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003808 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003809 {return __x.owner_before(__y);}
3810 template <class _Tp, class _Up>
3811 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003812 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003813 {return __x.owner_before(__y);}
3814 template <class _Tp, class _Up>
3815 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003816 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003817 {return __x.owner_before(__y);}
3818 typedef void is_transparent;
3819};
3820#endif
3821
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003823class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
3825 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003826protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003827 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003828 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003830 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003832 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3833 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003836public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003838 shared_ptr<_Tp> shared_from_this()
3839 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003841 shared_ptr<_Tp const> shared_from_this() const
3842 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843
Eric Fiselier84006862016-06-02 00:15:35 +00003844#if _LIBCPP_STD_VER > 14
3845 _LIBCPP_INLINE_VISIBILITY
3846 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3847 { return __weak_this_; }
3848
3849 _LIBCPP_INLINE_VISIBILITY
3850 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3851 { return __weak_this_; }
3852#endif // _LIBCPP_STD_VER > 14
3853
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854 template <class _Up> friend class shared_ptr;
3855};
3856
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003857template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003858struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003859{
3860 typedef shared_ptr<_Tp> argument_type;
3861 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003862
Howard Hinnant756c69b2010-09-22 16:48:34 +00003863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003864 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003865 {
zoecarverd73f42a2020-05-11 18:42:50 -07003866 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003867 }
3868};
3869
Howard Hinnantc834c512011-11-29 18:15:50 +00003870template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003871inline _LIBCPP_INLINE_VISIBILITY
3872basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003873operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003874
Eric Fiselier9b492672016-06-18 02:12:53 +00003875
3876#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003877
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003878class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003879{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003880 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003881public:
3882 void lock() _NOEXCEPT;
3883 void unlock() _NOEXCEPT;
3884
3885private:
3886 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3887 __sp_mut(const __sp_mut&);
3888 __sp_mut& operator=(const __sp_mut&);
3889
Howard Hinnant8331b762013-03-06 23:30:19 +00003890 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003891};
3892
Mehdi Amini228053d2017-05-04 17:08:54 +00003893_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3894__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003895
3896template <class _Tp>
3897inline _LIBCPP_INLINE_VISIBILITY
3898bool
3899atomic_is_lock_free(const shared_ptr<_Tp>*)
3900{
3901 return false;
3902}
3903
3904template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003905_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003906shared_ptr<_Tp>
3907atomic_load(const shared_ptr<_Tp>* __p)
3908{
3909 __sp_mut& __m = __get_sp_mut(__p);
3910 __m.lock();
3911 shared_ptr<_Tp> __q = *__p;
3912 __m.unlock();
3913 return __q;
3914}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003915
Howard Hinnant9fa30202012-07-30 01:40:57 +00003916template <class _Tp>
3917inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003918_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003919shared_ptr<_Tp>
3920atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3921{
3922 return atomic_load(__p);
3923}
3924
3925template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003926_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003927void
3928atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3929{
3930 __sp_mut& __m = __get_sp_mut(__p);
3931 __m.lock();
3932 __p->swap(__r);
3933 __m.unlock();
3934}
3935
3936template <class _Tp>
3937inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003938_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003939void
3940atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3941{
3942 atomic_store(__p, __r);
3943}
3944
3945template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003946_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003947shared_ptr<_Tp>
3948atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3949{
3950 __sp_mut& __m = __get_sp_mut(__p);
3951 __m.lock();
3952 __p->swap(__r);
3953 __m.unlock();
3954 return __r;
3955}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003956
Howard Hinnant9fa30202012-07-30 01:40:57 +00003957template <class _Tp>
3958inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003959_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003960shared_ptr<_Tp>
3961atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
3962{
3963 return atomic_exchange(__p, __r);
3964}
3965
3966template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003967_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003968bool
3969atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3970{
Marshall Clow4201ee82016-05-18 17:50:13 +00003971 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003972 __sp_mut& __m = __get_sp_mut(__p);
3973 __m.lock();
3974 if (__p->__owner_equivalent(*__v))
3975 {
Marshall Clow4201ee82016-05-18 17:50:13 +00003976 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003977 *__p = __w;
3978 __m.unlock();
3979 return true;
3980 }
Marshall Clow4201ee82016-05-18 17:50:13 +00003981 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003982 *__v = *__p;
3983 __m.unlock();
3984 return false;
3985}
3986
3987template <class _Tp>
3988inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003989_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003990bool
3991atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
3992{
3993 return atomic_compare_exchange_strong(__p, __v, __w);
3994}
3995
3996template <class _Tp>
3997inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003998_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003999bool
4000atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4001 shared_ptr<_Tp> __w, memory_order, memory_order)
4002{
4003 return atomic_compare_exchange_strong(__p, __v, __w);
4004}
4005
4006template <class _Tp>
4007inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004008_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004009bool
4010atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4011 shared_ptr<_Tp> __w, memory_order, memory_order)
4012{
4013 return atomic_compare_exchange_weak(__p, __v, __w);
4014}
4015
Eric Fiselier9b492672016-06-18 02:12:53 +00004016#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004017
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004018//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004019#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4020# ifndef _LIBCPP_CXX03_LANG
4021enum class pointer_safety : unsigned char {
4022 relaxed,
4023 preferred,
4024 strict
4025};
4026# endif
4027#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004028struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004030 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031 {
4032 relaxed,
4033 preferred,
4034 strict
4035 };
4036
Howard Hinnant49e145e2012-10-30 19:06:59 +00004037 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038
Howard Hinnant756c69b2010-09-22 16:48:34 +00004039 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004040 pointer_safety() : __v_() {}
4041
4042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004043 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045 operator int() const {return __v_;}
4046};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004047#endif
4048
4049#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004050 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004051_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4052#else
4053// This function is only offered in C++03 under ABI v1.
4054# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4055inline _LIBCPP_INLINE_VISIBILITY
4056pointer_safety get_pointer_safety() _NOEXCEPT {
4057 return pointer_safety::relaxed;
4058}
4059# endif
4060#endif
4061
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004063_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4064_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4065_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004066_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067
4068template <class _Tp>
4069inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004070_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071undeclare_reachable(_Tp* __p)
4072{
4073 return static_cast<_Tp*>(__undeclare_reachable(__p));
4074}
4075
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004076_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077
Marshall Clow8982dcd2015-07-13 20:04:56 +00004078// --- Helper for container swap --
4079template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004080_LIBCPP_INLINE_VISIBILITY
4081void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4082#if _LIBCPP_STD_VER >= 14
4083 _NOEXCEPT
4084#else
4085 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4086#endif
4087{
4088 using _VSTD::swap;
4089 swap(__a1, __a2);
4090}
4091
4092template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004093inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004094void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4095
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004096template <typename _Alloc>
4097inline _LIBCPP_INLINE_VISIBILITY
4098void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4099#if _LIBCPP_STD_VER >= 14
4100 _NOEXCEPT
4101#else
4102 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4103#endif
4104{
4105 _VSTD::__swap_allocator(__a1, __a2,
4106 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4107}
4108
Marshall Clowff91de82015-08-18 19:51:37 +00004109template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004110struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004111 _Traits::propagate_on_container_move_assignment::value
4112#if _LIBCPP_STD_VER > 14
4113 || _Traits::is_always_equal::value
4114#else
4115 && is_nothrow_move_assignable<_Alloc>::value
4116#endif
4117 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004118
Marshall Clowa591b9a2016-07-11 21:38:08 +00004119
Marshall Clowa591b9a2016-07-11 21:38:08 +00004120template <class _Tp, class _Alloc>
4121struct __temp_value {
4122 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004123
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004124 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004125 _Alloc &__a;
4126
4127 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4128 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004129
Marshall Clowa591b9a2016-07-11 21:38:08 +00004130 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004131 _LIBCPP_NO_CFI
4132 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4133 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4134 _VSTD::forward<_Args>(__args)...);
4135 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004136
Marshall Clowa591b9a2016-07-11 21:38:08 +00004137 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4138 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004139
Marshall Clowe46031a2018-07-02 18:41:15 +00004140template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004141struct __is_allocator : false_type {};
4142
4143template<typename _Alloc>
4144struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004145 typename __void_t<typename _Alloc::value_type>::type,
4146 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4147 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004148 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004149
Eric Fiselier74ebee62019-06-08 01:31:19 +00004150// __builtin_new_allocator -- A non-templated helper for allocating and
4151// deallocating memory using __builtin_operator_new and
4152// __builtin_operator_delete. It should be used in preference to
4153// `std::allocator<T>` to avoid additional instantiations.
4154struct __builtin_new_allocator {
4155 struct __builtin_new_deleter {
4156 typedef void* pointer_type;
4157
4158 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4159 : __size_(__size), __align_(__align) {}
4160
4161 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004162 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004163 }
4164
4165 private:
4166 size_t __size_;
4167 size_t __align_;
4168 };
4169
4170 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4171
4172 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004173 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004174 __builtin_new_deleter(__s, __align));
4175 }
4176
4177 static void __deallocate_bytes(void* __p, size_t __s,
4178 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004179 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004180 }
4181
4182 template <class _Tp>
4183 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4184 static __holder_t __allocate_type(size_t __n) {
4185 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4186 }
4187
4188 template <class _Tp>
4189 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4190 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4191 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4192 }
4193};
4194
4195
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196_LIBCPP_END_NAMESPACE_STD
4197
Eric Fiselierf4433a32017-05-31 22:07:49 +00004198_LIBCPP_POP_MACROS
4199
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004200#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004201# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004202#endif
4203
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204#endif // _LIBCPP_MEMORY