blob: 96bb8fb5ccbba6d52eeb924d69ef248c4bd81914 [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
83 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
84 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
85
Marshall Clow0e58cae2017-11-26 02:55:38 +000086 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant719bda32011-05-28 14:41:13 +000089 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
91 template <class T, class... Args>
92 static void construct(allocator_type& a, T* p, Args&&... args);
93
94 template <class T>
95 static void destroy(allocator_type& a, T* p);
96
Marshall Clow4f834b52013-08-27 20:22:15 +000097 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
99 static allocator_type
100 select_on_container_copy_construction(const allocator_type& a);
101};
102
103template <>
104class allocator<void>
105{
106public:
107 typedef void* pointer;
108 typedef const void* const_pointer;
109 typedef void value_type;
110
111 template <class _Up> struct rebind {typedef allocator<_Up> other;};
112};
113
114template <class T>
115class allocator
116{
117public:
118 typedef size_t size_type;
119 typedef ptrdiff_t difference_type;
120 typedef T* pointer;
121 typedef const T* const_pointer;
122 typedef typename add_lvalue_reference<T>::type reference;
123 typedef typename add_lvalue_reference<const T>::type const_reference;
124 typedef T value_type;
125
126 template <class U> struct rebind {typedef allocator<U> other;};
127
Marshall Clowbc759762018-03-20 23:02:53 +0000128 constexpr allocator() noexcept; // constexpr in C++20
129 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
130 template <class U>
131 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000132 ~allocator();
133 pointer address(reference x) const noexcept;
134 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000136 void deallocate(pointer p, size_type n) noexcept;
137 size_type max_size() const noexcept;
138 template<class U, class... Args>
139 void construct(U* p, Args&&... args);
140 template <class U>
141 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142};
143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000148bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
150template <class OutputIterator, class T>
151class raw_storage_iterator
152 : public iterator<output_iterator_tag,
153 T, // purposefully not C++03
154 ptrdiff_t, // purposefully not C++03
155 T*, // purposefully not C++03
156 raw_storage_iterator&> // purposefully not C++03
157{
158public:
159 explicit raw_storage_iterator(OutputIterator x);
160 raw_storage_iterator& operator*();
161 raw_storage_iterator& operator=(const T& element);
162 raw_storage_iterator& operator++();
163 raw_storage_iterator operator++(int);
164};
165
Howard Hinnant719bda32011-05-28 14:41:13 +0000166template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
167template <class T> void return_temporary_buffer(T* p) noexcept;
168
169template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000170template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class InputIterator, class ForwardIterator>
173ForwardIterator
174uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
175
Howard Hinnant719bda32011-05-28 14:41:13 +0000176template <class InputIterator, class Size, class ForwardIterator>
177ForwardIterator
178uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
179
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180template <class ForwardIterator, class T>
181void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
182
183template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000184ForwardIterator
185uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000187template <class T>
188void destroy_at(T* location);
189
190template <class ForwardIterator>
191 void destroy(ForwardIterator first, ForwardIterator last);
192
193template <class ForwardIterator, class Size>
194 ForwardIterator destroy_n(ForwardIterator first, Size n);
195
196template <class InputIterator, class ForwardIterator>
197 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
198
199template <class InputIterator, class Size, class ForwardIterator>
200 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
201
202template <class ForwardIterator>
203 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
204
205template <class ForwardIterator, class Size>
206 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
207
208template <class ForwardIterator>
209 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
210
211template <class ForwardIterator, class Size>
212 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
213
Louis Dionne481a2662018-09-23 18:35:00 +0000214template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
216template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000217class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218{
219public:
220 typedef X element_type;
221
222 explicit auto_ptr(X* p =0) throw();
223 auto_ptr(auto_ptr&) throw();
224 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
225 auto_ptr& operator=(auto_ptr&) throw();
226 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
227 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
228 ~auto_ptr() throw();
229
230 typename add_lvalue_reference<X>::type operator*() const throw();
231 X* operator->() const throw();
232 X* get() const throw();
233 X* release() throw();
234 void reset(X* p =0) throw();
235
236 auto_ptr(auto_ptr_ref<X>) throw();
237 template<class Y> operator auto_ptr_ref<Y>() throw();
238 template<class Y> operator auto_ptr<Y>() throw();
239};
240
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000241template <class T>
242struct default_delete
243{
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 constexpr default_delete() noexcept = default;
245 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000246
Howard Hinnant719bda32011-05-28 14:41:13 +0000247 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248};
249
250template <class T>
251struct default_delete<T[]>
252{
Howard Hinnant719bda32011-05-28 14:41:13 +0000253 constexpr default_delete() noexcept = default;
254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255 template <class U> void operator()(U*) const = delete;
256};
257
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000258template <class T, class D = default_delete<T>>
259class unique_ptr
260{
261public:
262 typedef see below pointer;
263 typedef T element_type;
264 typedef D deleter_type;
265
266 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000267 constexpr unique_ptr() noexcept;
268 explicit unique_ptr(pointer p) noexcept;
269 unique_ptr(pointer p, see below d1) noexcept;
270 unique_ptr(pointer p, see below d2) noexcept;
271 unique_ptr(unique_ptr&& u) noexcept;
272 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000273 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000275 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000276 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000277
278 // destructor
279 ~unique_ptr();
280
281 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr& operator=(unique_ptr&& u) noexcept;
283 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
284 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // observers
287 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000288 pointer operator->() const noexcept;
289 pointer get() const noexcept;
290 deleter_type& get_deleter() noexcept;
291 const deleter_type& get_deleter() const noexcept;
292 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer release() noexcept;
296 void reset(pointer p = pointer()) noexcept;
297 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000298};
299
300template <class T, class D>
301class unique_ptr<T[], D>
302{
303public:
304 typedef implementation-defined pointer;
305 typedef T element_type;
306 typedef D deleter_type;
307
308 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000309 constexpr unique_ptr() noexcept;
310 explicit unique_ptr(pointer p) noexcept;
311 unique_ptr(pointer p, see below d) noexcept;
312 unique_ptr(pointer p, see below d) noexcept;
313 unique_ptr(unique_ptr&& u) noexcept;
314 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000317 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000318
319 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000320 unique_ptr& operator=(unique_ptr&& u) noexcept;
321 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // observers
324 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000325 pointer get() const noexcept;
326 deleter_type& get_deleter() noexcept;
327 const deleter_type& get_deleter() const noexcept;
328 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000331 pointer release() noexcept;
332 void reset(pointer p = pointer()) noexcept;
333 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000334 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000335 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336};
337
338template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000340
341template <class T1, class D1, class T2, class D2>
342 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
343template <class T1, class D1, class T2, class D2>
344 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
345template <class T1, class D1, class T2, class D2>
346 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
347template <class T1, class D1, class T2, class D2>
348 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
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);
353
Howard Hinnant719bda32011-05-28 14:41:13 +0000354template <class T, class D>
355 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
356template <class T, class D>
357 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
358template <class T, class D>
359 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
360template <class T, class D>
361 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
362
363template <class T, class D>
364 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
365template <class T, class D>
366 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
367template <class T, class D>
368 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
369template <class T, class D>
370 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
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);
379
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000380class bad_weak_ptr
381 : public std::exception
382{
Howard Hinnant719bda32011-05-28 14:41:13 +0000383 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000384};
385
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000386template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
387template<class T> unique_ptr<T> make_unique(size_t n); // C++14
388template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
389
Marshall Clowe52a3242017-11-27 15:51:36 +0000390template<class E, class T, class Y, class D>
391 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
392
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393template<class T>
394class shared_ptr
395{
396public:
397 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000398 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000399
400 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000402 template<class Y> explicit shared_ptr(Y* p);
403 template<class Y, class D> shared_ptr(Y* p, D d);
404 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
405 template <class D> shared_ptr(nullptr_t p, D d);
406 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000407 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
408 shared_ptr(const shared_ptr& r) noexcept;
409 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
410 shared_ptr(shared_ptr&& r) noexcept;
411 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000412 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000413 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000414 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
415 shared_ptr(nullptr_t) : shared_ptr() { }
416
417 // destructor:
418 ~shared_ptr();
419
420 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000421 shared_ptr& operator=(const shared_ptr& r) noexcept;
422 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
423 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000424 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000425 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000426 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
427
428 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 void swap(shared_ptr& r) noexcept;
430 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> void reset(Y* p);
432 template<class Y, class D> void reset(Y* p, D d);
433 template<class Y, class D, class A> void reset(Y* p, D d, A a);
434
Howard Hinnant719bda32011-05-28 14:41:13 +0000435 // observers:
436 T* get() const noexcept;
437 T& operator*() const noexcept;
438 T* operator->() const noexcept;
439 long use_count() const noexcept;
440 bool unique() const noexcept;
441 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000442 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
443 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000444};
445
446// shared_ptr comparisons:
447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000453template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000454 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000455template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000456 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000457template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000458 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
459
460template <class T>
461 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
462template <class T>
463 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
464template <class T>
465 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
466template <class T>
467 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
468template <class T>
469 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
470template <class T>
471bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
472template <class T>
473 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
474template <class T>
475 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
476template <class T>
477 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
478template <class T>
479 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
480template <class T>
481 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
482template <class T>
483 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484
485// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000486template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487
488// shared_ptr casts:
489template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000490 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000492 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000493template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000494 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000495
496// shared_ptr I/O:
497template<class E, class T, class Y>
498 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
499
500// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000501template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503template<class T, class... Args>
504 shared_ptr<T> make_shared(Args&&... args);
505template<class T, class A, class... Args>
506 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
507
508template<class T>
509class weak_ptr
510{
511public:
512 typedef T element_type;
513
514 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000515 constexpr weak_ptr() noexcept;
516 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
517 weak_ptr(weak_ptr const& r) noexcept;
518 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000519 weak_ptr(weak_ptr&& r) noexcept; // C++14
520 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000521
522 // destructor
523 ~weak_ptr();
524
525 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000526 weak_ptr& operator=(weak_ptr const& r) noexcept;
527 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
528 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000529 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
530 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000531
532 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 void swap(weak_ptr& r) noexcept;
534 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000535
536 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000537 long use_count() const noexcept;
538 bool expired() const noexcept;
539 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000540 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
541 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542};
543
544// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000545template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000546
547// class owner_less:
548template<class T> struct owner_less;
549
550template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000551struct owner_less<shared_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
553{
554 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000555 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
556 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
557 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000558};
559
560template<class T>
Eric Fiselierea6fdf62019-06-23 20:47:21 +0000561struct owner_less<weak_ptr<T> >
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000562 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
563{
564 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000565 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
566 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
567 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
568};
569
570template <> // Added in C++14
571struct owner_less<void>
572{
573 template <class _Tp, class _Up>
574 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
575 template <class _Tp, class _Up>
576 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
577 template <class _Tp, class _Up>
578 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
579 template <class _Tp, class _Up>
580 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
581
582 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000583};
584
585template<class T>
586class enable_shared_from_this
587{
588protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000589 constexpr enable_shared_from_this() noexcept;
590 enable_shared_from_this(enable_shared_from_this const&) noexcept;
591 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000592 ~enable_shared_from_this();
593public:
594 shared_ptr<T> shared_from_this();
595 shared_ptr<T const> shared_from_this() const;
596};
597
598template<class T>
599 bool atomic_is_lock_free(const shared_ptr<T>* p);
600template<class T>
601 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
602template<class T>
603 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
604template<class T>
605 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
606template<class T>
607 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
608template<class T>
609 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
610template<class T>
611 shared_ptr<T>
612 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
613template<class T>
614 bool
615 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
616template<class T>
617 bool
618 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
619template<class T>
620 bool
621 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
622 shared_ptr<T> w, memory_order success,
623 memory_order failure);
624template<class T>
625 bool
626 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
627 shared_ptr<T> w, memory_order success,
628 memory_order failure);
629// Hash support
630template <class T> struct hash;
631template <class T, class D> struct hash<unique_ptr<T, D> >;
632template <class T> struct hash<shared_ptr<T> >;
633
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000634template <class T, class Alloc>
635 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
636
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000637// Pointer safety
638enum class pointer_safety { relaxed, preferred, strict };
639void declare_reachable(void *p);
640template <class T> T *undeclare_reachable(T *p);
641void declare_no_pointers(char *p, size_t n);
642void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000643pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000644
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
646
647} // std
648
649*/
650
651#include <__config>
652#include <type_traits>
653#include <typeinfo>
654#include <cstddef>
655#include <cstdint>
656#include <new>
657#include <utility>
658#include <limits>
659#include <iterator>
660#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000661#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000662#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000663#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000664#include <cstring>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000665#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000666# include <atomic>
667#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000668#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000669
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000670#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000672#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
Eric Fiselierf4433a32017-05-31 22:07:49 +0000674_LIBCPP_PUSH_MACROS
675#include <__undef_macros>
676
677
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678_LIBCPP_BEGIN_NAMESPACE_STD
679
Eric Fiselier89659d12015-07-07 00:27:16 +0000680template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000681inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000682_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
683#if !defined(_LIBCPP_HAS_NO_THREADS) && \
684 defined(__ATOMIC_RELAXED) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000685 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Eric Fiselier89659d12015-07-07 00:27:16 +0000686 return __atomic_load_n(__value, __ATOMIC_RELAXED);
687#else
688 return *__value;
689#endif
690}
691
Kuba Breckade9d6792016-09-04 09:55:12 +0000692template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000693inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000694_ValueType __libcpp_acquire_load(_ValueType const* __value) {
695#if !defined(_LIBCPP_HAS_NO_THREADS) && \
696 defined(__ATOMIC_ACQUIRE) && \
Richard Smithca47d0f2019-04-25 20:02:10 +0000697 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
Kuba Breckade9d6792016-09-04 09:55:12 +0000698 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
699#else
700 return *__value;
701#endif
702}
703
Marshall Clow78dbe462016-11-14 18:22:19 +0000704// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000705
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706template <class _Tp> class allocator;
707
708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710{
711public:
712 typedef void* pointer;
713 typedef const void* const_pointer;
714 typedef void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnant9f771052012-01-19 23:15:22 +0000719template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000720class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000721{
722public:
723 typedef const void* pointer;
724 typedef const void* const_pointer;
725 typedef const void value_type;
726
727 template <class _Up> struct rebind {typedef allocator<_Up> other;};
728};
729
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730// pointer_traits
731
Marshall Clow0be70c32017-06-14 21:23:57 +0000732template <class _Tp, class = void>
733struct __has_element_type : false_type {};
734
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000736struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000737 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738
739template <class _Ptr, bool = __has_element_type<_Ptr>::value>
740struct __pointer_traits_element_type;
741
742template <class _Ptr>
743struct __pointer_traits_element_type<_Ptr, true>
744{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000745 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746};
747
748#ifndef _LIBCPP_HAS_NO_VARIADICS
749
750template <template <class, class...> class _Sp, class _Tp, class ..._Args>
751struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
752{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000753 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754};
755
756template <template <class, class...> class _Sp, class _Tp, class ..._Args>
757struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
758{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000759 typedef _LIBCPP_NODEBUG_TYPE _Tp type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760};
761
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000762#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763
764template <template <class> class _Sp, class _Tp>
765struct __pointer_traits_element_type<_Sp<_Tp>, true>
766{
767 typedef typename _Sp<_Tp>::element_type type;
768};
769
770template <template <class> class _Sp, class _Tp>
771struct __pointer_traits_element_type<_Sp<_Tp>, false>
772{
773 typedef _Tp type;
774};
775
776template <template <class, class> class _Sp, class _Tp, class _A0>
777struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
778{
779 typedef typename _Sp<_Tp, _A0>::element_type type;
780};
781
782template <template <class, class> class _Sp, class _Tp, class _A0>
783struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
784{
785 typedef _Tp type;
786};
787
788template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
789struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
790{
791 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
792};
793
794template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
796{
797 typedef _Tp type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
803{
804 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
805};
806
807template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
808 class _A1, class _A2>
809struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
810{
811 typedef _Tp type;
812};
813
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000814#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815
Marshall Clow0be70c32017-06-14 21:23:57 +0000816template <class _Tp, class = void>
817struct __has_difference_type : false_type {};
818
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000820struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000821 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822
823template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
824struct __pointer_traits_difference_type
825{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000826 typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827};
828
829template <class _Ptr>
830struct __pointer_traits_difference_type<_Ptr, true>
831{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000832 typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833};
834
835template <class _Tp, class _Up>
836struct __has_rebind
837{
838private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000839 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840 template <class _Xp> static __two __test(...);
841 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
842public:
843 static const bool value = sizeof(__test<_Tp>(0)) == 1;
844};
845
846template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
847struct __pointer_traits_rebind
848{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000849#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000850 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000852 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853#endif
854};
855
856#ifndef _LIBCPP_HAS_NO_VARIADICS
857
858template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
859struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
860{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000861#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000862 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000864 typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865#endif
866};
867
868template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
869struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
870{
871 typedef _Sp<_Up, _Args...> type;
872};
873
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000874#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
876template <template <class> class _Sp, class _Tp, class _Up>
877struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
878{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000879#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 typedef typename _Sp<_Tp>::template rebind<_Up> type;
881#else
882 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
883#endif
884};
885
886template <template <class> class _Sp, class _Tp, class _Up>
887struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
888{
889 typedef _Sp<_Up> type;
890};
891
892template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
893struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
894{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000895#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
897#else
898 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
899#endif
900};
901
902template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
903struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
904{
905 typedef _Sp<_Up, _A0> type;
906};
907
908template <template <class, class, class> class _Sp, class _Tp, class _A0,
909 class _A1, class _Up>
910struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
911{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000912#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
914#else
915 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
916#endif
917};
918
919template <template <class, class, class> class _Sp, class _Tp, class _A0,
920 class _A1, class _Up>
921struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
922{
923 typedef _Sp<_Up, _A0, _A1> type;
924};
925
926template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
927 class _A1, class _A2, class _Up>
928struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
929{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000930#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
932#else
933 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
934#endif
935};
936
937template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
938 class _A1, class _A2, class _Up>
939struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
940{
941 typedef _Sp<_Up, _A0, _A1, _A2> type;
942};
943
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000944#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945
946template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000947struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948{
949 typedef _Ptr pointer;
950 typedef typename __pointer_traits_element_type<pointer>::type element_type;
951 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
952
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000953#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000954 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955#else
956 template <class _Up> struct rebind
957 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000958#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959
960private:
961 struct __nat {};
962public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 static pointer pointer_to(typename conditional<is_void<element_type>::value,
965 __nat, element_type>::type& __r)
966 {return pointer::pointer_to(__r);}
967};
968
969template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000970struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971{
972 typedef _Tp* pointer;
973 typedef _Tp element_type;
974 typedef ptrdiff_t difference_type;
975
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000976#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 template <class _Up> using rebind = _Up*;
978#else
979 template <class _Up> struct rebind {typedef _Up* other;};
980#endif
981
982private:
983 struct __nat {};
984public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000985 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000987 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000988 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989};
990
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991template <class _From, class _To>
992struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000993#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000994 typedef typename pointer_traits<_From>::template rebind<_To> type;
995#else
996 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
997#endif
998};
999
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000// allocator_traits
1001
Marshall Clow0be70c32017-06-14 21:23:57 +00001002template <class _Tp, class = void>
1003struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004
1005template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001006struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001007 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
1009namespace __pointer_type_imp
1010{
1011
1012template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1013struct __pointer_type
1014{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001015 typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016};
1017
1018template <class _Tp, class _Dp>
1019struct __pointer_type<_Tp, _Dp, false>
1020{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001021 typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022};
1023
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001024} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025
1026template <class _Tp, class _Dp>
1027struct __pointer_type
1028{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001029 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 +00001030};
1031
Marshall Clow0be70c32017-06-14 21:23:57 +00001032template <class _Tp, class = void>
1033struct __has_const_pointer : false_type {};
1034
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001036struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001037 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038
1039template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1040struct __const_pointer
1041{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001042 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043};
1044
1045template <class _Tp, class _Ptr, class _Alloc>
1046struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1047{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001048#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001049 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050#else
1051 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1052#endif
1053};
1054
Marshall Clow0be70c32017-06-14 21:23:57 +00001055template <class _Tp, class = void>
1056struct __has_void_pointer : false_type {};
1057
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001059struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001060 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061
1062template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1063struct __void_pointer
1064{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001065 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066};
1067
1068template <class _Ptr, class _Alloc>
1069struct __void_pointer<_Ptr, _Alloc, false>
1070{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001071#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001072 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001074 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075#endif
1076};
1077
Marshall Clow0be70c32017-06-14 21:23:57 +00001078template <class _Tp, class = void>
1079struct __has_const_void_pointer : false_type {};
1080
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001082struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001083 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
1085template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1086struct __const_void_pointer
1087{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001088 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089};
1090
1091template <class _Ptr, class _Alloc>
1092struct __const_void_pointer<_Ptr, _Alloc, false>
1093{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001094#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001095 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096#else
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001097 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098#endif
1099};
1100
Howard Hinnantc834c512011-11-29 18:15:50 +00001101template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001102inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001103_Tp*
1104__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105{
1106 return __p;
1107}
1108
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001109#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110template <class _Pointer>
1111inline _LIBCPP_INLINE_VISIBILITY
1112typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001113__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001115 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001117#else
1118template <class _Pointer>
1119inline _LIBCPP_INLINE_VISIBILITY
1120auto
1121__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1122-> decltype(pointer_traits<_Pointer>::to_address(__p))
1123{
1124 return pointer_traits<_Pointer>::to_address(__p);
1125}
1126
1127template <class _Pointer, class... _None>
1128inline _LIBCPP_INLINE_VISIBILITY
1129auto
1130__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1131{
1132 return _VSTD::__to_raw_pointer(__p.operator->());
1133}
1134
1135template <class _Tp>
1136inline _LIBCPP_INLINE_VISIBILITY constexpr
1137_Tp*
1138to_address(_Tp* __p) _NOEXCEPT
1139{
1140 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1141 return __p;
1142}
1143
1144template <class _Pointer>
1145inline _LIBCPP_INLINE_VISIBILITY
1146auto
1147to_address(const _Pointer& __p) _NOEXCEPT
1148{
1149 return _VSTD::__to_raw_pointer(__p);
1150}
1151#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152
Marshall Clow0be70c32017-06-14 21:23:57 +00001153template <class _Tp, class = void>
1154struct __has_size_type : false_type {};
1155
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001157struct __has_size_type<_Tp,
1158 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001160template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161struct __size_type
1162{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001163 typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164};
1165
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001166template <class _Alloc, class _DiffType>
1167struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001169 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170};
1171
Marshall Clow0be70c32017-06-14 21:23:57 +00001172template <class _Tp, class = void>
1173struct __has_propagate_on_container_copy_assignment : false_type {};
1174
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001176struct __has_propagate_on_container_copy_assignment<_Tp,
1177 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1178 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179
1180template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1181struct __propagate_on_container_copy_assignment
1182{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001183 typedef _LIBCPP_NODEBUG_TYPE false_type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184};
1185
1186template <class _Alloc>
1187struct __propagate_on_container_copy_assignment<_Alloc, true>
1188{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001189 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190};
1191
Marshall Clow0be70c32017-06-14 21:23:57 +00001192template <class _Tp, class = void>
1193struct __has_propagate_on_container_move_assignment : false_type {};
1194
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001196struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001197 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1198 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199
1200template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1201struct __propagate_on_container_move_assignment
1202{
1203 typedef false_type type;
1204};
1205
1206template <class _Alloc>
1207struct __propagate_on_container_move_assignment<_Alloc, true>
1208{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001209 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210};
1211
Marshall Clow0be70c32017-06-14 21:23:57 +00001212template <class _Tp, class = void>
1213struct __has_propagate_on_container_swap : false_type {};
1214
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001216struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001217 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1218 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
1220template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1221struct __propagate_on_container_swap
1222{
1223 typedef false_type type;
1224};
1225
1226template <class _Alloc>
1227struct __propagate_on_container_swap<_Alloc, true>
1228{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001229 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230};
1231
Marshall Clow0be70c32017-06-14 21:23:57 +00001232template <class _Tp, class = void>
1233struct __has_is_always_equal : false_type {};
1234
Marshall Clow0b587562015-06-02 16:34:03 +00001235template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001236struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001237 typename __void_t<typename _Tp::is_always_equal>::type>
1238 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001239
1240template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1241struct __is_always_equal
1242{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001243 typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
Marshall Clow0b587562015-06-02 16:34:03 +00001244};
1245
1246template <class _Alloc>
1247struct __is_always_equal<_Alloc, true>
1248{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001249 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
Marshall Clow0b587562015-06-02 16:34:03 +00001250};
1251
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1253struct __has_rebind_other
1254{
1255private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001256 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257 template <class _Xp> static __two __test(...);
1258 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1259public:
1260 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1261};
1262
1263template <class _Tp, class _Up>
1264struct __has_rebind_other<_Tp, _Up, false>
1265{
1266 static const bool value = false;
1267};
1268
1269template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1270struct __allocator_traits_rebind
1271{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001272 typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273};
1274
1275#ifndef _LIBCPP_HAS_NO_VARIADICS
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1279{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001280 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281};
1282
1283template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1284struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1285{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001286 typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287};
1288
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001289#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1293{
1294 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1295};
1296
1297template <template <class> class _Alloc, class _Tp, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1299{
1300 typedef _Alloc<_Up> type;
1301};
1302
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1305{
1306 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1307};
1308
1309template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1311{
1312 typedef _Alloc<_Up, _A0> type;
1313};
1314
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1316 class _A1, class _Up>
1317struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1318{
1319 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1320};
1321
1322template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1323 class _A1, class _Up>
1324struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1325{
1326 typedef _Alloc<_Up, _A0, _A1> type;
1327};
1328
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1330 class _A1, class _A2, class _Up>
1331struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1332{
1333 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1334};
1335
1336template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1337 class _A1, class _A2, class _Up>
1338struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1339{
1340 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1341};
1342
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001343#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001345#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
1347template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1348auto
1349__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001350 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351
1352template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1353auto
1354__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1355 -> false_type;
1356
1357template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1358struct __has_allocate_hint
1359 : integral_constant<bool,
1360 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001361 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 declval<_SizeType>(),
1363 declval<_ConstVoidPtr>())),
1364 true_type>::value>
1365{
1366};
1367
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001368#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369
1370template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1371struct __has_allocate_hint
1372 : true_type
1373{
1374};
1375
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001376#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001378#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379
1380template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001381decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1382 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 true_type())
1384__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1385
1386template <class _Alloc, class _Pointer, class ..._Args>
1387false_type
1388__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1389
1390template <class _Alloc, class _Pointer, class ..._Args>
1391struct __has_construct
1392 : integral_constant<bool,
1393 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001394 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 declval<_Pointer>(),
1396 declval<_Args>()...)),
1397 true_type>::value>
1398{
1399};
1400
1401template <class _Alloc, class _Pointer>
1402auto
1403__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1404 -> decltype(__a.destroy(__p), true_type());
1405
1406template <class _Alloc, class _Pointer>
1407auto
1408__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1409 -> false_type;
1410
1411template <class _Alloc, class _Pointer>
1412struct __has_destroy
1413 : integral_constant<bool,
1414 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001415 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 declval<_Pointer>())),
1417 true_type>::value>
1418{
1419};
1420
1421template <class _Alloc>
1422auto
1423__has_max_size_test(_Alloc&& __a)
1424 -> decltype(__a.max_size(), true_type());
1425
1426template <class _Alloc>
1427auto
1428__has_max_size_test(const volatile _Alloc& __a)
1429 -> false_type;
1430
1431template <class _Alloc>
1432struct __has_max_size
1433 : integral_constant<bool,
1434 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001435 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 true_type>::value>
1437{
1438};
1439
1440template <class _Alloc>
1441auto
1442__has_select_on_container_copy_construction_test(_Alloc&& __a)
1443 -> decltype(__a.select_on_container_copy_construction(), true_type());
1444
1445template <class _Alloc>
1446auto
1447__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1448 -> false_type;
1449
1450template <class _Alloc>
1451struct __has_select_on_container_copy_construction
1452 : integral_constant<bool,
1453 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001454 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 true_type>::value>
1456{
1457};
1458
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001459#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001461template <class _Alloc, class _Pointer, class _Tp, class = void>
1462struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001464template <class _Alloc, class _Pointer, class _Tp>
1465struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1466 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1467>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001469template <class _Alloc, class _Pointer, class = void>
1470struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471
1472template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001473struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1474 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1475>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476
1477template <class _Alloc>
1478struct __has_max_size
1479 : true_type
1480{
1481};
1482
1483template <class _Alloc>
1484struct __has_select_on_container_copy_construction
1485 : false_type
1486{
1487};
1488
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001489#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001491template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1492struct __alloc_traits_difference_type
1493{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001494 typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001495};
1496
1497template <class _Alloc, class _Ptr>
1498struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1499{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001500 typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001501};
1502
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001503template <class _Tp>
1504struct __is_default_allocator : false_type {};
1505
1506template <class _Tp>
1507struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1508
Eric Fiselier909fe962019-09-13 16:09:33 +00001509
1510
1511template <class _Alloc,
1512 bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
1513 >
1514struct __is_cpp17_move_insertable;
1515template <class _Alloc>
1516struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
1517template <class _Alloc>
1518struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
1519
1520template <class _Alloc,
1521 bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
1522 >
1523struct __is_cpp17_copy_insertable;
1524template <class _Alloc>
1525struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
1526template <class _Alloc>
1527struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
1528 std::is_copy_constructible<typename _Alloc::value_type>::value &&
1529 __is_cpp17_move_insertable<_Alloc>::value>
1530 {};
1531
1532
1533
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001535struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536{
1537 typedef _Alloc allocator_type;
1538 typedef typename allocator_type::value_type value_type;
1539
1540 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1541 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1542 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1543 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1544
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001545 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1546 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547
1548 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1549 propagate_on_container_copy_assignment;
1550 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1551 propagate_on_container_move_assignment;
1552 typedef typename __propagate_on_container_swap<allocator_type>::type
1553 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001554 typedef typename __is_always_equal<allocator_type>::type
1555 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001557#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001559 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Eric Fiselierea6fdf62019-06-23 20:47:21 +00001560 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001561#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 template <class _Tp> struct rebind_alloc
1563 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1564 template <class _Tp> struct rebind_traits
1565 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001566#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567
Marshall Clow0e58cae2017-11-26 02:55:38 +00001568 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 static pointer allocate(allocator_type& __a, size_type __n)
1570 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001571 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001573 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1575
Howard Hinnant756c69b2010-09-22 16:48:34 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001577 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 {__a.deallocate(__p, __n);}
1579
1580#ifndef _LIBCPP_HAS_NO_VARIADICS
1581 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001584 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001585 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001586#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001588 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001589 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 {
1591 ::new ((void*)__p) _Tp();
1592 }
1593 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001594 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001595 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001597 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1598 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 }
1600 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001601 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001602 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 const _A1& __a1)
1604 {
1605 ::new ((void*)__p) _Tp(__a0, __a1);
1606 }
1607 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001608 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001609 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 const _A1& __a1, const _A2& __a2)
1611 {
1612 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1613 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001614#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615
1616 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 static void destroy(allocator_type& __a, _Tp* __p)
1619 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1620
Howard Hinnant756c69b2010-09-22 16:48:34 +00001621 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001622 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1624
Howard Hinnant756c69b2010-09-22 16:48:34 +00001625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 static allocator_type
1627 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001628 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 __has_select_on_container_copy_construction<const allocator_type>(),
1630 __a);}
1631
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001632 template <class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001636 __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001637 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001638 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1639 "The specified type does not meet the requirements of Cpp17MoveInsertible");
Louis Dionne301a6772018-12-07 16:42:28 +00001640 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Eric Fiselier909fe962019-09-13 16:09:33 +00001641 construct(__a, _VSTD::__to_raw_pointer(__begin2),
1642#ifdef _LIBCPP_NO_EXCEPTIONS
1643 _VSTD::move(*__begin1)
1644#else
1645 _VSTD::move_if_noexcept(*__begin1)
1646#endif
1647 );
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 }
1649
1650 template <class _Tp>
1651 _LIBCPP_INLINE_VISIBILITY
1652 static
1653 typename enable_if
1654 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001655 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001656 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1657 is_trivially_move_constructible<_Tp>::value,
1658 void
1659 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001660 __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001661 {
1662 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001663 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001664 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001665 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001666 __begin2 += _Np;
1667 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001668 }
1669
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001670 template <class _Iter, class _Ptr>
1671 _LIBCPP_INLINE_VISIBILITY
1672 static
1673 void
1674 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1675 {
1676 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1677 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1678 }
1679
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001680 template <class _SourceTp, class _DestTp,
1681 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1682 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001683 _LIBCPP_INLINE_VISIBILITY
1684 static
1685 typename enable_if
1686 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001687 is_trivially_move_constructible<_DestTp>::value &&
1688 is_same<_RawSourceTp, _RawDestTp>::value &&
1689 (__is_default_allocator<allocator_type>::value ||
1690 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001691 void
1692 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001693 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001694 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001695 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001696 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001697 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001698 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001699 __begin2 += _Np;
1700 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001701 }
1702
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001703 template <class _Ptr>
1704 _LIBCPP_INLINE_VISIBILITY
1705 static
1706 void
Eric Fiselier909fe962019-09-13 16:09:33 +00001707 __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001708 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001709 static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1710 "The specified type does not meet the requirements of Cpp17MoveInsertable");
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001711 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001712 {
Eric Fiselier909fe962019-09-13 16:09:33 +00001713 construct(__a, _VSTD::__to_raw_pointer(__end2 - 1),
1714#ifdef _LIBCPP_NO_EXCEPTIONS
1715 _VSTD::move(*--__end1)
1716#else
1717 _VSTD::move_if_noexcept(*--__end1)
1718#endif
1719 );
1720 --__end2;
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001721 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001722 }
1723
1724 template <class _Tp>
1725 _LIBCPP_INLINE_VISIBILITY
1726 static
1727 typename enable_if
1728 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001729 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001730 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1731 is_trivially_move_constructible<_Tp>::value,
1732 void
1733 >::type
Eric Fiselier909fe962019-09-13 16:09:33 +00001734 __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001735 {
1736 ptrdiff_t _Np = __end1 - __begin1;
1737 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001738 if (_Np > 0)
1739 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001740 }
1741
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742private:
1743
Howard Hinnant756c69b2010-09-22 16:48:34 +00001744 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001745 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 const_void_pointer __hint, true_type)
1747 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001748 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001749 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001750 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 {return __a.allocate(__n);}
1752
1753#ifndef _LIBCPP_HAS_NO_VARIADICS
1754 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001757 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1761 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001762 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001764#else // _LIBCPP_HAS_NO_VARIADICS
1765 template <class _Tp, class _A0>
1766 _LIBCPP_INLINE_VISIBILITY
1767 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1768 const _A0& __a0)
1769 {__a.construct(__p, __a0);}
1770 template <class _Tp, class _A0>
1771 _LIBCPP_INLINE_VISIBILITY
1772 static void __construct(false_type, allocator_type&, _Tp* __p,
1773 const _A0& __a0)
1774 {
1775 ::new ((void*)__p) _Tp(__a0);
1776 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001777#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778
1779 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1782 {__a.destroy(__p);}
1783 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 static void __destroy(false_type, allocator_type&, _Tp* __p)
1786 {
1787 __p->~_Tp();
1788 }
1789
Howard Hinnant756c69b2010-09-22 16:48:34 +00001790 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001791 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001793 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001794 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001795 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796
Howard Hinnant756c69b2010-09-22 16:48:34 +00001797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001799 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001803 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 {return __a;}
1805};
1806
Marshall Clow940e01c2015-04-07 05:21:38 +00001807template <class _Traits, class _Tp>
1808struct __rebind_alloc_helper
1809{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001810#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001811 typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001812#else
1813 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1814#endif
1815};
1816
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817// allocator
1818
1819template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001820class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821{
1822public:
1823 typedef size_t size_type;
1824 typedef ptrdiff_t difference_type;
1825 typedef _Tp* pointer;
1826 typedef const _Tp* const_pointer;
1827 typedef _Tp& reference;
1828 typedef const _Tp& const_reference;
1829 typedef _Tp value_type;
1830
Howard Hinnant4931e092011-06-02 21:38:57 +00001831 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001832 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001833
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1835
Marshall Clowbc759762018-03-20 23:02:53 +00001836 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1837 allocator() _NOEXCEPT {}
1838
Louis Dionne481a2662018-09-23 18:35:00 +00001839 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001840 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1841 allocator(const allocator<_Up>&) _NOEXCEPT {}
1842
Howard Hinnant719bda32011-05-28 14:41:13 +00001843 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001844 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001845 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001846 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001847 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001848 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001849 {
1850 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001851 __throw_length_error("allocator<T>::allocate(size_t n)"
1852 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001853 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001854 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001855 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001856 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001857 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1858 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001859#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 template <class _Up, class... _Args>
1861 _LIBCPP_INLINE_VISIBILITY
1862 void
1863 construct(_Up* __p, _Args&&... __args)
1864 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001865 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001867#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 _LIBCPP_INLINE_VISIBILITY
1869 void
1870 construct(pointer __p)
1871 {
1872 ::new((void*)__p) _Tp();
1873 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001874# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001875
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 template <class _A0>
1877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001878 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 construct(pointer __p, _A0& __a0)
1880 {
1881 ::new((void*)__p) _Tp(__a0);
1882 }
1883 template <class _A0>
1884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001885 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 construct(pointer __p, const _A0& __a0)
1887 {
1888 ::new((void*)__p) _Tp(__a0);
1889 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001890# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891 template <class _A0, class _A1>
1892 _LIBCPP_INLINE_VISIBILITY
1893 void
1894 construct(pointer __p, _A0& __a0, _A1& __a1)
1895 {
1896 ::new((void*)__p) _Tp(__a0, __a1);
1897 }
1898 template <class _A0, class _A1>
1899 _LIBCPP_INLINE_VISIBILITY
1900 void
1901 construct(pointer __p, const _A0& __a0, _A1& __a1)
1902 {
1903 ::new((void*)__p) _Tp(__a0, __a1);
1904 }
1905 template <class _A0, class _A1>
1906 _LIBCPP_INLINE_VISIBILITY
1907 void
1908 construct(pointer __p, _A0& __a0, const _A1& __a1)
1909 {
1910 ::new((void*)__p) _Tp(__a0, __a1);
1911 }
1912 template <class _A0, class _A1>
1913 _LIBCPP_INLINE_VISIBILITY
1914 void
1915 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1916 {
1917 ::new((void*)__p) _Tp(__a0, __a1);
1918 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001919#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1921};
1922
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001923template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001924class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001925{
1926public:
1927 typedef size_t size_type;
1928 typedef ptrdiff_t difference_type;
1929 typedef const _Tp* pointer;
1930 typedef const _Tp* const_pointer;
1931 typedef const _Tp& reference;
1932 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001933 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001934
1935 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001936 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001937
1938 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1939
Marshall Clowbc759762018-03-20 23:02:53 +00001940 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1941 allocator() _NOEXCEPT {}
1942
1943 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001944 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001945 allocator(const allocator<_Up>&) _NOEXCEPT {}
1946
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001947 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1948 {return _VSTD::addressof(__x);}
1949 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001950 {
1951 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001952 __throw_length_error("allocator<const T>::allocate(size_t n)"
1953 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001954 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001955 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001956 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001957 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001958 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1959 {return size_type(~0) / sizeof(_Tp);}
1960#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1961 template <class _Up, class... _Args>
1962 _LIBCPP_INLINE_VISIBILITY
1963 void
1964 construct(_Up* __p, _Args&&... __args)
1965 {
1966 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1967 }
1968#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1969 _LIBCPP_INLINE_VISIBILITY
1970 void
1971 construct(pointer __p)
1972 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001973 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001974 }
1975# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001976
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001977 template <class _A0>
1978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001979 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001980 construct(pointer __p, _A0& __a0)
1981 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001982 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001983 }
1984 template <class _A0>
1985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001986 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001987 construct(pointer __p, const _A0& __a0)
1988 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001989 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001990 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001991# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1992 template <class _A0, class _A1>
1993 _LIBCPP_INLINE_VISIBILITY
1994 void
1995 construct(pointer __p, _A0& __a0, _A1& __a1)
1996 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001997 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001998 }
1999 template <class _A0, class _A1>
2000 _LIBCPP_INLINE_VISIBILITY
2001 void
2002 construct(pointer __p, const _A0& __a0, _A1& __a1)
2003 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002004 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002005 }
2006 template <class _A0, class _A1>
2007 _LIBCPP_INLINE_VISIBILITY
2008 void
2009 construct(pointer __p, _A0& __a0, const _A1& __a1)
2010 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002011 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002012 }
2013 template <class _A0, class _A1>
2014 _LIBCPP_INLINE_VISIBILITY
2015 void
2016 construct(pointer __p, const _A0& __a0, const _A1& __a1)
2017 {
Marshall Clow31350ab2017-06-14 16:54:43 +00002018 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002019 }
2020#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2021 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
2022};
2023
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024template <class _Tp, class _Up>
2025inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002026bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027
2028template <class _Tp, class _Up>
2029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002030bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031
2032template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002033class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034 : public iterator<output_iterator_tag,
2035 _Tp, // purposefully not C++03
2036 ptrdiff_t, // purposefully not C++03
2037 _Tp*, // purposefully not C++03
2038 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2039{
2040private:
2041 _OutputIterator __x_;
2042public:
2043 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2044 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2045 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002046 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002047#if _LIBCPP_STD_VER >= 14
2048 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002049 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002050#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2052 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2053 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002054#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002055 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002056#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057};
2058
2059template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002060_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002061pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002062get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063{
2064 pair<_Tp*, ptrdiff_t> __r(0, 0);
2065 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2066 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2067 / sizeof(_Tp);
2068 if (__n > __m)
2069 __n = __m;
2070 while (__n > 0)
2071 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002072#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002073 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002074 {
2075 std::align_val_t __al =
2076 std::align_val_t(std::alignment_of<_Tp>::value);
2077 __r.first = static_cast<_Tp*>(::operator new(
2078 __n * sizeof(_Tp), __al, nothrow));
2079 } else {
2080 __r.first = static_cast<_Tp*>(::operator new(
2081 __n * sizeof(_Tp), nothrow));
2082 }
2083#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002084 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002085 {
2086 // Since aligned operator new is unavailable, return an empty
2087 // buffer rather than one with invalid alignment.
2088 return __r;
2089 }
2090
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002092#endif
2093
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 if (__r.first)
2095 {
2096 __r.second = __n;
2097 break;
2098 }
2099 __n /= 2;
2100 }
2101 return __r;
2102}
2103
2104template <class _Tp>
2105inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002106void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2107{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002108 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002109}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110
Marshall Clowb22274f2017-01-24 22:22:33 +00002111#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002113struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114{
2115 _Tp* __ptr_;
2116};
2117
2118template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002119class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120{
2121private:
2122 _Tp* __ptr_;
2123public:
2124 typedef _Tp element_type;
2125
2126 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2127 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2128 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2129 : __ptr_(__p.release()) {}
2130 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2131 {reset(__p.release()); return *this;}
2132 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2133 {reset(__p.release()); return *this;}
2134 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2135 {reset(__p.__ptr_); return *this;}
2136 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2137
2138 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2139 {return *__ptr_;}
2140 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2141 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2142 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2143 {
2144 _Tp* __t = __ptr_;
2145 __ptr_ = 0;
2146 return __t;
2147 }
2148 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2149 {
2150 if (__ptr_ != __p)
2151 delete __ptr_;
2152 __ptr_ = __p;
2153 }
2154
2155 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2156 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2157 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2158 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2159 {return auto_ptr<_Up>(release());}
2160};
2161
2162template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002163class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164{
2165public:
2166 typedef void element_type;
2167};
Marshall Clowb22274f2017-01-24 22:22:33 +00002168#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169
Eric Fiselier9d355982017-04-12 23:45:53 +00002170template <class _Tp, int _Idx,
2171 bool _CanBeEmptyBase =
2172 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2173struct __compressed_pair_elem {
2174 typedef _Tp _ParamT;
2175 typedef _Tp& reference;
2176 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177
Eric Fiselier9d355982017-04-12 23:45:53 +00002178#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002179 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
Eric Fiselier9d355982017-04-12 23:45:53 +00002181 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002182 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2183 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002184 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002185 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002186 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002187 : __value_(_VSTD::forward<_Up>(__u))
2188 {
2189 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190
Eric Fiselier9d355982017-04-12 23:45:53 +00002191 template <class... _Args, size_t... _Indexes>
2192 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2193 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2194 __tuple_indices<_Indexes...>)
2195 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2196#else
Alex Lorenz76132112017-11-09 17:54:49 +00002197 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2198 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002199 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2200#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201
Alex Lorenz76132112017-11-09 17:54:49 +00002202 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2203 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002204 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002207 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208};
2209
Eric Fiselier9d355982017-04-12 23:45:53 +00002210template <class _Tp, int _Idx>
2211struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2212 typedef _Tp _ParamT;
2213 typedef _Tp& reference;
2214 typedef const _Tp& const_reference;
2215 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216
Eric Fiselier9d355982017-04-12 23:45:53 +00002217#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002218 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219
Eric Fiselier9d355982017-04-12 23:45:53 +00002220 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002221 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2222 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002223 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002224 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002225 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002226 : __value_type(_VSTD::forward<_Up>(__u))
2227 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228
Eric Fiselier9d355982017-04-12 23:45:53 +00002229 template <class... _Args, size_t... _Indexes>
2230 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2231 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2232 __tuple_indices<_Indexes...>)
2233 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2234#else
Alex Lorenz76132112017-11-09 17:54:49 +00002235 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2236 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002237 __compressed_pair_elem(_ParamT __p)
2238 : __value_type(std::forward<_ParamT>(__p)) {}
2239#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240
Alex Lorenz76132112017-11-09 17:54:49 +00002241 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2242 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002243 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244};
2245
Eric Fiselier9d355982017-04-12 23:45:53 +00002246// Tag used to construct the second element of the compressed pair.
2247struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248
2249template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002250class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2251 private __compressed_pair_elem<_T2, 1> {
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002252 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
2253 typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
Eric Fiselier9d355982017-04-12 23:45:53 +00002254
2255 // NOTE: This static assert should never fire because __compressed_pair
2256 // is *almost never* used in a scenario where it's possible for T1 == T2.
2257 // (The exception is std::function where it is possible that the function
2258 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002259 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002260 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2261 "The current implementation is NOT ABI-compatible with the previous "
2262 "implementation for this configuration");
2263
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002265#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002266 template <bool _Dummy = true,
2267 class = typename enable_if<
2268 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2269 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2270 >::type
2271 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002272 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002273 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274
Eric Fiselier9d355982017-04-12 23:45:53 +00002275 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2276 __compressed_pair>::value,
2277 bool>::type = true>
2278 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2279 __compressed_pair(_Tp&& __t)
2280 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 template <class _Tp>
2283 _LIBCPP_INLINE_VISIBILITY constexpr
2284 __compressed_pair(__second_tag, _Tp&& __t)
2285 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286
Eric Fiselier9d355982017-04-12 23:45:53 +00002287 template <class _U1, class _U2>
2288 _LIBCPP_INLINE_VISIBILITY constexpr
2289 __compressed_pair(_U1&& __t1, _U2&& __t2)
2290 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291
Eric Fiselier9d355982017-04-12 23:45:53 +00002292 template <class... _Args1, class... _Args2>
2293 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2294 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2295 tuple<_Args2...> __second_args)
2296 : _Base1(__pc, _VSTD::move(__first_args),
2297 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2298 _Base2(__pc, _VSTD::move(__second_args),
2299 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002300
Eric Fiselier9d355982017-04-12 23:45:53 +00002301#else
2302 _LIBCPP_INLINE_VISIBILITY
2303 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002304
Eric Fiselier9d355982017-04-12 23:45:53 +00002305 _LIBCPP_INLINE_VISIBILITY explicit
2306 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002307
Eric Fiselier9d355982017-04-12 23:45:53 +00002308 _LIBCPP_INLINE_VISIBILITY
2309 __compressed_pair(__second_tag, _T2 __t2)
2310 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311
Eric Fiselier9d355982017-04-12 23:45:53 +00002312 _LIBCPP_INLINE_VISIBILITY
2313 __compressed_pair(_T1 __t1, _T2 __t2)
2314 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2315#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316
Eric Fiselier9d355982017-04-12 23:45:53 +00002317 _LIBCPP_INLINE_VISIBILITY
2318 typename _Base1::reference first() _NOEXCEPT {
2319 return static_cast<_Base1&>(*this).__get();
2320 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321
Eric Fiselier9d355982017-04-12 23:45:53 +00002322 _LIBCPP_INLINE_VISIBILITY
2323 typename _Base1::const_reference first() const _NOEXCEPT {
2324 return static_cast<_Base1 const&>(*this).__get();
2325 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326
Eric Fiselier9d355982017-04-12 23:45:53 +00002327 _LIBCPP_INLINE_VISIBILITY
2328 typename _Base2::reference second() _NOEXCEPT {
2329 return static_cast<_Base2&>(*this).__get();
2330 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331
Eric Fiselier9d355982017-04-12 23:45:53 +00002332 _LIBCPP_INLINE_VISIBILITY
2333 typename _Base2::const_reference second() const _NOEXCEPT {
2334 return static_cast<_Base2 const&>(*this).__get();
2335 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336
Eric Fiselier9d355982017-04-12 23:45:53 +00002337 _LIBCPP_INLINE_VISIBILITY
2338 void swap(__compressed_pair& __x)
2339 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2340 __is_nothrow_swappable<_T2>::value)
2341 {
2342 using std::swap;
2343 swap(first(), __x.first());
2344 swap(second(), __x.second());
2345 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346};
2347
2348template <class _T1, class _T2>
2349inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002350void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2351 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2352 __is_nothrow_swappable<_T2>::value) {
2353 __x.swap(__y);
2354}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002356// default_delete
2357
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002359struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002360 static_assert(!is_function<_Tp>::value,
2361 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002362#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002363 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002364#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002365 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002366#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002367 template <class _Up>
2368 _LIBCPP_INLINE_VISIBILITY
2369 default_delete(const default_delete<_Up>&,
2370 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2371 0) _NOEXCEPT {}
2372
2373 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2374 static_assert(sizeof(_Tp) > 0,
2375 "default_delete can not delete incomplete type");
2376 static_assert(!is_void<_Tp>::value,
2377 "default_delete can not delete incomplete type");
2378 delete __ptr;
2379 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380};
2381
2382template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002383struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2384private:
2385 template <class _Up>
2386 struct _EnableIfConvertible
2387 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2388
Howard Hinnant4500ca52011-12-18 21:19:44 +00002389public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002390#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002391 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002392#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002393 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002394#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002395
2396 template <class _Up>
2397 _LIBCPP_INLINE_VISIBILITY
2398 default_delete(const default_delete<_Up[]>&,
2399 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2400
2401 template <class _Up>
2402 _LIBCPP_INLINE_VISIBILITY
2403 typename _EnableIfConvertible<_Up>::type
2404 operator()(_Up* __ptr) const _NOEXCEPT {
2405 static_assert(sizeof(_Tp) > 0,
2406 "default_delete can not delete incomplete type");
2407 static_assert(!is_void<_Tp>::value,
2408 "default_delete can not delete void type");
2409 delete[] __ptr;
2410 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411};
2412
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002413template <class _Deleter>
2414struct __unique_ptr_deleter_sfinae {
2415 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2416 typedef const _Deleter& __lval_ref_type;
2417 typedef _Deleter&& __good_rval_ref_type;
2418 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419};
2420
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002421template <class _Deleter>
2422struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2423 typedef const _Deleter& __lval_ref_type;
2424 typedef const _Deleter&& __bad_rval_ref_type;
2425 typedef false_type __enable_rval_overload;
2426};
2427
2428template <class _Deleter>
2429struct __unique_ptr_deleter_sfinae<_Deleter&> {
2430 typedef _Deleter& __lval_ref_type;
2431 typedef _Deleter&& __bad_rval_ref_type;
2432 typedef false_type __enable_rval_overload;
2433};
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002434
2435template <class _Tp, class _Dp = default_delete<_Tp> >
2436class _LIBCPP_TEMPLATE_VIS unique_ptr {
2437public:
2438 typedef _Tp element_type;
2439 typedef _Dp deleter_type;
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002440 typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002441
2442 static_assert(!is_rvalue_reference<deleter_type>::value,
2443 "the specified deleter type cannot be an rvalue reference");
2444
2445private:
2446 __compressed_pair<pointer, deleter_type> __ptr_;
2447
2448 struct __nat { int __for_bool_; };
2449
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002450 typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002451
2452 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002453 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002454 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002455
2456 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002457 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002458 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002459
2460 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002461 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002462 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002463
2464 template <bool _Dummy, class _Deleter = typename __dependent_type<
2465 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002466 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002467 typename enable_if<is_default_constructible<_Deleter>::value &&
2468 !is_pointer<_Deleter>::value>::type;
2469
2470 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002471 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002472 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2473
2474 template <class _UPtr, class _Up>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002475 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002476 is_convertible<typename _UPtr::pointer, pointer>::value &&
2477 !is_array<_Up>::value
2478 >::type;
2479
2480 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002481 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002482 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2483 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2484 >::type;
2485
2486 template <class _UDel>
2487 using _EnableIfDeleterAssignable = typename enable_if<
2488 is_assignable<_Dp&, _UDel&&>::value
2489 >::type;
2490
2491public:
2492 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002493 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002494 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002495 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002496
2497 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002498 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002499 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002500 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002501
2502 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002503 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002504 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002505 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002506
2507 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002508 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002509 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002510 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002511 : __ptr_(__p, __d) {}
2512
2513 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002514 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002515 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002516 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002517 : __ptr_(__p, _VSTD::move(__d)) {
2518 static_assert(!is_reference<deleter_type>::value,
2519 "rvalue deleter bound to reference");
2520 }
2521
2522 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002523 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002524 _LIBCPP_INLINE_VISIBILITY
2525 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2526
2527 _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 }
2531
2532 template <class _Up, class _Ep,
2533 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2534 class = _EnableIfDeleterConvertible<_Ep>
2535 >
2536 _LIBCPP_INLINE_VISIBILITY
2537 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2538 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2539
2540#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2541 template <class _Up>
2542 _LIBCPP_INLINE_VISIBILITY
2543 unique_ptr(auto_ptr<_Up>&& __p,
2544 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002545 is_same<_Dp, default_delete<_Tp> >::value,
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002546 __nat>::type = __nat()) _NOEXCEPT
2547 : __ptr_(__p.release()) {}
2548#endif
2549
2550 _LIBCPP_INLINE_VISIBILITY
2551 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2552 reset(__u.release());
2553 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2554 return *this;
2555 }
2556
2557 template <class _Up, class _Ep,
2558 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2559 class = _EnableIfDeleterAssignable<_Ep>
2560 >
2561 _LIBCPP_INLINE_VISIBILITY
2562 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2563 reset(__u.release());
2564 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2565 return *this;
2566 }
2567
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002568#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2569 template <class _Up>
2570 _LIBCPP_INLINE_VISIBILITY
2571 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2572 is_same<_Dp, default_delete<_Tp> >::value,
2573 unique_ptr&>::type
2574 operator=(auto_ptr<_Up> __p) {
2575 reset(__p.release());
2576 return *this;
2577 }
2578#endif
2579
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002580#ifdef _LIBCPP_CXX03_LANG
2581 unique_ptr(unique_ptr const&) = delete;
2582 unique_ptr& operator=(unique_ptr const&) = delete;
2583#endif
2584
2585
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002586 _LIBCPP_INLINE_VISIBILITY
2587 ~unique_ptr() { reset(); }
2588
2589 _LIBCPP_INLINE_VISIBILITY
2590 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2591 reset();
2592 return *this;
2593 }
2594
2595 _LIBCPP_INLINE_VISIBILITY
2596 typename add_lvalue_reference<_Tp>::type
2597 operator*() const {
2598 return *__ptr_.first();
2599 }
2600 _LIBCPP_INLINE_VISIBILITY
2601 pointer operator->() const _NOEXCEPT {
2602 return __ptr_.first();
2603 }
2604 _LIBCPP_INLINE_VISIBILITY
2605 pointer get() const _NOEXCEPT {
2606 return __ptr_.first();
2607 }
2608 _LIBCPP_INLINE_VISIBILITY
2609 deleter_type& get_deleter() _NOEXCEPT {
2610 return __ptr_.second();
2611 }
2612 _LIBCPP_INLINE_VISIBILITY
2613 const deleter_type& get_deleter() const _NOEXCEPT {
2614 return __ptr_.second();
2615 }
2616 _LIBCPP_INLINE_VISIBILITY
2617 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2618 return __ptr_.first() != nullptr;
2619 }
2620
2621 _LIBCPP_INLINE_VISIBILITY
2622 pointer release() _NOEXCEPT {
2623 pointer __t = __ptr_.first();
2624 __ptr_.first() = pointer();
2625 return __t;
2626 }
2627
2628 _LIBCPP_INLINE_VISIBILITY
2629 void reset(pointer __p = pointer()) _NOEXCEPT {
2630 pointer __tmp = __ptr_.first();
2631 __ptr_.first() = __p;
2632 if (__tmp)
2633 __ptr_.second()(__tmp);
2634 }
2635
2636 _LIBCPP_INLINE_VISIBILITY
2637 void swap(unique_ptr& __u) _NOEXCEPT {
2638 __ptr_.swap(__u.__ptr_);
2639 }
2640};
2641
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002642
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002644class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002646 typedef _Tp element_type;
2647 typedef _Dp deleter_type;
2648 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2649
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002651 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652
Eric Fiselier31127cd2017-04-16 02:14:31 +00002653 template <class _From>
2654 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655
Eric Fiselier31127cd2017-04-16 02:14:31 +00002656 template <class _FromElem>
2657 struct _CheckArrayPointerConversion<_FromElem*>
2658 : integral_constant<bool,
2659 is_same<_FromElem*, pointer>::value ||
2660 (is_same<pointer, element_type*>::value &&
2661 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2662 >
2663 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664
Eric Fiselier31127cd2017-04-16 02:14:31 +00002665 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002666
2667 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002668 using _LValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002669 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002670
2671 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002672 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002673 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002674
2675 template <bool _Dummy>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002676 using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002677 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002678
2679 template <bool _Dummy, class _Deleter = typename __dependent_type<
2680 __identity<deleter_type>, _Dummy>::type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002681 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002682 typename enable_if<is_default_constructible<_Deleter>::value &&
2683 !is_pointer<_Deleter>::value>::type;
2684
2685 template <class _ArgType>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002686 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002687 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2688
2689 template <class _Pp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002690 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002691 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002692 >::type;
2693
2694 template <class _UPtr, class _Up,
2695 class _ElemT = typename _UPtr::element_type>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002696 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002697 is_array<_Up>::value &&
2698 is_same<pointer, element_type*>::value &&
2699 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2700 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2701 >::type;
2702
2703 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002704 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002705 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2706 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2707 >::type;
2708
2709 template <class _UDel>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00002710 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002711 is_assignable<_Dp&, _UDel&&>::value
2712 >::type;
2713
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002716 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002718 _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002720 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002721 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002722 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002723 _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002725 template <class _Pp, bool _Dummy = true,
2726 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002727 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002728 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002729 explicit unique_ptr(_Pp __p) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002730 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002732 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002733 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
2734 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002736 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002737 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002739 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002740 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002741 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002742 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002743 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002745 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002746 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
2747 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002748 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002749 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002750 : __ptr_(__p, _VSTD::move(__d)) {
2751 static_assert(!is_reference<deleter_type>::value,
2752 "rvalue deleter bound to reference");
2753 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002755 template <bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002756 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002757 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002758 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002759 : __ptr_(nullptr, _VSTD::move(__d)) {
2760 static_assert(!is_reference<deleter_type>::value,
2761 "rvalue deleter bound to reference");
2762 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002763
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002764 template <class _Pp, bool _Dummy = true,
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002765 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
2766 class = _EnableIfPointerConvertible<_Pp> >
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002767 _LIBCPP_INLINE_VISIBILITY
2768 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002769
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002770 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002771 unique_ptr(unique_ptr&& __u) _NOEXCEPT
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002772 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2773 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002774
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002775 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002776 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 reset(__u.release());
2778 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2779 return *this;
2780 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 template <class _Up, class _Ep,
2783 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2784 class = _EnableIfDeleterConvertible<_Ep>
2785 >
2786 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002787 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002788 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002789 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002791 template <class _Up, class _Ep,
2792 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2793 class = _EnableIfDeleterAssignable<_Ep>
2794 >
2795 _LIBCPP_INLINE_VISIBILITY
2796 unique_ptr&
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002797 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002798 reset(__u.release());
2799 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2800 return *this;
2801 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802
Eric Fiselierea6fdf62019-06-23 20:47:21 +00002803#ifdef _LIBCPP_CXX03_LANG
2804 unique_ptr(unique_ptr const&) = delete;
2805 unique_ptr& operator=(unique_ptr const&) = delete;
2806#endif
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002807
2808public:
2809 _LIBCPP_INLINE_VISIBILITY
2810 ~unique_ptr() { reset(); }
2811
2812 _LIBCPP_INLINE_VISIBILITY
2813 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2814 reset();
2815 return *this;
2816 }
2817
2818 _LIBCPP_INLINE_VISIBILITY
2819 typename add_lvalue_reference<_Tp>::type
2820 operator[](size_t __i) const {
2821 return __ptr_.first()[__i];
2822 }
2823 _LIBCPP_INLINE_VISIBILITY
2824 pointer get() const _NOEXCEPT {
2825 return __ptr_.first();
2826 }
2827
2828 _LIBCPP_INLINE_VISIBILITY
2829 deleter_type& get_deleter() _NOEXCEPT {
2830 return __ptr_.second();
2831 }
2832
2833 _LIBCPP_INLINE_VISIBILITY
2834 const deleter_type& get_deleter() const _NOEXCEPT {
2835 return __ptr_.second();
2836 }
2837 _LIBCPP_INLINE_VISIBILITY
2838 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2839 return __ptr_.first() != nullptr;
2840 }
2841
2842 _LIBCPP_INLINE_VISIBILITY
2843 pointer release() _NOEXCEPT {
2844 pointer __t = __ptr_.first();
2845 __ptr_.first() = pointer();
2846 return __t;
2847 }
2848
2849 template <class _Pp>
2850 _LIBCPP_INLINE_VISIBILITY
2851 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002852 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002853 >::type
2854 reset(_Pp __p) _NOEXCEPT {
2855 pointer __tmp = __ptr_.first();
2856 __ptr_.first() = __p;
2857 if (__tmp)
2858 __ptr_.second()(__tmp);
2859 }
2860
2861 _LIBCPP_INLINE_VISIBILITY
2862 void reset(nullptr_t = nullptr) _NOEXCEPT {
2863 pointer __tmp = __ptr_.first();
2864 __ptr_.first() = nullptr;
2865 if (__tmp)
2866 __ptr_.second()(__tmp);
2867 }
2868
2869 _LIBCPP_INLINE_VISIBILITY
2870 void swap(unique_ptr& __u) _NOEXCEPT {
2871 __ptr_.swap(__u.__ptr_);
2872 }
2873
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874};
2875
2876template <class _Tp, class _Dp>
2877inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002878typename enable_if<
2879 __is_swappable<_Dp>::value,
2880 void
2881>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002882swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883
2884template <class _T1, class _D1, class _T2, class _D2>
2885inline _LIBCPP_INLINE_VISIBILITY
2886bool
2887operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2888
2889template <class _T1, class _D1, class _T2, class _D2>
2890inline _LIBCPP_INLINE_VISIBILITY
2891bool
2892operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2893
2894template <class _T1, class _D1, class _T2, class _D2>
2895inline _LIBCPP_INLINE_VISIBILITY
2896bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002897operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2898{
2899 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2900 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002901 typedef typename common_type<_P1, _P2>::type _Vp;
2902 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002903}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904
2905template <class _T1, class _D1, class _T2, class _D2>
2906inline _LIBCPP_INLINE_VISIBILITY
2907bool
2908operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2909
2910template <class _T1, class _D1, class _T2, class _D2>
2911inline _LIBCPP_INLINE_VISIBILITY
2912bool
2913operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2914
2915template <class _T1, class _D1, class _T2, class _D2>
2916inline _LIBCPP_INLINE_VISIBILITY
2917bool
2918operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2919
Howard Hinnantb17caf92012-02-21 21:02:58 +00002920template <class _T1, class _D1>
2921inline _LIBCPP_INLINE_VISIBILITY
2922bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002923operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002924{
2925 return !__x;
2926}
2927
2928template <class _T1, class _D1>
2929inline _LIBCPP_INLINE_VISIBILITY
2930bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002931operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002932{
2933 return !__x;
2934}
2935
2936template <class _T1, class _D1>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002939operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002940{
2941 return static_cast<bool>(__x);
2942}
2943
2944template <class _T1, class _D1>
2945inline _LIBCPP_INLINE_VISIBILITY
2946bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002947operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002948{
2949 return static_cast<bool>(__x);
2950}
2951
2952template <class _T1, class _D1>
2953inline _LIBCPP_INLINE_VISIBILITY
2954bool
2955operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2956{
2957 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2958 return less<_P1>()(__x.get(), nullptr);
2959}
2960
2961template <class _T1, class _D1>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
2964operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2965{
2966 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2967 return less<_P1>()(nullptr, __x.get());
2968}
2969
2970template <class _T1, class _D1>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
2973operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2974{
2975 return nullptr < __x;
2976}
2977
2978template <class _T1, class _D1>
2979inline _LIBCPP_INLINE_VISIBILITY
2980bool
2981operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2982{
2983 return __x < nullptr;
2984}
2985
2986template <class _T1, class _D1>
2987inline _LIBCPP_INLINE_VISIBILITY
2988bool
2989operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2990{
2991 return !(nullptr < __x);
2992}
2993
2994template <class _T1, class _D1>
2995inline _LIBCPP_INLINE_VISIBILITY
2996bool
2997operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2998{
2999 return !(__x < nullptr);
3000}
3001
3002template <class _T1, class _D1>
3003inline _LIBCPP_INLINE_VISIBILITY
3004bool
3005operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3006{
3007 return !(__x < nullptr);
3008}
3009
3010template <class _T1, class _D1>
3011inline _LIBCPP_INLINE_VISIBILITY
3012bool
3013operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3014{
3015 return !(nullptr < __x);
3016}
3017
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003018#if _LIBCPP_STD_VER > 11
3019
3020template<class _Tp>
3021struct __unique_if
3022{
3023 typedef unique_ptr<_Tp> __unique_single;
3024};
3025
3026template<class _Tp>
3027struct __unique_if<_Tp[]>
3028{
3029 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3030};
3031
3032template<class _Tp, size_t _Np>
3033struct __unique_if<_Tp[_Np]>
3034{
3035 typedef void __unique_array_known_bound;
3036};
3037
3038template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003039inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003040typename __unique_if<_Tp>::__unique_single
3041make_unique(_Args&&... __args)
3042{
3043 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3044}
3045
3046template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003047inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003048typename __unique_if<_Tp>::__unique_array_unknown_bound
3049make_unique(size_t __n)
3050{
3051 typedef typename remove_extent<_Tp>::type _Up;
3052 return unique_ptr<_Tp>(new _Up[__n]());
3053}
3054
3055template<class _Tp, class... _Args>
3056 typename __unique_if<_Tp>::__unique_array_known_bound
3057 make_unique(_Args&&...) = delete;
3058
3059#endif // _LIBCPP_STD_VER > 11
3060
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003061template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003062#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003063struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003064#else
3065struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
Eric Fiselierea6fdf62019-06-23 20:47:21 +00003066 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003067#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003068{
3069 typedef unique_ptr<_Tp, _Dp> argument_type;
3070 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003071 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003072 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003073 {
3074 typedef typename argument_type::pointer pointer;
3075 return hash<pointer>()(__ptr.get());
3076 }
3077};
3078
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079struct __destruct_n
3080{
3081private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003082 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083
3084 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003085 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003086 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087
3088 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003089 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 {}
3091
Howard Hinnant719bda32011-05-28 14:41:13 +00003092 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003093 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003094 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095 {}
3096
Howard Hinnant719bda32011-05-28 14:41:13 +00003097 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003098 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003099 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100 {}
3101public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003102 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003103 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104
3105 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003106 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003107 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108
3109 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003110 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003111 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112
3113 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003114 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003115 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116};
3117
3118template <class _Alloc>
3119class __allocator_destructor
3120{
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003121 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00003123 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
3124 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125private:
3126 _Alloc& __alloc_;
3127 size_type __s_;
3128public:
3129 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003130 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003133 void operator()(pointer __p) _NOEXCEPT
3134 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135};
3136
3137template <class _InputIterator, class _ForwardIterator>
3138_ForwardIterator
3139uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3140{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003142#ifndef _LIBCPP_NO_EXCEPTIONS
3143 _ForwardIterator __s = __r;
3144 try
3145 {
3146#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003147 for (; __f != __l; ++__f, (void) ++__r)
3148 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003149#ifndef _LIBCPP_NO_EXCEPTIONS
3150 }
3151 catch (...)
3152 {
3153 for (; __s != __r; ++__s)
3154 __s->~value_type();
3155 throw;
3156 }
3157#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158 return __r;
3159}
3160
3161template <class _InputIterator, class _Size, class _ForwardIterator>
3162_ForwardIterator
3163uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3164{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003166#ifndef _LIBCPP_NO_EXCEPTIONS
3167 _ForwardIterator __s = __r;
3168 try
3169 {
3170#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003171 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3172 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003173#ifndef _LIBCPP_NO_EXCEPTIONS
3174 }
3175 catch (...)
3176 {
3177 for (; __s != __r; ++__s)
3178 __s->~value_type();
3179 throw;
3180 }
3181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182 return __r;
3183}
3184
3185template <class _ForwardIterator, class _Tp>
3186void
3187uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3188{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003190#ifndef _LIBCPP_NO_EXCEPTIONS
3191 _ForwardIterator __s = __f;
3192 try
3193 {
3194#endif
3195 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003196 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003197#ifndef _LIBCPP_NO_EXCEPTIONS
3198 }
3199 catch (...)
3200 {
3201 for (; __s != __f; ++__s)
3202 __s->~value_type();
3203 throw;
3204 }
3205#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206}
3207
3208template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003209_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3211{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003213#ifndef _LIBCPP_NO_EXCEPTIONS
3214 _ForwardIterator __s = __f;
3215 try
3216 {
3217#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003218 for (; __n > 0; ++__f, (void) --__n)
3219 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003220#ifndef _LIBCPP_NO_EXCEPTIONS
3221 }
3222 catch (...)
3223 {
3224 for (; __s != __f; ++__s)
3225 __s->~value_type();
3226 throw;
3227 }
3228#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003229 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230}
3231
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003232#if _LIBCPP_STD_VER > 14
3233
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003234template <class _Tp>
3235inline _LIBCPP_INLINE_VISIBILITY
3236void destroy_at(_Tp* __loc) {
3237 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3238 __loc->~_Tp();
3239}
3240
3241template <class _ForwardIterator>
3242inline _LIBCPP_INLINE_VISIBILITY
3243void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3244 for (; __first != __last; ++__first)
3245 _VSTD::destroy_at(_VSTD::addressof(*__first));
3246}
3247
3248template <class _ForwardIterator, class _Size>
3249inline _LIBCPP_INLINE_VISIBILITY
3250_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3251 for (; __n > 0; (void)++__first, --__n)
3252 _VSTD::destroy_at(_VSTD::addressof(*__first));
3253 return __first;
3254}
3255
Eric Fiselier290c07c2016-10-11 21:13:44 +00003256template <class _ForwardIterator>
3257inline _LIBCPP_INLINE_VISIBILITY
3258void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3259 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3260 auto __idx = __first;
3261#ifndef _LIBCPP_NO_EXCEPTIONS
3262 try {
3263#endif
3264 for (; __idx != __last; ++__idx)
3265 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3266#ifndef _LIBCPP_NO_EXCEPTIONS
3267 } catch (...) {
3268 _VSTD::destroy(__first, __idx);
3269 throw;
3270 }
3271#endif
3272}
3273
3274template <class _ForwardIterator, class _Size>
3275inline _LIBCPP_INLINE_VISIBILITY
3276_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3277 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3278 auto __idx = __first;
3279#ifndef _LIBCPP_NO_EXCEPTIONS
3280 try {
3281#endif
3282 for (; __n > 0; (void)++__idx, --__n)
3283 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3284 return __idx;
3285#ifndef _LIBCPP_NO_EXCEPTIONS
3286 } catch (...) {
3287 _VSTD::destroy(__first, __idx);
3288 throw;
3289 }
3290#endif
3291}
3292
3293
3294template <class _ForwardIterator>
3295inline _LIBCPP_INLINE_VISIBILITY
3296void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3297 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3298 auto __idx = __first;
3299#ifndef _LIBCPP_NO_EXCEPTIONS
3300 try {
3301#endif
3302 for (; __idx != __last; ++__idx)
3303 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3304#ifndef _LIBCPP_NO_EXCEPTIONS
3305 } catch (...) {
3306 _VSTD::destroy(__first, __idx);
3307 throw;
3308 }
3309#endif
3310}
3311
3312template <class _ForwardIterator, class _Size>
3313inline _LIBCPP_INLINE_VISIBILITY
3314_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3315 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3316 auto __idx = __first;
3317#ifndef _LIBCPP_NO_EXCEPTIONS
3318 try {
3319#endif
3320 for (; __n > 0; (void)++__idx, --__n)
3321 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3322 return __idx;
3323#ifndef _LIBCPP_NO_EXCEPTIONS
3324 } catch (...) {
3325 _VSTD::destroy(__first, __idx);
3326 throw;
3327 }
3328#endif
3329}
3330
3331
3332template <class _InputIt, class _ForwardIt>
3333inline _LIBCPP_INLINE_VISIBILITY
3334_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3335 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3336 auto __idx = __first_res;
3337#ifndef _LIBCPP_NO_EXCEPTIONS
3338 try {
3339#endif
3340 for (; __first != __last; (void)++__idx, ++__first)
3341 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3342 return __idx;
3343#ifndef _LIBCPP_NO_EXCEPTIONS
3344 } catch (...) {
3345 _VSTD::destroy(__first_res, __idx);
3346 throw;
3347 }
3348#endif
3349}
3350
3351template <class _InputIt, class _Size, class _ForwardIt>
3352inline _LIBCPP_INLINE_VISIBILITY
3353pair<_InputIt, _ForwardIt>
3354uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3355 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3356 auto __idx = __first_res;
3357#ifndef _LIBCPP_NO_EXCEPTIONS
3358 try {
3359#endif
3360 for (; __n > 0; ++__idx, (void)++__first, --__n)
3361 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3362 return {__first, __idx};
3363#ifndef _LIBCPP_NO_EXCEPTIONS
3364 } catch (...) {
3365 _VSTD::destroy(__first_res, __idx);
3366 throw;
3367 }
3368#endif
3369}
3370
3371
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003372#endif // _LIBCPP_STD_VER > 14
3373
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003374// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3375// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003376// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003377#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3378 && defined(__ATOMIC_RELAXED) \
3379 && defined(__ATOMIC_ACQ_REL)
3380# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
Richard Smithca47d0f2019-04-25 20:02:10 +00003381#elif defined(_LIBCPP_COMPILER_GCC)
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003382# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3383#endif
3384
3385template <class _Tp>
3386inline _LIBCPP_INLINE_VISIBILITY _Tp
3387__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3388{
3389#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3390 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3391#else
3392 return __t += 1;
3393#endif
3394}
3395
3396template <class _Tp>
3397inline _LIBCPP_INLINE_VISIBILITY _Tp
3398__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3399{
3400#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3401 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3402#else
3403 return __t -= 1;
3404#endif
3405}
3406
Howard Hinnant756c69b2010-09-22 16:48:34 +00003407class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408 : public std::exception
3409{
3410public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003411 virtual ~bad_weak_ptr() _NOEXCEPT;
3412 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413};
3414
Louis Dionne16fe2952018-07-11 23:14:33 +00003415_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003416void __throw_bad_weak_ptr()
3417{
3418#ifndef _LIBCPP_NO_EXCEPTIONS
3419 throw bad_weak_ptr();
3420#else
3421 _VSTD::abort();
3422#endif
3423}
3424
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003425template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003427class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428{
3429 __shared_count(const __shared_count&);
3430 __shared_count& operator=(const __shared_count&);
3431
3432protected:
3433 long __shared_owners_;
3434 virtual ~__shared_count();
3435private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003436 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437
3438public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003440 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441 : __shared_owners_(__refs) {}
3442
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003443#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003444 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003445 void __add_shared() _NOEXCEPT;
3446 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003447#else
3448 _LIBCPP_INLINE_VISIBILITY
3449 void __add_shared() _NOEXCEPT {
3450 __libcpp_atomic_refcount_increment(__shared_owners_);
3451 }
3452 _LIBCPP_INLINE_VISIBILITY
3453 bool __release_shared() _NOEXCEPT {
3454 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3455 __on_zero_shared();
3456 return true;
3457 }
3458 return false;
3459 }
3460#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003461 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003462 long use_count() const _NOEXCEPT {
3463 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3464 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465};
3466
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003467class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468 : private __shared_count
3469{
3470 long __shared_weak_owners_;
3471
3472public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003474 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475 : __shared_count(__refs),
3476 __shared_weak_owners_(__refs) {}
3477protected:
3478 virtual ~__shared_weak_count();
3479
3480public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003481#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003482 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003483 void __add_shared() _NOEXCEPT;
3484 void __add_weak() _NOEXCEPT;
3485 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003486#else
3487 _LIBCPP_INLINE_VISIBILITY
3488 void __add_shared() _NOEXCEPT {
3489 __shared_count::__add_shared();
3490 }
3491 _LIBCPP_INLINE_VISIBILITY
3492 void __add_weak() _NOEXCEPT {
3493 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3494 }
3495 _LIBCPP_INLINE_VISIBILITY
3496 void __release_shared() _NOEXCEPT {
3497 if (__shared_count::__release_shared())
3498 __release_weak();
3499 }
3500#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003501 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003503 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3504 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505
Howard Hinnant807d6332013-02-25 15:50:36 +00003506 // Define the function out only if we build static libc++ without RTTI.
3507 // Otherwise we may break clients who need to compile their projects with
3508 // -fno-rtti and yet link against a libc++.dylib compiled
3509 // without -fno-rtti.
3510#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003511 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003512#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003514 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515};
3516
3517template <class _Tp, class _Dp, class _Alloc>
3518class __shared_ptr_pointer
3519 : public __shared_weak_count
3520{
3521 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3522public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003525 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526
Howard Hinnant72f73582010-08-11 17:04:31 +00003527#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003528 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003529#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530
3531private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003532 virtual void __on_zero_shared() _NOEXCEPT;
3533 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534};
3535
Howard Hinnant72f73582010-08-11 17:04:31 +00003536#ifndef _LIBCPP_NO_RTTI
3537
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538template <class _Tp, class _Dp, class _Alloc>
3539const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003540__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003542 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543}
3544
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003545#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003546
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547template <class _Tp, class _Dp, class _Alloc>
3548void
Howard Hinnant719bda32011-05-28 14:41:13 +00003549__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550{
3551 __data_.first().second()(__data_.first().first());
3552 __data_.first().second().~_Dp();
3553}
3554
3555template <class _Tp, class _Dp, class _Alloc>
3556void
Howard Hinnant719bda32011-05-28 14:41:13 +00003557__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003559 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3560 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003561 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3562
Eric Fiselierf8898c82015-02-05 23:01:40 +00003563 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003565 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566}
3567
3568template <class _Tp, class _Alloc>
3569class __shared_ptr_emplace
3570 : public __shared_weak_count
3571{
3572 __compressed_pair<_Alloc, _Tp> __data_;
3573public:
3574#ifndef _LIBCPP_HAS_NO_VARIADICS
3575
Howard Hinnant756c69b2010-09-22 16:48:34 +00003576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003578 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579
3580 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003583 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3584 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585
3586#else // _LIBCPP_HAS_NO_VARIADICS
3587
Howard Hinnant756c69b2010-09-22 16:48:34 +00003588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589 __shared_ptr_emplace(_Alloc __a)
3590 : __data_(__a) {}
3591
3592 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3595 : __data_(__a, _Tp(__a0)) {}
3596
3597 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3600 : __data_(__a, _Tp(__a0, __a1)) {}
3601
3602 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3605 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3606
3607#endif // _LIBCPP_HAS_NO_VARIADICS
3608
3609private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003610 virtual void __on_zero_shared() _NOEXCEPT;
3611 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003613 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003614 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615};
3616
3617template <class _Tp, class _Alloc>
3618void
Howard Hinnant719bda32011-05-28 14:41:13 +00003619__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620{
3621 __data_.second().~_Tp();
3622}
3623
3624template <class _Tp, class _Alloc>
3625void
Howard Hinnant719bda32011-05-28 14:41:13 +00003626__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003628 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3629 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003630 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003631 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003633 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634}
3635
Erik Pilkington2a398762017-05-25 15:43:31 +00003636struct __shared_ptr_dummy_rebind_allocator_type;
3637template <>
3638class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3639{
3640public:
3641 template <class _Other>
3642 struct rebind
3643 {
3644 typedef allocator<_Other> other;
3645 };
3646};
3647
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003648template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649
3650template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003651class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003653public:
3654 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003655
Eric Fiselierae5b6672016-06-27 01:02:43 +00003656#if _LIBCPP_STD_VER > 14
3657 typedef weak_ptr<_Tp> weak_type;
3658#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659private:
3660 element_type* __ptr_;
3661 __shared_weak_count* __cntrl_;
3662
3663 struct __nat {int __for_bool_;};
3664public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003666 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003668 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003669 template<class _Yp>
3670 explicit shared_ptr(_Yp* __p,
3671 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3672 template<class _Yp, class _Dp>
3673 shared_ptr(_Yp* __p, _Dp __d,
3674 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3675 template<class _Yp, class _Dp, class _Alloc>
3676 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3677 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3679 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003680 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003682 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003686 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003687 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003688#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003690 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003691 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003692 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003693 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003694#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003696 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003697#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003698#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003699 template<class _Yp>
3700 shared_ptr(auto_ptr<_Yp>&& __r,
3701 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003702#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003703 template<class _Yp>
3704 shared_ptr(auto_ptr<_Yp> __r,
3705 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003707#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003708#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003709 template <class _Yp, class _Dp>
3710 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3711 typename enable_if
3712 <
3713 !is_lvalue_reference<_Dp>::value &&
3714 !is_array<_Yp>::value &&
3715 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3716 __nat
3717 >::type = __nat());
3718 template <class _Yp, class _Dp>
3719 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3720 typename enable_if
3721 <
3722 is_lvalue_reference<_Dp>::value &&
3723 !is_array<_Yp>::value &&
3724 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3725 __nat
3726 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003727#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003728 template <class _Yp, class _Dp>
3729 shared_ptr(unique_ptr<_Yp, _Dp>,
3730 typename enable_if
3731 <
3732 !is_lvalue_reference<_Dp>::value &&
3733 !is_array<_Yp>::value &&
3734 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3735 __nat
3736 >::type = __nat());
3737 template <class _Yp, class _Dp>
3738 shared_ptr(unique_ptr<_Yp, _Dp>,
3739 typename enable_if
3740 <
3741 is_lvalue_reference<_Dp>::value &&
3742 !is_array<_Yp>::value &&
3743 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3744 __nat
3745 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003746#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747
3748 ~shared_ptr();
3749
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003751 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003752 template<class _Yp>
3753 typename enable_if
3754 <
3755 is_convertible<_Yp*, element_type*>::value,
3756 shared_ptr&
3757 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003759 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003760#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003762 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003763 template<class _Yp>
3764 typename enable_if
3765 <
3766 is_convertible<_Yp*, element_type*>::value,
3767 shared_ptr<_Tp>&
3768 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003770 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003771#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003772 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003774 typename enable_if
3775 <
3776 !is_array<_Yp>::value &&
3777 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003778 shared_ptr
3779 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003780 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003781#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003782#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003783#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003784 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003786 typename enable_if
3787 <
3788 !is_array<_Yp>::value &&
3789 is_convertible<_Yp*, element_type*>::value,
3790 shared_ptr&
3791 >::type
3792 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003794#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003795 template <class _Yp, class _Dp>
3796 typename enable_if
3797 <
3798 !is_array<_Yp>::value &&
3799 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3800 shared_ptr&
3801 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003802#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003805#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003807 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808#endif
3809
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003811 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003813 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003814 template<class _Yp>
3815 typename enable_if
3816 <
3817 is_convertible<_Yp*, element_type*>::value,
3818 void
3819 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003821 reset(_Yp* __p);
3822 template<class _Yp, class _Dp>
3823 typename enable_if
3824 <
3825 is_convertible<_Yp*, element_type*>::value,
3826 void
3827 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003829 reset(_Yp* __p, _Dp __d);
3830 template<class _Yp, class _Dp, class _Alloc>
3831 typename enable_if
3832 <
3833 is_convertible<_Yp*, element_type*>::value,
3834 void
3835 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003837 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838
Howard Hinnant756c69b2010-09-22 16:48:34 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003840 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003842 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3843 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003845 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003847 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003849 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003851 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003852 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003853 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003854 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003856 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003857 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003858 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003860 _LIBCPP_INLINE_VISIBILITY
3861 bool
3862 __owner_equivalent(const shared_ptr& __p) const
3863 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864
Howard Hinnant72f73582010-08-11 17:04:31 +00003865#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003868 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003869 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003870 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003871 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003872#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873
Zoe Carverd9040c72019-10-22 15:16:49 +00003874 template<class _Yp, class _CntrlBlk>
3875 static shared_ptr<_Tp>
3876 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
3877 {
3878 shared_ptr<_Tp> __r;
3879 __r.__ptr_ = __p;
3880 __r.__cntrl_ = __cntrl;
3881 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
3882 return __r;
3883 }
Zoe Carver6cd05c32019-08-19 15:47:16 +00003884
3885 template<class _Alloc, class ..._Args>
3886 static
3887 shared_ptr<_Tp>
3888 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3889
Howard Hinnantc51e1022010-05-11 19:42:16 +00003890private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003891 template <class _Yp, bool = is_function<_Yp>::value>
3892 struct __shared_ptr_default_allocator
3893 {
3894 typedef allocator<_Yp> type;
3895 };
3896
3897 template <class _Yp>
3898 struct __shared_ptr_default_allocator<_Yp, true>
3899 {
3900 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3901 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003903 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003904 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003905 typename enable_if<is_convertible<_OrigPtr*,
3906 const enable_shared_from_this<_Yp>*
3907 >::value,
3908 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003909 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3910 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003912 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003913 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003914 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003915 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3916 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003917 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 }
3919
Erik Pilkington2a398762017-05-25 15:43:31 +00003920 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003922 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3923 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924};
3925
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003926
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003928inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003929_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003930shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931 : __ptr_(0),
3932 __cntrl_(0)
3933{
3934}
3935
3936template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003937inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003938_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003939shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940 : __ptr_(0),
3941 __cntrl_(0)
3942{
3943}
3944
3945template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003946template<class _Yp>
3947shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3948 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949 : __ptr_(__p)
3950{
3951 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00003952 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3953 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
3954 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003956 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957}
3958
3959template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003960template<class _Yp, class _Dp>
3961shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3962 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963 : __ptr_(__p)
3964{
3965#ifndef _LIBCPP_NO_EXCEPTIONS
3966 try
3967 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003968#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003969 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
3970 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
3971 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003972 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973#ifndef _LIBCPP_NO_EXCEPTIONS
3974 }
3975 catch (...)
3976 {
3977 __d(__p);
3978 throw;
3979 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003980#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981}
3982
3983template<class _Tp>
3984template<class _Dp>
3985shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3986 : __ptr_(0)
3987{
3988#ifndef _LIBCPP_NO_EXCEPTIONS
3989 try
3990 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003991#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00003992 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
3993 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
3994 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995#ifndef _LIBCPP_NO_EXCEPTIONS
3996 }
3997 catch (...)
3998 {
3999 __d(__p);
4000 throw;
4001 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004002#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003}
4004
4005template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004006template<class _Yp, class _Dp, class _Alloc>
4007shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4008 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009 : __ptr_(__p)
4010{
4011#ifndef _LIBCPP_NO_EXCEPTIONS
4012 try
4013 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004014#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004016 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017 typedef __allocator_destructor<_A2> _D2;
4018 _A2 __a2(__a);
4019 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004020 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4021 _CntrlBlk(__p, __d, __a);
4022 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004023 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024#ifndef _LIBCPP_NO_EXCEPTIONS
4025 }
4026 catch (...)
4027 {
4028 __d(__p);
4029 throw;
4030 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004031#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032}
4033
4034template<class _Tp>
4035template<class _Dp, class _Alloc>
4036shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4037 : __ptr_(0)
4038{
4039#ifndef _LIBCPP_NO_EXCEPTIONS
4040 try
4041 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004042#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004044 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045 typedef __allocator_destructor<_A2> _D2;
4046 _A2 __a2(__a);
4047 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004048 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4049 _CntrlBlk(__p, __d, __a);
4050 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051#ifndef _LIBCPP_NO_EXCEPTIONS
4052 }
4053 catch (...)
4054 {
4055 __d(__p);
4056 throw;
4057 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004058#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059}
4060
4061template<class _Tp>
4062template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004063inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004064shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065 : __ptr_(__p),
4066 __cntrl_(__r.__cntrl_)
4067{
4068 if (__cntrl_)
4069 __cntrl_->__add_shared();
4070}
4071
4072template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004073inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004074shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075 : __ptr_(__r.__ptr_),
4076 __cntrl_(__r.__cntrl_)
4077{
4078 if (__cntrl_)
4079 __cntrl_->__add_shared();
4080}
4081
4082template<class _Tp>
4083template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004084inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004086 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004087 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088 : __ptr_(__r.__ptr_),
4089 __cntrl_(__r.__cntrl_)
4090{
4091 if (__cntrl_)
4092 __cntrl_->__add_shared();
4093}
4094
Howard Hinnant74279a52010-09-04 23:28:19 +00004095#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096
4097template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004098inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004099shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100 : __ptr_(__r.__ptr_),
4101 __cntrl_(__r.__cntrl_)
4102{
4103 __r.__ptr_ = 0;
4104 __r.__cntrl_ = 0;
4105}
4106
4107template<class _Tp>
4108template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004109inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004111 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004112 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113 : __ptr_(__r.__ptr_),
4114 __cntrl_(__r.__cntrl_)
4115{
4116 __r.__ptr_ = 0;
4117 __r.__cntrl_ = 0;
4118}
4119
Howard Hinnant74279a52010-09-04 23:28:19 +00004120#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121
Marshall Clowb22274f2017-01-24 22:22:33 +00004122#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004124template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004125#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004126shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004128shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004130 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131 : __ptr_(__r.get())
4132{
4133 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4134 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004135 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136 __r.release();
4137}
Marshall Clowb22274f2017-01-24 22:22:33 +00004138#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139
4140template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004141template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004142#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4144#else
4145shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4146#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004147 typename enable_if
4148 <
4149 !is_lvalue_reference<_Dp>::value &&
4150 !is_array<_Yp>::value &&
4151 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4152 __nat
4153 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154 : __ptr_(__r.get())
4155{
Marshall Clow35cde742015-05-10 13:59:45 +00004156#if _LIBCPP_STD_VER > 11
4157 if (__ptr_ == nullptr)
4158 __cntrl_ = nullptr;
4159 else
4160#endif
4161 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004162 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4163 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4164 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004165 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004166 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167 __r.release();
4168}
4169
4170template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004171template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4174#else
4175shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4176#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004177 typename enable_if
4178 <
4179 is_lvalue_reference<_Dp>::value &&
4180 !is_array<_Yp>::value &&
4181 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4182 __nat
4183 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184 : __ptr_(__r.get())
4185{
Marshall Clow35cde742015-05-10 13:59:45 +00004186#if _LIBCPP_STD_VER > 11
4187 if (__ptr_ == nullptr)
4188 __cntrl_ = nullptr;
4189 else
4190#endif
4191 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004192 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004193 typedef __shared_ptr_pointer<_Yp*,
4194 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004195 _AllocT > _CntrlBlk;
4196 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004197 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004198 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199 __r.release();
4200}
4201
Zoe Carver6cd05c32019-08-19 15:47:16 +00004202template<class _Tp>
Zoe Carver6cd05c32019-08-19 15:47:16 +00004203template<class _Alloc, class ..._Args>
4204shared_ptr<_Tp>
4205shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4206{
4207 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
4208 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4209 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
4210 typedef __allocator_destructor<_A2> _D2;
4211 _A2 __a2(__a);
4212 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4213 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4214 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
4215 shared_ptr<_Tp> __r;
4216 __r.__ptr_ = __hold2.get()->get();
4217 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
4218 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
4219 return __r;
4220}
4221
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222template<class _Tp>
4223shared_ptr<_Tp>::~shared_ptr()
4224{
4225 if (__cntrl_)
4226 __cntrl_->__release_shared();
4227}
4228
4229template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004230inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004232shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233{
4234 shared_ptr(__r).swap(*this);
4235 return *this;
4236}
4237
4238template<class _Tp>
4239template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004240inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004241typename enable_if
4242<
Marshall Clow7e384b72017-01-10 16:59:33 +00004243 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004244 shared_ptr<_Tp>&
4245>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004246shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
4248 shared_ptr(__r).swap(*this);
4249 return *this;
4250}
4251
Howard Hinnant74279a52010-09-04 23:28:19 +00004252#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253
4254template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004255inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004257shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004259 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260 return *this;
4261}
4262
4263template<class _Tp>
4264template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004265inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004266typename enable_if
4267<
Marshall Clow7e384b72017-01-10 16:59:33 +00004268 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004269 shared_ptr<_Tp>&
4270>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4272{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004273 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274 return *this;
4275}
4276
Marshall Clowb22274f2017-01-24 22:22:33 +00004277#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278template<class _Tp>
4279template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004280inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004281typename enable_if
4282<
4283 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004284 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004285 shared_ptr<_Tp>
4286>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4288{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004289 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290 return *this;
4291}
Marshall Clowb22274f2017-01-24 22:22:33 +00004292#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293
4294template<class _Tp>
4295template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004296inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004297typename enable_if
4298<
4299 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004300 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004301 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004302 shared_ptr<_Tp>&
4303>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4305{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004306 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 return *this;
4308}
4309
Howard Hinnant74279a52010-09-04 23:28:19 +00004310#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311
Marshall Clowb22274f2017-01-24 22:22:33 +00004312#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313template<class _Tp>
4314template<class _Yp>
4315inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004316typename enable_if
4317<
4318 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004319 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004320 shared_ptr<_Tp>&
4321>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004322shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323{
4324 shared_ptr(__r).swap(*this);
4325 return *this;
4326}
Marshall Clowb22274f2017-01-24 22:22:33 +00004327#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328
4329template<class _Tp>
4330template <class _Yp, class _Dp>
4331inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004332typename enable_if
4333<
4334 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004335 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004336 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004337 shared_ptr<_Tp>&
4338>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4340{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004341 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342 return *this;
4343}
4344
Howard Hinnant74279a52010-09-04 23:28:19 +00004345#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346
4347template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004348inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349void
Howard Hinnant719bda32011-05-28 14:41:13 +00004350shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004352 _VSTD::swap(__ptr_, __r.__ptr_);
4353 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354}
4355
4356template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004357inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358void
Howard Hinnant719bda32011-05-28 14:41:13 +00004359shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360{
4361 shared_ptr().swap(*this);
4362}
4363
4364template<class _Tp>
4365template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004366inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004367typename enable_if
4368<
Marshall Clow7e384b72017-01-10 16:59:33 +00004369 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004370 void
4371>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372shared_ptr<_Tp>::reset(_Yp* __p)
4373{
4374 shared_ptr(__p).swap(*this);
4375}
4376
4377template<class _Tp>
4378template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004379inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004380typename enable_if
4381<
Marshall Clow7e384b72017-01-10 16:59:33 +00004382 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004383 void
4384>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4386{
4387 shared_ptr(__p, __d).swap(*this);
4388}
4389
4390template<class _Tp>
4391template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004392inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004393typename enable_if
4394<
Marshall Clow7e384b72017-01-10 16:59:33 +00004395 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004396 void
4397>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4399{
4400 shared_ptr(__p, __d, __a).swap(*this);
4401}
4402
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004403template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004405typename enable_if
4406<
4407 !is_array<_Tp>::value,
4408 shared_ptr<_Tp>
4409>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004410make_shared(_Args&& ...__args)
4411{
Zoe Carverd9040c72019-10-22 15:16:49 +00004412 static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
4413 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4414 typedef allocator<_CntrlBlk> _A2;
4415 typedef __allocator_destructor<_A2> _D2;
4416
4417 _A2 __a2;
4418 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4419 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
4420
4421 _Tp *__ptr = __hold2.get()->get();
4422 return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423}
4424
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004425template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004427typename enable_if
4428<
4429 !is_array<_Tp>::value,
4430 shared_ptr<_Tp>
4431>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432allocate_shared(const _Alloc& __a, _Args&& ...__args)
4433{
Zoe Carver6cd05c32019-08-19 15:47:16 +00004434 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435}
4436
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437template<class _Tp, class _Up>
4438inline _LIBCPP_INLINE_VISIBILITY
4439bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004440operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441{
4442 return __x.get() == __y.get();
4443}
4444
4445template<class _Tp, class _Up>
4446inline _LIBCPP_INLINE_VISIBILITY
4447bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004448operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449{
4450 return !(__x == __y);
4451}
4452
4453template<class _Tp, class _Up>
4454inline _LIBCPP_INLINE_VISIBILITY
4455bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004456operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004458#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004459 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4460 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004461#else
4462 return less<>()(__x.get(), __y.get());
4463#endif
4464
Howard Hinnantb17caf92012-02-21 21:02:58 +00004465}
4466
4467template<class _Tp, class _Up>
4468inline _LIBCPP_INLINE_VISIBILITY
4469bool
4470operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4471{
4472 return __y < __x;
4473}
4474
4475template<class _Tp, class _Up>
4476inline _LIBCPP_INLINE_VISIBILITY
4477bool
4478operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4479{
4480 return !(__y < __x);
4481}
4482
4483template<class _Tp, class _Up>
4484inline _LIBCPP_INLINE_VISIBILITY
4485bool
4486operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4487{
4488 return !(__x < __y);
4489}
4490
4491template<class _Tp>
4492inline _LIBCPP_INLINE_VISIBILITY
4493bool
4494operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4495{
4496 return !__x;
4497}
4498
4499template<class _Tp>
4500inline _LIBCPP_INLINE_VISIBILITY
4501bool
4502operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4503{
4504 return !__x;
4505}
4506
4507template<class _Tp>
4508inline _LIBCPP_INLINE_VISIBILITY
4509bool
4510operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4511{
4512 return static_cast<bool>(__x);
4513}
4514
4515template<class _Tp>
4516inline _LIBCPP_INLINE_VISIBILITY
4517bool
4518operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4519{
4520 return static_cast<bool>(__x);
4521}
4522
4523template<class _Tp>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
4526operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4527{
4528 return less<_Tp*>()(__x.get(), nullptr);
4529}
4530
4531template<class _Tp>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
4534operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4535{
4536 return less<_Tp*>()(nullptr, __x.get());
4537}
4538
4539template<class _Tp>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
4542operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4543{
4544 return nullptr < __x;
4545}
4546
4547template<class _Tp>
4548inline _LIBCPP_INLINE_VISIBILITY
4549bool
4550operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4551{
4552 return __x < nullptr;
4553}
4554
4555template<class _Tp>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
4558operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4559{
4560 return !(nullptr < __x);
4561}
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565bool
4566operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4567{
4568 return !(__x < nullptr);
4569}
4570
4571template<class _Tp>
4572inline _LIBCPP_INLINE_VISIBILITY
4573bool
4574operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4575{
4576 return !(__x < nullptr);
4577}
4578
4579template<class _Tp>
4580inline _LIBCPP_INLINE_VISIBILITY
4581bool
4582operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4583{
4584 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004585}
4586
4587template<class _Tp>
4588inline _LIBCPP_INLINE_VISIBILITY
4589void
Howard Hinnant719bda32011-05-28 14:41:13 +00004590swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591{
4592 __x.swap(__y);
4593}
4594
4595template<class _Tp, class _Up>
4596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004597typename enable_if
4598<
4599 !is_array<_Tp>::value && !is_array<_Up>::value,
4600 shared_ptr<_Tp>
4601>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004602static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603{
4604 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4605}
4606
4607template<class _Tp, class _Up>
4608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004609typename enable_if
4610<
4611 !is_array<_Tp>::value && !is_array<_Up>::value,
4612 shared_ptr<_Tp>
4613>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004614dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004615{
4616 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4617 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4618}
4619
4620template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004621typename enable_if
4622<
4623 is_array<_Tp>::value == is_array<_Up>::value,
4624 shared_ptr<_Tp>
4625>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004626const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004627{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004628 typedef typename remove_extent<_Tp>::type _RTp;
4629 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004630}
4631
Howard Hinnant72f73582010-08-11 17:04:31 +00004632#ifndef _LIBCPP_NO_RTTI
4633
Howard Hinnantc51e1022010-05-11 19:42:16 +00004634template<class _Dp, class _Tp>
4635inline _LIBCPP_INLINE_VISIBILITY
4636_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004637get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004638{
4639 return __p.template __get_deleter<_Dp>();
4640}
4641
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004642#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004643
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004645class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004646{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004647public:
4648 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649private:
4650 element_type* __ptr_;
4651 __shared_weak_count* __cntrl_;
4652
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004653public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004655 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004656 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004657 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4658 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004660 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004661 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004662 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4663 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004664
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004665#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004667 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004668 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004669 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4670 _NOEXCEPT;
4671#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004672 ~weak_ptr();
4673
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004675 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004676 template<class _Yp>
4677 typename enable_if
4678 <
4679 is_convertible<_Yp*, element_type*>::value,
4680 weak_ptr&
4681 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004683 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4684
4685#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4686
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004688 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4689 template<class _Yp>
4690 typename enable_if
4691 <
4692 is_convertible<_Yp*, element_type*>::value,
4693 weak_ptr&
4694 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004696 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4697
4698#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4699
4700 template<class _Yp>
4701 typename enable_if
4702 <
4703 is_convertible<_Yp*, element_type*>::value,
4704 weak_ptr&
4705 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004707 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004708
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004710 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004712 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004713
Howard Hinnant756c69b2010-09-22 16:48:34 +00004714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004715 long use_count() const _NOEXCEPT
4716 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004718 bool expired() const _NOEXCEPT
4719 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4720 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004721 template<class _Up>
4722 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004723 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004724 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004725 template<class _Up>
4726 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004727 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004728 {return __cntrl_ < __r.__cntrl_;}
4729
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004730 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4731 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004732};
4733
4734template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004735inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004736_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004737weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004738 : __ptr_(0),
4739 __cntrl_(0)
4740{
4741}
4742
4743template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004744inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004745weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004746 : __ptr_(__r.__ptr_),
4747 __cntrl_(__r.__cntrl_)
4748{
4749 if (__cntrl_)
4750 __cntrl_->__add_weak();
4751}
4752
4753template<class _Tp>
4754template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004755inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004756weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004757 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004758 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004759 : __ptr_(__r.__ptr_),
4760 __cntrl_(__r.__cntrl_)
4761{
4762 if (__cntrl_)
4763 __cntrl_->__add_weak();
4764}
4765
4766template<class _Tp>
4767template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004768inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004769weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004770 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004771 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004772 : __ptr_(__r.__ptr_),
4773 __cntrl_(__r.__cntrl_)
4774{
4775 if (__cntrl_)
4776 __cntrl_->__add_weak();
4777}
4778
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004779#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4780
4781template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004782inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004783weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4784 : __ptr_(__r.__ptr_),
4785 __cntrl_(__r.__cntrl_)
4786{
4787 __r.__ptr_ = 0;
4788 __r.__cntrl_ = 0;
4789}
4790
4791template<class _Tp>
4792template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004793inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004794weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4795 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4796 _NOEXCEPT
4797 : __ptr_(__r.__ptr_),
4798 __cntrl_(__r.__cntrl_)
4799{
4800 __r.__ptr_ = 0;
4801 __r.__cntrl_ = 0;
4802}
4803
4804#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4805
Howard Hinnantc51e1022010-05-11 19:42:16 +00004806template<class _Tp>
4807weak_ptr<_Tp>::~weak_ptr()
4808{
4809 if (__cntrl_)
4810 __cntrl_->__release_weak();
4811}
4812
4813template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004814inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004815weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004816weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004817{
4818 weak_ptr(__r).swap(*this);
4819 return *this;
4820}
4821
4822template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004823template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004824inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004825typename enable_if
4826<
4827 is_convertible<_Yp*, _Tp*>::value,
4828 weak_ptr<_Tp>&
4829>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004830weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004831{
4832 weak_ptr(__r).swap(*this);
4833 return *this;
4834}
4835
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004836#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4837
4838template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004839inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004840weak_ptr<_Tp>&
4841weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4842{
4843 weak_ptr(_VSTD::move(__r)).swap(*this);
4844 return *this;
4845}
4846
Howard Hinnantc51e1022010-05-11 19:42:16 +00004847template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004848template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004849inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004850typename enable_if
4851<
4852 is_convertible<_Yp*, _Tp*>::value,
4853 weak_ptr<_Tp>&
4854>::type
4855weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4856{
4857 weak_ptr(_VSTD::move(__r)).swap(*this);
4858 return *this;
4859}
4860
4861#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4862
4863template<class _Tp>
4864template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004865inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004866typename enable_if
4867<
4868 is_convertible<_Yp*, _Tp*>::value,
4869 weak_ptr<_Tp>&
4870>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004871weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004872{
4873 weak_ptr(__r).swap(*this);
4874 return *this;
4875}
4876
4877template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004878inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004879void
Howard Hinnant719bda32011-05-28 14:41:13 +00004880weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004881{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004882 _VSTD::swap(__ptr_, __r.__ptr_);
4883 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884}
4885
4886template<class _Tp>
4887inline _LIBCPP_INLINE_VISIBILITY
4888void
Howard Hinnant719bda32011-05-28 14:41:13 +00004889swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004890{
4891 __x.swap(__y);
4892}
4893
4894template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004895inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004896void
Howard Hinnant719bda32011-05-28 14:41:13 +00004897weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004898{
4899 weak_ptr().swap(*this);
4900}
4901
4902template<class _Tp>
4903template<class _Yp>
4904shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004905 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004906 : __ptr_(__r.__ptr_),
4907 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4908{
4909 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004910 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911}
4912
4913template<class _Tp>
4914shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004915weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916{
4917 shared_ptr<_Tp> __r;
4918 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4919 if (__r.__cntrl_)
4920 __r.__ptr_ = __ptr_;
4921 return __r;
4922}
4923
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004924#if _LIBCPP_STD_VER > 14
4925template <class _Tp = void> struct owner_less;
4926#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004927template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004928#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004929
4930template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004931struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004932 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004933{
4934 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004935 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004936 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004937 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004938 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004939 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004940 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004941 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004942 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004943 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004944};
Howard Hinnantc51e1022010-05-11 19:42:16 +00004945
4946template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004947struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00004948 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4949{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004950 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004951 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004952 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004953 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004954 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004955 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004956 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004957 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004958 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004959 {return __x.owner_before(__y);}
4960};
4961
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004962#if _LIBCPP_STD_VER > 14
4963template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004964struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004965{
4966 template <class _Tp, class _Up>
4967 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004968 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004969 {return __x.owner_before(__y);}
4970 template <class _Tp, class _Up>
4971 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004972 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004973 {return __x.owner_before(__y);}
4974 template <class _Tp, class _Up>
4975 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004976 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004977 {return __x.owner_before(__y);}
4978 template <class _Tp, class _Up>
4979 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004980 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004981 {return __x.owner_before(__y);}
4982 typedef void is_transparent;
4983};
4984#endif
4985
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004987class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00004988{
4989 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004990protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004991 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004992 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004994 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004996 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4997 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004999 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005000public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005002 shared_ptr<_Tp> shared_from_this()
5003 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005005 shared_ptr<_Tp const> shared_from_this() const
5006 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005007
Eric Fiselier84006862016-06-02 00:15:35 +00005008#if _LIBCPP_STD_VER > 14
5009 _LIBCPP_INLINE_VISIBILITY
5010 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5011 { return __weak_this_; }
5012
5013 _LIBCPP_INLINE_VISIBILITY
5014 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5015 { return __weak_this_; }
5016#endif // _LIBCPP_STD_VER > 14
5017
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018 template <class _Up> friend class shared_ptr;
5019};
5020
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005021template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005022struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005023{
5024 typedef shared_ptr<_Tp> argument_type;
5025 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005026
Howard Hinnant756c69b2010-09-22 16:48:34 +00005027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005028 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005029 {
5030 return hash<_Tp*>()(__ptr.get());
5031 }
5032};
5033
Howard Hinnantc834c512011-11-29 18:15:50 +00005034template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005035inline _LIBCPP_INLINE_VISIBILITY
5036basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005037operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005038
Eric Fiselier9b492672016-06-18 02:12:53 +00005039
5040#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005041
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005042class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005043{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005044 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005045public:
5046 void lock() _NOEXCEPT;
5047 void unlock() _NOEXCEPT;
5048
5049private:
5050 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5051 __sp_mut(const __sp_mut&);
5052 __sp_mut& operator=(const __sp_mut&);
5053
Howard Hinnant8331b762013-03-06 23:30:19 +00005054 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005055};
5056
Mehdi Amini228053d2017-05-04 17:08:54 +00005057_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5058__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005059
5060template <class _Tp>
5061inline _LIBCPP_INLINE_VISIBILITY
5062bool
5063atomic_is_lock_free(const shared_ptr<_Tp>*)
5064{
5065 return false;
5066}
5067
5068template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005069_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005070shared_ptr<_Tp>
5071atomic_load(const shared_ptr<_Tp>* __p)
5072{
5073 __sp_mut& __m = __get_sp_mut(__p);
5074 __m.lock();
5075 shared_ptr<_Tp> __q = *__p;
5076 __m.unlock();
5077 return __q;
5078}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005079
Howard Hinnant9fa30202012-07-30 01:40:57 +00005080template <class _Tp>
5081inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005082_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005083shared_ptr<_Tp>
5084atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5085{
5086 return atomic_load(__p);
5087}
5088
5089template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005090_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005091void
5092atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5093{
5094 __sp_mut& __m = __get_sp_mut(__p);
5095 __m.lock();
5096 __p->swap(__r);
5097 __m.unlock();
5098}
5099
5100template <class _Tp>
5101inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005102_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005103void
5104atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5105{
5106 atomic_store(__p, __r);
5107}
5108
5109template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005110_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005111shared_ptr<_Tp>
5112atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5113{
5114 __sp_mut& __m = __get_sp_mut(__p);
5115 __m.lock();
5116 __p->swap(__r);
5117 __m.unlock();
5118 return __r;
5119}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005120
Howard Hinnant9fa30202012-07-30 01:40:57 +00005121template <class _Tp>
5122inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005123_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005124shared_ptr<_Tp>
5125atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5126{
5127 return atomic_exchange(__p, __r);
5128}
5129
5130template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005131_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005132bool
5133atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5134{
Marshall Clow4201ee82016-05-18 17:50:13 +00005135 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005136 __sp_mut& __m = __get_sp_mut(__p);
5137 __m.lock();
5138 if (__p->__owner_equivalent(*__v))
5139 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005140 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005141 *__p = __w;
5142 __m.unlock();
5143 return true;
5144 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005145 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005146 *__v = *__p;
5147 __m.unlock();
5148 return false;
5149}
5150
5151template <class _Tp>
5152inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005153_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005154bool
5155atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5156{
5157 return atomic_compare_exchange_strong(__p, __v, __w);
5158}
5159
5160template <class _Tp>
5161inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005162_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005163bool
5164atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5165 shared_ptr<_Tp> __w, memory_order, memory_order)
5166{
5167 return atomic_compare_exchange_strong(__p, __v, __w);
5168}
5169
5170template <class _Tp>
5171inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005172_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005173bool
5174atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5175 shared_ptr<_Tp> __w, memory_order, memory_order)
5176{
5177 return atomic_compare_exchange_weak(__p, __v, __w);
5178}
5179
Eric Fiselier9b492672016-06-18 02:12:53 +00005180#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005181
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005182//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005183#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5184# ifndef _LIBCPP_CXX03_LANG
5185enum class pointer_safety : unsigned char {
5186 relaxed,
5187 preferred,
5188 strict
5189};
5190# endif
5191#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005192struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005193{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005194 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005195 {
5196 relaxed,
5197 preferred,
5198 strict
5199 };
5200
Howard Hinnant49e145e2012-10-30 19:06:59 +00005201 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005202
Howard Hinnant756c69b2010-09-22 16:48:34 +00005203 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005204 pointer_safety() : __v_() {}
5205
5206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005207 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005209 operator int() const {return __v_;}
5210};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005211#endif
5212
5213#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005214 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005215_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5216#else
5217// This function is only offered in C++03 under ABI v1.
5218# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5219inline _LIBCPP_INLINE_VISIBILITY
5220pointer_safety get_pointer_safety() _NOEXCEPT {
5221 return pointer_safety::relaxed;
5222}
5223# endif
5224#endif
5225
Howard Hinnantc51e1022010-05-11 19:42:16 +00005226
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005227_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5228_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5229_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005230_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005231
5232template <class _Tp>
5233inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005234_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005235undeclare_reachable(_Tp* __p)
5236{
5237 return static_cast<_Tp*>(__undeclare_reachable(__p));
5238}
5239
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005240_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005241
Marshall Clow8982dcd2015-07-13 20:04:56 +00005242// --- Helper for container swap --
5243template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005244inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005245void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5246#if _LIBCPP_STD_VER >= 14
5247 _NOEXCEPT
5248#else
5249 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5250#endif
5251{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005252 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005253 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5254}
5255
5256template <typename _Alloc>
5257_LIBCPP_INLINE_VISIBILITY
5258void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5259#if _LIBCPP_STD_VER >= 14
5260 _NOEXCEPT
5261#else
5262 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5263#endif
5264{
5265 using _VSTD::swap;
5266 swap(__a1, __a2);
5267}
5268
5269template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005270inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005271void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5272
Marshall Clowff91de82015-08-18 19:51:37 +00005273template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005274struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005275 _Traits::propagate_on_container_move_assignment::value
5276#if _LIBCPP_STD_VER > 14
5277 || _Traits::is_always_equal::value
5278#else
5279 && is_nothrow_move_assignable<_Alloc>::value
5280#endif
5281 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005282
Marshall Clowa591b9a2016-07-11 21:38:08 +00005283
5284#ifndef _LIBCPP_HAS_NO_VARIADICS
5285template <class _Tp, class _Alloc>
5286struct __temp_value {
5287 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005288
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005289 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005290 _Alloc &__a;
5291
5292 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5293 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005294
Marshall Clowa591b9a2016-07-11 21:38:08 +00005295 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005296 _LIBCPP_NO_CFI
5297 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5298 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5299 _VSTD::forward<_Args>(__args)...);
5300 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005301
Marshall Clowa591b9a2016-07-11 21:38:08 +00005302 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5303 };
5304#endif
5305
Marshall Clowe46031a2018-07-02 18:41:15 +00005306template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005307struct __is_allocator : false_type {};
5308
5309template<typename _Alloc>
5310struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005311 typename __void_t<typename _Alloc::value_type>::type,
5312 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5313 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005314 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005315
Eric Fiselier74ebee62019-06-08 01:31:19 +00005316// __builtin_new_allocator -- A non-templated helper for allocating and
5317// deallocating memory using __builtin_operator_new and
5318// __builtin_operator_delete. It should be used in preference to
5319// `std::allocator<T>` to avoid additional instantiations.
5320struct __builtin_new_allocator {
5321 struct __builtin_new_deleter {
5322 typedef void* pointer_type;
5323
5324 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5325 : __size_(__size), __align_(__align) {}
5326
5327 void operator()(void* p) const _NOEXCEPT {
5328 std::__libcpp_deallocate(p, __size_, __align_);
5329 }
5330
5331 private:
5332 size_t __size_;
5333 size_t __align_;
5334 };
5335
5336 typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5337
5338 static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5339 return __holder_t(std::__libcpp_allocate(__s, __align),
5340 __builtin_new_deleter(__s, __align));
5341 }
5342
5343 static void __deallocate_bytes(void* __p, size_t __s,
5344 size_t __align) _NOEXCEPT {
5345 std::__libcpp_deallocate(__p, __s, __align);
5346 }
5347
5348 template <class _Tp>
5349 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5350 static __holder_t __allocate_type(size_t __n) {
5351 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5352 }
5353
5354 template <class _Tp>
5355 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5356 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5357 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5358 }
5359};
5360
5361
Howard Hinnantc51e1022010-05-11 19:42:16 +00005362_LIBCPP_END_NAMESPACE_STD
5363
Eric Fiselierf4433a32017-05-31 22:07:49 +00005364_LIBCPP_POP_MACROS
5365
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005366#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +00005367# include <__pstl_memory>
Louis Dionne59d0b3c2019-08-05 18:29:14 +00005368#endif
5369
Howard Hinnantc51e1022010-05-11 19:42:16 +00005370#endif // _LIBCPP_MEMORY