blob: 112ee9d4462ce7bfcfe1aae922e501d1af7d7c54 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MEMORY
11#define _LIBCPP_MEMORY
12
13/*
14 memory synopsis
15
16namespace std
17{
18
19struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000020inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template <class T, class Alloc> struct uses_allocator;
23
24template <class Ptr>
25struct pointer_traits
26{
27 typedef Ptr pointer;
28 typedef <details> element_type;
29 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000030
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000032
Howard Hinnantc51e1022010-05-11 19:42:16 +000033 static pointer pointer_to(<details>);
34};
35
Howard Hinnant719bda32011-05-28 14:41:13 +000036template <class T>
37struct pointer_traits<T*>
38{
39 typedef T* pointer;
40 typedef T element_type;
41 typedef ptrdiff_t difference_type;
42
43 template <class U> using rebind = U*;
44
Louis Dionne44e1ea82018-11-13 17:04:05 +000045 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000046};
47
Eric Fiselier45c9aac2017-11-22 19:49:21 +000048template <class T> constexpr T* to_address(T* p) noexcept; // C++20
49template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
50
Howard Hinnantc51e1022010-05-11 19:42:16 +000051template <class Alloc>
52struct allocator_traits
53{
54 typedef Alloc allocator_type;
55 typedef typename allocator_type::value_type
56 value_type;
57
58 typedef Alloc::pointer | value_type* pointer;
59 typedef Alloc::const_pointer
60 | pointer_traits<pointer>::rebind<const value_type>
61 const_pointer;
62 typedef Alloc::void_pointer
63 | pointer_traits<pointer>::rebind<void>
64 void_pointer;
65 typedef Alloc::const_void_pointer
66 | pointer_traits<pointer>::rebind<const void>
67 const_void_pointer;
68 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000069 | pointer_traits<pointer>::difference_type
70 difference_type;
71 typedef Alloc::size_type
72 | make_unsigned<difference_type>::type
73 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 typedef Alloc::propagate_on_container_copy_assignment
75 | false_type propagate_on_container_copy_assignment;
76 typedef Alloc::propagate_on_container_move_assignment
77 | false_type propagate_on_container_move_assignment;
78 typedef Alloc::propagate_on_container_swap
79 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000080 typedef Alloc::is_always_equal
81 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Michael Park99f9e912020-03-04 11:27:14 -050083 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
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>
668#include <type_traits>
669#include <typeinfo>
670#include <cstddef>
671#include <cstdint>
672#include <new>
673#include <utility>
674#include <limits>
675#include <iterator>
676#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000677#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000678#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000679#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000680#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000681#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000682# include <atomic>
683#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000684#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000685
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000686#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
Eric Fiselierf4433a32017-05-31 22:07:49 +0000690_LIBCPP_PUSH_MACROS
691#include <__undef_macros>
692
693
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694_LIBCPP_BEGIN_NAMESPACE_STD
695
Eric Fiselier89659d12015-07-07 00:27:16 +0000696template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000697inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000698_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
699#if !defined(_LIBCPP_HAS_NO_THREADS) && \
700 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000701 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000702 return __atomic_load_n(__value, __ATOMIC_RELAXED);
703#else
704 return *__value;
705#endif
706}
707
Kuba Breckade9d6792016-09-04 09:55:12 +0000708template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000709inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000710_ValueType __libcpp_acquire_load(_ValueType const* __value) {
711#if !defined(_LIBCPP_HAS_NO_THREADS) && \
712 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000713 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000714 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
715#else
716 return *__value;
717#endif
718}
719
Marshall Clow78dbe462016-11-14 18:22:19 +0000720// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000721
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722template <class _Tp> class allocator;
723
Michael Park99f9e912020-03-04 11:27:14 -0500724#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725template <>
Michael Park99f9e912020-03-04 11:27:14 -0500726class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727{
728public:
729 typedef void* pointer;
730 typedef const void* const_pointer;
731 typedef void value_type;
732
733 template <class _Up> struct rebind {typedef allocator<_Up> other;};
734};
735
Howard Hinnant9f771052012-01-19 23:15:22 +0000736template <>
Michael Park99f9e912020-03-04 11:27:14 -0500737class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000738{
739public:
740 typedef const void* pointer;
741 typedef const void* const_pointer;
742 typedef const void value_type;
743
744 template <class _Up> struct rebind {typedef allocator<_Up> other;};
745};
Michael Park99f9e912020-03-04 11:27:14 -0500746#endif
Howard Hinnant9f771052012-01-19 23:15:22 +0000747
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748// pointer_traits
749
Marshall Clow0be70c32017-06-14 21:23:57 +0000750template <class _Tp, class = void>
751struct __has_element_type : false_type {};
752
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000754struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000755 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
757template <class _Ptr, bool = __has_element_type<_Ptr>::value>
758struct __pointer_traits_element_type;
759
760template <class _Ptr>
761struct __pointer_traits_element_type<_Ptr, true>
762{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000763 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764};
765
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766template <template <class, class...> class _Sp, class _Tp, class ..._Args>
767struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
768{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000769 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770};
771
772template <template <class, class...> class _Sp, class _Tp, class ..._Args>
773struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
774{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000775 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776};
777
Marshall Clow0be70c32017-06-14 21:23:57 +0000778template <class _Tp, class = void>
779struct __has_difference_type : false_type {};
780
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000782struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000783 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784
785template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
786struct __pointer_traits_difference_type
787{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000788 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789};
790
791template <class _Ptr>
792struct __pointer_traits_difference_type<_Ptr, true>
793{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000794 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795};
796
797template <class _Tp, class _Up>
Louis Dionnef1bb8492020-03-04 13:54:58 -0500798struct __has_rebind
799{
800private:
801 struct __two {char __lx; char __lxx;};
802 template <class _Xp> static __two __test(...);
Louis Dionne95f24792020-03-04 14:38:51 -0500803 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Louis Dionnef1bb8492020-03-04 13:54:58 -0500804 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
Louis Dionne95f24792020-03-04 14:38:51 -0500805 _LIBCPP_SUPPRESS_DEPRECATED_POP
Louis Dionnef1bb8492020-03-04 13:54:58 -0500806public:
807 static const bool value = sizeof(__test<_Tp>(0)) == 1;
808};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809
810template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
811struct __pointer_traits_rebind
812{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000813#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000814 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000816 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817#endif
818};
819
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
821struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
822{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000823#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000824 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000826 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827#endif
828};
829
830template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
831struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
832{
833 typedef _Sp<_Up, _Args...> type;
834};
835
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000837struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838{
839 typedef _Ptr pointer;
840 typedef typename __pointer_traits_element_type<pointer>::type element_type;
841 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
842
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000843#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000844 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845#else
846 template <class _Up> struct rebind
847 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000848#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849
850private:
851 struct __nat {};
852public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 static pointer pointer_to(typename conditional<is_void<element_type>::value,
855 __nat, element_type>::type& __r)
856 {return pointer::pointer_to(__r);}
857};
858
859template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000860struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861{
862 typedef _Tp* pointer;
863 typedef _Tp element_type;
864 typedef ptrdiff_t difference_type;
865
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000866#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000867 template <class _Up> using rebind = _Up*;
868#else
869 template <class _Up> struct rebind {typedef _Up* other;};
870#endif
871
872private:
873 struct __nat {};
874public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000875 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000877 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000878 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879};
880
Eric Fiselierae3ab842015-08-23 02:56:05 +0000881template <class _From, class _To>
882struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000883#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000884 typedef typename pointer_traits<_From>::template rebind<_To> type;
885#else
886 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
887#endif
888};
889
Louis Dionne99f59432020-09-17 12:06:13 -0400890// construct_at
891
892#if _LIBCPP_STD_VER > 17
893
894template<class _Tp>
895_LIBCPP_CONSTEXPR_AFTER_CXX17 void* __voidify(_Tp& __ptr) noexcept {
896 return const_cast<void*>(static_cast<const volatile void*>(_VSTD::addressof(__ptr)));
897}
898
899template<class _Tp, class ..._Args, class = decltype(
900 ::new (_VSTD::declval<void*>()) _Tp(_VSTD::declval<_Args>()...)
901)>
902_LIBCPP_INLINE_VISIBILITY
903constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
904 _LIBCPP_ASSERT(__location, "null pointer given to construct_at");
905 return ::new (_VSTD::__voidify(*__location)) _Tp(_VSTD::forward<_Args>(__args)...);
906}
907
908#endif
909
910// destroy_at
911
912#if _LIBCPP_STD_VER > 14
913
914template <class _Tp>
915inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
916void destroy_at(_Tp* __loc) {
917 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
918 __loc->~_Tp();
919}
920
921#endif
922
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923// allocator_traits
924
Marshall Clow0be70c32017-06-14 21:23:57 +0000925template <class _Tp, class = void>
926struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
928template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000929struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000930 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931
932namespace __pointer_type_imp
933{
934
935template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
936struct __pointer_type
937{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000938 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939};
940
941template <class _Tp, class _Dp>
942struct __pointer_type<_Tp, _Dp, false>
943{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000944 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945};
946
Howard Hinnant8e4e6002010-11-18 01:40:00 +0000947} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948
949template <class _Tp, class _Dp>
950struct __pointer_type
951{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000952 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953};
954
Marshall Clow0be70c32017-06-14 21:23:57 +0000955template <class _Tp, class = void>
956struct __has_const_pointer : false_type {};
957
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000959struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000960 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
962template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
963struct __const_pointer
964{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000965 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966};
967
968template <class _Tp, class _Ptr, class _Alloc>
969struct __const_pointer<_Tp, _Ptr, _Alloc, false>
970{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000971#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000972 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973#else
974 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
975#endif
976};
977
Marshall Clow0be70c32017-06-14 21:23:57 +0000978template <class _Tp, class = void>
979struct __has_void_pointer : false_type {};
980
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000982struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000983 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984
985template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
986struct __void_pointer
987{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000988 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989};
990
991template <class _Ptr, class _Alloc>
992struct __void_pointer<_Ptr, _Alloc, false>
993{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000994#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000995 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000997 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998#endif
999};
1000
Marshall Clow0be70c32017-06-14 21:23:57 +00001001template <class _Tp, class = void>
1002struct __has_const_void_pointer : false_type {};
1003
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001005struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001006 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007
1008template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1009struct __const_void_pointer
1010{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001011 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012};
1013
1014template <class _Ptr, class _Alloc>
1015struct __const_void_pointer<_Ptr, _Alloc, false>
1016{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001017#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001018 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001020 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021#endif
1022};
1023
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001024
1025template <bool _UsePointerTraits> struct __to_address_helper;
1026
1027template <> struct __to_address_helper<true> {
1028 template <class _Pointer>
1029 using __return_type = decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()));
1030
1031 template <class _Pointer>
1032 _LIBCPP_CONSTEXPR
1033 static __return_type<_Pointer>
1034 __do_it(const _Pointer &__p) _NOEXCEPT { return pointer_traits<_Pointer>::to_address(__p); }
1035};
1036
1037template <class _Pointer, bool _Dummy = true>
1038using __choose_to_address = __to_address_helper<_IsValidExpansion<__to_address_helper<_Dummy>::template __return_type, _Pointer>::value>;
1039
1040
Howard Hinnantc834c512011-11-29 18:15:50 +00001041template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001042inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001043_Tp*
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001044__to_address(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001046 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 return __p;
1048}
1049
1050template <class _Pointer>
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001051inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1052typename __choose_to_address<_Pointer>::template __return_type<_Pointer>
1053__to_address(const _Pointer& __p) _NOEXCEPT {
1054 return __choose_to_address<_Pointer>::__do_it(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001055}
1056
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001057template <> struct __to_address_helper<false> {
1058 template <class _Pointer>
1059 using __return_type = typename pointer_traits<_Pointer>::element_type*;
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001060
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001061 template <class _Pointer>
1062 _LIBCPP_CONSTEXPR
1063 static __return_type<_Pointer>
1064 __do_it(const _Pointer &__p) _NOEXCEPT { return std::__to_address(__p.operator->()); }
1065};
1066
1067
1068#if _LIBCPP_STD_VER > 17
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001069template <class _Tp>
1070inline _LIBCPP_INLINE_VISIBILITY constexpr
1071_Tp*
1072to_address(_Tp* __p) _NOEXCEPT
1073{
1074 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1075 return __p;
1076}
1077
1078template <class _Pointer>
1079inline _LIBCPP_INLINE_VISIBILITY
1080auto
1081to_address(const _Pointer& __p) _NOEXCEPT
1082{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001083 return _VSTD::__to_address(__p);
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001084}
1085#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086
Marshall Clow0be70c32017-06-14 21:23:57 +00001087template <class _Tp, class = void>
1088struct __has_size_type : false_type {};
1089
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001091struct __has_size_type<_Tp,
1092 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001094template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095struct __size_type
1096{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001097 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098};
1099
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001100template <class _Alloc, class _DiffType>
1101struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001103 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104};
1105
Marshall Clow0be70c32017-06-14 21:23:57 +00001106template <class _Tp, class = void>
1107struct __has_propagate_on_container_copy_assignment : false_type {};
1108
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001110struct __has_propagate_on_container_copy_assignment<_Tp,
1111 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1112 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113
1114template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1115struct __propagate_on_container_copy_assignment
1116{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001117 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118};
1119
1120template <class _Alloc>
1121struct __propagate_on_container_copy_assignment<_Alloc, true>
1122{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001123 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124};
1125
Marshall Clow0be70c32017-06-14 21:23:57 +00001126template <class _Tp, class = void>
1127struct __has_propagate_on_container_move_assignment : false_type {};
1128
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001130struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001131 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1132 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133
1134template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1135struct __propagate_on_container_move_assignment
1136{
1137 typedef false_type type;
1138};
1139
1140template <class _Alloc>
1141struct __propagate_on_container_move_assignment<_Alloc, true>
1142{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001143 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144};
1145
Marshall Clow0be70c32017-06-14 21:23:57 +00001146template <class _Tp, class = void>
1147struct __has_propagate_on_container_swap : false_type {};
1148
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001150struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001151 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1152 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153
1154template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1155struct __propagate_on_container_swap
1156{
1157 typedef false_type type;
1158};
1159
1160template <class _Alloc>
1161struct __propagate_on_container_swap<_Alloc, true>
1162{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001163 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164};
1165
Marshall Clow0be70c32017-06-14 21:23:57 +00001166template <class _Tp, class = void>
1167struct __has_is_always_equal : false_type {};
1168
Marshall Clow0b587562015-06-02 16:34:03 +00001169template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001170struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001171 typename __void_t<typename _Tp::is_always_equal>::type>
1172 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001173
1174template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1175struct __is_always_equal
1176{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001177 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001178};
1179
1180template <class _Alloc>
1181struct __is_always_equal<_Alloc, true>
1182{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001183 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001184};
1185
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1187struct __has_rebind_other
1188{
1189private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001190 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 template <class _Xp> static __two __test(...);
Michael Park99f9e912020-03-04 11:27:14 -05001192 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
Michael Park99f9e912020-03-04 11:27:14 -05001194 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195public:
1196 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1197};
1198
1199template <class _Tp, class _Up>
1200struct __has_rebind_other<_Tp, _Up, false>
1201{
1202 static const bool value = false;
1203};
1204
1205template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1206struct __allocator_traits_rebind
1207{
Michael Park99f9e912020-03-04 11:27:14 -05001208 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001209 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001210 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211};
1212
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1214struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1215{
Michael Park99f9e912020-03-04 11:27:14 -05001216 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001217 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Michael Park99f9e912020-03-04 11:27:14 -05001218 _LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219};
1220
1221template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1222struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1223{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001224 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225};
1226
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001227#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228
Michael Park99f9e912020-03-04 11:27:14 -05001229_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1231auto
1232__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001233 -> decltype((void)__a.allocate(__sz, __p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001234_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
1236template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1237auto
1238__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1239 -> false_type;
1240
1241template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1242struct __has_allocate_hint
Michael Park99f9e912020-03-04 11:27:14 -05001243 : decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
1244 declval<_SizeType>(),
1245 declval<_ConstVoidPtr>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246{
1247};
1248
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001249#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250
1251template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1252struct __has_allocate_hint
1253 : true_type
1254{
1255};
1256
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001257#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
zoecarver75816d42020-05-23 14:32:12 -07001259_LIBCPP_SUPPRESS_DEPRECATED_PUSH
zoecarver20590dd2020-05-23 14:03:04 -07001260template <class _Alloc, class ..._Args,
1261 class = decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Args>()...))>
1262static true_type __test_has_construct(int);
zoecarver75816d42020-05-23 14:32:12 -07001263_LIBCPP_SUPPRESS_DEPRECATED_POP
1264
zoecarver20590dd2020-05-23 14:03:04 -07001265template <class _Alloc, class...>
1266static false_type __test_has_construct(...);
1267
1268template <class _Alloc, class ..._Args>
1269struct __has_construct : decltype(__test_has_construct<_Alloc, _Args...>(0)) {};
1270
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001271#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Michael Park99f9e912020-03-04 11:27:14 -05001273_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274template <class _Alloc, class _Pointer>
1275auto
1276__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1277 -> decltype(__a.destroy(__p), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001278_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
1280template <class _Alloc, class _Pointer>
1281auto
1282__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1283 -> false_type;
1284
1285template <class _Alloc, class _Pointer>
1286struct __has_destroy
Michael Park99f9e912020-03-04 11:27:14 -05001287 : decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
1288 declval<_Pointer>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289{
1290};
1291
Michael Park99f9e912020-03-04 11:27:14 -05001292_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293template <class _Alloc>
1294auto
1295__has_max_size_test(_Alloc&& __a)
1296 -> decltype(__a.max_size(), true_type());
Michael Park99f9e912020-03-04 11:27:14 -05001297_LIBCPP_SUPPRESS_DEPRECATED_POP
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
1299template <class _Alloc>
1300auto
1301__has_max_size_test(const volatile _Alloc& __a)
1302 -> false_type;
1303
1304template <class _Alloc>
1305struct __has_max_size
Michael Park99f9e912020-03-04 11:27:14 -05001306 : decltype(_VSTD::__has_max_size_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307{
1308};
1309
1310template <class _Alloc>
1311auto
1312__has_select_on_container_copy_construction_test(_Alloc&& __a)
1313 -> decltype(__a.select_on_container_copy_construction(), true_type());
1314
1315template <class _Alloc>
1316auto
1317__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1318 -> false_type;
1319
1320template <class _Alloc>
1321struct __has_select_on_container_copy_construction
Michael Park99f9e912020-03-04 11:27:14 -05001322 : decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323{
1324};
1325
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001326#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001328template <class _Alloc, class _Pointer, class = void>
1329struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330
1331template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001332struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1333 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1334>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335
1336template <class _Alloc>
1337struct __has_max_size
1338 : true_type
1339{
1340};
1341
1342template <class _Alloc>
1343struct __has_select_on_container_copy_construction
1344 : false_type
1345{
1346};
1347
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001348#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001350template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1351struct __alloc_traits_difference_type
1352{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001353 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001354};
1355
1356template <class _Alloc, class _Ptr>
1357struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1358{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001359 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001360};
1361
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001362template <class _Tp>
1363struct __is_default_allocator : false_type {};
1364
1365template <class _Tp>
1366struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1367
Eric Fiselier909fe962019-09-13 16:09:33 +00001368
1369
1370template <class _Alloc,
1371 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1372 >
1373struct __is_cpp17_move_insertable;
1374template <class _Alloc>
1375struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1376template <class _Alloc>
1377struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1378
1379template <class _Alloc,
1380 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1381 >
1382struct __is_cpp17_copy_insertable;
1383template <class _Alloc>
1384struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1385template <class _Alloc>
1386struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1387 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1388 __is_cpp17_move_insertable<_Alloc>::value>
1389 {};
1390
1391
1392
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001394struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395{
1396 typedef _Alloc allocator_type;
1397 typedef typename allocator_type::value_type value_type;
1398
1399 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1400 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1401 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1402 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1403
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001404 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1405 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406
1407 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1408 propagate_on_container_copy_assignment;
1409 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1410 propagate_on_container_move_assignment;
1411 typedef typename __propagate_on_container_swap<allocator_type>::type
1412 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001413 typedef typename __is_always_equal<allocator_type>::type
1414 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001416#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001418 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001419 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001420#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 template <class _Tp> struct rebind_alloc
1422 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1423 template <class _Tp> struct rebind_traits
1424 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001425#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426
Louis Dionne99f59432020-09-17 12:06:13 -04001427 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 static pointer allocate(allocator_type& __a, size_type __n)
1429 {return __a.allocate(__n);}
Louis Dionne99f59432020-09-17 12:06:13 -04001430 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001432 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1434
Louis Dionne99f59432020-09-17 12:06:13 -04001435 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001436 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 {__a.deallocate(__p, __n);}
1438
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001440 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001442 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001443 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444
1445 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001446 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 static void destroy(allocator_type& __a, _Tp* __p)
1448 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1449
Louis Dionne99f59432020-09-17 12:06:13 -04001450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow4f834b52013-08-27 20:22:15 +00001451 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1453
Louis Dionne99f59432020-09-17 12:06:13 -04001454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 static allocator_type
1456 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001457 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 __has_select_on_container_copy_construction<const allocator_type>(),
1459 __a);}
1460
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001461 template <class _Ptr>
1462 _LIBCPP_INLINE_VISIBILITY
1463 static
1464 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001465 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001466 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001467 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1468 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001469 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001470 construct(__a, _VSTD::__to_address(__begin2),
Eric Fiselier909fe962019-09-13 16:09:33 +00001471#ifdef _LIBCPP_NO_EXCEPTIONS
1472 _VSTD::move(*__begin1)
1473#else
1474 _VSTD::move_if_noexcept(*__begin1)
1475#endif
1476 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001477 }
1478
1479 template <class _Tp>
1480 _LIBCPP_INLINE_VISIBILITY
1481 static
1482 typename enable_if
1483 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001484 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001485 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1486 is_trivially_move_constructible<_Tp>::value,
1487 void
1488 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001489 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001490 {
1491 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001492 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001493 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001494 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001495 __begin2 += _Np;
1496 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001497 }
1498
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001499 template <class _Iter, class _Ptr>
1500 _LIBCPP_INLINE_VISIBILITY
1501 static
1502 void
1503 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1504 {
1505 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001506 construct(__a, _VSTD::__to_address(__begin2), *__begin1);
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001507 }
1508
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001509 template <class _SourceTp, class _DestTp,
1510 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1511 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 static
1514 typename enable_if
1515 <
Louis Dionne358c6cb2020-02-11 17:11:00 +01001516 is_trivially_copy_constructible<_DestTp>::value &&
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001517 is_same<_RawSourceTp, _RawDestTp>::value &&
1518 (__is_default_allocator<allocator_type>::value ||
1519 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001520 void
1521 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001522 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001523 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001524 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001525 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001526 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001527 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001528 __begin2 += _Np;
1529 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001530 }
1531
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001532 template <class _Ptr>
1533 _LIBCPP_INLINE_VISIBILITY
1534 static
1535 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001536 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001537 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001538 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1539 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001540 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001541 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001542 construct(__a, _VSTD::__to_address(__end2 - 1),
Eric Fiselier909fe962019-09-13 16:09:33 +00001543#ifdef _LIBCPP_NO_EXCEPTIONS
1544 _VSTD::move(*--__end1)
1545#else
1546 _VSTD::move_if_noexcept(*--__end1)
1547#endif
1548 );
1549 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001550 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001551 }
1552
1553 template <class _Tp>
1554 _LIBCPP_INLINE_VISIBILITY
1555 static
1556 typename enable_if
1557 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001558 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001559 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1560 is_trivially_move_constructible<_Tp>::value,
1561 void
1562 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001563 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001564 {
1565 ptrdiff_t _Np = __end1 - __begin1;
1566 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001567 if (_Np > 0)
1568 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001569 }
1570
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571private:
1572
Louis Dionne99f59432020-09-17 12:06:13 -04001573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001574 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 const_void_pointer __hint, true_type)
Michael Park99f9e912020-03-04 11:27:14 -05001576 {
1577 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1578 return __a.allocate(__n, __hint);
1579 _LIBCPP_SUPPRESS_DEPRECATED_POP
1580 }
Louis Dionne99f59432020-09-17 12:06:13 -04001581 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001582 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001583 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 {return __a.allocate(__n);}
1585
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001587 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Michael Park99f9e912020-03-04 11:27:14 -05001589 {
1590 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1591 __a.construct(__p, _VSTD::forward<_Args>(__args)...);
1592 _LIBCPP_SUPPRESS_DEPRECATED_POP
1593 }
1594
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 template <class _Tp, class... _Args>
Louis Dionne99f59432020-09-17 12:06:13 -04001596 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1598 {
Louis Dionne99f59432020-09-17 12:06:13 -04001599#if _LIBCPP_STD_VER > 17
1600 _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
1601#else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001602 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Louis Dionne99f59432020-09-17 12:06:13 -04001603#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605
1606 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001607 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
Michael Park99f9e912020-03-04 11:27:14 -05001609 {
1610 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1611 __a.destroy(__p);
1612 _LIBCPP_SUPPRESS_DEPRECATED_POP
1613 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 template <class _Tp>
Louis Dionne99f59432020-09-17 12:06:13 -04001615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 static void __destroy(false_type, allocator_type&, _Tp* __p)
1617 {
Louis Dionne99f59432020-09-17 12:06:13 -04001618#if _LIBCPP_STD_VER > 17
1619 _VSTD::destroy_at(__p);
1620#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 __p->~_Tp();
Louis Dionne99f59432020-09-17 12:06:13 -04001622#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623 }
1624
Louis Dionne99f59432020-09-17 12:06:13 -04001625 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001626 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Michael Park99f9e912020-03-04 11:27:14 -05001627 {
1628 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1629 return __a.max_size();
1630 _LIBCPP_SUPPRESS_DEPRECATED_POP
1631 }
1632
Louis Dionne99f59432020-09-17 12:06:13 -04001633 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowf817a352017-12-05 15:56:26 +00001634 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001635 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636
Louis Dionne99f59432020-09-17 12:06:13 -04001637 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001639 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 {return __a.select_on_container_copy_construction();}
Louis Dionne99f59432020-09-17 12:06:13 -04001641 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001643 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 {return __a;}
1645};
1646
Marshall Clow940e01c2015-04-07 05:21:38 +00001647template <class _Traits, class _Tp>
1648struct __rebind_alloc_helper
1649{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001650#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001651 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001652#else
1653 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1654#endif
1655};
1656
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657// allocator
1658
1659template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001660class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661{
1662public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001663 typedef size_t size_type;
1664 typedef ptrdiff_t difference_type;
1665 typedef _Tp value_type;
1666 typedef true_type propagate_on_container_move_assignment;
1667 typedef true_type is_always_equal;
1668
1669 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1670 allocator() _NOEXCEPT { }
1671
1672 template <class _Up>
1673 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1674 allocator(const allocator<_Up>&) _NOEXCEPT { }
1675
1676 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1677 _Tp* allocate(size_t __n) {
1678 if (__n > allocator_traits<allocator>::max_size(*this))
1679 __throw_length_error("allocator<T>::allocate(size_t n)"
1680 " 'n' exceeds maximum supported size");
1681 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
1682 }
1683
1684 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1685 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
1686 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1687 }
1688
1689 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001690#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Michael Park99f9e912020-03-04 11:27:14 -05001691 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
1692 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1693 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
1694 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1695
Louis Dionne481a2662018-09-23 18:35:00 +00001696 template <class _Up>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001697 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1698 typedef allocator<_Up> other;
1699 };
Marshall Clowbc759762018-03-20 23:02:53 +00001700
Michael Park99f9e912020-03-04 11:27:14 -05001701 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001702 pointer address(reference __x) const _NOEXCEPT {
1703 return _VSTD::addressof(__x);
1704 }
Michael Park99f9e912020-03-04 11:27:14 -05001705 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
Louis Dionnef8b6c022020-09-21 10:36:37 -04001706 const_pointer address(const_reference __x) const _NOEXCEPT {
1707 return _VSTD::addressof(__x);
1708 }
Michael Park99f9e912020-03-04 11:27:14 -05001709
Michael Park99f9e912020-03-04 11:27:14 -05001710 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001711 _Tp* allocate(size_t __n, const void*) {
1712 return allocate(__n);
1713 }
Michael Park99f9e912020-03-04 11:27:14 -05001714
Louis Dionnef8b6c022020-09-21 10:36:37 -04001715 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1716 return size_type(~0) / sizeof(_Tp);
1717 }
Michael Park99f9e912020-03-04 11:27:14 -05001718
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001720 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1721 void construct(_Up* __p, _Args&&... __args) {
1722 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1723 }
1724
1725 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1726 void destroy(pointer __p) {
1727 __p->~_Tp();
1728 }
Michael Park99f9e912020-03-04 11:27:14 -05001729#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730};
1731
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001732template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001733class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001734{
1735public:
Louis Dionnef8b6c022020-09-21 10:36:37 -04001736 typedef size_t size_type;
1737 typedef ptrdiff_t difference_type;
1738 typedef const _Tp value_type;
1739 typedef true_type propagate_on_container_move_assignment;
1740 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001741
Marshall Clowbc759762018-03-20 23:02:53 +00001742 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001743 allocator() _NOEXCEPT { }
Marshall Clowbc759762018-03-20 23:02:53 +00001744
1745 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001746 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001747 allocator(const allocator<_Up>&) _NOEXCEPT { }
Michael Park99f9e912020-03-04 11:27:14 -05001748
Louis Dionne99f59432020-09-17 12:06:13 -04001749 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001750 const _Tp* allocate(size_t __n) {
Louis Dionne99f59432020-09-17 12:06:13 -04001751 if (__n > allocator_traits<allocator>::max_size(*this))
Marshall Clowed3e2292016-08-25 17:47:09 +00001752 __throw_length_error("allocator<const T>::allocate(size_t n)"
1753 " 'n' exceeds maximum supported size");
Michael Park99f9e912020-03-04 11:27:14 -05001754 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001755 }
Michael Park99f9e912020-03-04 11:27:14 -05001756
Louis Dionne99f59432020-09-17 12:06:13 -04001757 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionnef8b6c022020-09-21 10:36:37 -04001758 void deallocate(const _Tp* __p, size_t __n) {
1759 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
1760 }
Michael Park99f9e912020-03-04 11:27:14 -05001761
Louis Dionnef8b6c022020-09-21 10:36:37 -04001762 // C++20 Removed members
Michael Park99f9e912020-03-04 11:27:14 -05001763#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
Louis Dionnef8b6c022020-09-21 10:36:37 -04001764 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
1765 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
1766 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
1767 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
1768
1769 template <class _Up>
1770 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
1771 typedef allocator<_Up> other;
1772 };
1773
1774 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1775 const_pointer address(const_reference __x) const _NOEXCEPT {
1776 return _VSTD::addressof(__x);
1777 }
1778
1779 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
1780 const _Tp* allocate(size_t __n, const void*) {
1781 return allocate(__n);
1782 }
1783
1784 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
1785 return size_type(~0) / sizeof(_Tp);
1786 }
Michael Park99f9e912020-03-04 11:27:14 -05001787
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001788 template <class _Up, class... _Args>
Louis Dionnef8b6c022020-09-21 10:36:37 -04001789 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1790 void construct(_Up* __p, _Args&&... __args) {
1791 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1792 }
Howard Hinnant9e028f12012-05-01 15:37:54 +00001793
Louis Dionnef8b6c022020-09-21 10:36:37 -04001794 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
1795 void destroy(pointer __p) {
1796 __p->~_Tp();
1797 }
Michael Park99f9e912020-03-04 11:27:14 -05001798#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001799};
1800
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001802inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001803bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804
1805template <class _Tp, class _Up>
Louis Dionne99f59432020-09-17 12:06:13 -04001806inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant719bda32011-05-28 14:41:13 +00001807bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808
1809template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001810class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 : public iterator<output_iterator_tag,
1812 _Tp, // purposefully not C++03
1813 ptrdiff_t, // purposefully not C++03
1814 _Tp*, // purposefully not C++03
1815 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1816{
1817private:
1818 _OutputIterator __x_;
1819public:
1820 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1821 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1822 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001823 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001824#if _LIBCPP_STD_VER >= 14
1825 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001826 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001827#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1829 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1830 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001831#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001832 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001833#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834};
1835
1836template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00001837_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001839get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840{
1841 pair<_Tp*, ptrdiff_t> __r(0, 0);
1842 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1843 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1844 / sizeof(_Tp);
1845 if (__n > __m)
1846 __n = __m;
1847 while (__n > 0)
1848 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00001849#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001850 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001851 {
1852 std::align_val_t __al =
1853 std::align_val_t(std::alignment_of<_Tp>::value);
1854 __r.first = static_cast<_Tp*>(::operator new(
1855 __n * sizeof(_Tp), __al, nothrow));
1856 } else {
1857 __r.first = static_cast<_Tp*>(::operator new(
1858 __n * sizeof(_Tp), nothrow));
1859 }
1860#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001861 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00001862 {
1863 // Since aligned operator new is unavailable, return an empty
1864 // buffer rather than one with invalid alignment.
1865 return __r;
1866 }
1867
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00001869#endif
1870
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 if (__r.first)
1872 {
1873 __r.second = __n;
1874 break;
1875 }
1876 __n /= 2;
1877 }
1878 return __r;
1879}
1880
1881template <class _Tp>
1882inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00001883void return_temporary_buffer(_Tp* __p) _NOEXCEPT
1884{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001885 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00001886}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887
Marshall Clowb22274f2017-01-24 22:22:33 +00001888#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001890struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
1892 _Tp* __ptr_;
1893};
1894
1895template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001896class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897{
1898private:
1899 _Tp* __ptr_;
1900public:
1901 typedef _Tp element_type;
1902
Dimitry Andric47269ce2020-03-13 19:36:26 +01001903 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
1904 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
1905 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906 : __ptr_(__p.release()) {}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001907 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001909 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 {reset(__p.release()); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001911 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 {reset(__p.__ptr_); return *this;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001913 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914
Dimitry Andric47269ce2020-03-13 19:36:26 +01001915 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916 {return *__ptr_;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001917 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
1918 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
1919 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 {
1921 _Tp* __t = __ptr_;
1922 __ptr_ = 0;
1923 return __t;
1924 }
Dimitry Andric47269ce2020-03-13 19:36:26 +01001925 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 {
1927 if (__ptr_ != __p)
1928 delete __ptr_;
1929 __ptr_ = __p;
1930 }
1931
Dimitry Andric47269ce2020-03-13 19:36:26 +01001932 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
1933 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
Dimitry Andric47269ce2020-03-13 19:36:26 +01001935 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 {return auto_ptr<_Up>(release());}
1937};
1938
1939template <>
Louis Dionne481a2662018-09-23 18:35:00 +00001940class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941{
1942public:
1943 typedef void element_type;
1944};
Marshall Clowb22274f2017-01-24 22:22:33 +00001945#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001947// Tag used to default initialize one or both of the pair's elements.
1948struct __default_init_tag {};
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001949struct __value_init_tag {};
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001950
Eric Fiselier9d355982017-04-12 23:45:53 +00001951template <class _Tp, int _Idx,
1952 bool _CanBeEmptyBase =
1953 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
1954struct __compressed_pair_elem {
1955 typedef _Tp _ParamT;
1956 typedef _Tp& reference;
1957 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001959 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier15eae3b2019-12-16 17:00:10 -05001960 __compressed_pair_elem(__default_init_tag) {}
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001961 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1962 __compressed_pair_elem(__value_init_tag) : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963
Eric Fiselier9d355982017-04-12 23:45:53 +00001964 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00001965 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
1966 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00001967 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001968 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00001969 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001970 : __value_(_VSTD::forward<_Up>(__u))
1971 {
1972 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001974
1975#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00001976 template <class... _Args, size_t... _Indexes>
1977 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1978 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
1979 __tuple_indices<_Indexes...>)
1980 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00001981#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001983
Alex Lorenz76132112017-11-09 17:54:49 +00001984 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
1985 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00001986 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988private:
Eric Fiselier9d355982017-04-12 23:45:53 +00001989 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990};
1991
Eric Fiselier9d355982017-04-12 23:45:53 +00001992template <class _Tp, int _Idx>
1993struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
1994 typedef _Tp _ParamT;
1995 typedef _Tp& reference;
1996 typedef const _Tp& const_reference;
1997 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001999 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
2000 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2001 __compressed_pair_elem(__default_init_tag) {}
2002 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2003 __compressed_pair_elem(__value_init_tag) : __value_type() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004
Eric Fiselier9d355982017-04-12 23:45:53 +00002005 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002006 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2007 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002008 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002009 _LIBCPP_CONSTEXPR explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002010 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002011 : __value_type(_VSTD::forward<_Up>(__u))
2012 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002014#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002015 template <class... _Args, size_t... _Indexes>
2016 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2017 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2018 __tuple_indices<_Indexes...>)
2019 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002020#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021
Alex Lorenz76132112017-11-09 17:54:49 +00002022 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2023 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002024 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025};
2026
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002028class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2029 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002030 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2031 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002032
2033 // NOTE: This static assert should never fire because __compressed_pair
2034 // is *almost never* used in a scenario where it's possible for T1 == T2.
2035 // (The exception is std::function where it is possible that the function
2036 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002037 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002038 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2039 "The current implementation is NOT ABI-compatible with the previous "
2040 "implementation for this configuration");
2041
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042public:
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002043 template <bool _Dummy = true,
Eric Fiselier209ecde2017-04-17 22:32:02 +00002044 class = typename enable_if<
2045 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2046 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2047 >::type
2048 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002049 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002050 _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051
Eric Fiselier9d355982017-04-12 23:45:53 +00002052 template <class _U1, class _U2>
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002053 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier9d355982017-04-12 23:45:53 +00002054 __compressed_pair(_U1&& __t1, _U2&& __t2)
2055 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002057#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier9d355982017-04-12 23:45:53 +00002058 template <class... _Args1, class... _Args2>
2059 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2060 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2061 tuple<_Args2...> __second_args)
2062 : _Base1(__pc, _VSTD::move(__first_args),
2063 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2064 _Base2(__pc, _VSTD::move(__second_args),
2065 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Eric Fiselier9d355982017-04-12 23:45:53 +00002066#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067
Eric Fiselier9d355982017-04-12 23:45:53 +00002068 _LIBCPP_INLINE_VISIBILITY
2069 typename _Base1::reference first() _NOEXCEPT {
2070 return static_cast<_Base1&>(*this).__get();
2071 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Eric Fiselier9d355982017-04-12 23:45:53 +00002073 _LIBCPP_INLINE_VISIBILITY
2074 typename _Base1::const_reference first() const _NOEXCEPT {
2075 return static_cast<_Base1 const&>(*this).__get();
2076 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002077
Eric Fiselier9d355982017-04-12 23:45:53 +00002078 _LIBCPP_INLINE_VISIBILITY
2079 typename _Base2::reference second() _NOEXCEPT {
2080 return static_cast<_Base2&>(*this).__get();
2081 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082
Eric Fiselier9d355982017-04-12 23:45:53 +00002083 _LIBCPP_INLINE_VISIBILITY
2084 typename _Base2::const_reference second() const _NOEXCEPT {
2085 return static_cast<_Base2 const&>(*this).__get();
2086 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087
Eric Fiselier9d355982017-04-12 23:45:53 +00002088 _LIBCPP_INLINE_VISIBILITY
2089 void swap(__compressed_pair& __x)
2090 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2091 __is_nothrow_swappable<_T2>::value)
2092 {
2093 using std::swap;
2094 swap(first(), __x.first());
2095 swap(second(), __x.second());
2096 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097};
2098
2099template <class _T1, class _T2>
2100inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002101void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2102 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2103 __is_nothrow_swappable<_T2>::value) {
2104 __x.swap(__y);
2105}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002107// default_delete
2108
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002110struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002111 static_assert(!is_function<_Tp>::value,
2112 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002113#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002114 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002115#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002116 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002117#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002118 template <class _Up>
2119 _LIBCPP_INLINE_VISIBILITY
2120 default_delete(const default_delete<_Up>&,
2121 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2122 0) _NOEXCEPT {}
2123
2124 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2125 static_assert(sizeof(_Tp) > 0,
2126 "default_delete can not delete incomplete type");
2127 static_assert(!is_void<_Tp>::value,
2128 "default_delete can not delete incomplete type");
2129 delete __ptr;
2130 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131};
2132
2133template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002134struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2135private:
2136 template <class _Up>
2137 struct _EnableIfConvertible
2138 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2139
Howard Hinnant4500ca52011-12-18 21:19:44 +00002140public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002141#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002142 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002143#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002144 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002145#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002146
2147 template <class _Up>
2148 _LIBCPP_INLINE_VISIBILITY
2149 default_delete(const default_delete<_Up[]>&,
2150 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2151
2152 template <class _Up>
2153 _LIBCPP_INLINE_VISIBILITY
2154 typename _EnableIfConvertible<_Up>::type
2155 operator()(_Up* __ptr) const _NOEXCEPT {
2156 static_assert(sizeof(_Tp) > 0,
2157 "default_delete can not delete incomplete type");
2158 static_assert(!is_void<_Tp>::value,
2159 "default_delete can not delete void type");
2160 delete[] __ptr;
2161 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162};
2163
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002164template <class _Deleter>
2165struct __unique_ptr_deleter_sfinae {
2166 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2167 typedef const _Deleter& __lval_ref_type;
2168 typedef _Deleter&& __good_rval_ref_type;
2169 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170};
2171
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002172template <class _Deleter>
2173struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2174 typedef const _Deleter& __lval_ref_type;
2175 typedef const _Deleter&& __bad_rval_ref_type;
2176 typedef false_type __enable_rval_overload;
2177};
2178
2179template <class _Deleter>
2180struct __unique_ptr_deleter_sfinae<_Deleter&> {
2181 typedef _Deleter& __lval_ref_type;
2182 typedef _Deleter&& __bad_rval_ref_type;
2183 typedef false_type __enable_rval_overload;
2184};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002185
Vy Nguyene369bd92020-07-13 12:34:37 -04002186#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
2187# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
2188#else
2189# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
2190#endif
2191
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002192template <class _Tp, class _Dp = default_delete<_Tp> >
Vy Nguyene369bd92020-07-13 12:34:37 -04002193class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002194public:
2195 typedef _Tp element_type;
2196 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002197 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002198
2199 static_assert(!is_rvalue_reference<deleter_type>::value,
2200 "the specified deleter type cannot be an rvalue reference");
2201
2202private:
2203 __compressed_pair<pointer, deleter_type> __ptr_;
2204
2205 struct __nat { int __for_bool_; };
2206
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002207 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002208
2209 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002210 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002211 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002212
2213 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002214 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002215 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002216
2217 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002218 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002219 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002220
2221 template <bool _Dummy, class _Deleter = typename __dependent_type<
2222 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002223 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002224 typename enable_if<is_default_constructible<_Deleter>::value &&
2225 !is_pointer<_Deleter>::value>::type;
2226
2227 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002228 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002229 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2230
2231 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002232 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002233 is_convertible<typename _UPtr::pointer, pointer>::value &&
2234 !is_array<_Up>::value
2235 >::type;
2236
2237 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002238 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002239 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2240 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2241 >::type;
2242
2243 template <class _UDel>
2244 using _EnableIfDeleterAssignable = typename enable_if<
2245 is_assignable<_Dp&, _UDel&&>::value
2246 >::type;
2247
2248public:
2249 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002250 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002251 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002252 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002253
2254 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002255 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002256 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002257 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002258
2259 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002260 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002261 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002262 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002263
2264 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002265 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002266 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002267 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002268 : __ptr_(__p, __d) {}
2269
2270 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002271 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002272 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002273 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002274 : __ptr_(__p, _VSTD::move(__d)) {
2275 static_assert(!is_reference<deleter_type>::value,
2276 "rvalue deleter bound to reference");
2277 }
2278
2279 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002280 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002281 _LIBCPP_INLINE_VISIBILITY
2282 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2283
2284 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002285 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002286 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2287 }
2288
2289 template <class _Up, class _Ep,
2290 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2291 class = _EnableIfDeleterConvertible<_Ep>
2292 >
2293 _LIBCPP_INLINE_VISIBILITY
2294 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2295 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2296
2297#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2298 template <class _Up>
2299 _LIBCPP_INLINE_VISIBILITY
2300 unique_ptr(auto_ptr<_Up>&& __p,
2301 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002302 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002303 __nat>::type = __nat()) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002304 : __ptr_(__p.release(), __default_init_tag()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002305#endif
2306
2307 _LIBCPP_INLINE_VISIBILITY
2308 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2309 reset(__u.release());
2310 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2311 return *this;
2312 }
2313
2314 template <class _Up, class _Ep,
2315 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2316 class = _EnableIfDeleterAssignable<_Ep>
2317 >
2318 _LIBCPP_INLINE_VISIBILITY
2319 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2320 reset(__u.release());
2321 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2322 return *this;
2323 }
2324
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002325#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2326 template <class _Up>
2327 _LIBCPP_INLINE_VISIBILITY
2328 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2329 is_same<_Dp, default_delete<_Tp> >::value,
2330 unique_ptr&>::type
2331 operator=(auto_ptr<_Up> __p) {
2332 reset(__p.release());
2333 return *this;
2334 }
2335#endif
2336
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002337#ifdef _LIBCPP_CXX03_LANG
2338 unique_ptr(unique_ptr const&) = delete;
2339 unique_ptr& operator=(unique_ptr const&) = delete;
2340#endif
2341
2342
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002343 _LIBCPP_INLINE_VISIBILITY
2344 ~unique_ptr() { reset(); }
2345
2346 _LIBCPP_INLINE_VISIBILITY
2347 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2348 reset();
2349 return *this;
2350 }
2351
2352 _LIBCPP_INLINE_VISIBILITY
2353 typename add_lvalue_reference<_Tp>::type
2354 operator*() const {
2355 return *__ptr_.first();
2356 }
2357 _LIBCPP_INLINE_VISIBILITY
2358 pointer operator->() const _NOEXCEPT {
2359 return __ptr_.first();
2360 }
2361 _LIBCPP_INLINE_VISIBILITY
2362 pointer get() const _NOEXCEPT {
2363 return __ptr_.first();
2364 }
2365 _LIBCPP_INLINE_VISIBILITY
2366 deleter_type& get_deleter() _NOEXCEPT {
2367 return __ptr_.second();
2368 }
2369 _LIBCPP_INLINE_VISIBILITY
2370 const deleter_type& get_deleter() const _NOEXCEPT {
2371 return __ptr_.second();
2372 }
2373 _LIBCPP_INLINE_VISIBILITY
2374 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2375 return __ptr_.first() != nullptr;
2376 }
2377
2378 _LIBCPP_INLINE_VISIBILITY
2379 pointer release() _NOEXCEPT {
2380 pointer __t = __ptr_.first();
2381 __ptr_.first() = pointer();
2382 return __t;
2383 }
2384
2385 _LIBCPP_INLINE_VISIBILITY
2386 void reset(pointer __p = pointer()) _NOEXCEPT {
2387 pointer __tmp = __ptr_.first();
2388 __ptr_.first() = __p;
2389 if (__tmp)
2390 __ptr_.second()(__tmp);
2391 }
2392
2393 _LIBCPP_INLINE_VISIBILITY
2394 void swap(unique_ptr& __u) _NOEXCEPT {
2395 __ptr_.swap(__u.__ptr_);
2396 }
2397};
2398
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002399
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400template <class _Tp, class _Dp>
Vy Nguyene369bd92020-07-13 12:34:37 -04002401class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002403 typedef _Tp element_type;
2404 typedef _Dp deleter_type;
2405 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2406
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002408 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409
Eric Fiselier31127cd2017-04-16 02:14:31 +00002410 template <class _From>
2411 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412
Eric Fiselier31127cd2017-04-16 02:14:31 +00002413 template <class _FromElem>
2414 struct _CheckArrayPointerConversion<_FromElem*>
2415 : integral_constant<bool,
2416 is_same<_FromElem*, pointer>::value ||
2417 (is_same<pointer, element_type*>::value &&
2418 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2419 >
2420 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421
Eric Fiselier31127cd2017-04-16 02:14:31 +00002422 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002423
2424 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002425 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002426 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002427
2428 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002429 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002430 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002431
2432 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002433 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002434 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002435
2436 template <bool _Dummy, class _Deleter = typename __dependent_type<
2437 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002438 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002439 typename enable_if<is_default_constructible<_Deleter>::value &&
2440 !is_pointer<_Deleter>::value>::type;
2441
2442 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002443 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002444 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2445
2446 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002447 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002448 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002449 >::type;
2450
2451 template <class _UPtr, class _Up,
2452 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002453 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002454 is_array<_Up>::value &&
2455 is_same<pointer, element_type*>::value &&
2456 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2457 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2458 >::type;
2459
2460 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002461 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002462 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2463 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2464 >::type;
2465
2466 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002467 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002468 is_assignable<_Dp&, _UDel&&>::value
2469 >::type;
2470
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002472 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002473 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002474 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002475 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002477 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002478 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002479 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002480 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002482 template <class _Pp, bool _Dummy = true,
2483 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002484 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002485 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002486 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselier33ebfb62019-12-16 18:23:39 -05002487 : __ptr_(__p, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002489 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002490 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2491 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002492 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002493 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002494 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002496 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002497 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002498 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002499 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002500 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002502 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002503 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2504 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002505 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002506 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002507 : __ptr_(__p, _VSTD::move(__d)) {
2508 static_assert(!is_reference<deleter_type>::value,
2509 "rvalue deleter bound to reference");
2510 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002512 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002513 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002514 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002515 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002516 : __ptr_(nullptr, _VSTD::move(__d)) {
2517 static_assert(!is_reference<deleter_type>::value,
2518 "rvalue deleter bound to reference");
2519 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002520
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002521 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002522 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2523 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002524 _LIBCPP_INLINE_VISIBILITY
2525 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002526
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002527 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002528 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002529 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2530 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002531
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002532 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002533 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002534 reset(__u.release());
2535 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2536 return *this;
2537 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002539 template <class _Up, class _Ep,
2540 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2541 class = _EnableIfDeleterConvertible<_Ep>
2542 >
2543 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002544 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002545 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002546 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002548 template <class _Up, class _Ep,
2549 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2550 class = _EnableIfDeleterAssignable<_Ep>
2551 >
2552 _LIBCPP_INLINE_VISIBILITY
2553 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002554 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002555 reset(__u.release());
2556 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2557 return *this;
2558 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002560#ifdef _LIBCPP_CXX03_LANG
2561 unique_ptr(unique_ptr const&) = delete;
2562 unique_ptr& operator=(unique_ptr const&) = delete;
2563#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002564
2565public:
2566 _LIBCPP_INLINE_VISIBILITY
2567 ~unique_ptr() { reset(); }
2568
2569 _LIBCPP_INLINE_VISIBILITY
2570 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2571 reset();
2572 return *this;
2573 }
2574
2575 _LIBCPP_INLINE_VISIBILITY
2576 typename add_lvalue_reference<_Tp>::type
2577 operator[](size_t __i) const {
2578 return __ptr_.first()[__i];
2579 }
2580 _LIBCPP_INLINE_VISIBILITY
2581 pointer get() const _NOEXCEPT {
2582 return __ptr_.first();
2583 }
2584
2585 _LIBCPP_INLINE_VISIBILITY
2586 deleter_type& get_deleter() _NOEXCEPT {
2587 return __ptr_.second();
2588 }
2589
2590 _LIBCPP_INLINE_VISIBILITY
2591 const deleter_type& get_deleter() const _NOEXCEPT {
2592 return __ptr_.second();
2593 }
2594 _LIBCPP_INLINE_VISIBILITY
2595 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2596 return __ptr_.first() != nullptr;
2597 }
2598
2599 _LIBCPP_INLINE_VISIBILITY
2600 pointer release() _NOEXCEPT {
2601 pointer __t = __ptr_.first();
2602 __ptr_.first() = pointer();
2603 return __t;
2604 }
2605
2606 template <class _Pp>
2607 _LIBCPP_INLINE_VISIBILITY
2608 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002609 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002610 >::type
2611 reset(_Pp __p) _NOEXCEPT {
2612 pointer __tmp = __ptr_.first();
2613 __ptr_.first() = __p;
2614 if (__tmp)
2615 __ptr_.second()(__tmp);
2616 }
2617
2618 _LIBCPP_INLINE_VISIBILITY
2619 void reset(nullptr_t = nullptr) _NOEXCEPT {
2620 pointer __tmp = __ptr_.first();
2621 __ptr_.first() = nullptr;
2622 if (__tmp)
2623 __ptr_.second()(__tmp);
2624 }
2625
2626 _LIBCPP_INLINE_VISIBILITY
2627 void swap(unique_ptr& __u) _NOEXCEPT {
2628 __ptr_.swap(__u.__ptr_);
2629 }
2630
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631};
2632
2633template <class _Tp, class _Dp>
2634inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002635typename enable_if<
2636 __is_swappable<_Dp>::value,
2637 void
2638>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002639swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640
2641template <class _T1, class _D1, class _T2, class _D2>
2642inline _LIBCPP_INLINE_VISIBILITY
2643bool
2644operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2645
2646template <class _T1, class _D1, class _T2, class _D2>
2647inline _LIBCPP_INLINE_VISIBILITY
2648bool
2649operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2650
2651template <class _T1, class _D1, class _T2, class _D2>
2652inline _LIBCPP_INLINE_VISIBILITY
2653bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002654operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2655{
2656 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2657 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002658 typedef typename common_type<_P1, _P2>::type _Vp;
2659 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002660}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661
2662template <class _T1, class _D1, class _T2, class _D2>
2663inline _LIBCPP_INLINE_VISIBILITY
2664bool
2665operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2666
2667template <class _T1, class _D1, class _T2, class _D2>
2668inline _LIBCPP_INLINE_VISIBILITY
2669bool
2670operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2671
2672template <class _T1, class _D1, class _T2, class _D2>
2673inline _LIBCPP_INLINE_VISIBILITY
2674bool
2675operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2676
Howard Hinnantb17caf92012-02-21 21:02:58 +00002677template <class _T1, class _D1>
2678inline _LIBCPP_INLINE_VISIBILITY
2679bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002680operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002681{
2682 return !__x;
2683}
2684
2685template <class _T1, class _D1>
2686inline _LIBCPP_INLINE_VISIBILITY
2687bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002688operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002689{
2690 return !__x;
2691}
2692
2693template <class _T1, class _D1>
2694inline _LIBCPP_INLINE_VISIBILITY
2695bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002696operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002697{
2698 return static_cast<bool>(__x);
2699}
2700
2701template <class _T1, class _D1>
2702inline _LIBCPP_INLINE_VISIBILITY
2703bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002704operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002705{
2706 return static_cast<bool>(__x);
2707}
2708
2709template <class _T1, class _D1>
2710inline _LIBCPP_INLINE_VISIBILITY
2711bool
2712operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2713{
2714 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2715 return less<_P1>()(__x.get(), nullptr);
2716}
2717
2718template <class _T1, class _D1>
2719inline _LIBCPP_INLINE_VISIBILITY
2720bool
2721operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2722{
2723 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2724 return less<_P1>()(nullptr, __x.get());
2725}
2726
2727template <class _T1, class _D1>
2728inline _LIBCPP_INLINE_VISIBILITY
2729bool
2730operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2731{
2732 return nullptr < __x;
2733}
2734
2735template <class _T1, class _D1>
2736inline _LIBCPP_INLINE_VISIBILITY
2737bool
2738operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2739{
2740 return __x < nullptr;
2741}
2742
2743template <class _T1, class _D1>
2744inline _LIBCPP_INLINE_VISIBILITY
2745bool
2746operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2747{
2748 return !(nullptr < __x);
2749}
2750
2751template <class _T1, class _D1>
2752inline _LIBCPP_INLINE_VISIBILITY
2753bool
2754operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2755{
2756 return !(__x < nullptr);
2757}
2758
2759template <class _T1, class _D1>
2760inline _LIBCPP_INLINE_VISIBILITY
2761bool
2762operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2763{
2764 return !(__x < nullptr);
2765}
2766
2767template <class _T1, class _D1>
2768inline _LIBCPP_INLINE_VISIBILITY
2769bool
2770operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2771{
2772 return !(nullptr < __x);
2773}
2774
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002775#if _LIBCPP_STD_VER > 11
2776
2777template<class _Tp>
2778struct __unique_if
2779{
2780 typedef unique_ptr<_Tp> __unique_single;
2781};
2782
2783template<class _Tp>
2784struct __unique_if<_Tp[]>
2785{
2786 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2787};
2788
2789template<class _Tp, size_t _Np>
2790struct __unique_if<_Tp[_Np]>
2791{
2792 typedef void __unique_array_known_bound;
2793};
2794
2795template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002796inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002797typename __unique_if<_Tp>::__unique_single
2798make_unique(_Args&&... __args)
2799{
2800 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2801}
2802
2803template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002804inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002805typename __unique_if<_Tp>::__unique_array_unknown_bound
2806make_unique(size_t __n)
2807{
2808 typedef typename remove_extent<_Tp>::type _Up;
2809 return unique_ptr<_Tp>(new _Up[__n]());
2810}
2811
2812template<class _Tp, class... _Args>
2813 typename __unique_if<_Tp>::__unique_array_known_bound
2814 make_unique(_Args&&...) = delete;
2815
2816#endif // _LIBCPP_STD_VER > 11
2817
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002818template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002819#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002820struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002821#else
2822struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002823 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002824#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002825{
2826 typedef unique_ptr<_Tp, _Dp> argument_type;
2827 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002828 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002829 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002830 {
2831 typedef typename argument_type::pointer pointer;
2832 return hash<pointer>()(__ptr.get());
2833 }
2834};
2835
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836struct __destruct_n
2837{
2838private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002839 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840
2841 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002842 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002843 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844
2845 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002846 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847 {}
2848
Howard Hinnant719bda32011-05-28 14:41:13 +00002849 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002850 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002851 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852 {}
2853
Howard Hinnant719bda32011-05-28 14:41:13 +00002854 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002855 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002856 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 {}
2858public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002859 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00002860 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861
2862 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002863 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002864 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865
2866 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002867 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002868 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869
2870 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002871 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002872 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873};
2874
2875template <class _Alloc>
2876class __allocator_destructor
2877{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002878 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002880 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
2881 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882private:
2883 _Alloc& __alloc_;
2884 size_type __s_;
2885public:
2886 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002887 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002890 void operator()(pointer __p) _NOEXCEPT
2891 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892};
2893
2894template <class _InputIterator, class _ForwardIterator>
2895_ForwardIterator
2896uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2897{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002899#ifndef _LIBCPP_NO_EXCEPTIONS
2900 _ForwardIterator __s = __r;
2901 try
2902 {
2903#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002904 for (; __f != __l; ++__f, (void) ++__r)
2905 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002906#ifndef _LIBCPP_NO_EXCEPTIONS
2907 }
2908 catch (...)
2909 {
2910 for (; __s != __r; ++__s)
2911 __s->~value_type();
2912 throw;
2913 }
2914#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 return __r;
2916}
2917
2918template <class _InputIterator, class _Size, class _ForwardIterator>
2919_ForwardIterator
2920uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2921{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002923#ifndef _LIBCPP_NO_EXCEPTIONS
2924 _ForwardIterator __s = __r;
2925 try
2926 {
2927#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002928 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
2929 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002930#ifndef _LIBCPP_NO_EXCEPTIONS
2931 }
2932 catch (...)
2933 {
2934 for (; __s != __r; ++__s)
2935 __s->~value_type();
2936 throw;
2937 }
2938#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 return __r;
2940}
2941
2942template <class _ForwardIterator, class _Tp>
2943void
2944uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2945{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002947#ifndef _LIBCPP_NO_EXCEPTIONS
2948 _ForwardIterator __s = __f;
2949 try
2950 {
2951#endif
2952 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002953 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002954#ifndef _LIBCPP_NO_EXCEPTIONS
2955 }
2956 catch (...)
2957 {
2958 for (; __s != __f; ++__s)
2959 __s->~value_type();
2960 throw;
2961 }
2962#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963}
2964
2965template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00002966_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2968{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002970#ifndef _LIBCPP_NO_EXCEPTIONS
2971 _ForwardIterator __s = __f;
2972 try
2973 {
2974#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00002975 for (; __n > 0; ++__f, (void) --__n)
2976 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002977#ifndef _LIBCPP_NO_EXCEPTIONS
2978 }
2979 catch (...)
2980 {
2981 for (; __s != __f; ++__s)
2982 __s->~value_type();
2983 throw;
2984 }
2985#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00002986 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987}
2988
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002989#if _LIBCPP_STD_VER > 14
2990
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002991template <class _ForwardIterator>
Louis Dionne99f59432020-09-17 12:06:13 -04002992inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00002993void destroy(_ForwardIterator __first, _ForwardIterator __last) {
2994 for (; __first != __last; ++__first)
2995 _VSTD::destroy_at(_VSTD::addressof(*__first));
2996}
2997
2998template <class _ForwardIterator, class _Size>
Louis Dionne99f59432020-09-17 12:06:13 -04002999inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003000_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3001 for (; __n > 0; (void)++__first, --__n)
3002 _VSTD::destroy_at(_VSTD::addressof(*__first));
3003 return __first;
3004}
3005
Eric Fiselier290c07c2016-10-11 21:13:44 +00003006template <class _ForwardIterator>
3007inline _LIBCPP_INLINE_VISIBILITY
3008void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3009 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3010 auto __idx = __first;
3011#ifndef _LIBCPP_NO_EXCEPTIONS
3012 try {
3013#endif
3014 for (; __idx != __last; ++__idx)
3015 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3016#ifndef _LIBCPP_NO_EXCEPTIONS
3017 } catch (...) {
3018 _VSTD::destroy(__first, __idx);
3019 throw;
3020 }
3021#endif
3022}
3023
3024template <class _ForwardIterator, class _Size>
3025inline _LIBCPP_INLINE_VISIBILITY
3026_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3027 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3028 auto __idx = __first;
3029#ifndef _LIBCPP_NO_EXCEPTIONS
3030 try {
3031#endif
3032 for (; __n > 0; (void)++__idx, --__n)
3033 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3034 return __idx;
3035#ifndef _LIBCPP_NO_EXCEPTIONS
3036 } catch (...) {
3037 _VSTD::destroy(__first, __idx);
3038 throw;
3039 }
3040#endif
3041}
3042
3043
3044template <class _ForwardIterator>
3045inline _LIBCPP_INLINE_VISIBILITY
3046void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3047 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3048 auto __idx = __first;
3049#ifndef _LIBCPP_NO_EXCEPTIONS
3050 try {
3051#endif
3052 for (; __idx != __last; ++__idx)
3053 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3054#ifndef _LIBCPP_NO_EXCEPTIONS
3055 } catch (...) {
3056 _VSTD::destroy(__first, __idx);
3057 throw;
3058 }
3059#endif
3060}
3061
3062template <class _ForwardIterator, class _Size>
3063inline _LIBCPP_INLINE_VISIBILITY
3064_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3065 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3066 auto __idx = __first;
3067#ifndef _LIBCPP_NO_EXCEPTIONS
3068 try {
3069#endif
3070 for (; __n > 0; (void)++__idx, --__n)
3071 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3072 return __idx;
3073#ifndef _LIBCPP_NO_EXCEPTIONS
3074 } catch (...) {
3075 _VSTD::destroy(__first, __idx);
3076 throw;
3077 }
3078#endif
3079}
3080
3081
3082template <class _InputIt, class _ForwardIt>
3083inline _LIBCPP_INLINE_VISIBILITY
3084_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3085 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3086 auto __idx = __first_res;
3087#ifndef _LIBCPP_NO_EXCEPTIONS
3088 try {
3089#endif
3090 for (; __first != __last; (void)++__idx, ++__first)
3091 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3092 return __idx;
3093#ifndef _LIBCPP_NO_EXCEPTIONS
3094 } catch (...) {
3095 _VSTD::destroy(__first_res, __idx);
3096 throw;
3097 }
3098#endif
3099}
3100
3101template <class _InputIt, class _Size, class _ForwardIt>
3102inline _LIBCPP_INLINE_VISIBILITY
3103pair<_InputIt, _ForwardIt>
3104uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3105 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3106 auto __idx = __first_res;
3107#ifndef _LIBCPP_NO_EXCEPTIONS
3108 try {
3109#endif
3110 for (; __n > 0; ++__idx, (void)++__first, --__n)
3111 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3112 return {__first, __idx};
3113#ifndef _LIBCPP_NO_EXCEPTIONS
3114 } catch (...) {
3115 _VSTD::destroy(__first_res, __idx);
3116 throw;
3117 }
3118#endif
3119}
3120
3121
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003122#endif // _LIBCPP_STD_VER > 14
3123
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003124// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3125// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003126// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003127#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3128 && defined(__ATOMIC_RELAXED) \
3129 && defined(__ATOMIC_ACQ_REL)
3130# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003131#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003132# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3133#endif
3134
3135template <class _Tp>
3136inline _LIBCPP_INLINE_VISIBILITY _Tp
3137__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3138{
3139#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3140 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3141#else
3142 return __t += 1;
3143#endif
3144}
3145
3146template <class _Tp>
3147inline _LIBCPP_INLINE_VISIBILITY _Tp
3148__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3149{
3150#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3151 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3152#else
3153 return __t -= 1;
3154#endif
3155}
3156
Howard Hinnant756c69b2010-09-22 16:48:34 +00003157class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 : public std::exception
3159{
3160public:
Dimitry Andric47269ce2020-03-13 19:36:26 +01003161 bad_weak_ptr() _NOEXCEPT = default;
3162 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 virtual ~bad_weak_ptr() _NOEXCEPT;
3164 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165};
3166
Louis Dionne16fe2952018-07-11 23:14:33 +00003167_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003168void __throw_bad_weak_ptr()
3169{
3170#ifndef _LIBCPP_NO_EXCEPTIONS
3171 throw bad_weak_ptr();
3172#else
3173 _VSTD::abort();
3174#endif
3175}
3176
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003177template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003179class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180{
3181 __shared_count(const __shared_count&);
3182 __shared_count& operator=(const __shared_count&);
3183
3184protected:
3185 long __shared_owners_;
3186 virtual ~__shared_count();
3187private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003188 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189
3190public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003192 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193 : __shared_owners_(__refs) {}
3194
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003195#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003196 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003197 void __add_shared() _NOEXCEPT;
3198 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003199#else
3200 _LIBCPP_INLINE_VISIBILITY
3201 void __add_shared() _NOEXCEPT {
3202 __libcpp_atomic_refcount_increment(__shared_owners_);
3203 }
3204 _LIBCPP_INLINE_VISIBILITY
3205 bool __release_shared() _NOEXCEPT {
3206 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3207 __on_zero_shared();
3208 return true;
3209 }
3210 return false;
3211 }
3212#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003213 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003214 long use_count() const _NOEXCEPT {
3215 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3216 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217};
3218
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003219class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220 : private __shared_count
3221{
3222 long __shared_weak_owners_;
3223
3224public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003226 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227 : __shared_count(__refs),
3228 __shared_weak_owners_(__refs) {}
3229protected:
3230 virtual ~__shared_weak_count();
3231
3232public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003233#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003234 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003235 void __add_shared() _NOEXCEPT;
3236 void __add_weak() _NOEXCEPT;
3237 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003238#else
3239 _LIBCPP_INLINE_VISIBILITY
3240 void __add_shared() _NOEXCEPT {
3241 __shared_count::__add_shared();
3242 }
3243 _LIBCPP_INLINE_VISIBILITY
3244 void __add_weak() _NOEXCEPT {
3245 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3246 }
3247 _LIBCPP_INLINE_VISIBILITY
3248 void __release_shared() _NOEXCEPT {
3249 if (__shared_count::__release_shared())
3250 __release_weak();
3251 }
3252#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003253 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003255 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3256 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257
Howard Hinnant807d6332013-02-25 15:50:36 +00003258 // Define the function out only if we build static libc++ without RTTI.
3259 // Otherwise we may break clients who need to compile their projects with
3260 // -fno-rtti and yet link against a libc++.dylib compiled
3261 // without -fno-rtti.
3262#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003263 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003264#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003266 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267};
3268
3269template <class _Tp, class _Dp, class _Alloc>
3270class __shared_ptr_pointer
3271 : public __shared_weak_count
3272{
3273 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3274public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003277 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278
Howard Hinnant72f73582010-08-11 17:04:31 +00003279#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003280 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003281#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282
3283private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003284 virtual void __on_zero_shared() _NOEXCEPT;
3285 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286};
3287
Howard Hinnant72f73582010-08-11 17:04:31 +00003288#ifndef _LIBCPP_NO_RTTI
3289
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290template <class _Tp, class _Dp, class _Alloc>
3291const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003292__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003294 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295}
3296
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003297#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003298
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299template <class _Tp, class _Dp, class _Alloc>
3300void
Howard Hinnant719bda32011-05-28 14:41:13 +00003301__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302{
3303 __data_.first().second()(__data_.first().first());
3304 __data_.first().second().~_Dp();
3305}
3306
3307template <class _Tp, class _Dp, class _Alloc>
3308void
Howard Hinnant719bda32011-05-28 14:41:13 +00003309__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003311 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3312 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003313 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3314
Eric Fiselierf8898c82015-02-05 23:01:40 +00003315 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003317 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318}
3319
3320template <class _Tp, class _Alloc>
3321class __shared_ptr_emplace
3322 : public __shared_weak_count
3323{
3324 __compressed_pair<_Alloc, _Tp> __data_;
3325public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326
Howard Hinnant756c69b2010-09-22 16:48:34 +00003327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328 __shared_ptr_emplace(_Alloc __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05003329 : __data_(_VSTD::move(__a), __value_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003331#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003335 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3336 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003337#else
3338 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003339 _LIBCPP_INLINE_VISIBILITY
Louis Dionne1a1fc9d2020-07-30 10:00:53 -04003340 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3341 : __data_(__a, _Tp(_VSTD::forward<_Args>(__args)...)) {}
3342#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343
3344private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003345 virtual void __on_zero_shared() _NOEXCEPT;
3346 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003348 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003349 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350};
3351
3352template <class _Tp, class _Alloc>
3353void
Howard Hinnant719bda32011-05-28 14:41:13 +00003354__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355{
3356 __data_.second().~_Tp();
3357}
3358
3359template <class _Tp, class _Alloc>
3360void
Howard Hinnant719bda32011-05-28 14:41:13 +00003361__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003363 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3364 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003365 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003366 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003368 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369}
3370
Erik Pilkington2a398762017-05-25 15:43:31 +00003371struct __shared_ptr_dummy_rebind_allocator_type;
3372template <>
3373class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3374{
3375public:
3376 template <class _Other>
3377 struct rebind
3378 {
3379 typedef allocator<_Other> other;
3380 };
3381};
3382
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003383template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384
zoecarverd73f42a2020-05-11 18:42:50 -07003385template<class _Tp, class _Up>
3386struct __compatible_with
3387#if _LIBCPP_STD_VER > 14
3388 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
3389#else
3390 : is_convertible<_Tp*, _Up*> {};
3391#endif // _LIBCPP_STD_VER > 14
3392
Vy Nguyene369bd92020-07-13 12:34:37 -04003393#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
3394# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
3395#else
3396# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
3397#endif
3398
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04003400class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003402public:
Eric Fiselierae5b6672016-06-27 01:02:43 +00003403#if _LIBCPP_STD_VER > 14
3404 typedef weak_ptr<_Tp> weak_type;
zoecarverd73f42a2020-05-11 18:42:50 -07003405 typedef remove_extent_t<_Tp> element_type;
3406#else
3407 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003408#endif
zoecarverd73f42a2020-05-11 18:42:50 -07003409
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410private:
3411 element_type* __ptr_;
3412 __shared_weak_count* __cntrl_;
3413
3414 struct __nat {int __for_bool_;};
3415public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003417 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003419 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003420 template<class _Yp>
3421 explicit shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003422 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003423 template<class _Yp, class _Dp>
3424 shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003425 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Logan Chiend435f8b2014-01-31 09:30:46 +00003426 template<class _Yp, class _Dp, class _Alloc>
3427 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003428 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3430 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003431 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003433 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003437 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003438 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003440 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003441 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003442 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003443 _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003445 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003446#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Logan Chiend435f8b2014-01-31 09:30:46 +00003447 template<class _Yp>
3448 shared_ptr(auto_ptr<_Yp>&& __r,
3449 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003450#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003451 template <class _Yp, class _Dp>
3452 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3453 typename enable_if
3454 <
3455 !is_lvalue_reference<_Dp>::value &&
3456 !is_array<_Yp>::value &&
3457 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3458 __nat
3459 >::type = __nat());
3460 template <class _Yp, class _Dp>
3461 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3462 typename enable_if
3463 <
3464 is_lvalue_reference<_Dp>::value &&
3465 !is_array<_Yp>::value &&
3466 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3467 __nat
3468 >::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469
3470 ~shared_ptr();
3471
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003473 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003474 template<class _Yp>
3475 typename enable_if
3476 <
zoecarverd73f42a2020-05-11 18:42:50 -07003477 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003478 shared_ptr&
3479 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003481 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003483 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003484 template<class _Yp>
3485 typename enable_if
3486 <
zoecarverd73f42a2020-05-11 18:42:50 -07003487 __compatible_with<_Yp, element_type>::value,
3488 shared_ptr&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003489 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003491 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003492#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003493 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003495 typename enable_if
3496 <
3497 !is_array<_Yp>::value &&
3498 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003499 shared_ptr
3500 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003501 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003502#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003503 template <class _Yp, class _Dp>
3504 typename enable_if
3505 <
3506 !is_array<_Yp>::value &&
3507 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3508 shared_ptr&
3509 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003511 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003514 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003516 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003517 template<class _Yp>
3518 typename enable_if
3519 <
zoecarverd73f42a2020-05-11 18:42:50 -07003520 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003521 void
3522 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003524 reset(_Yp* __p);
3525 template<class _Yp, class _Dp>
3526 typename enable_if
3527 <
zoecarverd73f42a2020-05-11 18:42:50 -07003528 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003529 void
3530 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003532 reset(_Yp* __p, _Dp __d);
3533 template<class _Yp, class _Dp, class _Alloc>
3534 typename enable_if
3535 <
zoecarverd73f42a2020-05-11 18:42:50 -07003536 __compatible_with<_Yp, element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003537 void
3538 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003540 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541
Howard Hinnant756c69b2010-09-22 16:48:34 +00003542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003543 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003545 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3546 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003547 _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07003548 element_type* operator->() const _NOEXCEPT
3549 {
3550 static_assert(!_VSTD::is_array<_Tp>::value,
3551 "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
3552 return __ptr_;
3553 }
Howard Hinnant756c69b2010-09-22 16:48:34 +00003554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003555 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003557 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003559 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003560 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003561 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003562 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003564 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003565 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003566 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003568 _LIBCPP_INLINE_VISIBILITY
3569 bool
3570 __owner_equivalent(const shared_ptr& __p) const
3571 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572
zoecarverd73f42a2020-05-11 18:42:50 -07003573#if _LIBCPP_STD_VER > 14
3574 typename add_lvalue_reference<element_type>::type
3575 _LIBCPP_INLINE_VISIBILITY
3576 operator[](ptrdiff_t __i) const
3577 {
3578 static_assert(_VSTD::is_array<_Tp>::value,
3579 "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
3580 return __ptr_[__i];
3581 }
3582#endif
3583
Howard Hinnant72f73582010-08-11 17:04:31 +00003584#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003587 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003588 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003589 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003590 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003591#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592
Zoe Carverd9040c72019-10-22 15:16:49 +00003593 template<class _Yp, class _CntrlBlk>
3594 static shared_ptr<_Tp>
zoecarver867d3112020-05-19 17:17:16 -07003595 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
Zoe Carverd9040c72019-10-22 15:16:49 +00003596 {
3597 shared_ptr<_Tp> __r;
3598 __r.__ptr_ = __p;
3599 __r.__cntrl_ = __cntrl;
3600 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3601 return __r;
3602 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003603
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003605 template <class _Yp, bool = is_function<_Yp>::value>
3606 struct __shared_ptr_default_allocator
3607 {
3608 typedef allocator<_Yp> type;
3609 };
3610
3611 template <class _Yp>
3612 struct __shared_ptr_default_allocator<_Yp, true>
3613 {
3614 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3615 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003617 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003618 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003619 typename enable_if<is_convertible<_OrigPtr*,
3620 const enable_shared_from_this<_Yp>*
3621 >::value,
3622 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003623 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3624 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003626 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003627 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003628 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003629 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3630 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003631 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632 }
3633
Erik Pilkington2a398762017-05-25 15:43:31 +00003634 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635
zoecarverd73f42a2020-05-11 18:42:50 -07003636 template <class, class _Yp>
3637 struct __shared_ptr_default_delete
3638 : default_delete<_Yp> {};
3639
3640 template <class _Yp, class _Un, size_t _Sz>
3641 struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
3642 : default_delete<_Yp[]> {};
3643
3644 template <class _Yp, class _Un>
3645 struct __shared_ptr_default_delete<_Yp[], _Un>
3646 : default_delete<_Yp[]> {};
3647
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003648 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3649 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650};
3651
Logan Smitha5e4d7e2020-05-07 12:07:01 -04003652#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3653template<class _Tp>
3654shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
3655template<class _Tp, class _Dp>
3656shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
3657#endif
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003658
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003660inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003661_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003662shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663 : __ptr_(0),
3664 __cntrl_(0)
3665{
3666}
3667
3668template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003669inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003670_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003671shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672 : __ptr_(0),
3673 __cntrl_(0)
3674{
3675}
3676
3677template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003678template<class _Yp>
3679shared_ptr<_Tp>::shared_ptr(_Yp* __p,
zoecarverd73f42a2020-05-11 18:42:50 -07003680 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681 : __ptr_(__p)
3682{
3683 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003684 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
zoecarverd73f42a2020-05-11 18:42:50 -07003685 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
3686 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003688 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689}
3690
3691template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003692template<class _Yp, class _Dp>
3693shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
zoecarverd73f42a2020-05-11 18:42:50 -07003694 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695 : __ptr_(__p)
3696{
3697#ifndef _LIBCPP_NO_EXCEPTIONS
3698 try
3699 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003700#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003701 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3702 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3703 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003704 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705#ifndef _LIBCPP_NO_EXCEPTIONS
3706 }
3707 catch (...)
3708 {
3709 __d(__p);
3710 throw;
3711 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003712#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713}
3714
3715template<class _Tp>
3716template<class _Dp>
3717shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3718 : __ptr_(0)
3719{
3720#ifndef _LIBCPP_NO_EXCEPTIONS
3721 try
3722 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003723#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003724 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3725 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3726 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727#ifndef _LIBCPP_NO_EXCEPTIONS
3728 }
3729 catch (...)
3730 {
3731 __d(__p);
3732 throw;
3733 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003734#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735}
3736
3737template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003738template<class _Yp, class _Dp, class _Alloc>
3739shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
zoecarverd73f42a2020-05-11 18:42:50 -07003740 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741 : __ptr_(__p)
3742{
3743#ifndef _LIBCPP_NO_EXCEPTIONS
3744 try
3745 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003746#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003748 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749 typedef __allocator_destructor<_A2> _D2;
3750 _A2 __a2(__a);
3751 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003752 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3753 _CntrlBlk(__p, __d, __a);
3754 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003755 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756#ifndef _LIBCPP_NO_EXCEPTIONS
3757 }
3758 catch (...)
3759 {
3760 __d(__p);
3761 throw;
3762 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003763#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764}
3765
3766template<class _Tp>
3767template<class _Dp, class _Alloc>
3768shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3769 : __ptr_(0)
3770{
3771#ifndef _LIBCPP_NO_EXCEPTIONS
3772 try
3773 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003774#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003776 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777 typedef __allocator_destructor<_A2> _D2;
3778 _A2 __a2(__a);
3779 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003780 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3781 _CntrlBlk(__p, __d, __a);
3782 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783#ifndef _LIBCPP_NO_EXCEPTIONS
3784 }
3785 catch (...)
3786 {
3787 __d(__p);
3788 throw;
3789 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003790#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _Tp>
3794template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003795inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003796shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797 : __ptr_(__p),
3798 __cntrl_(__r.__cntrl_)
3799{
3800 if (__cntrl_)
3801 __cntrl_->__add_shared();
3802}
3803
3804template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003805inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003806shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807 : __ptr_(__r.__ptr_),
3808 __cntrl_(__r.__cntrl_)
3809{
3810 if (__cntrl_)
3811 __cntrl_->__add_shared();
3812}
3813
3814template<class _Tp>
3815template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003816inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003818 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003819 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820 : __ptr_(__r.__ptr_),
3821 __cntrl_(__r.__cntrl_)
3822{
3823 if (__cntrl_)
3824 __cntrl_->__add_shared();
3825}
3826
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003828inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003829shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830 : __ptr_(__r.__ptr_),
3831 __cntrl_(__r.__cntrl_)
3832{
3833 __r.__ptr_ = 0;
3834 __r.__cntrl_ = 0;
3835}
3836
3837template<class _Tp>
3838template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003839inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
zoecarverd73f42a2020-05-11 18:42:50 -07003841 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003842 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 : __ptr_(__r.__ptr_),
3844 __cntrl_(__r.__cntrl_)
3845{
3846 __r.__ptr_ = 0;
3847 __r.__cntrl_ = 0;
3848}
3849
Marshall Clowb22274f2017-01-24 22:22:33 +00003850#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003852template<class _Yp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003853shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003854 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855 : __ptr_(__r.get())
3856{
3857 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3858 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003859 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860 __r.release();
3861}
Marshall Clowb22274f2017-01-24 22:22:33 +00003862#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863
3864template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003865template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003867 typename enable_if
3868 <
3869 !is_lvalue_reference<_Dp>::value &&
3870 !is_array<_Yp>::value &&
3871 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3872 __nat
3873 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874 : __ptr_(__r.get())
3875{
Marshall Clow35cde742015-05-10 13:59:45 +00003876#if _LIBCPP_STD_VER > 11
3877 if (__ptr_ == nullptr)
3878 __cntrl_ = nullptr;
3879 else
3880#endif
3881 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003882 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3883 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3884 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003885 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003886 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887 __r.release();
3888}
3889
3890template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003891template <class _Yp, class _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
Logan Chiend435f8b2014-01-31 09:30:46 +00003893 typename enable_if
3894 <
3895 is_lvalue_reference<_Dp>::value &&
3896 !is_array<_Yp>::value &&
3897 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3898 __nat
3899 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900 : __ptr_(__r.get())
3901{
Marshall Clow35cde742015-05-10 13:59:45 +00003902#if _LIBCPP_STD_VER > 11
3903 if (__ptr_ == nullptr)
3904 __cntrl_ = nullptr;
3905 else
3906#endif
3907 {
Erik Pilkington2a398762017-05-25 15:43:31 +00003908 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00003909 typedef __shared_ptr_pointer<_Yp*,
3910 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00003911 _AllocT > _CntrlBlk;
Logan Smith85cb0812020-02-20 12:23:36 -05003912 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003913 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00003914 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 __r.release();
3916}
3917
Zoe Carver6cd05c32019-08-19 15:47:16 +00003918template<class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919shared_ptr<_Tp>::~shared_ptr()
3920{
3921 if (__cntrl_)
3922 __cntrl_->__release_shared();
3923}
3924
3925template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003926inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003928shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929{
3930 shared_ptr(__r).swap(*this);
3931 return *this;
3932}
3933
3934template<class _Tp>
3935template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003936inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003937typename enable_if
3938<
zoecarverd73f42a2020-05-11 18:42:50 -07003939 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003940 shared_ptr<_Tp>&
3941>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003942shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943{
3944 shared_ptr(__r).swap(*this);
3945 return *this;
3946}
3947
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003949inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00003951shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003953 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954 return *this;
3955}
3956
3957template<class _Tp>
3958template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003959inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003960typename enable_if
3961<
zoecarverd73f42a2020-05-11 18:42:50 -07003962 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003963 shared_ptr<_Tp>&
3964>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3966{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003967 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 return *this;
3969}
3970
Marshall Clowb22274f2017-01-24 22:22:33 +00003971#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972template<class _Tp>
3973template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003974inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003975typename enable_if
3976<
3977 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00003978 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003979 shared_ptr<_Tp>
3980>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3982{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003983 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003984 return *this;
3985}
Marshall Clowb22274f2017-01-24 22:22:33 +00003986#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987
3988template<class _Tp>
3989template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003990inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003991typename enable_if
3992<
3993 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00003994 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00003995 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003996 shared_ptr<_Tp>&
3997>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3999{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004000 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001 return *this;
4002}
4003
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004005inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006void
Howard Hinnant719bda32011-05-28 14:41:13 +00004007shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004009 _VSTD::swap(__ptr_, __r.__ptr_);
4010 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011}
4012
4013template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004014inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015void
Howard Hinnant719bda32011-05-28 14:41:13 +00004016shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017{
4018 shared_ptr().swap(*this);
4019}
4020
4021template<class _Tp>
4022template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004023inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004024typename enable_if
4025<
zoecarverd73f42a2020-05-11 18:42:50 -07004026 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004027 void
4028>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029shared_ptr<_Tp>::reset(_Yp* __p)
4030{
4031 shared_ptr(__p).swap(*this);
4032}
4033
4034template<class _Tp>
4035template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004036inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004037typename enable_if
4038<
zoecarverd73f42a2020-05-11 18:42:50 -07004039 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004040 void
4041>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4043{
4044 shared_ptr(__p, __d).swap(*this);
4045}
4046
4047template<class _Tp>
4048template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004049inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004050typename enable_if
4051<
zoecarverd73f42a2020-05-11 18:42:50 -07004052 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004053 void
4054>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4056{
4057 shared_ptr(__p, __d, __a).swap(*this);
4058}
4059
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004060template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004062typename enable_if
4063<
4064 !is_array<_Tp>::value,
4065 shared_ptr<_Tp>
4066>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067make_shared(_Args&& ...__args)
4068{
Zoe Carverd9040c72019-10-22 15:16:49 +00004069 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4070 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4071 typedef allocator<_CntrlBlk> _A2;
4072 typedef __allocator_destructor<_A2> _D2;
4073
4074 _A2 __a2;
4075 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4076 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4077
4078 _Tp *__ptr = __hold2.get()->get();
4079 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080}
4081
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004082template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004084typename enable_if
4085<
4086 !is_array<_Tp>::value,
4087 shared_ptr<_Tp>
4088>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089allocate_shared(const _Alloc& __a, _Args&& ...__args)
4090{
zoecarver505730a2020-02-25 16:50:57 -08004091 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared");
4092
4093 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4094 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4095 typedef __allocator_destructor<_A2> _D2;
4096
4097 _A2 __a2(__a);
4098 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4099 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4100 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4101
4102 typename shared_ptr<_Tp>::element_type *__p = __hold2.get()->get();
4103 return shared_ptr<_Tp>::__create_with_control_block(__p, _VSTD::addressof(*__hold2.release()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104}
4105
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106template<class _Tp, class _Up>
4107inline _LIBCPP_INLINE_VISIBILITY
4108bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004109operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110{
4111 return __x.get() == __y.get();
4112}
4113
4114template<class _Tp, class _Up>
4115inline _LIBCPP_INLINE_VISIBILITY
4116bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004117operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
4119 return !(__x == __y);
4120}
4121
4122template<class _Tp, class _Up>
4123inline _LIBCPP_INLINE_VISIBILITY
4124bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004125operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004127#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004128 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4129 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004130#else
4131 return less<>()(__x.get(), __y.get());
4132#endif
4133
Howard Hinnantb17caf92012-02-21 21:02:58 +00004134}
4135
4136template<class _Tp, class _Up>
4137inline _LIBCPP_INLINE_VISIBILITY
4138bool
4139operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4140{
4141 return __y < __x;
4142}
4143
4144template<class _Tp, class _Up>
4145inline _LIBCPP_INLINE_VISIBILITY
4146bool
4147operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4148{
4149 return !(__y < __x);
4150}
4151
4152template<class _Tp, class _Up>
4153inline _LIBCPP_INLINE_VISIBILITY
4154bool
4155operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4156{
4157 return !(__x < __y);
4158}
4159
4160template<class _Tp>
4161inline _LIBCPP_INLINE_VISIBILITY
4162bool
4163operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4164{
4165 return !__x;
4166}
4167
4168template<class _Tp>
4169inline _LIBCPP_INLINE_VISIBILITY
4170bool
4171operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4172{
4173 return !__x;
4174}
4175
4176template<class _Tp>
4177inline _LIBCPP_INLINE_VISIBILITY
4178bool
4179operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4180{
4181 return static_cast<bool>(__x);
4182}
4183
4184template<class _Tp>
4185inline _LIBCPP_INLINE_VISIBILITY
4186bool
4187operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4188{
4189 return static_cast<bool>(__x);
4190}
4191
4192template<class _Tp>
4193inline _LIBCPP_INLINE_VISIBILITY
4194bool
4195operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4196{
4197 return less<_Tp*>()(__x.get(), nullptr);
4198}
4199
4200template<class _Tp>
4201inline _LIBCPP_INLINE_VISIBILITY
4202bool
4203operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4204{
4205 return less<_Tp*>()(nullptr, __x.get());
4206}
4207
4208template<class _Tp>
4209inline _LIBCPP_INLINE_VISIBILITY
4210bool
4211operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4212{
4213 return nullptr < __x;
4214}
4215
4216template<class _Tp>
4217inline _LIBCPP_INLINE_VISIBILITY
4218bool
4219operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4220{
4221 return __x < nullptr;
4222}
4223
4224template<class _Tp>
4225inline _LIBCPP_INLINE_VISIBILITY
4226bool
4227operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4228{
4229 return !(nullptr < __x);
4230}
4231
4232template<class _Tp>
4233inline _LIBCPP_INLINE_VISIBILITY
4234bool
4235operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4236{
4237 return !(__x < nullptr);
4238}
4239
4240template<class _Tp>
4241inline _LIBCPP_INLINE_VISIBILITY
4242bool
4243operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4244{
4245 return !(__x < nullptr);
4246}
4247
4248template<class _Tp>
4249inline _LIBCPP_INLINE_VISIBILITY
4250bool
4251operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4252{
4253 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254}
4255
4256template<class _Tp>
4257inline _LIBCPP_INLINE_VISIBILITY
4258void
Howard Hinnant719bda32011-05-28 14:41:13 +00004259swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260{
4261 __x.swap(__y);
4262}
4263
4264template<class _Tp, class _Up>
4265inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004266shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004267static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268{
zoecarverd73f42a2020-05-11 18:42:50 -07004269 return shared_ptr<_Tp>(__r,
4270 static_cast<
4271 typename shared_ptr<_Tp>::element_type*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272}
4273
4274template<class _Tp, class _Up>
4275inline _LIBCPP_INLINE_VISIBILITY
zoecarverd73f42a2020-05-11 18:42:50 -07004276shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004277dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278{
zoecarverd73f42a2020-05-11 18:42:50 -07004279 typedef typename shared_ptr<_Tp>::element_type _ET;
4280 _ET* __p = dynamic_cast<_ET*>(__r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4282}
4283
4284template<class _Tp, class _Up>
zoecarverd73f42a2020-05-11 18:42:50 -07004285shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004286const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287{
zoecarverd73f42a2020-05-11 18:42:50 -07004288 typedef typename shared_ptr<_Tp>::element_type _RTp;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004289 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290}
4291
zoecarverd73f42a2020-05-11 18:42:50 -07004292template<class _Tp, class _Up>
4293shared_ptr<_Tp>
4294reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
4295{
4296 return shared_ptr<_Tp>(__r,
4297 reinterpret_cast<
4298 typename shared_ptr<_Tp>::element_type*>(__r.get()));
4299}
4300
Howard Hinnant72f73582010-08-11 17:04:31 +00004301#ifndef _LIBCPP_NO_RTTI
4302
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303template<class _Dp, class _Tp>
4304inline _LIBCPP_INLINE_VISIBILITY
4305_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004306get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307{
4308 return __p.template __get_deleter<_Dp>();
4309}
4310
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004311#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004312
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313template<class _Tp>
Vy Nguyene369bd92020-07-13 12:34:37 -04004314class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004316public:
4317 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318private:
4319 element_type* __ptr_;
4320 __shared_weak_count* __cntrl_;
4321
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004322public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004324 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004325 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004326 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4327 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004329 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004330 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004331 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4332 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004333
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004335 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004336 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004337 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4338 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004339 ~weak_ptr();
4340
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004342 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004343 template<class _Yp>
4344 typename enable_if
4345 <
4346 is_convertible<_Yp*, element_type*>::value,
4347 weak_ptr&
4348 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004350 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4351
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004353 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4354 template<class _Yp>
4355 typename enable_if
4356 <
4357 is_convertible<_Yp*, element_type*>::value,
4358 weak_ptr&
4359 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004361 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4362
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004363 template<class _Yp>
4364 typename enable_if
4365 <
4366 is_convertible<_Yp*, element_type*>::value,
4367 weak_ptr&
4368 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004370 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004371
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004373 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004375 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004376
Howard Hinnant756c69b2010-09-22 16:48:34 +00004377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004378 long use_count() const _NOEXCEPT
4379 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004381 bool expired() const _NOEXCEPT
4382 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4383 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004384 template<class _Up>
4385 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004386 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004388 template<class _Up>
4389 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004390 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004391 {return __cntrl_ < __r.__cntrl_;}
4392
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004393 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4394 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395};
4396
Logan Smitha5e4d7e2020-05-07 12:07:01 -04004397#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
4398template<class _Tp>
4399weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
4400#endif
4401
Howard Hinnantc51e1022010-05-11 19:42:16 +00004402template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004403inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004404_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004405weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406 : __ptr_(0),
4407 __cntrl_(0)
4408{
4409}
4410
4411template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004412inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004413weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004414 : __ptr_(__r.__ptr_),
4415 __cntrl_(__r.__cntrl_)
4416{
4417 if (__cntrl_)
4418 __cntrl_->__add_weak();
4419}
4420
4421template<class _Tp>
4422template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004423inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004425 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004426 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427 : __ptr_(__r.__ptr_),
4428 __cntrl_(__r.__cntrl_)
4429{
4430 if (__cntrl_)
4431 __cntrl_->__add_weak();
4432}
4433
4434template<class _Tp>
4435template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004436inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004438 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004439 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440 : __ptr_(__r.__ptr_),
4441 __cntrl_(__r.__cntrl_)
4442{
4443 if (__cntrl_)
4444 __cntrl_->__add_weak();
4445}
4446
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004447template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004448inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004449weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4450 : __ptr_(__r.__ptr_),
4451 __cntrl_(__r.__cntrl_)
4452{
4453 __r.__ptr_ = 0;
4454 __r.__cntrl_ = 0;
4455}
4456
4457template<class _Tp>
4458template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004459inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004460weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4461 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4462 _NOEXCEPT
4463 : __ptr_(__r.__ptr_),
4464 __cntrl_(__r.__cntrl_)
4465{
4466 __r.__ptr_ = 0;
4467 __r.__cntrl_ = 0;
4468}
4469
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470template<class _Tp>
4471weak_ptr<_Tp>::~weak_ptr()
4472{
4473 if (__cntrl_)
4474 __cntrl_->__release_weak();
4475}
4476
4477template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004478inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004480weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481{
4482 weak_ptr(__r).swap(*this);
4483 return *this;
4484}
4485
4486template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004487template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004488inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004489typename enable_if
4490<
4491 is_convertible<_Yp*, _Tp*>::value,
4492 weak_ptr<_Tp>&
4493>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004494weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495{
4496 weak_ptr(__r).swap(*this);
4497 return *this;
4498}
4499
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004500template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004501inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004502weak_ptr<_Tp>&
4503weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4504{
4505 weak_ptr(_VSTD::move(__r)).swap(*this);
4506 return *this;
4507}
4508
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004510template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004511inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004512typename enable_if
4513<
4514 is_convertible<_Yp*, _Tp*>::value,
4515 weak_ptr<_Tp>&
4516>::type
4517weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4518{
4519 weak_ptr(_VSTD::move(__r)).swap(*this);
4520 return *this;
4521}
4522
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004523template<class _Tp>
4524template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004525inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004526typename enable_if
4527<
4528 is_convertible<_Yp*, _Tp*>::value,
4529 weak_ptr<_Tp>&
4530>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004531weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532{
4533 weak_ptr(__r).swap(*this);
4534 return *this;
4535}
4536
4537template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004538inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539void
Howard Hinnant719bda32011-05-28 14:41:13 +00004540weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004541{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004542 _VSTD::swap(__ptr_, __r.__ptr_);
4543 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544}
4545
4546template<class _Tp>
4547inline _LIBCPP_INLINE_VISIBILITY
4548void
Howard Hinnant719bda32011-05-28 14:41:13 +00004549swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004550{
4551 __x.swap(__y);
4552}
4553
4554template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004555inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556void
Howard Hinnant719bda32011-05-28 14:41:13 +00004557weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004558{
4559 weak_ptr().swap(*this);
4560}
4561
4562template<class _Tp>
4563template<class _Yp>
4564shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004565 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566 : __ptr_(__r.__ptr_),
4567 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4568{
4569 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004570 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571}
4572
4573template<class _Tp>
4574shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004575weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004576{
4577 shared_ptr<_Tp> __r;
4578 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4579 if (__r.__cntrl_)
4580 __r.__ptr_ = __ptr_;
4581 return __r;
4582}
4583
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004584#if _LIBCPP_STD_VER > 14
4585template <class _Tp = void> struct owner_less;
4586#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004587template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004588#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004589
4590template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004591struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004593{
4594 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004595 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004596 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004598 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004599 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004600 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004601 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004602 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004604};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605
4606template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004607struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004608 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4609{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004610 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004611 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004612 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004613 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004614 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004615 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004616 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004617 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004618 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004619 {return __x.owner_before(__y);}
4620};
4621
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004622#if _LIBCPP_STD_VER > 14
4623template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004624struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004625{
4626 template <class _Tp, class _Up>
4627 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004628 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004629 {return __x.owner_before(__y);}
4630 template <class _Tp, class _Up>
4631 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004632 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004633 {return __x.owner_before(__y);}
4634 template <class _Tp, class _Up>
4635 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004636 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004637 {return __x.owner_before(__y);}
4638 template <class _Tp, class _Up>
4639 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004640 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004641 {return __x.owner_before(__y);}
4642 typedef void is_transparent;
4643};
4644#endif
4645
Howard Hinnantc51e1022010-05-11 19:42:16 +00004646template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004647class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004648{
4649 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004650protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004651 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004652 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004654 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004656 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4657 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004659 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004660public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004662 shared_ptr<_Tp> shared_from_this()
4663 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004665 shared_ptr<_Tp const> shared_from_this() const
4666 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004667
Eric Fiselier84006862016-06-02 00:15:35 +00004668#if _LIBCPP_STD_VER > 14
4669 _LIBCPP_INLINE_VISIBILITY
4670 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
4671 { return __weak_this_; }
4672
4673 _LIBCPP_INLINE_VISIBILITY
4674 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
4675 { return __weak_this_; }
4676#endif // _LIBCPP_STD_VER > 14
4677
Howard Hinnantc51e1022010-05-11 19:42:16 +00004678 template <class _Up> friend class shared_ptr;
4679};
4680
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004681template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004682struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004683{
4684 typedef shared_ptr<_Tp> argument_type;
4685 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00004686
Howard Hinnant756c69b2010-09-22 16:48:34 +00004687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004688 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004689 {
zoecarverd73f42a2020-05-11 18:42:50 -07004690 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
Howard Hinnant36b31ae2010-06-03 16:42:57 +00004691 }
4692};
4693
Howard Hinnantc834c512011-11-29 18:15:50 +00004694template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00004695inline _LIBCPP_INLINE_VISIBILITY
4696basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00004697operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00004698
Eric Fiselier9b492672016-06-18 02:12:53 +00004699
4700#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004701
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004702class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00004703{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004704 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004705public:
4706 void lock() _NOEXCEPT;
4707 void unlock() _NOEXCEPT;
4708
4709private:
4710 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
4711 __sp_mut(const __sp_mut&);
4712 __sp_mut& operator=(const __sp_mut&);
4713
Howard Hinnant8331b762013-03-06 23:30:19 +00004714 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004715};
4716
Mehdi Amini228053d2017-05-04 17:08:54 +00004717_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
4718__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004719
4720template <class _Tp>
4721inline _LIBCPP_INLINE_VISIBILITY
4722bool
4723atomic_is_lock_free(const shared_ptr<_Tp>*)
4724{
4725 return false;
4726}
4727
4728template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004729_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004730shared_ptr<_Tp>
4731atomic_load(const shared_ptr<_Tp>* __p)
4732{
4733 __sp_mut& __m = __get_sp_mut(__p);
4734 __m.lock();
4735 shared_ptr<_Tp> __q = *__p;
4736 __m.unlock();
4737 return __q;
4738}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004739
Howard Hinnant9fa30202012-07-30 01:40:57 +00004740template <class _Tp>
4741inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004742_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004743shared_ptr<_Tp>
4744atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
4745{
4746 return atomic_load(__p);
4747}
4748
4749template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004750_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004751void
4752atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4753{
4754 __sp_mut& __m = __get_sp_mut(__p);
4755 __m.lock();
4756 __p->swap(__r);
4757 __m.unlock();
4758}
4759
4760template <class _Tp>
4761inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004762_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004763void
4764atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4765{
4766 atomic_store(__p, __r);
4767}
4768
4769template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004770_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004771shared_ptr<_Tp>
4772atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
4773{
4774 __sp_mut& __m = __get_sp_mut(__p);
4775 __m.lock();
4776 __p->swap(__r);
4777 __m.unlock();
4778 return __r;
4779}
Aditya Kumar7c5db692017-08-20 10:38:55 +00004780
Howard Hinnant9fa30202012-07-30 01:40:57 +00004781template <class _Tp>
4782inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004783_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004784shared_ptr<_Tp>
4785atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
4786{
4787 return atomic_exchange(__p, __r);
4788}
4789
4790template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00004791_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004792bool
4793atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4794{
Marshall Clow4201ee82016-05-18 17:50:13 +00004795 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00004796 __sp_mut& __m = __get_sp_mut(__p);
4797 __m.lock();
4798 if (__p->__owner_equivalent(*__v))
4799 {
Marshall Clow4201ee82016-05-18 17:50:13 +00004800 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004801 *__p = __w;
4802 __m.unlock();
4803 return true;
4804 }
Marshall Clow4201ee82016-05-18 17:50:13 +00004805 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00004806 *__v = *__p;
4807 __m.unlock();
4808 return false;
4809}
4810
4811template <class _Tp>
4812inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004813_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004814bool
4815atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
4816{
4817 return atomic_compare_exchange_strong(__p, __v, __w);
4818}
4819
4820template <class _Tp>
4821inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004822_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004823bool
4824atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4825 shared_ptr<_Tp> __w, memory_order, memory_order)
4826{
4827 return atomic_compare_exchange_strong(__p, __v, __w);
4828}
4829
4830template <class _Tp>
4831inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00004832_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00004833bool
4834atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
4835 shared_ptr<_Tp> __w, memory_order, memory_order)
4836{
4837 return atomic_compare_exchange_weak(__p, __v, __w);
4838}
4839
Eric Fiselier9b492672016-06-18 02:12:53 +00004840#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00004841
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004842//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00004843#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
4844# ifndef _LIBCPP_CXX03_LANG
4845enum class pointer_safety : unsigned char {
4846 relaxed,
4847 preferred,
4848 strict
4849};
4850# endif
4851#else
Howard Hinnant8331b762013-03-06 23:30:19 +00004852struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00004853{
Howard Hinnant49e145e2012-10-30 19:06:59 +00004854 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00004855 {
4856 relaxed,
4857 preferred,
4858 strict
4859 };
4860
Howard Hinnant49e145e2012-10-30 19:06:59 +00004861 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004862
Howard Hinnant756c69b2010-09-22 16:48:34 +00004863 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00004864 pointer_safety() : __v_() {}
4865
4866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00004867 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869 operator int() const {return __v_;}
4870};
Eric Fiselierab6bb302017-01-05 01:15:42 +00004871#endif
4872
4873#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00004874 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00004875_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
4876#else
4877// This function is only offered in C++03 under ABI v1.
4878# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
4879inline _LIBCPP_INLINE_VISIBILITY
4880pointer_safety get_pointer_safety() _NOEXCEPT {
4881 return pointer_safety::relaxed;
4882}
4883# endif
4884#endif
4885
Howard Hinnantc51e1022010-05-11 19:42:16 +00004886
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004887_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
4888_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
4889_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004890_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004891
4892template <class _Tp>
4893inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004894_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00004895undeclare_reachable(_Tp* __p)
4896{
4897 return static_cast<_Tp*>(__undeclare_reachable(__p));
4898}
4899
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004900_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004901
Marshall Clow8982dcd2015-07-13 20:04:56 +00004902// --- Helper for container swap --
4903template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004904inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004905void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
4906#if _LIBCPP_STD_VER >= 14
4907 _NOEXCEPT
4908#else
4909 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4910#endif
4911{
Aditya Kumar7c5db692017-08-20 10:38:55 +00004912 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00004913 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
4914}
4915
4916template <typename _Alloc>
4917_LIBCPP_INLINE_VISIBILITY
4918void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
4919#if _LIBCPP_STD_VER >= 14
4920 _NOEXCEPT
4921#else
4922 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
4923#endif
4924{
4925 using _VSTD::swap;
4926 swap(__a1, __a2);
4927}
4928
4929template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00004930inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00004931void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
4932
Marshall Clowff91de82015-08-18 19:51:37 +00004933template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00004934struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00004935 _Traits::propagate_on_container_move_assignment::value
4936#if _LIBCPP_STD_VER > 14
4937 || _Traits::is_always_equal::value
4938#else
4939 && is_nothrow_move_assignable<_Alloc>::value
4940#endif
4941 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00004942
Marshall Clowa591b9a2016-07-11 21:38:08 +00004943
Marshall Clowa591b9a2016-07-11 21:38:08 +00004944template <class _Tp, class _Alloc>
4945struct __temp_value {
4946 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00004947
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00004948 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00004949 _Alloc &__a;
4950
4951 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
4952 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004953
Marshall Clowa591b9a2016-07-11 21:38:08 +00004954 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00004955 _LIBCPP_NO_CFI
4956 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
4957 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
4958 _VSTD::forward<_Args>(__args)...);
4959 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00004960
Marshall Clowa591b9a2016-07-11 21:38:08 +00004961 ~__temp_value() { _Traits::destroy(__a, __addr()); }
4962 };
Marshall Clowa591b9a2016-07-11 21:38:08 +00004963
Marshall Clowe46031a2018-07-02 18:41:15 +00004964template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00004965struct __is_allocator : false_type {};
4966
4967template<typename _Alloc>
4968struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00004969 typename __void_t<typename _Alloc::value_type>::type,
4970 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
4971 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00004972 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00004973
Eric Fiselier74ebee62019-06-08 01:31:19 +00004974// __builtin_new_allocator -- A non-templated helper for allocating and
4975// deallocating memory using __builtin_operator_new and
4976// __builtin_operator_delete. It should be used in preference to
4977// `std::allocator<T>` to avoid additional instantiations.
4978struct __builtin_new_allocator {
4979 struct __builtin_new_deleter {
4980 typedef void* pointer_type;
4981
4982 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
4983 : __size_(__size), __align_(__align) {}
4984
4985 void operator()(void* p) const _NOEXCEPT {
4986 std::__libcpp_deallocate(p, __size_, __align_);
4987 }
4988
4989 private:
4990 size_t __size_;
4991 size_t __align_;
4992 };
4993
4994 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
4995
4996 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
4997 return __holder_t(std::__libcpp_allocate(__s, __align),
4998 __builtin_new_deleter(__s, __align));
4999 }
5000
5001 static void __deallocate_bytes(void* __p, size_t __s,
5002 size_t __align) _NOEXCEPT {
5003 std::__libcpp_deallocate(__p, __s, __align);
5004 }
5005
5006 template <class _Tp>
5007 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5008 static __holder_t __allocate_type(size_t __n) {
5009 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5010 }
5011
5012 template <class _Tp>
5013 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5014 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5015 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5016 }
5017};
5018
5019
Howard Hinnantc51e1022010-05-11 19:42:16 +00005020_LIBCPP_END_NAMESPACE_STD
5021
Eric Fiselierf4433a32017-05-31 22:07:49 +00005022_LIBCPP_POP_MACROS
5023
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005024#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005025# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005026#endif
5027
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028#endif // _LIBCPP_MEMORY