blob: 484dafe7677c992123e9f91487975aa172b54b14 [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>
676#include <limits>
677#include <iterator>
678#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000679#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000680#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000681#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000682#include <cstring>
Louis Dionnefa621d72020-12-10 18:28:13 -0500683#include <__memory/allocator_traits.h>
684#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
Marshall Clowb22274f2017-01-24 22:22:33 +00001086#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001088struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089{
1090 _Tp* __ptr_;
1091};
1092
1093template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001094class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095{
1096private:
1097 _Tp* __ptr_;
1098public:
1099 typedef _Tp element_type;
1100
Dimitry Andric47269ce2020-03-13 19:36:26 +01001101 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1102 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1103 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001105 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001107 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001109 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001111 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112
Dimitry Andric47269ce2020-03-13 19:36:26 +01001113 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001115 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1116 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1117 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 {
1119 _Tp* __t = __ptr_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001120 __ptr_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 return __t;
1122 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001123 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 {
1125 if (__ptr_ != __p)
1126 delete __ptr_;
1127 __ptr_ = __p;
1128 }
1129
Dimitry Andric47269ce2020-03-13 19:36:26 +01001130 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1131 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001133 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 {return auto_ptr<_Up>(release());}
1135};
1136
1137template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001138class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139{
1140public:
1141 typedef void element_type;
1142};
Marshall Clowb22274f2017-01-24 22:22:33 +00001143#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001145// Tag used to default initialize one or both of the pair's elements.
1146struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001147struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001148
Eric Fiselier9d355982017-04-12 23:45:53 +00001149template <class _Tp, int _Idx,
1150 bool _CanBeEmptyBase =
1151 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1152struct __compressed_pair_elem {
1153 typedef _Tp _ParamT;
1154 typedef _Tp& reference;
1155 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001157 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001158 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001159 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1160 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
Eric Fiselier9d355982017-04-12 23:45:53 +00001162 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001163 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1164 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001165 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001166 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001167 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001168 : __value_(_VSTD::forward<_Up>(__u))
1169 {
1170 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001172
1173#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001174 template <class... _Args, size_t... _Indexes>
1175 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1176 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1177 __tuple_indices<_Indexes...>)
1178 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001179#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001181
Alex Lorenz76132112017-11-09 17:54:49 +00001182 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1183 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001184 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001187 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188};
1189
Eric Fiselier9d355982017-04-12 23:45:53 +00001190template <class _Tp, int _Idx>
1191struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1192 typedef _Tp _ParamT;
1193 typedef _Tp& reference;
1194 typedef const _Tp& const_reference;
1195 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
1198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1199 __compressed_pair_elem(__default_init_tag) {}
1200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1201 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202
Eric Fiselier9d355982017-04-12 23:45:53 +00001203 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001204 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1205 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001206 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001207 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001208 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001209 : __value_type(_VSTD::forward<_Up>(__u))
1210 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001212#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001213 template <class... _Args, size_t... _Indexes>
1214 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1215 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1216 __tuple_indices<_Indexes...>)
1217 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001218#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
Alex Lorenz76132112017-11-09 17:54:49 +00001220 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
1221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001222 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223};
1224
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00001226class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
1227 private __compressed_pair_elem<_T2, 1> {
Louis Dionneda463cb2020-12-11 12:22:16 -05001228public:
Eric Fiselier9d355982017-04-12 23:45:53 +00001229 // NOTE: This static assert should never fire because __compressed_pair
1230 // is *almost never* used in a scenario where it's possible for T1 == T2.
1231 // (The exception is std::function where it is possible that the function
1232 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00001233 static_assert((!is_same<_T1, _T2>::value),
Arthur O'Dwyerfed1ff62020-12-01 20:02:46 -05001234 "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
Eric Fiselier9d355982017-04-12 23:45:53 +00001235 "The current implementation is NOT ABI-compatible with the previous "
1236 "implementation for this configuration");
1237
Louis Dionneda463cb2020-12-11 12:22:16 -05001238 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
1239 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
1240
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001241 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00001242 class = typename enable_if<
1243 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
1244 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
1245 >::type
1246 >
Eric Fiselier9d355982017-04-12 23:45:53 +00001247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001248 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249
Eric Fiselier9d355982017-04-12 23:45:53 +00001250 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001251 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00001252 __compressed_pair(_U1&& __t1, _U2&& __t2)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001253 : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001255#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001256 template <class... _Args1, class... _Args2>
1257 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1258 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
1259 tuple<_Args2...> __second_args)
1260 : _Base1(__pc, _VSTD::move(__first_args),
1261 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
1262 _Base2(__pc, _VSTD::move(__second_args),
1263 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001264#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265
Eric Fiselier9d355982017-04-12 23:45:53 +00001266 _LIBCPP_INLINE_VISIBILITY
1267 typename _Base1::reference first() _NOEXCEPT {
1268 return static_cast<_Base1&>(*this).__get();
1269 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Eric Fiselier9d355982017-04-12 23:45:53 +00001271 _LIBCPP_INLINE_VISIBILITY
1272 typename _Base1::const_reference first() const _NOEXCEPT {
1273 return static_cast<_Base1 const&>(*this).__get();
1274 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Eric Fiselier9d355982017-04-12 23:45:53 +00001276 _LIBCPP_INLINE_VISIBILITY
1277 typename _Base2::reference second() _NOEXCEPT {
1278 return static_cast<_Base2&>(*this).__get();
1279 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
Eric Fiselier9d355982017-04-12 23:45:53 +00001281 _LIBCPP_INLINE_VISIBILITY
1282 typename _Base2::const_reference second() const _NOEXCEPT {
1283 return static_cast<_Base2 const&>(*this).__get();
1284 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Louis Dionneda463cb2020-12-11 12:22:16 -05001286 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1287 static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
1288 return static_cast<_Base1*>(__pair);
1289 }
1290 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1291 static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
1292 return static_cast<_Base2*>(__pair);
1293 }
1294
Eric Fiselier9d355982017-04-12 23:45:53 +00001295 _LIBCPP_INLINE_VISIBILITY
1296 void swap(__compressed_pair& __x)
1297 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1298 __is_nothrow_swappable<_T2>::value)
1299 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001300 using _VSTD::swap;
Eric Fiselier9d355982017-04-12 23:45:53 +00001301 swap(first(), __x.first());
1302 swap(second(), __x.second());
1303 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304};
1305
1306template <class _T1, class _T2>
1307inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001308void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1309 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1310 __is_nothrow_swappable<_T2>::value) {
1311 __x.swap(__y);
1312}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001314// default_delete
1315
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001317struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00001318 static_assert(!is_function<_Tp>::value,
1319 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00001320#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001321 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001322#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001323 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001324#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001325 template <class _Up>
1326 _LIBCPP_INLINE_VISIBILITY
1327 default_delete(const default_delete<_Up>&,
1328 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
1329 0) _NOEXCEPT {}
1330
1331 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
1332 static_assert(sizeof(_Tp) > 0,
1333 "default_delete can not delete incomplete type");
1334 static_assert(!is_void<_Tp>::value,
1335 "default_delete can not delete incomplete type");
1336 delete __ptr;
1337 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338};
1339
1340template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00001341struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
1342private:
1343 template <class _Up>
1344 struct _EnableIfConvertible
1345 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
1346
Howard Hinnant4500ca52011-12-18 21:19:44 +00001347public:
Eric Fiselier6779b062017-04-16 02:06:25 +00001348#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001349 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001350#else
Eric Fiselier6779b062017-04-16 02:06:25 +00001351 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001352#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00001353
1354 template <class _Up>
1355 _LIBCPP_INLINE_VISIBILITY
1356 default_delete(const default_delete<_Up[]>&,
1357 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
1358
1359 template <class _Up>
1360 _LIBCPP_INLINE_VISIBILITY
1361 typename _EnableIfConvertible<_Up>::type
1362 operator()(_Up* __ptr) const _NOEXCEPT {
1363 static_assert(sizeof(_Tp) > 0,
1364 "default_delete can not delete incomplete type");
1365 static_assert(!is_void<_Tp>::value,
1366 "default_delete can not delete void type");
1367 delete[] __ptr;
1368 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369};
1370
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001371template <class _Deleter>
1372struct __unique_ptr_deleter_sfinae {
1373 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
1374 typedef const _Deleter& __lval_ref_type;
1375 typedef _Deleter&& __good_rval_ref_type;
1376 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377};
1378
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001379template <class _Deleter>
1380struct __unique_ptr_deleter_sfinae<_Deleter const&> {
1381 typedef const _Deleter& __lval_ref_type;
1382 typedef const _Deleter&& __bad_rval_ref_type;
1383 typedef false_type __enable_rval_overload;
1384};
1385
1386template <class _Deleter>
1387struct __unique_ptr_deleter_sfinae<_Deleter&> {
1388 typedef _Deleter& __lval_ref_type;
1389 typedef _Deleter&& __bad_rval_ref_type;
1390 typedef false_type __enable_rval_overload;
1391};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001392
Vy Nguyene369bd92020-07-13 12:34:37 -04001393#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
1394# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
1395#else
1396# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
1397#endif
1398
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001399template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04001400class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001401public:
1402 typedef _Tp element_type;
1403 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001404 typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001405
1406 static_assert(!is_rvalue_reference<deleter_type>::value,
1407 "the specified deleter type cannot be an rvalue reference");
1408
1409private:
1410 __compressed_pair<pointer, deleter_type> __ptr_;
1411
1412 struct __nat { int __for_bool_; };
1413
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001414 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001415
1416 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001417 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001418 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001419
1420 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001421 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001422 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001423
1424 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001425 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001426 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001427
1428 template <bool _Dummy, class _Deleter = typename __dependent_type<
1429 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001430 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001431 typename enable_if<is_default_constructible<_Deleter>::value &&
1432 !is_pointer<_Deleter>::value>::type;
1433
1434 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001435 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001436 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1437
1438 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001439 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001440 is_convertible<typename _UPtr::pointer, pointer>::value &&
1441 !is_array<_Up>::value
1442 >::type;
1443
1444 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001445 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001446 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1447 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1448 >::type;
1449
1450 template <class _UDel>
1451 using _EnableIfDeleterAssignable = typename enable_if<
1452 is_assignable<_Dp&, _UDel&&>::value
1453 >::type;
1454
1455public:
1456 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001457 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001458 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001459 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001460
1461 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001462 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001463 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001464 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001465
1466 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001467 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001468 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001469 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001470
1471 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001472 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001473 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001474 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001475 : __ptr_(__p, __d) {}
1476
1477 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001478 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001479 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001480 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001481 : __ptr_(__p, _VSTD::move(__d)) {
1482 static_assert(!is_reference<deleter_type>::value,
1483 "rvalue deleter bound to reference");
1484 }
1485
1486 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001487 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001488 _LIBCPP_INLINE_VISIBILITY
1489 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
1490
1491 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001492 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001493 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1494 }
1495
1496 template <class _Up, class _Ep,
1497 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1498 class = _EnableIfDeleterConvertible<_Ep>
1499 >
1500 _LIBCPP_INLINE_VISIBILITY
1501 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
1502 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
1503
1504#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1505 template <class _Up>
1506 _LIBCPP_INLINE_VISIBILITY
1507 unique_ptr(auto_ptr<_Up>&& __p,
1508 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001509 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001510 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001511 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001512#endif
1513
1514 _LIBCPP_INLINE_VISIBILITY
1515 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
1516 reset(__u.release());
1517 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1518 return *this;
1519 }
1520
1521 template <class _Up, class _Ep,
1522 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1523 class = _EnableIfDeleterAssignable<_Ep>
1524 >
1525 _LIBCPP_INLINE_VISIBILITY
1526 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
1527 reset(__u.release());
1528 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1529 return *this;
1530 }
1531
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001532#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
1533 template <class _Up>
1534 _LIBCPP_INLINE_VISIBILITY
1535 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
1536 is_same<_Dp, default_delete<_Tp> >::value,
1537 unique_ptr&>::type
1538 operator=(auto_ptr<_Up> __p) {
1539 reset(__p.release());
1540 return *this;
1541 }
1542#endif
1543
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001544#ifdef _LIBCPP_CXX03_LANG
1545 unique_ptr(unique_ptr const&) = delete;
1546 unique_ptr& operator=(unique_ptr const&) = delete;
1547#endif
1548
1549
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001550 _LIBCPP_INLINE_VISIBILITY
1551 ~unique_ptr() { reset(); }
1552
1553 _LIBCPP_INLINE_VISIBILITY
1554 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1555 reset();
1556 return *this;
1557 }
1558
1559 _LIBCPP_INLINE_VISIBILITY
1560 typename add_lvalue_reference<_Tp>::type
1561 operator*() const {
1562 return *__ptr_.first();
1563 }
1564 _LIBCPP_INLINE_VISIBILITY
1565 pointer operator->() const _NOEXCEPT {
1566 return __ptr_.first();
1567 }
1568 _LIBCPP_INLINE_VISIBILITY
1569 pointer get() const _NOEXCEPT {
1570 return __ptr_.first();
1571 }
1572 _LIBCPP_INLINE_VISIBILITY
1573 deleter_type& get_deleter() _NOEXCEPT {
1574 return __ptr_.second();
1575 }
1576 _LIBCPP_INLINE_VISIBILITY
1577 const deleter_type& get_deleter() const _NOEXCEPT {
1578 return __ptr_.second();
1579 }
1580 _LIBCPP_INLINE_VISIBILITY
1581 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1582 return __ptr_.first() != nullptr;
1583 }
1584
1585 _LIBCPP_INLINE_VISIBILITY
1586 pointer release() _NOEXCEPT {
1587 pointer __t = __ptr_.first();
1588 __ptr_.first() = pointer();
1589 return __t;
1590 }
1591
1592 _LIBCPP_INLINE_VISIBILITY
1593 void reset(pointer __p = pointer()) _NOEXCEPT {
1594 pointer __tmp = __ptr_.first();
1595 __ptr_.first() = __p;
1596 if (__tmp)
1597 __ptr_.second()(__tmp);
1598 }
1599
1600 _LIBCPP_INLINE_VISIBILITY
1601 void swap(unique_ptr& __u) _NOEXCEPT {
1602 __ptr_.swap(__u.__ptr_);
1603 }
1604};
1605
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001606
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04001608class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001610 typedef _Tp element_type;
1611 typedef _Dp deleter_type;
Louis Dionne88978f62021-01-12 11:53:24 -05001612 typedef typename __pointer<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001613
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001615 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616
Eric Fiselier31127cd2017-04-16 02:14:31 +00001617 template <class _From>
1618 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619
Eric Fiselier31127cd2017-04-16 02:14:31 +00001620 template <class _FromElem>
1621 struct _CheckArrayPointerConversion<_FromElem*>
1622 : integral_constant<bool,
1623 is_same<_FromElem*, pointer>::value ||
1624 (is_same<pointer, element_type*>::value &&
1625 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
1626 >
1627 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628
Eric Fiselier31127cd2017-04-16 02:14:31 +00001629 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001630
1631 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001632 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001633 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001634
1635 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001636 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001637 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001638
1639 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001640 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00001641 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001642
1643 template <bool _Dummy, class _Deleter = typename __dependent_type<
1644 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001645 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001646 typename enable_if<is_default_constructible<_Deleter>::value &&
1647 !is_pointer<_Deleter>::value>::type;
1648
1649 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001650 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001651 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
1652
1653 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001654 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001655 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001656 >::type;
1657
1658 template <class _UPtr, class _Up,
1659 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001660 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001661 is_array<_Up>::value &&
1662 is_same<pointer, element_type*>::value &&
1663 is_same<typename _UPtr::pointer, _ElemT*>::value &&
1664 is_convertible<_ElemT(*)[], element_type(*)[]>::value
1665 >::type;
1666
1667 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001668 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001669 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
1670 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
1671 >::type;
1672
1673 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001674 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001675 is_assignable<_Dp&, _UDel&&>::value
1676 >::type;
1677
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001679 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001680 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001681 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001682 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001684 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001685 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001686 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001687 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001689 template <class _Pp, bool _Dummy = true,
1690 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001691 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001692 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001693 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001694 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001696 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001697 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
1698 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001699 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001700 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001701 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001703 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001704 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001705 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001706 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001707 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001709 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001710 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
1711 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001712 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001713 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001714 : __ptr_(__p, _VSTD::move(__d)) {
1715 static_assert(!is_reference<deleter_type>::value,
1716 "rvalue deleter bound to reference");
1717 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001719 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001720 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001721 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001722 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001723 : __ptr_(nullptr, _VSTD::move(__d)) {
1724 static_assert(!is_reference<deleter_type>::value,
1725 "rvalue deleter bound to reference");
1726 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001727
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001728 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001729 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
1730 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001731 _LIBCPP_INLINE_VISIBILITY
1732 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00001733
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001734 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001735 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001736 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
1737 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00001738
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001739 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001740 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001741 reset(__u.release());
1742 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
1743 return *this;
1744 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001746 template <class _Up, class _Ep,
1747 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1748 class = _EnableIfDeleterConvertible<_Ep>
1749 >
1750 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001751 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00001752 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001753 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001755 template <class _Up, class _Ep,
1756 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
1757 class = _EnableIfDeleterAssignable<_Ep>
1758 >
1759 _LIBCPP_INLINE_VISIBILITY
1760 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001761 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001762 reset(__u.release());
1763 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
1764 return *this;
1765 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001767#ifdef _LIBCPP_CXX03_LANG
1768 unique_ptr(unique_ptr const&) = delete;
1769 unique_ptr& operator=(unique_ptr const&) = delete;
1770#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001771
1772public:
1773 _LIBCPP_INLINE_VISIBILITY
1774 ~unique_ptr() { reset(); }
1775
1776 _LIBCPP_INLINE_VISIBILITY
1777 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
1778 reset();
1779 return *this;
1780 }
1781
1782 _LIBCPP_INLINE_VISIBILITY
1783 typename add_lvalue_reference<_Tp>::type
1784 operator[](size_t __i) const {
1785 return __ptr_.first()[__i];
1786 }
1787 _LIBCPP_INLINE_VISIBILITY
1788 pointer get() const _NOEXCEPT {
1789 return __ptr_.first();
1790 }
1791
1792 _LIBCPP_INLINE_VISIBILITY
1793 deleter_type& get_deleter() _NOEXCEPT {
1794 return __ptr_.second();
1795 }
1796
1797 _LIBCPP_INLINE_VISIBILITY
1798 const deleter_type& get_deleter() const _NOEXCEPT {
1799 return __ptr_.second();
1800 }
1801 _LIBCPP_INLINE_VISIBILITY
1802 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
1803 return __ptr_.first() != nullptr;
1804 }
1805
1806 _LIBCPP_INLINE_VISIBILITY
1807 pointer release() _NOEXCEPT {
1808 pointer __t = __ptr_.first();
1809 __ptr_.first() = pointer();
1810 return __t;
1811 }
1812
1813 template <class _Pp>
1814 _LIBCPP_INLINE_VISIBILITY
1815 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00001816 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00001817 >::type
1818 reset(_Pp __p) _NOEXCEPT {
1819 pointer __tmp = __ptr_.first();
1820 __ptr_.first() = __p;
1821 if (__tmp)
1822 __ptr_.second()(__tmp);
1823 }
1824
1825 _LIBCPP_INLINE_VISIBILITY
1826 void reset(nullptr_t = nullptr) _NOEXCEPT {
1827 pointer __tmp = __ptr_.first();
1828 __ptr_.first() = nullptr;
1829 if (__tmp)
1830 __ptr_.second()(__tmp);
1831 }
1832
1833 _LIBCPP_INLINE_VISIBILITY
1834 void swap(unique_ptr& __u) _NOEXCEPT {
1835 __ptr_.swap(__u.__ptr_);
1836 }
1837
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838};
1839
1840template <class _Tp, class _Dp>
1841inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00001842typename enable_if<
1843 __is_swappable<_Dp>::value,
1844 void
1845>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00001846swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847
1848template <class _T1, class _D1, class _T2, class _D2>
1849inline _LIBCPP_INLINE_VISIBILITY
1850bool
1851operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
1852
1853template <class _T1, class _D1, class _T2, class _D2>
1854inline _LIBCPP_INLINE_VISIBILITY
1855bool
1856operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
1857
1858template <class _T1, class _D1, class _T2, class _D2>
1859inline _LIBCPP_INLINE_VISIBILITY
1860bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00001861operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
1862{
1863 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1864 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00001865 typedef typename common_type<_P1, _P2>::type _Vp;
1866 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00001867}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868
1869template <class _T1, class _D1, class _T2, class _D2>
1870inline _LIBCPP_INLINE_VISIBILITY
1871bool
1872operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
1873
1874template <class _T1, class _D1, class _T2, class _D2>
1875inline _LIBCPP_INLINE_VISIBILITY
1876bool
1877operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
1878
1879template <class _T1, class _D1, class _T2, class _D2>
1880inline _LIBCPP_INLINE_VISIBILITY
1881bool
1882operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
1883
Howard Hinnantb17caf92012-02-21 21:02:58 +00001884template <class _T1, class _D1>
1885inline _LIBCPP_INLINE_VISIBILITY
1886bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001887operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001888{
1889 return !__x;
1890}
1891
1892template <class _T1, class _D1>
1893inline _LIBCPP_INLINE_VISIBILITY
1894bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001895operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001896{
1897 return !__x;
1898}
1899
1900template <class _T1, class _D1>
1901inline _LIBCPP_INLINE_VISIBILITY
1902bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001903operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001904{
1905 return static_cast<bool>(__x);
1906}
1907
1908template <class _T1, class _D1>
1909inline _LIBCPP_INLINE_VISIBILITY
1910bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00001911operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00001912{
1913 return static_cast<bool>(__x);
1914}
1915
1916template <class _T1, class _D1>
1917inline _LIBCPP_INLINE_VISIBILITY
1918bool
1919operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1920{
1921 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1922 return less<_P1>()(__x.get(), nullptr);
1923}
1924
1925template <class _T1, class _D1>
1926inline _LIBCPP_INLINE_VISIBILITY
1927bool
1928operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1929{
1930 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
1931 return less<_P1>()(nullptr, __x.get());
1932}
1933
1934template <class _T1, class _D1>
1935inline _LIBCPP_INLINE_VISIBILITY
1936bool
1937operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1938{
1939 return nullptr < __x;
1940}
1941
1942template <class _T1, class _D1>
1943inline _LIBCPP_INLINE_VISIBILITY
1944bool
1945operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1946{
1947 return __x < nullptr;
1948}
1949
1950template <class _T1, class _D1>
1951inline _LIBCPP_INLINE_VISIBILITY
1952bool
1953operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1954{
1955 return !(nullptr < __x);
1956}
1957
1958template <class _T1, class _D1>
1959inline _LIBCPP_INLINE_VISIBILITY
1960bool
1961operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1962{
1963 return !(__x < nullptr);
1964}
1965
1966template <class _T1, class _D1>
1967inline _LIBCPP_INLINE_VISIBILITY
1968bool
1969operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
1970{
1971 return !(__x < nullptr);
1972}
1973
1974template <class _T1, class _D1>
1975inline _LIBCPP_INLINE_VISIBILITY
1976bool
1977operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
1978{
1979 return !(nullptr < __x);
1980}
1981
Marshall Clow9e8f4a92013-07-01 18:16:03 +00001982#if _LIBCPP_STD_VER > 11
1983
1984template<class _Tp>
1985struct __unique_if
1986{
1987 typedef unique_ptr<_Tp> __unique_single;
1988};
1989
1990template<class _Tp>
1991struct __unique_if<_Tp[]>
1992{
1993 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
1994};
1995
1996template<class _Tp, size_t _Np>
1997struct __unique_if<_Tp[_Np]>
1998{
1999 typedef void __unique_array_known_bound;
2000};
2001
2002template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002003inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002004typename __unique_if<_Tp>::__unique_single
2005make_unique(_Args&&... __args)
2006{
2007 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2008}
2009
2010template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002011inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002012typename __unique_if<_Tp>::__unique_array_unknown_bound
2013make_unique(size_t __n)
2014{
2015 typedef typename remove_extent<_Tp>::type _Up;
2016 return unique_ptr<_Tp>(new _Up[__n]());
2017}
2018
2019template<class _Tp, class... _Args>
2020 typename __unique_if<_Tp>::__unique_array_known_bound
2021 make_unique(_Args&&...) = delete;
2022
2023#endif // _LIBCPP_STD_VER > 11
2024
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002025template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002026#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002027struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002028#else
2029struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002030 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002031#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002032{
2033 typedef unique_ptr<_Tp, _Dp> argument_type;
2034 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002035 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002036 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002037 {
2038 typedef typename argument_type::pointer pointer;
2039 return hash<pointer>()(__ptr.get());
2040 }
2041};
2042
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043struct __destruct_n
2044{
2045private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002046 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047
2048 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002049 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002050 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051
2052 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002053 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 {}
2055
Howard Hinnant719bda32011-05-28 14:41:13 +00002056 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002057 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002058 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 {}
2060
Howard Hinnant719bda32011-05-28 14:41:13 +00002061 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002062 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002063 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 {}
2065public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002066 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002067 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068
2069 template <class _Tp>
Bruce Mitchener170d8972020-11-24 12:53:53 -05002070 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002071 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
2073 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002074 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002075 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076
2077 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002078 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002079 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080};
2081
2082template <class _Alloc>
2083class __allocator_destructor
2084{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002085 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002087 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2088 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089private:
2090 _Alloc& __alloc_;
2091 size_type __s_;
2092public:
2093 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002094 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002097 void operator()(pointer __p) _NOEXCEPT
2098 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099};
2100
2101template <class _InputIterator, class _ForwardIterator>
2102_ForwardIterator
2103uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2104{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002106#ifndef _LIBCPP_NO_EXCEPTIONS
2107 _ForwardIterator __s = __r;
2108 try
2109 {
2110#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002111 for (; __f != __l; ++__f, (void) ++__r)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002112 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002113#ifndef _LIBCPP_NO_EXCEPTIONS
2114 }
2115 catch (...)
2116 {
2117 for (; __s != __r; ++__s)
2118 __s->~value_type();
2119 throw;
2120 }
2121#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122 return __r;
2123}
2124
2125template <class _InputIterator, class _Size, class _ForwardIterator>
2126_ForwardIterator
2127uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2128{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002130#ifndef _LIBCPP_NO_EXCEPTIONS
2131 _ForwardIterator __s = __r;
2132 try
2133 {
2134#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002135 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002136 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002137#ifndef _LIBCPP_NO_EXCEPTIONS
2138 }
2139 catch (...)
2140 {
2141 for (; __s != __r; ++__s)
2142 __s->~value_type();
2143 throw;
2144 }
2145#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 return __r;
2147}
2148
2149template <class _ForwardIterator, class _Tp>
2150void
2151uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2152{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002154#ifndef _LIBCPP_NO_EXCEPTIONS
2155 _ForwardIterator __s = __f;
2156 try
2157 {
2158#endif
2159 for (; __f != __l; ++__f)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002160 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002161#ifndef _LIBCPP_NO_EXCEPTIONS
2162 }
2163 catch (...)
2164 {
2165 for (; __s != __f; ++__s)
2166 __s->~value_type();
2167 throw;
2168 }
2169#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170}
2171
2172template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002173_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2175{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002177#ifndef _LIBCPP_NO_EXCEPTIONS
2178 _ForwardIterator __s = __f;
2179 try
2180 {
2181#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002182 for (; __n > 0; ++__f, (void) --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002183 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002184#ifndef _LIBCPP_NO_EXCEPTIONS
2185 }
2186 catch (...)
2187 {
2188 for (; __s != __f; ++__s)
2189 __s->~value_type();
2190 throw;
2191 }
2192#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002193 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194}
2195
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002196#if _LIBCPP_STD_VER > 14
2197
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002198template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002199inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002200void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2201 for (; __first != __last; ++__first)
2202 _VSTD::destroy_at(_VSTD::addressof(*__first));
2203}
2204
2205template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002206inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002207_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
2208 for (; __n > 0; (void)++__first, --__n)
2209 _VSTD::destroy_at(_VSTD::addressof(*__first));
2210 return __first;
2211}
2212
Eric Fiselier290c07c2016-10-11 21:13:44 +00002213template <class _ForwardIterator>
2214inline _LIBCPP_INLINE_VISIBILITY
2215void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
2216 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2217 auto __idx = __first;
2218#ifndef _LIBCPP_NO_EXCEPTIONS
2219 try {
2220#endif
2221 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002222 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002223#ifndef _LIBCPP_NO_EXCEPTIONS
2224 } catch (...) {
2225 _VSTD::destroy(__first, __idx);
2226 throw;
2227 }
2228#endif
2229}
2230
2231template <class _ForwardIterator, class _Size>
2232inline _LIBCPP_INLINE_VISIBILITY
2233_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
2234 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2235 auto __idx = __first;
2236#ifndef _LIBCPP_NO_EXCEPTIONS
2237 try {
2238#endif
2239 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002240 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
Eric Fiselier290c07c2016-10-11 21:13:44 +00002241 return __idx;
2242#ifndef _LIBCPP_NO_EXCEPTIONS
2243 } catch (...) {
2244 _VSTD::destroy(__first, __idx);
2245 throw;
2246 }
2247#endif
2248}
2249
2250
2251template <class _ForwardIterator>
2252inline _LIBCPP_INLINE_VISIBILITY
2253void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
2254 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2255 auto __idx = __first;
2256#ifndef _LIBCPP_NO_EXCEPTIONS
2257 try {
2258#endif
2259 for (; __idx != __last; ++__idx)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002260 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002261#ifndef _LIBCPP_NO_EXCEPTIONS
2262 } catch (...) {
2263 _VSTD::destroy(__first, __idx);
2264 throw;
2265 }
2266#endif
2267}
2268
2269template <class _ForwardIterator, class _Size>
2270inline _LIBCPP_INLINE_VISIBILITY
2271_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
2272 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
2273 auto __idx = __first;
2274#ifndef _LIBCPP_NO_EXCEPTIONS
2275 try {
2276#endif
2277 for (; __n > 0; (void)++__idx, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002278 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
Eric Fiselier290c07c2016-10-11 21:13:44 +00002279 return __idx;
2280#ifndef _LIBCPP_NO_EXCEPTIONS
2281 } catch (...) {
2282 _VSTD::destroy(__first, __idx);
2283 throw;
2284 }
2285#endif
2286}
2287
2288
2289template <class _InputIt, class _ForwardIt>
2290inline _LIBCPP_INLINE_VISIBILITY
2291_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
2292 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2293 auto __idx = __first_res;
2294#ifndef _LIBCPP_NO_EXCEPTIONS
2295 try {
2296#endif
2297 for (; __first != __last; (void)++__idx, ++__first)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002298 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002299 return __idx;
2300#ifndef _LIBCPP_NO_EXCEPTIONS
2301 } catch (...) {
2302 _VSTD::destroy(__first_res, __idx);
2303 throw;
2304 }
2305#endif
2306}
2307
2308template <class _InputIt, class _Size, class _ForwardIt>
2309inline _LIBCPP_INLINE_VISIBILITY
2310pair<_InputIt, _ForwardIt>
2311uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
2312 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
2313 auto __idx = __first_res;
2314#ifndef _LIBCPP_NO_EXCEPTIONS
2315 try {
2316#endif
2317 for (; __n > 0; ++__idx, (void)++__first, --__n)
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002318 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
Eric Fiselier290c07c2016-10-11 21:13:44 +00002319 return {__first, __idx};
2320#ifndef _LIBCPP_NO_EXCEPTIONS
2321 } catch (...) {
2322 _VSTD::destroy(__first_res, __idx);
2323 throw;
2324 }
2325#endif
2326}
2327
2328
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002329#endif // _LIBCPP_STD_VER > 14
2330
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002331// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2332// should be sufficient for thread safety.
Louis Dionne38437402021-03-03 13:45:06 -05002333// See https://llvm.org/PR22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002334#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
2335 && defined(__ATOMIC_RELAXED) \
2336 && defined(__ATOMIC_ACQ_REL)
2337# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00002338#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002339# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
2340#endif
2341
2342template <class _Tp>
2343inline _LIBCPP_INLINE_VISIBILITY _Tp
2344__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
2345{
2346#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2347 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
2348#else
2349 return __t += 1;
2350#endif
2351}
2352
2353template <class _Tp>
2354inline _LIBCPP_INLINE_VISIBILITY _Tp
2355__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
2356{
2357#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
2358 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
2359#else
2360 return __t -= 1;
2361#endif
2362}
2363
Howard Hinnant756c69b2010-09-22 16:48:34 +00002364class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 : public std::exception
2366{
2367public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01002368 bad_weak_ptr() _NOEXCEPT = default;
2369 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00002370 virtual ~bad_weak_ptr() _NOEXCEPT;
2371 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372};
2373
Louis Dionne16fe2952018-07-11 23:14:33 +00002374_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00002375void __throw_bad_weak_ptr()
2376{
2377#ifndef _LIBCPP_NO_EXCEPTIONS
2378 throw bad_weak_ptr();
2379#else
2380 _VSTD::abort();
2381#endif
2382}
2383
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002384template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002386class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387{
2388 __shared_count(const __shared_count&);
2389 __shared_count& operator=(const __shared_count&);
2390
2391protected:
2392 long __shared_owners_;
2393 virtual ~__shared_count();
2394private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002395 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396
2397public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002399 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 : __shared_owners_(__refs) {}
2401
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002402#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002403 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002404 void __add_shared() _NOEXCEPT;
2405 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002406#else
2407 _LIBCPP_INLINE_VISIBILITY
2408 void __add_shared() _NOEXCEPT {
2409 __libcpp_atomic_refcount_increment(__shared_owners_);
2410 }
2411 _LIBCPP_INLINE_VISIBILITY
2412 bool __release_shared() _NOEXCEPT {
2413 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
2414 __on_zero_shared();
2415 return true;
2416 }
2417 return false;
2418 }
2419#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00002420 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00002421 long use_count() const _NOEXCEPT {
2422 return __libcpp_relaxed_load(&__shared_owners_) + 1;
2423 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424};
2425
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002426class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427 : private __shared_count
2428{
2429 long __shared_weak_owners_;
2430
2431public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002433 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434 : __shared_count(__refs),
2435 __shared_weak_owners_(__refs) {}
2436protected:
2437 virtual ~__shared_weak_count();
2438
2439public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002440#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00002441 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00002442 void __add_shared() _NOEXCEPT;
2443 void __add_weak() _NOEXCEPT;
2444 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00002445#else
2446 _LIBCPP_INLINE_VISIBILITY
2447 void __add_shared() _NOEXCEPT {
2448 __shared_count::__add_shared();
2449 }
2450 _LIBCPP_INLINE_VISIBILITY
2451 void __add_weak() _NOEXCEPT {
2452 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
2453 }
2454 _LIBCPP_INLINE_VISIBILITY
2455 void __release_shared() _NOEXCEPT {
2456 if (__shared_count::__release_shared())
2457 __release_weak();
2458 }
2459#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00002460 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002462 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2463 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464
Howard Hinnant719bda32011-05-28 14:41:13 +00002465 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002467 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468};
2469
2470template <class _Tp, class _Dp, class _Alloc>
2471class __shared_ptr_pointer
2472 : public __shared_weak_count
2473{
2474 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2475public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00002476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002478 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479
Howard Hinnant72f73582010-08-11 17:04:31 +00002480#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00002481 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00002482#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483
2484private:
Howard Hinnant719bda32011-05-28 14:41:13 +00002485 virtual void __on_zero_shared() _NOEXCEPT;
2486 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487};
2488
Howard Hinnant72f73582010-08-11 17:04:31 +00002489#ifndef _LIBCPP_NO_RTTI
2490
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491template <class _Tp, class _Dp, class _Alloc>
2492const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00002493__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494{
Eric Fiselierc7490d02017-05-04 01:06:56 +00002495 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496}
2497
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002498#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002499
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500template <class _Tp, class _Dp, class _Alloc>
2501void
Howard Hinnant719bda32011-05-28 14:41:13 +00002502__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503{
2504 __data_.first().second()(__data_.first().first());
2505 __data_.first().second().~_Dp();
2506}
2507
2508template <class _Tp, class _Dp, class _Alloc>
2509void
Howard Hinnant719bda32011-05-28 14:41:13 +00002510__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511{
Eric Fiselierf8898c82015-02-05 23:01:40 +00002512 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
2513 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002514 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
2515
Eric Fiselierf8898c82015-02-05 23:01:40 +00002516 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00002518 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519}
2520
2521template <class _Tp, class _Alloc>
Louis Dionne86549d72020-12-09 16:22:17 -05002522struct __shared_ptr_emplace
2523 : __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Louis Dionneda463cb2020-12-11 12:22:16 -05002525 template<class ..._Args>
Louis Dionne86549d72020-12-09 16:22:17 -05002526 _LIBCPP_HIDE_FROM_ABI
2527 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Louis Dionneda463cb2020-12-11 12:22:16 -05002528 : __storage_(_VSTD::move(__a))
2529 {
2530#if _LIBCPP_STD_VER > 17
2531 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2532 _TpAlloc __tmp(*__get_alloc());
2533 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002534#else
Louis Dionneda463cb2020-12-11 12:22:16 -05002535 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04002536#endif
Louis Dionneda463cb2020-12-11 12:22:16 -05002537 }
Louis Dionne86549d72020-12-09 16:22:17 -05002538
2539 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002540 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
Louis Dionne86549d72020-12-09 16:22:17 -05002541
2542 _LIBCPP_HIDE_FROM_ABI
Louis Dionneda463cb2020-12-11 12:22:16 -05002543 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544
2545private:
Louis Dionne86549d72020-12-09 16:22:17 -05002546 virtual void __on_zero_shared() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002547#if _LIBCPP_STD_VER > 17
2548 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
2549 _TpAlloc __tmp(*__get_alloc());
2550 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
2551#else
Louis Dionne86549d72020-12-09 16:22:17 -05002552 __get_elem()->~_Tp();
Louis Dionneda463cb2020-12-11 12:22:16 -05002553#endif
Louis Dionne86549d72020-12-09 16:22:17 -05002554 }
2555
2556 virtual void __on_zero_shared_weak() _NOEXCEPT {
2557 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
2558 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
Louis Dionneca117a22020-12-14 17:40:56 -05002559 _ControlBlockAlloc __tmp(*__get_alloc());
Louis Dionneda463cb2020-12-11 12:22:16 -05002560 __storage_.~_Storage();
Louis Dionne86549d72020-12-09 16:22:17 -05002561 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
2562 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
2563 }
2564
Louis Dionneda463cb2020-12-11 12:22:16 -05002565 // This class implements the control block for non-array shared pointers created
2566 // through `std::allocate_shared` and `std::make_shared`.
2567 //
2568 // In previous versions of the library, we used a compressed pair to store
2569 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
2570 // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
2571 // we now use a properly aligned char buffer while making sure that we maintain
2572 // the same layout that we had when we used a compressed pair.
2573 using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
2574 struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
2575 char __blob_[sizeof(_CompressedPair)];
2576
2577 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
2578 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
2579 }
2580 _LIBCPP_HIDE_FROM_ABI ~_Storage() {
2581 __get_alloc()->~_Alloc();
2582 }
2583 _Alloc* __get_alloc() _NOEXCEPT {
2584 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2585 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
2586 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
2587 return __alloc;
2588 }
Reid Klecknera43e87e2021-02-01 15:18:42 -08002589 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
Louis Dionneda463cb2020-12-11 12:22:16 -05002590 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
2591 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
2592 _Tp *__elem = reinterpret_cast<_Tp*>(__second);
2593 return __elem;
2594 }
2595 };
2596
2597 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
2598 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
2599 _Storage __storage_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600};
2601
Erik Pilkington2a398762017-05-25 15:43:31 +00002602struct __shared_ptr_dummy_rebind_allocator_type;
2603template <>
2604class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
2605{
2606public:
2607 template <class _Other>
2608 struct rebind
2609 {
2610 typedef allocator<_Other> other;
2611 };
2612};
2613
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002614template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615
zoecarverd73f42a2020-05-11 18:42:50 -07002616template<class _Tp, class _Up>
2617struct __compatible_with
2618#if _LIBCPP_STD_VER > 14
2619 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
2620#else
2621 : is_convertible<_Tp*, _Up*> {};
2622#endif // _LIBCPP_STD_VER > 14
2623
zoecarver9bc39102021-02-19 11:10:36 -08002624template <class _Dp, class _Pt,
2625 class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
2626static true_type __well_formed_deleter_test(int);
2627
2628template <class, class>
2629static false_type __well_formed_deleter_test(...);
2630
2631template <class _Dp, class _Pt>
2632struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
2633
2634template<class _Dp, class _Tp, class _Yp>
2635struct __shared_ptr_deleter_ctor_reqs
2636{
2637 static const bool value = __compatible_with<_Tp, _Yp>::value &&
2638 is_move_constructible<_Dp>::value &&
2639 __well_formed_deleter<_Dp, _Tp*>::value;
2640};
2641
Vy Nguyene369bd92020-07-13 12:34:37 -04002642#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
2643# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2644#else
2645# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
2646#endif
2647
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002649class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002651public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00002652#if _LIBCPP_STD_VER > 14
2653 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07002654 typedef remove_extent_t<_Tp> element_type;
2655#else
2656 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00002657#endif
zoecarverd73f42a2020-05-11 18:42:50 -07002658
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659private:
2660 element_type* __ptr_;
2661 __shared_weak_count* __cntrl_;
2662
2663 struct __nat {int __for_bool_;};
2664public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002666 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002668 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00002669 template<class _Yp>
2670 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002671 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002672 template<class _Yp, class _Dp>
2673 shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002674 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00002675 template<class _Yp, class _Dp, class _Alloc>
2676 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002677 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2679 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002680 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002682 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002686 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002687 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002689 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002690 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07002691 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002692 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00002694 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002695#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00002696 template<class _Yp>
2697 shared_ptr(auto_ptr<_Yp>&& __r,
2698 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00002699#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00002700 template <class _Yp, class _Dp>
2701 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2702 typename enable_if
2703 <
2704 !is_lvalue_reference<_Dp>::value &&
2705 !is_array<_Yp>::value &&
2706 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2707 __nat
2708 >::type = __nat());
2709 template <class _Yp, class _Dp>
2710 shared_ptr(unique_ptr<_Yp, _Dp>&&,
2711 typename enable_if
2712 <
2713 is_lvalue_reference<_Dp>::value &&
2714 !is_array<_Yp>::value &&
2715 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2716 __nat
2717 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718
2719 ~shared_ptr();
2720
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002722 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002723 template<class _Yp>
2724 typename enable_if
2725 <
zoecarverd73f42a2020-05-11 18:42:50 -07002726 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002727 shared_ptr&
2728 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002730 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002732 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002733 template<class _Yp>
2734 typename enable_if
2735 <
zoecarverd73f42a2020-05-11 18:42:50 -07002736 __compatible_with<_Yp, element_type>::value,
2737 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002738 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002740 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002741#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002742 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00002743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002744 typename enable_if
2745 <
2746 !is_array<_Yp>::value &&
2747 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00002748 shared_ptr
2749 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002750 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00002751#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002752 template <class _Yp, class _Dp>
2753 typename enable_if
2754 <
2755 !is_array<_Yp>::value &&
2756 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
2757 shared_ptr&
2758 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002760 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002763 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002765 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002766 template<class _Yp>
2767 typename enable_if
2768 <
zoecarverd73f42a2020-05-11 18:42:50 -07002769 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002770 void
2771 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002773 reset(_Yp* __p);
2774 template<class _Yp, class _Dp>
2775 typename enable_if
2776 <
zoecarverd73f42a2020-05-11 18:42:50 -07002777 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002778 void
2779 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002781 reset(_Yp* __p, _Dp __d);
2782 template<class _Yp, class _Dp, class _Alloc>
2783 typename enable_if
2784 <
zoecarverd73f42a2020-05-11 18:42:50 -07002785 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002786 void
2787 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002789 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790
Howard Hinnant756c69b2010-09-22 16:48:34 +00002791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002792 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002794 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2795 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002796 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07002797 element_type* operator->() const _NOEXCEPT
2798 {
2799 static_assert(!_VSTD::is_array<_Tp>::value,
2800 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
2801 return __ptr_;
2802 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00002803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002804 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002806 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002807 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05002808 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002809 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002810 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002811 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00002813 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002814 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00002815 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00002817 _LIBCPP_INLINE_VISIBILITY
2818 bool
2819 __owner_equivalent(const shared_ptr& __p) const
2820 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821
zoecarverd73f42a2020-05-11 18:42:50 -07002822#if _LIBCPP_STD_VER > 14
2823 typename add_lvalue_reference<element_type>::type
2824 _LIBCPP_INLINE_VISIBILITY
2825 operator[](ptrdiff_t __i) const
2826 {
2827 static_assert(_VSTD::is_array<_Tp>::value,
2828 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
2829 return __ptr_[__i];
2830 }
2831#endif
2832
Howard Hinnant72f73582010-08-11 17:04:31 +00002833#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002836 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00002837 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00002838 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00002839 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002840#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841
Zoe Carverd9040c72019-10-22 15:16:49 +00002842 template<class _Yp, class _CntrlBlk>
2843 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07002844 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00002845 {
2846 shared_ptr<_Tp> __r;
2847 __r.__ptr_ = __p;
2848 __r.__cntrl_ = __cntrl;
2849 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
2850 return __r;
2851 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00002852
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853private:
Erik Pilkington2a398762017-05-25 15:43:31 +00002854 template <class _Yp, bool = is_function<_Yp>::value>
2855 struct __shared_ptr_default_allocator
2856 {
2857 typedef allocator<_Yp> type;
2858 };
2859
2860 template <class _Yp>
2861 struct __shared_ptr_default_allocator<_Yp, true>
2862 {
2863 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
2864 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002866 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00002867 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002868 typename enable_if<is_convertible<_OrigPtr*,
2869 const enable_shared_from_this<_Yp>*
2870 >::value,
2871 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002872 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
2873 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002875 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00002876 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00002877 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002878 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
2879 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00002880 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 }
2882
Erik Pilkington2a398762017-05-25 15:43:31 +00002883 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884
zoecarverd73f42a2020-05-11 18:42:50 -07002885 template <class, class _Yp>
2886 struct __shared_ptr_default_delete
2887 : default_delete<_Yp> {};
2888
2889 template <class _Yp, class _Un, size_t _Sz>
2890 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
2891 : default_delete<_Yp[]> {};
2892
2893 template <class _Yp, class _Un>
2894 struct __shared_ptr_default_delete<_Yp[], _Un>
2895 : default_delete<_Yp[]> {};
2896
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002897 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2898 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899};
2900
Logan Smitha5e4d7e2020-05-07 12:07:01 -04002901#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2902template<class _Tp>
2903shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
2904template<class _Tp, class _Dp>
2905shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
2906#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00002907
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002909inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002910_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002911shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002912 : __ptr_(nullptr),
2913 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914{
2915}
2916
2917template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002918inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002919_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00002920shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05002921 : __ptr_(nullptr),
2922 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923{
2924}
2925
2926template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002927template<class _Yp>
2928shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07002929 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 : __ptr_(__p)
2931{
2932 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00002933 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07002934 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
2935 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002937 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938}
2939
2940template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002941template<class _Yp, class _Dp>
2942shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarver9bc39102021-02-19 11:10:36 -08002943 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 : __ptr_(__p)
2945{
2946#ifndef _LIBCPP_NO_EXCEPTIONS
2947 try
2948 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002949#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002950 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
2951 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002952#ifndef _LIBCPP_CXX03_LANG
2953 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2954#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002955 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002956#endif // not _LIBCPP_CXX03_LANG
Eric Fiselierf16c93f2016-06-26 23:56:32 +00002957 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958#ifndef _LIBCPP_NO_EXCEPTIONS
2959 }
2960 catch (...)
2961 {
2962 __d(__p);
2963 throw;
2964 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002965#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966}
2967
2968template<class _Tp>
2969template<class _Dp>
2970shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
Bruce Mitchener170d8972020-11-24 12:53:53 -05002971 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972{
2973#ifndef _LIBCPP_NO_EXCEPTIONS
2974 try
2975 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002976#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00002977 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
2978 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
zoecarverbec93cf2021-02-18 21:31:07 -08002979#ifndef _LIBCPP_CXX03_LANG
2980 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
2981#else
Erik Pilkington2a398762017-05-25 15:43:31 +00002982 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
zoecarverbec93cf2021-02-18 21:31:07 -08002983#endif // not _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984#ifndef _LIBCPP_NO_EXCEPTIONS
2985 }
2986 catch (...)
2987 {
2988 __d(__p);
2989 throw;
2990 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002991#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992}
2993
2994template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00002995template<class _Yp, class _Dp, class _Alloc>
2996shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarver9bc39102021-02-19 11:10:36 -08002997 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 : __ptr_(__p)
2999{
3000#ifndef _LIBCPP_NO_EXCEPTIONS
3001 try
3002 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003003#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003005 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 typedef __allocator_destructor<_A2> _D2;
3007 _A2 __a2(__a);
3008 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08003009 ::new ((void*)_VSTD::addressof(*__hold2.get()))
3010#ifndef _LIBCPP_CXX03_LANG
3011 _CntrlBlk(__p, _VSTD::move(__d), __a);
3012#else
3013 _CntrlBlk(__p, __d, __a);
3014#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003015 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003016 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017#ifndef _LIBCPP_NO_EXCEPTIONS
3018 }
3019 catch (...)
3020 {
3021 __d(__p);
3022 throw;
3023 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003024#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025}
3026
3027template<class _Tp>
3028template<class _Dp, class _Alloc>
3029shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05003030 : __ptr_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003031{
3032#ifndef _LIBCPP_NO_EXCEPTIONS
3033 try
3034 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003035#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003037 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038 typedef __allocator_destructor<_A2> _D2;
3039 _A2 __a2(__a);
3040 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
zoecarverbec93cf2021-02-18 21:31:07 -08003041 ::new ((void*)_VSTD::addressof(*__hold2.get()))
3042#ifndef _LIBCPP_CXX03_LANG
3043 _CntrlBlk(__p, _VSTD::move(__d), __a);
3044#else
3045 _CntrlBlk(__p, __d, __a);
3046#endif // not _LIBCPP_CXX03_LANG
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003047 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048#ifndef _LIBCPP_NO_EXCEPTIONS
3049 }
3050 catch (...)
3051 {
3052 __d(__p);
3053 throw;
3054 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003055#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056}
3057
3058template<class _Tp>
3059template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003060inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003061shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 : __ptr_(__p),
3063 __cntrl_(__r.__cntrl_)
3064{
3065 if (__cntrl_)
3066 __cntrl_->__add_shared();
3067}
3068
3069template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003070inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003071shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072 : __ptr_(__r.__ptr_),
3073 __cntrl_(__r.__cntrl_)
3074{
3075 if (__cntrl_)
3076 __cntrl_->__add_shared();
3077}
3078
3079template<class _Tp>
3080template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003081inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003083 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003084 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 : __ptr_(__r.__ptr_),
3086 __cntrl_(__r.__cntrl_)
3087{
3088 if (__cntrl_)
3089 __cntrl_->__add_shared();
3090}
3091
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003093inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003094shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095 : __ptr_(__r.__ptr_),
3096 __cntrl_(__r.__cntrl_)
3097{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003098 __r.__ptr_ = nullptr;
3099 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100}
3101
3102template<class _Tp>
3103template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003104inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003106 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003107 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 : __ptr_(__r.__ptr_),
3109 __cntrl_(__r.__cntrl_)
3110{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003111 __r.__ptr_ = nullptr;
3112 __r.__cntrl_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113}
3114
Marshall Clowb22274f2017-01-24 22:22:33 +00003115#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003117template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003118shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003119 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 : __ptr_(__r.get())
3121{
3122 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3123 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003124 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 __r.release();
3126}
Marshall Clowb22274f2017-01-24 22:22:33 +00003127#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128
3129template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003130template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003132 typename enable_if
3133 <
3134 !is_lvalue_reference<_Dp>::value &&
3135 !is_array<_Yp>::value &&
3136 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3137 __nat
3138 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 : __ptr_(__r.get())
3140{
Marshall Clow35cde742015-05-10 13:59:45 +00003141#if _LIBCPP_STD_VER > 11
3142 if (__ptr_ == nullptr)
3143 __cntrl_ = nullptr;
3144 else
3145#endif
3146 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003147 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3148 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3149 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003150 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003151 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152 __r.release();
3153}
3154
3155template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003156template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003158 typename enable_if
3159 <
3160 is_lvalue_reference<_Dp>::value &&
3161 !is_array<_Yp>::value &&
3162 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3163 __nat
3164 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 : __ptr_(__r.get())
3166{
Marshall Clow35cde742015-05-10 13:59:45 +00003167#if _LIBCPP_STD_VER > 11
3168 if (__ptr_ == nullptr)
3169 __cntrl_ = nullptr;
3170 else
3171#endif
3172 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003173 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003174 typedef __shared_ptr_pointer<_Yp*,
3175 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003176 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003177 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003178 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003179 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180 __r.release();
3181}
3182
Zoe Carver6cd05c32019-08-19 15:47:16 +00003183template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184shared_ptr<_Tp>::~shared_ptr()
3185{
3186 if (__cntrl_)
3187 __cntrl_->__release_shared();
3188}
3189
3190template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003191inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003193shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194{
3195 shared_ptr(__r).swap(*this);
3196 return *this;
3197}
3198
3199template<class _Tp>
3200template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003201inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003202typename enable_if
3203<
zoecarverd73f42a2020-05-11 18:42:50 -07003204 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003205 shared_ptr<_Tp>&
3206>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003207shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208{
3209 shared_ptr(__r).swap(*this);
3210 return *this;
3211}
3212
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003216shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003218 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219 return *this;
3220}
3221
3222template<class _Tp>
3223template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003224inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003225typename enable_if
3226<
zoecarverd73f42a2020-05-11 18:42:50 -07003227 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003228 shared_ptr<_Tp>&
3229>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3231{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003232 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233 return *this;
3234}
3235
Marshall Clowb22274f2017-01-24 22:22:33 +00003236#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003237template<class _Tp>
3238template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003239inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003240typename enable_if
3241<
3242 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003243 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003244 shared_ptr<_Tp>
3245>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3247{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003248 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249 return *this;
3250}
Marshall Clowb22274f2017-01-24 22:22:33 +00003251#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252
3253template<class _Tp>
3254template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003255inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003256typename enable_if
3257<
3258 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003259 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003260 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003261 shared_ptr<_Tp>&
3262>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3264{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003265 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266 return *this;
3267}
3268
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003270inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271void
Howard Hinnant719bda32011-05-28 14:41:13 +00003272shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003273{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003274 _VSTD::swap(__ptr_, __r.__ptr_);
3275 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276}
3277
3278template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003279inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280void
Howard Hinnant719bda32011-05-28 14:41:13 +00003281shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282{
3283 shared_ptr().swap(*this);
3284}
3285
3286template<class _Tp>
3287template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003288inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003289typename enable_if
3290<
zoecarverd73f42a2020-05-11 18:42:50 -07003291 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003292 void
3293>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294shared_ptr<_Tp>::reset(_Yp* __p)
3295{
3296 shared_ptr(__p).swap(*this);
3297}
3298
3299template<class _Tp>
3300template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003301inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003302typename enable_if
3303<
zoecarverd73f42a2020-05-11 18:42:50 -07003304 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003305 void
3306>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3308{
3309 shared_ptr(__p, __d).swap(*this);
3310}
3311
3312template<class _Tp>
3313template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003314inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003315typename enable_if
3316<
zoecarverd73f42a2020-05-11 18:42:50 -07003317 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003318 void
3319>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3321{
3322 shared_ptr(__p, __d, __a).swap(*this);
3323}
3324
Louis Dionne74aef9f2020-12-11 12:20:06 -05003325//
3326// std::allocate_shared and std::make_shared
3327//
3328template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3329_LIBCPP_HIDE_FROM_ABI
3330shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331{
Louis Dionne74aef9f2020-12-11 12:20:06 -05003332 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
3333 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
3334 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
3335 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
3336 auto __control_block = __guard.__release_ptr();
3337 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338}
3339
Louis Dionne43c9f8f2020-12-09 16:57:28 -05003340template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
3341_LIBCPP_HIDE_FROM_ABI
3342shared_ptr<_Tp> make_shared(_Args&& ...__args)
3343{
3344 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
3345}
3346
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347template<class _Tp, class _Up>
3348inline _LIBCPP_INLINE_VISIBILITY
3349bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003350operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351{
3352 return __x.get() == __y.get();
3353}
3354
3355template<class _Tp, class _Up>
3356inline _LIBCPP_INLINE_VISIBILITY
3357bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003358operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359{
3360 return !(__x == __y);
3361}
3362
3363template<class _Tp, class _Up>
3364inline _LIBCPP_INLINE_VISIBILITY
3365bool
Howard Hinnant719bda32011-05-28 14:41:13 +00003366operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003368#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00003369 typedef typename common_type<_Tp*, _Up*>::type _Vp;
3370 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00003371#else
3372 return less<>()(__x.get(), __y.get());
3373#endif
3374
Howard Hinnantb17caf92012-02-21 21:02:58 +00003375}
3376
3377template<class _Tp, class _Up>
3378inline _LIBCPP_INLINE_VISIBILITY
3379bool
3380operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3381{
3382 return __y < __x;
3383}
3384
3385template<class _Tp, class _Up>
3386inline _LIBCPP_INLINE_VISIBILITY
3387bool
3388operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3389{
3390 return !(__y < __x);
3391}
3392
3393template<class _Tp, class _Up>
3394inline _LIBCPP_INLINE_VISIBILITY
3395bool
3396operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
3397{
3398 return !(__x < __y);
3399}
3400
3401template<class _Tp>
3402inline _LIBCPP_INLINE_VISIBILITY
3403bool
3404operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3405{
3406 return !__x;
3407}
3408
3409template<class _Tp>
3410inline _LIBCPP_INLINE_VISIBILITY
3411bool
3412operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3413{
3414 return !__x;
3415}
3416
3417template<class _Tp>
3418inline _LIBCPP_INLINE_VISIBILITY
3419bool
3420operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3421{
3422 return static_cast<bool>(__x);
3423}
3424
3425template<class _Tp>
3426inline _LIBCPP_INLINE_VISIBILITY
3427bool
3428operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3429{
3430 return static_cast<bool>(__x);
3431}
3432
3433template<class _Tp>
3434inline _LIBCPP_INLINE_VISIBILITY
3435bool
3436operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3437{
3438 return less<_Tp*>()(__x.get(), nullptr);
3439}
3440
3441template<class _Tp>
3442inline _LIBCPP_INLINE_VISIBILITY
3443bool
3444operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3445{
3446 return less<_Tp*>()(nullptr, __x.get());
3447}
3448
3449template<class _Tp>
3450inline _LIBCPP_INLINE_VISIBILITY
3451bool
3452operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3453{
3454 return nullptr < __x;
3455}
3456
3457template<class _Tp>
3458inline _LIBCPP_INLINE_VISIBILITY
3459bool
3460operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3461{
3462 return __x < nullptr;
3463}
3464
3465template<class _Tp>
3466inline _LIBCPP_INLINE_VISIBILITY
3467bool
3468operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3469{
3470 return !(nullptr < __x);
3471}
3472
3473template<class _Tp>
3474inline _LIBCPP_INLINE_VISIBILITY
3475bool
3476operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3477{
3478 return !(__x < nullptr);
3479}
3480
3481template<class _Tp>
3482inline _LIBCPP_INLINE_VISIBILITY
3483bool
3484operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
3485{
3486 return !(__x < nullptr);
3487}
3488
3489template<class _Tp>
3490inline _LIBCPP_INLINE_VISIBILITY
3491bool
3492operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
3493{
3494 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495}
3496
3497template<class _Tp>
3498inline _LIBCPP_INLINE_VISIBILITY
3499void
Howard Hinnant719bda32011-05-28 14:41:13 +00003500swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
3502 __x.swap(__y);
3503}
3504
3505template<class _Tp, class _Up>
3506inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003507shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003508static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509{
zoecarverd73f42a2020-05-11 18:42:50 -07003510 return shared_ptr<_Tp>(__r,
3511 static_cast<
3512 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513}
3514
3515template<class _Tp, class _Up>
3516inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003517shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003518dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519{
zoecarverd73f42a2020-05-11 18:42:50 -07003520 typedef typename shared_ptr<_Tp>::element_type _ET;
3521 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3523}
3524
3525template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07003526shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003527const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528{
zoecarverd73f42a2020-05-11 18:42:50 -07003529 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003530 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531}
3532
zoecarverd73f42a2020-05-11 18:42:50 -07003533template<class _Tp, class _Up>
3534shared_ptr<_Tp>
3535reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
3536{
3537 return shared_ptr<_Tp>(__r,
3538 reinterpret_cast<
3539 typename shared_ptr<_Tp>::element_type*>(__r.get()));
3540}
3541
Howard Hinnant72f73582010-08-11 17:04:31 +00003542#ifndef _LIBCPP_NO_RTTI
3543
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544template<class _Dp, class _Tp>
3545inline _LIBCPP_INLINE_VISIBILITY
3546_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00003547get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548{
3549 return __p.template __get_deleter<_Dp>();
3550}
3551
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003552#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003553
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003555class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003557public:
3558 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559private:
3560 element_type* __ptr_;
3561 __shared_weak_count* __cntrl_;
3562
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003563public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003565 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003566 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003567 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3568 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003570 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003571 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003572 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3573 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003574
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003576 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003577 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003578 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3579 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003580 ~weak_ptr();
3581
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003583 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003584 template<class _Yp>
3585 typename enable_if
3586 <
3587 is_convertible<_Yp*, element_type*>::value,
3588 weak_ptr&
3589 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003591 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3592
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003594 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
3595 template<class _Yp>
3596 typename enable_if
3597 <
3598 is_convertible<_Yp*, element_type*>::value,
3599 weak_ptr&
3600 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003602 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
3603
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003604 template<class _Yp>
3605 typename enable_if
3606 <
3607 is_convertible<_Yp*, element_type*>::value,
3608 weak_ptr&
3609 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003611 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003612
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003614 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003616 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003617
Howard Hinnant756c69b2010-09-22 16:48:34 +00003618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003619 long use_count() const _NOEXCEPT
3620 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003622 bool expired() const _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003623 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003624 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003625 template<class _Up>
3626 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003627 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003629 template<class _Up>
3630 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003631 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632 {return __cntrl_ < __r.__cntrl_;}
3633
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003634 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
3635 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636};
3637
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003638#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3639template<class _Tp>
3640weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
3641#endif
3642
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003644inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003645_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003646weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05003647 : __ptr_(nullptr),
3648 __cntrl_(nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
3650}
3651
3652template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003653inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003654weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655 : __ptr_(__r.__ptr_),
3656 __cntrl_(__r.__cntrl_)
3657{
3658 if (__cntrl_)
3659 __cntrl_->__add_weak();
3660}
3661
3662template<class _Tp>
3663template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003666 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003667 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668 : __ptr_(__r.__ptr_),
3669 __cntrl_(__r.__cntrl_)
3670{
3671 if (__cntrl_)
3672 __cntrl_->__add_weak();
3673}
3674
3675template<class _Tp>
3676template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003677inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00003679 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003680 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681 : __ptr_(__r.__ptr_),
3682 __cntrl_(__r.__cntrl_)
3683{
3684 if (__cntrl_)
3685 __cntrl_->__add_weak();
3686}
3687
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003688template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003689inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003690weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
3691 : __ptr_(__r.__ptr_),
3692 __cntrl_(__r.__cntrl_)
3693{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003694 __r.__ptr_ = nullptr;
3695 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003696}
3697
3698template<class _Tp>
3699template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003700inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003701weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
3702 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
3703 _NOEXCEPT
3704 : __ptr_(__r.__ptr_),
3705 __cntrl_(__r.__cntrl_)
3706{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003707 __r.__ptr_ = nullptr;
3708 __r.__cntrl_ = nullptr;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003709}
3710
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711template<class _Tp>
3712weak_ptr<_Tp>::~weak_ptr()
3713{
3714 if (__cntrl_)
3715 __cntrl_->__release_weak();
3716}
3717
3718template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003719inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003721weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722{
3723 weak_ptr(__r).swap(*this);
3724 return *this;
3725}
3726
3727template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003728template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003729inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003730typename enable_if
3731<
3732 is_convertible<_Yp*, _Tp*>::value,
3733 weak_ptr<_Tp>&
3734>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003735weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736{
3737 weak_ptr(__r).swap(*this);
3738 return *this;
3739}
3740
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003741template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003742inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003743weak_ptr<_Tp>&
3744weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
3745{
3746 weak_ptr(_VSTD::move(__r)).swap(*this);
3747 return *this;
3748}
3749
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003751template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003752inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003753typename enable_if
3754<
3755 is_convertible<_Yp*, _Tp*>::value,
3756 weak_ptr<_Tp>&
3757>::type
3758weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
3759{
3760 weak_ptr(_VSTD::move(__r)).swap(*this);
3761 return *this;
3762}
3763
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003764template<class _Tp>
3765template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003766inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003767typename enable_if
3768<
3769 is_convertible<_Yp*, _Tp*>::value,
3770 weak_ptr<_Tp>&
3771>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003772weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773{
3774 weak_ptr(__r).swap(*this);
3775 return *this;
3776}
3777
3778template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003779inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780void
Howard Hinnant719bda32011-05-28 14:41:13 +00003781weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003783 _VSTD::swap(__ptr_, __r.__ptr_);
3784 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785}
3786
3787template<class _Tp>
3788inline _LIBCPP_INLINE_VISIBILITY
3789void
Howard Hinnant719bda32011-05-28 14:41:13 +00003790swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791{
3792 __x.swap(__y);
3793}
3794
3795template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003796inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797void
Howard Hinnant719bda32011-05-28 14:41:13 +00003798weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799{
3800 weak_ptr().swap(*this);
3801}
3802
3803template<class _Tp>
3804template<class _Yp>
3805shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003806 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807 : __ptr_(__r.__ptr_),
3808 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3809{
Bruce Mitchener170d8972020-11-24 12:53:53 -05003810 if (__cntrl_ == nullptr)
Marshall Clow8fea1612016-08-25 15:09:01 +00003811 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812}
3813
3814template<class _Tp>
3815shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003816weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817{
3818 shared_ptr<_Tp> __r;
3819 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3820 if (__r.__cntrl_)
3821 __r.__ptr_ = __ptr_;
3822 return __r;
3823}
3824
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003825#if _LIBCPP_STD_VER > 14
3826template <class _Tp = void> struct owner_less;
3827#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003828template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003829#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830
3831template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003832struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003834{
3835 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003837 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003839 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003840 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003842 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003843 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003845};
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846
3847template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003848struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3850{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003851 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003852 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003853 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003855 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003856 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003858 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003859 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860 {return __x.owner_before(__y);}
3861};
3862
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003863#if _LIBCPP_STD_VER > 14
3864template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003865struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003866{
3867 template <class _Tp, class _Up>
3868 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003869 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003870 {return __x.owner_before(__y);}
3871 template <class _Tp, class _Up>
3872 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003873 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003874 {return __x.owner_before(__y);}
3875 template <class _Tp, class _Up>
3876 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003877 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003878 {return __x.owner_before(__y);}
3879 template <class _Tp, class _Up>
3880 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003881 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00003882 {return __x.owner_before(__y);}
3883 typedef void is_transparent;
3884};
3885#endif
3886
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003888class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889{
3890 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003891protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003892 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003893 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003895 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003897 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3898 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003901public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003903 shared_ptr<_Tp> shared_from_this()
3904 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003906 shared_ptr<_Tp const> shared_from_this() const
3907 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908
Eric Fiselier84006862016-06-02 00:15:35 +00003909#if _LIBCPP_STD_VER > 14
3910 _LIBCPP_INLINE_VISIBILITY
3911 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
3912 { return __weak_this_; }
3913
3914 _LIBCPP_INLINE_VISIBILITY
3915 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
3916 { return __weak_this_; }
3917#endif // _LIBCPP_STD_VER > 14
3918
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919 template <class _Up> friend class shared_ptr;
3920};
3921
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003922template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003923struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003924{
3925 typedef shared_ptr<_Tp> argument_type;
3926 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00003927
Howard Hinnant756c69b2010-09-22 16:48:34 +00003928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003929 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003930 {
zoecarverd73f42a2020-05-11 18:42:50 -07003931 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003932 }
3933};
3934
Howard Hinnantc834c512011-11-29 18:15:50 +00003935template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00003936inline _LIBCPP_INLINE_VISIBILITY
3937basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00003938operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00003939
Eric Fiselier9b492672016-06-18 02:12:53 +00003940
3941#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00003942
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003943class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00003944{
Howard Hinnant49e145e2012-10-30 19:06:59 +00003945 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00003946public:
3947 void lock() _NOEXCEPT;
3948 void unlock() _NOEXCEPT;
3949
3950private:
3951 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
3952 __sp_mut(const __sp_mut&);
3953 __sp_mut& operator=(const __sp_mut&);
3954
Howard Hinnant8331b762013-03-06 23:30:19 +00003955 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003956};
3957
Mehdi Amini228053d2017-05-04 17:08:54 +00003958_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
3959__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00003960
3961template <class _Tp>
3962inline _LIBCPP_INLINE_VISIBILITY
3963bool
3964atomic_is_lock_free(const shared_ptr<_Tp>*)
3965{
3966 return false;
3967}
3968
3969template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003970_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003971shared_ptr<_Tp>
3972atomic_load(const shared_ptr<_Tp>* __p)
3973{
3974 __sp_mut& __m = __get_sp_mut(__p);
3975 __m.lock();
3976 shared_ptr<_Tp> __q = *__p;
3977 __m.unlock();
3978 return __q;
3979}
Aditya Kumar7c5db692017-08-20 10:38:55 +00003980
Howard Hinnant9fa30202012-07-30 01:40:57 +00003981template <class _Tp>
3982inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00003983_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003984shared_ptr<_Tp>
3985atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
3986{
3987 return atomic_load(__p);
3988}
3989
3990template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00003991_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00003992void
3993atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
3994{
3995 __sp_mut& __m = __get_sp_mut(__p);
3996 __m.lock();
3997 __p->swap(__r);
3998 __m.unlock();
3999}
4000
4001template <class _Tp>
4002inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004003_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004004void
4005atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4006{
4007 atomic_store(__p, __r);
4008}
4009
4010template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004011_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004012shared_ptr<_Tp>
4013atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4014{
4015 __sp_mut& __m = __get_sp_mut(__p);
4016 __m.lock();
4017 __p->swap(__r);
4018 __m.unlock();
4019 return __r;
4020}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004021
Howard Hinnant9fa30202012-07-30 01:40:57 +00004022template <class _Tp>
4023inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004024_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004025shared_ptr<_Tp>
4026atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4027{
4028 return atomic_exchange(__p, __r);
4029}
4030
4031template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004032_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004033bool
4034atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4035{
Marshall Clow4201ee82016-05-18 17:50:13 +00004036 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004037 __sp_mut& __m = __get_sp_mut(__p);
4038 __m.lock();
4039 if (__p->__owner_equivalent(*__v))
4040 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004041 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004042 *__p = __w;
4043 __m.unlock();
4044 return true;
4045 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004046 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004047 *__v = *__p;
4048 __m.unlock();
4049 return false;
4050}
4051
4052template <class _Tp>
4053inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004054_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004055bool
4056atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4057{
4058 return atomic_compare_exchange_strong(__p, __v, __w);
4059}
4060
4061template <class _Tp>
4062inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004063_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004064bool
4065atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4066 shared_ptr<_Tp> __w, memory_order, memory_order)
4067{
4068 return atomic_compare_exchange_strong(__p, __v, __w);
4069}
4070
4071template <class _Tp>
4072inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004073_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004074bool
4075atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4076 shared_ptr<_Tp> __w, memory_order, memory_order)
4077{
4078 return atomic_compare_exchange_weak(__p, __v, __w);
4079}
4080
Eric Fiselier9b492672016-06-18 02:12:53 +00004081#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004082
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004083//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004084#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4085# ifndef _LIBCPP_CXX03_LANG
4086enum class pointer_safety : unsigned char {
4087 relaxed,
4088 preferred,
4089 strict
4090};
4091# endif
4092#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004093struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004095 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096 {
4097 relaxed,
4098 preferred,
4099 strict
4100 };
4101
Howard Hinnant49e145e2012-10-30 19:06:59 +00004102 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103
Howard Hinnant756c69b2010-09-22 16:48:34 +00004104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004105 pointer_safety() : __v_() {}
4106
4107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004108 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110 operator int() const {return __v_;}
4111};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004112#endif
4113
4114#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004115 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004116_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4117#else
4118// This function is only offered in C++03 under ABI v1.
4119# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4120inline _LIBCPP_INLINE_VISIBILITY
4121pointer_safety get_pointer_safety() _NOEXCEPT {
4122 return pointer_safety::relaxed;
4123}
4124# endif
4125#endif
4126
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004128_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4129_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4130_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004131_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132
4133template <class _Tp>
4134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004135_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136undeclare_reachable(_Tp* __p)
4137{
4138 return static_cast<_Tp*>(__undeclare_reachable(__p));
4139}
4140
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004141_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142
Marshall Clow8982dcd2015-07-13 20:04:56 +00004143// --- Helper for container swap --
4144template <typename _Alloc>
Marshall Clow8982dcd2015-07-13 20:04:56 +00004145_LIBCPP_INLINE_VISIBILITY
4146void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4147#if _LIBCPP_STD_VER >= 14
4148 _NOEXCEPT
4149#else
4150 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4151#endif
4152{
4153 using _VSTD::swap;
4154 swap(__a1, __a2);
4155}
4156
4157template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004158inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004159void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4160
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05004161template <typename _Alloc>
4162inline _LIBCPP_INLINE_VISIBILITY
4163void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4164#if _LIBCPP_STD_VER >= 14
4165 _NOEXCEPT
4166#else
4167 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4168#endif
4169{
4170 _VSTD::__swap_allocator(__a1, __a2,
4171 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4172}
4173
Marshall Clowff91de82015-08-18 19:51:37 +00004174template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004175struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004176 _Traits::propagate_on_container_move_assignment::value
4177#if _LIBCPP_STD_VER > 14
4178 || _Traits::is_always_equal::value
4179#else
4180 && is_nothrow_move_assignable<_Alloc>::value
4181#endif
4182 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004183
Marshall Clowa591b9a2016-07-11 21:38:08 +00004184
Marshall Clowa591b9a2016-07-11 21:38:08 +00004185template <class _Tp, class _Alloc>
4186struct __temp_value {
4187 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004188
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004189 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004190 _Alloc &__a;
4191
4192 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4193 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004194
Marshall Clowa591b9a2016-07-11 21:38:08 +00004195 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004196 _LIBCPP_NO_CFI
4197 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4198 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4199 _VSTD::forward<_Args>(__args)...);
4200 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004201
Marshall Clowa591b9a2016-07-11 21:38:08 +00004202 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4203 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004204
Marshall Clowe46031a2018-07-02 18:41:15 +00004205template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004206struct __is_allocator : false_type {};
4207
4208template<typename _Alloc>
4209struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004210 typename __void_t<typename _Alloc::value_type>::type,
4211 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4212 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004213 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004214
Eric Fiselier74ebee62019-06-08 01:31:19 +00004215// __builtin_new_allocator -- A non-templated helper for allocating and
4216// deallocating memory using __builtin_operator_new and
4217// __builtin_operator_delete. It should be used in preference to
4218// `std::allocator<T>` to avoid additional instantiations.
4219struct __builtin_new_allocator {
4220 struct __builtin_new_deleter {
4221 typedef void* pointer_type;
4222
4223 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4224 : __size_(__size), __align_(__align) {}
4225
4226 void operator()(void* p) const _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004227 _VSTD::__libcpp_deallocate(p, __size_, __align_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004228 }
4229
4230 private:
4231 size_t __size_;
4232 size_t __align_;
4233 };
4234
4235 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4236
4237 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004238 return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
Eric Fiselier74ebee62019-06-08 01:31:19 +00004239 __builtin_new_deleter(__s, __align));
4240 }
4241
4242 static void __deallocate_bytes(void* __p, size_t __s,
4243 size_t __align) _NOEXCEPT {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004244 _VSTD::__libcpp_deallocate(__p, __s, __align);
Eric Fiselier74ebee62019-06-08 01:31:19 +00004245 }
4246
4247 template <class _Tp>
4248 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4249 static __holder_t __allocate_type(size_t __n) {
4250 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4251 }
4252
4253 template <class _Tp>
4254 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
4255 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
4256 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
4257 }
4258};
4259
4260
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261_LIBCPP_END_NAMESPACE_STD
4262
Eric Fiselierf4433a32017-05-31 22:07:49 +00004263_LIBCPP_POP_MACROS
4264
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004265#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00004266# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00004267#endif
4268
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269#endif // _LIBCPP_MEMORY