blob: 3398629c168ac706c28bc1d02ec8687d7cf70634 [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>
551struct owner_less<shared_ptr<T>>
552 : 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>
561struct owner_less<weak_ptr<T>>
562 : 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 Fiselier9d355982017-04-12 23:45:53 +0000665#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000666#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000667# include <atomic>
668#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000669#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000670
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000671#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000673#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674
Eric Fiselierf4433a32017-05-31 22:07:49 +0000675_LIBCPP_PUSH_MACROS
676#include <__undef_macros>
677
678
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679_LIBCPP_BEGIN_NAMESPACE_STD
680
Eric Fiselier89659d12015-07-07 00:27:16 +0000681template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000682inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000683_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_RELAXED) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_RELAXED);
688#else
689 return *__value;
690#endif
691}
692
Kuba Breckade9d6792016-09-04 09:55:12 +0000693template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000694inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000695_ValueType __libcpp_acquire_load(_ValueType const* __value) {
696#if !defined(_LIBCPP_HAS_NO_THREADS) && \
697 defined(__ATOMIC_ACQUIRE) && \
698 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
699 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
700#else
701 return *__value;
702#endif
703}
704
Marshall Clow78dbe462016-11-14 18:22:19 +0000705// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707template <class _Tp> class allocator;
708
709template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000710class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711{
712public:
713 typedef void* pointer;
714 typedef const void* const_pointer;
715 typedef void value_type;
716
717 template <class _Up> struct rebind {typedef allocator<_Up> other;};
718};
719
Howard Hinnant9f771052012-01-19 23:15:22 +0000720template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000721class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000722{
723public:
724 typedef const void* pointer;
725 typedef const void* const_pointer;
726 typedef const void value_type;
727
728 template <class _Up> struct rebind {typedef allocator<_Up> other;};
729};
730
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731// pointer_traits
732
Marshall Clow0be70c32017-06-14 21:23:57 +0000733template <class _Tp, class = void>
734struct __has_element_type : false_type {};
735
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000737struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000738 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
740template <class _Ptr, bool = __has_element_type<_Ptr>::value>
741struct __pointer_traits_element_type;
742
743template <class _Ptr>
744struct __pointer_traits_element_type<_Ptr, true>
745{
746 typedef typename _Ptr::element_type type;
747};
748
749#ifndef _LIBCPP_HAS_NO_VARIADICS
750
751template <template <class, class...> class _Sp, class _Tp, class ..._Args>
752struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
753{
754 typedef typename _Sp<_Tp, _Args...>::element_type type;
755};
756
757template <template <class, class...> class _Sp, class _Tp, class ..._Args>
758struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
759{
760 typedef _Tp type;
761};
762
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000763#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
765template <template <class> class _Sp, class _Tp>
766struct __pointer_traits_element_type<_Sp<_Tp>, true>
767{
768 typedef typename _Sp<_Tp>::element_type type;
769};
770
771template <template <class> class _Sp, class _Tp>
772struct __pointer_traits_element_type<_Sp<_Tp>, false>
773{
774 typedef _Tp type;
775};
776
777template <template <class, class> class _Sp, class _Tp, class _A0>
778struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
779{
780 typedef typename _Sp<_Tp, _A0>::element_type type;
781};
782
783template <template <class, class> class _Sp, class _Tp, class _A0>
784struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
785{
786 typedef _Tp type;
787};
788
789template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
790struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
791{
792 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
793};
794
795template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
796struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
797{
798 typedef _Tp type;
799};
800
801template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
802 class _A1, class _A2>
803struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
804{
805 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
806};
807
808template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
809 class _A1, class _A2>
810struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
811{
812 typedef _Tp type;
813};
814
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000815#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
Marshall Clow0be70c32017-06-14 21:23:57 +0000817template <class _Tp, class = void>
818struct __has_difference_type : false_type {};
819
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000821struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000822 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
824template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
825struct __pointer_traits_difference_type
826{
827 typedef ptrdiff_t type;
828};
829
830template <class _Ptr>
831struct __pointer_traits_difference_type<_Ptr, true>
832{
833 typedef typename _Ptr::difference_type type;
834};
835
836template <class _Tp, class _Up>
837struct __has_rebind
838{
839private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000840 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 template <class _Xp> static __two __test(...);
842 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
843public:
844 static const bool value = sizeof(__test<_Tp>(0)) == 1;
845};
846
847template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
848struct __pointer_traits_rebind
849{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000850#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851 typedef typename _Tp::template rebind<_Up> type;
852#else
853 typedef typename _Tp::template rebind<_Up>::other type;
854#endif
855};
856
857#ifndef _LIBCPP_HAS_NO_VARIADICS
858
859template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
861{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000862#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
864#else
865 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
866#endif
867};
868
869template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
870struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
871{
872 typedef _Sp<_Up, _Args...> type;
873};
874
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000875#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876
877template <template <class> class _Sp, class _Tp, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
879{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000880#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 typedef typename _Sp<_Tp>::template rebind<_Up> type;
882#else
883 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
884#endif
885};
886
887template <template <class> class _Sp, class _Tp, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
889{
890 typedef _Sp<_Up> type;
891};
892
893template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
894struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
895{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000896#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
898#else
899 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
900#endif
901};
902
903template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
905{
906 typedef _Sp<_Up, _A0> type;
907};
908
909template <template <class, class, class> class _Sp, class _Tp, class _A0,
910 class _A1, class _Up>
911struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
912{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000913#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
915#else
916 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
917#endif
918};
919
920template <template <class, class, class> class _Sp, class _Tp, class _A0,
921 class _A1, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
923{
924 typedef _Sp<_Up, _A0, _A1> type;
925};
926
927template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
928 class _A1, class _A2, class _Up>
929struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
930{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
933#else
934 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
935#endif
936};
937
938template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
939 class _A1, class _A2, class _Up>
940struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
941{
942 typedef _Sp<_Up, _A0, _A1, _A2> type;
943};
944
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000945#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
947template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000948struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949{
950 typedef _Ptr pointer;
951 typedef typename __pointer_traits_element_type<pointer>::type element_type;
952 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
953
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000954#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000955 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956#else
957 template <class _Up> struct rebind
958 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000959#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960
961private:
962 struct __nat {};
963public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 static pointer pointer_to(typename conditional<is_void<element_type>::value,
966 __nat, element_type>::type& __r)
967 {return pointer::pointer_to(__r);}
968};
969
970template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000971struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972{
973 typedef _Tp* pointer;
974 typedef _Tp element_type;
975 typedef ptrdiff_t difference_type;
976
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000977#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 template <class _Up> using rebind = _Up*;
979#else
980 template <class _Up> struct rebind {typedef _Up* other;};
981#endif
982
983private:
984 struct __nat {};
985public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000986 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000988 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000989 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990};
991
Eric Fiselierae3ab842015-08-23 02:56:05 +0000992template <class _From, class _To>
993struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000994#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000995 typedef typename pointer_traits<_From>::template rebind<_To> type;
996#else
997 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
998#endif
999};
1000
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001// allocator_traits
1002
Marshall Clow0be70c32017-06-14 21:23:57 +00001003template <class _Tp, class = void>
1004struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005
1006template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001007struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001008 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009
1010namespace __pointer_type_imp
1011{
1012
1013template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1014struct __pointer_type
1015{
1016 typedef typename _Dp::pointer type;
1017};
1018
1019template <class _Tp, class _Dp>
1020struct __pointer_type<_Tp, _Dp, false>
1021{
1022 typedef _Tp* type;
1023};
1024
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001025} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026
1027template <class _Tp, class _Dp>
1028struct __pointer_type
1029{
1030 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1031};
1032
Marshall Clow0be70c32017-06-14 21:23:57 +00001033template <class _Tp, class = void>
1034struct __has_const_pointer : false_type {};
1035
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001037struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001038 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039
1040template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1041struct __const_pointer
1042{
1043 typedef typename _Alloc::const_pointer type;
1044};
1045
1046template <class _Tp, class _Ptr, class _Alloc>
1047struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1048{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001049#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1051#else
1052 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1053#endif
1054};
1055
Marshall Clow0be70c32017-06-14 21:23:57 +00001056template <class _Tp, class = void>
1057struct __has_void_pointer : false_type {};
1058
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001060struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001061 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062
1063template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1064struct __void_pointer
1065{
1066 typedef typename _Alloc::void_pointer type;
1067};
1068
1069template <class _Ptr, class _Alloc>
1070struct __void_pointer<_Ptr, _Alloc, false>
1071{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001072#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1074#else
1075 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1076#endif
1077};
1078
Marshall Clow0be70c32017-06-14 21:23:57 +00001079template <class _Tp, class = void>
1080struct __has_const_void_pointer : false_type {};
1081
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001083struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001084 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
1086template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1087struct __const_void_pointer
1088{
1089 typedef typename _Alloc::const_void_pointer type;
1090};
1091
1092template <class _Ptr, class _Alloc>
1093struct __const_void_pointer<_Ptr, _Alloc, false>
1094{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1097#else
1098 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1099#endif
1100};
1101
Howard Hinnantc834c512011-11-29 18:15:50 +00001102template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001103inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001104_Tp*
1105__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106{
1107 return __p;
1108}
1109
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001110#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111template <class _Pointer>
1112inline _LIBCPP_INLINE_VISIBILITY
1113typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001114__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001116 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001118#else
1119template <class _Pointer>
1120inline _LIBCPP_INLINE_VISIBILITY
1121auto
1122__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1123-> decltype(pointer_traits<_Pointer>::to_address(__p))
1124{
1125 return pointer_traits<_Pointer>::to_address(__p);
1126}
1127
1128template <class _Pointer, class... _None>
1129inline _LIBCPP_INLINE_VISIBILITY
1130auto
1131__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1132{
1133 return _VSTD::__to_raw_pointer(__p.operator->());
1134}
1135
1136template <class _Tp>
1137inline _LIBCPP_INLINE_VISIBILITY constexpr
1138_Tp*
1139to_address(_Tp* __p) _NOEXCEPT
1140{
1141 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1142 return __p;
1143}
1144
1145template <class _Pointer>
1146inline _LIBCPP_INLINE_VISIBILITY
1147auto
1148to_address(const _Pointer& __p) _NOEXCEPT
1149{
1150 return _VSTD::__to_raw_pointer(__p);
1151}
1152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153
Marshall Clow0be70c32017-06-14 21:23:57 +00001154template <class _Tp, class = void>
1155struct __has_size_type : false_type {};
1156
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001158struct __has_size_type<_Tp,
1159 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001161template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162struct __size_type
1163{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001164 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165};
1166
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001167template <class _Alloc, class _DiffType>
1168struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169{
1170 typedef typename _Alloc::size_type type;
1171};
1172
Marshall Clow0be70c32017-06-14 21:23:57 +00001173template <class _Tp, class = void>
1174struct __has_propagate_on_container_copy_assignment : false_type {};
1175
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001177struct __has_propagate_on_container_copy_assignment<_Tp,
1178 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1179 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
1181template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1182struct __propagate_on_container_copy_assignment
1183{
1184 typedef false_type type;
1185};
1186
1187template <class _Alloc>
1188struct __propagate_on_container_copy_assignment<_Alloc, true>
1189{
1190 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1191};
1192
Marshall Clow0be70c32017-06-14 21:23:57 +00001193template <class _Tp, class = void>
1194struct __has_propagate_on_container_move_assignment : false_type {};
1195
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001197struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001198 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1199 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200
1201template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1202struct __propagate_on_container_move_assignment
1203{
1204 typedef false_type type;
1205};
1206
1207template <class _Alloc>
1208struct __propagate_on_container_move_assignment<_Alloc, true>
1209{
1210 typedef typename _Alloc::propagate_on_container_move_assignment type;
1211};
1212
Marshall Clow0be70c32017-06-14 21:23:57 +00001213template <class _Tp, class = void>
1214struct __has_propagate_on_container_swap : false_type {};
1215
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001217struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001218 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1219 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
1221template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1222struct __propagate_on_container_swap
1223{
1224 typedef false_type type;
1225};
1226
1227template <class _Alloc>
1228struct __propagate_on_container_swap<_Alloc, true>
1229{
1230 typedef typename _Alloc::propagate_on_container_swap type;
1231};
1232
Marshall Clow0be70c32017-06-14 21:23:57 +00001233template <class _Tp, class = void>
1234struct __has_is_always_equal : false_type {};
1235
Marshall Clow0b587562015-06-02 16:34:03 +00001236template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001237struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001238 typename __void_t<typename _Tp::is_always_equal>::type>
1239 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001240
1241template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1242struct __is_always_equal
1243{
1244 typedef typename _VSTD::is_empty<_Alloc>::type type;
1245};
1246
1247template <class _Alloc>
1248struct __is_always_equal<_Alloc, true>
1249{
1250 typedef typename _Alloc::is_always_equal type;
1251};
1252
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1254struct __has_rebind_other
1255{
1256private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001257 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 template <class _Xp> static __two __test(...);
1259 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1260public:
1261 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1262};
1263
1264template <class _Tp, class _Up>
1265struct __has_rebind_other<_Tp, _Up, false>
1266{
1267 static const bool value = false;
1268};
1269
1270template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1271struct __allocator_traits_rebind
1272{
1273 typedef typename _Tp::template rebind<_Up>::other type;
1274};
1275
1276#ifndef _LIBCPP_HAS_NO_VARIADICS
1277
1278template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1279struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1280{
1281 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1282};
1283
1284template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1285struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1286{
1287 typedef _Alloc<_Up, _Args...> type;
1288};
1289
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001290#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
1292template <template <class> class _Alloc, class _Tp, class _Up>
1293struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1294{
1295 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1296};
1297
1298template <template <class> class _Alloc, class _Tp, class _Up>
1299struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1300{
1301 typedef _Alloc<_Up> type;
1302};
1303
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1305struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1306{
1307 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1308};
1309
1310template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1312{
1313 typedef _Alloc<_Up, _A0> type;
1314};
1315
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1319{
1320 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1321};
1322
1323template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1326{
1327 typedef _Alloc<_Up, _A0, _A1> type;
1328};
1329
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1333{
1334 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1335};
1336
1337template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1338 class _A1, class _A2, class _Up>
1339struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1340{
1341 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1342};
1343
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001344#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001346#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347
1348template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1349auto
1350__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001351 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352
1353template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1354auto
1355__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1356 -> false_type;
1357
1358template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1359struct __has_allocate_hint
1360 : integral_constant<bool,
1361 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001362 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 declval<_SizeType>(),
1364 declval<_ConstVoidPtr>())),
1365 true_type>::value>
1366{
1367};
1368
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001369#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370
1371template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1372struct __has_allocate_hint
1373 : true_type
1374{
1375};
1376
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001377#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001379#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
1381template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001382decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1383 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 true_type())
1385__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1386
1387template <class _Alloc, class _Pointer, class ..._Args>
1388false_type
1389__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1390
1391template <class _Alloc, class _Pointer, class ..._Args>
1392struct __has_construct
1393 : integral_constant<bool,
1394 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001395 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 declval<_Pointer>(),
1397 declval<_Args>()...)),
1398 true_type>::value>
1399{
1400};
1401
1402template <class _Alloc, class _Pointer>
1403auto
1404__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1405 -> decltype(__a.destroy(__p), true_type());
1406
1407template <class _Alloc, class _Pointer>
1408auto
1409__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1410 -> false_type;
1411
1412template <class _Alloc, class _Pointer>
1413struct __has_destroy
1414 : integral_constant<bool,
1415 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001416 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 declval<_Pointer>())),
1418 true_type>::value>
1419{
1420};
1421
1422template <class _Alloc>
1423auto
1424__has_max_size_test(_Alloc&& __a)
1425 -> decltype(__a.max_size(), true_type());
1426
1427template <class _Alloc>
1428auto
1429__has_max_size_test(const volatile _Alloc& __a)
1430 -> false_type;
1431
1432template <class _Alloc>
1433struct __has_max_size
1434 : integral_constant<bool,
1435 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001436 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 true_type>::value>
1438{
1439};
1440
1441template <class _Alloc>
1442auto
1443__has_select_on_container_copy_construction_test(_Alloc&& __a)
1444 -> decltype(__a.select_on_container_copy_construction(), true_type());
1445
1446template <class _Alloc>
1447auto
1448__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1449 -> false_type;
1450
1451template <class _Alloc>
1452struct __has_select_on_container_copy_construction
1453 : integral_constant<bool,
1454 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001455 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 true_type>::value>
1457{
1458};
1459
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001460#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001462template <class _Alloc, class _Pointer, class _Tp, class = void>
1463struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001465template <class _Alloc, class _Pointer, class _Tp>
1466struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1467 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1468>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001470template <class _Alloc, class _Pointer, class = void>
1471struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001474struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1475 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1476>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
1478template <class _Alloc>
1479struct __has_max_size
1480 : true_type
1481{
1482};
1483
1484template <class _Alloc>
1485struct __has_select_on_container_copy_construction
1486 : false_type
1487{
1488};
1489
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001490#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001492template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1493struct __alloc_traits_difference_type
1494{
1495 typedef typename pointer_traits<_Ptr>::difference_type type;
1496};
1497
1498template <class _Alloc, class _Ptr>
1499struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1500{
1501 typedef typename _Alloc::difference_type type;
1502};
1503
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001504template <class _Tp>
1505struct __is_default_allocator : false_type {};
1506
1507template <class _Tp>
1508struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1509
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001511struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512{
1513 typedef _Alloc allocator_type;
1514 typedef typename allocator_type::value_type value_type;
1515
1516 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1517 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1518 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1519 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1520
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001521 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1522 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
1524 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1525 propagate_on_container_copy_assignment;
1526 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1527 propagate_on_container_move_assignment;
1528 typedef typename __propagate_on_container_swap<allocator_type>::type
1529 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001530 typedef typename __is_always_equal<allocator_type>::type
1531 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001533#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001535 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 template <class _Tp> struct rebind_alloc
1539 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1540 template <class _Tp> struct rebind_traits
1541 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001542#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543
Marshall Clow0e58cae2017-11-26 02:55:38 +00001544 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 static pointer allocate(allocator_type& __a, size_type __n)
1546 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001547 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001549 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1551
Howard Hinnant756c69b2010-09-22 16:48:34 +00001552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001553 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 {__a.deallocate(__p, __n);}
1555
1556#ifndef _LIBCPP_HAS_NO_VARIADICS
1557 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001560 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001561 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001562#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001564 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001565 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 {
1567 ::new ((void*)__p) _Tp();
1568 }
1569 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001570 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001571 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001573 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1574 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 }
1576 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001577 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001578 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 const _A1& __a1)
1580 {
1581 ::new ((void*)__p) _Tp(__a0, __a1);
1582 }
1583 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001584 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001585 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 const _A1& __a1, const _A2& __a2)
1587 {
1588 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1589 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001590#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591
1592 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 static void destroy(allocator_type& __a, _Tp* __p)
1595 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1596
Howard Hinnant756c69b2010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001598 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1600
Howard Hinnant756c69b2010-09-22 16:48:34 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 static allocator_type
1603 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001604 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 __has_select_on_container_copy_construction<const allocator_type>(),
1606 __a);}
1607
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001608 template <class _Ptr>
1609 _LIBCPP_INLINE_VISIBILITY
1610 static
1611 void
1612 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1613 {
Louis Dionne301a6772018-12-07 16:42:28 +00001614 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1616 }
1617
1618 template <class _Tp>
1619 _LIBCPP_INLINE_VISIBILITY
1620 static
1621 typename enable_if
1622 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001623 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001624 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1625 is_trivially_move_constructible<_Tp>::value,
1626 void
1627 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001628 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001629 {
1630 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001631 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001632 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001633 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001634 __begin2 += _Np;
1635 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001636 }
1637
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001638 template <class _Iter, class _Ptr>
1639 _LIBCPP_INLINE_VISIBILITY
1640 static
1641 void
1642 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1643 {
1644 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1645 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1646 }
1647
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001648 template <class _SourceTp, class _DestTp,
1649 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1650 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001651 _LIBCPP_INLINE_VISIBILITY
1652 static
1653 typename enable_if
1654 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001655 is_trivially_move_constructible<_DestTp>::value &&
1656 is_same<_RawSourceTp, _RawDestTp>::value &&
1657 (__is_default_allocator<allocator_type>::value ||
1658 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001659 void
1660 >::type
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001661 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001662 {
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001663 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001664 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001665 {
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001666 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001667 __begin2 += _Np;
1668 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001669 }
1670
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001671 template <class _Ptr>
1672 _LIBCPP_INLINE_VISIBILITY
1673 static
1674 void
1675 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1676 {
1677 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001678 {
1679 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1680 --__end2;
1681 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001682 }
1683
1684 template <class _Tp>
1685 _LIBCPP_INLINE_VISIBILITY
1686 static
1687 typename enable_if
1688 <
Volodymyr Sapsai5c163022019-01-08 00:03:16 +00001689 (__is_default_allocator<allocator_type>::value
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001690 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1691 is_trivially_move_constructible<_Tp>::value,
1692 void
1693 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001694 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001695 {
1696 ptrdiff_t _Np = __end1 - __begin1;
1697 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001698 if (_Np > 0)
1699 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001700 }
1701
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702private:
1703
Howard Hinnant756c69b2010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001705 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 const_void_pointer __hint, true_type)
1707 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001709 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001710 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 {return __a.allocate(__n);}
1712
1713#ifndef _LIBCPP_HAS_NO_VARIADICS
1714 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001717 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1721 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001722 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001724#else // _LIBCPP_HAS_NO_VARIADICS
1725 template <class _Tp, class _A0>
1726 _LIBCPP_INLINE_VISIBILITY
1727 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1728 const _A0& __a0)
1729 {__a.construct(__p, __a0);}
1730 template <class _Tp, class _A0>
1731 _LIBCPP_INLINE_VISIBILITY
1732 static void __construct(false_type, allocator_type&, _Tp* __p,
1733 const _A0& __a0)
1734 {
1735 ::new ((void*)__p) _Tp(__a0);
1736 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001737#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738
1739 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1742 {__a.destroy(__p);}
1743 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 static void __destroy(false_type, allocator_type&, _Tp* __p)
1746 {
1747 __p->~_Tp();
1748 }
1749
Howard Hinnant756c69b2010-09-22 16:48:34 +00001750 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001751 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001753 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001754 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001755 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756
Howard Hinnant756c69b2010-09-22 16:48:34 +00001757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001759 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001763 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764 {return __a;}
1765};
1766
Marshall Clow940e01c2015-04-07 05:21:38 +00001767template <class _Traits, class _Tp>
1768struct __rebind_alloc_helper
1769{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001770#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001771 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001772#else
1773 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1774#endif
1775};
1776
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777// allocator
1778
1779template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001780class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781{
1782public:
1783 typedef size_t size_type;
1784 typedef ptrdiff_t difference_type;
1785 typedef _Tp* pointer;
1786 typedef const _Tp* const_pointer;
1787 typedef _Tp& reference;
1788 typedef const _Tp& const_reference;
1789 typedef _Tp value_type;
1790
Howard Hinnant4931e092011-06-02 21:38:57 +00001791 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001792 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001793
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1795
Marshall Clowbc759762018-03-20 23:02:53 +00001796 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1797 allocator() _NOEXCEPT {}
1798
Louis Dionne481a2662018-09-23 18:35:00 +00001799 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001800 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1801 allocator(const allocator<_Up>&) _NOEXCEPT {}
1802
Howard Hinnant719bda32011-05-28 14:41:13 +00001803 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001804 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001805 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001806 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001807 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001808 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001809 {
1810 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001811 __throw_length_error("allocator<T>::allocate(size_t n)"
1812 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001813 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001814 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001815 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001816 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001817 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1818 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001819#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 template <class _Up, class... _Args>
1821 _LIBCPP_INLINE_VISIBILITY
1822 void
1823 construct(_Up* __p, _Args&&... __args)
1824 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001825 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001827#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 _LIBCPP_INLINE_VISIBILITY
1829 void
1830 construct(pointer __p)
1831 {
1832 ::new((void*)__p) _Tp();
1833 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001834# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001835
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 template <class _A0>
1837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001838 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 construct(pointer __p, _A0& __a0)
1840 {
1841 ::new((void*)__p) _Tp(__a0);
1842 }
1843 template <class _A0>
1844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001845 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 construct(pointer __p, const _A0& __a0)
1847 {
1848 ::new((void*)__p) _Tp(__a0);
1849 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001850# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 template <class _A0, class _A1>
1852 _LIBCPP_INLINE_VISIBILITY
1853 void
1854 construct(pointer __p, _A0& __a0, _A1& __a1)
1855 {
1856 ::new((void*)__p) _Tp(__a0, __a1);
1857 }
1858 template <class _A0, class _A1>
1859 _LIBCPP_INLINE_VISIBILITY
1860 void
1861 construct(pointer __p, const _A0& __a0, _A1& __a1)
1862 {
1863 ::new((void*)__p) _Tp(__a0, __a1);
1864 }
1865 template <class _A0, class _A1>
1866 _LIBCPP_INLINE_VISIBILITY
1867 void
1868 construct(pointer __p, _A0& __a0, const _A1& __a1)
1869 {
1870 ::new((void*)__p) _Tp(__a0, __a1);
1871 }
1872 template <class _A0, class _A1>
1873 _LIBCPP_INLINE_VISIBILITY
1874 void
1875 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1876 {
1877 ::new((void*)__p) _Tp(__a0, __a1);
1878 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001879#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1881};
1882
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001883template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001884class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001885{
1886public:
1887 typedef size_t size_type;
1888 typedef ptrdiff_t difference_type;
1889 typedef const _Tp* pointer;
1890 typedef const _Tp* const_pointer;
1891 typedef const _Tp& reference;
1892 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001893 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001894
1895 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001896 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001897
1898 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1899
Marshall Clowbc759762018-03-20 23:02:53 +00001900 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1901 allocator() _NOEXCEPT {}
1902
1903 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001904 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001905 allocator(const allocator<_Up>&) _NOEXCEPT {}
1906
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001907 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1908 {return _VSTD::addressof(__x);}
1909 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001910 {
1911 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001912 __throw_length_error("allocator<const T>::allocate(size_t n)"
1913 " 'n' exceeds maximum supported size");
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001914 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001915 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001916 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001917 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001918 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1919 {return size_type(~0) / sizeof(_Tp);}
1920#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1921 template <class _Up, class... _Args>
1922 _LIBCPP_INLINE_VISIBILITY
1923 void
1924 construct(_Up* __p, _Args&&... __args)
1925 {
1926 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1927 }
1928#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1929 _LIBCPP_INLINE_VISIBILITY
1930 void
1931 construct(pointer __p)
1932 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001933 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001934 }
1935# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001936
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001937 template <class _A0>
1938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001939 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001940 construct(pointer __p, _A0& __a0)
1941 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001942 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001943 }
1944 template <class _A0>
1945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001946 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001947 construct(pointer __p, const _A0& __a0)
1948 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001949 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001950 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001951# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1952 template <class _A0, class _A1>
1953 _LIBCPP_INLINE_VISIBILITY
1954 void
1955 construct(pointer __p, _A0& __a0, _A1& __a1)
1956 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001957 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001958 }
1959 template <class _A0, class _A1>
1960 _LIBCPP_INLINE_VISIBILITY
1961 void
1962 construct(pointer __p, const _A0& __a0, _A1& __a1)
1963 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001964 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001965 }
1966 template <class _A0, class _A1>
1967 _LIBCPP_INLINE_VISIBILITY
1968 void
1969 construct(pointer __p, _A0& __a0, const _A1& __a1)
1970 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001971 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001972 }
1973 template <class _A0, class _A1>
1974 _LIBCPP_INLINE_VISIBILITY
1975 void
1976 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1977 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001978 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001979 }
1980#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1981 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1982};
1983
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984template <class _Tp, class _Up>
1985inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001986bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987
1988template <class _Tp, class _Up>
1989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001990bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991
1992template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001993class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994 : public iterator<output_iterator_tag,
1995 _Tp, // purposefully not C++03
1996 ptrdiff_t, // purposefully not C++03
1997 _Tp*, // purposefully not C++03
1998 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1999{
2000private:
2001 _OutputIterator __x_;
2002public:
2003 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2004 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2005 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002006 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002007#if _LIBCPP_STD_VER >= 14
2008 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002009 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002010#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2012 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2013 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002014#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002015 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002016#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017};
2018
2019template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002020_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002022get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024 pair<_Tp*, ptrdiff_t> __r(0, 0);
2025 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2026 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2027 / sizeof(_Tp);
2028 if (__n > __m)
2029 __n = __m;
2030 while (__n > 0)
2031 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002032#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002033 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002034 {
2035 std::align_val_t __al =
2036 std::align_val_t(std::alignment_of<_Tp>::value);
2037 __r.first = static_cast<_Tp*>(::operator new(
2038 __n * sizeof(_Tp), __al, nothrow));
2039 } else {
2040 __r.first = static_cast<_Tp*>(::operator new(
2041 __n * sizeof(_Tp), nothrow));
2042 }
2043#else
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002044 if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002045 {
2046 // Since aligned operator new is unavailable, return an empty
2047 // buffer rather than one with invalid alignment.
2048 return __r;
2049 }
2050
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002052#endif
2053
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054 if (__r.first)
2055 {
2056 __r.second = __n;
2057 break;
2058 }
2059 __n /= 2;
2060 }
2061 return __r;
2062}
2063
2064template <class _Tp>
2065inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002066void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2067{
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00002068 _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002069}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002073struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002074{
2075 _Tp* __ptr_;
2076};
2077
2078template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002079class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080{
2081private:
2082 _Tp* __ptr_;
2083public:
2084 typedef _Tp element_type;
2085
2086 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2087 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2088 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2089 : __ptr_(__p.release()) {}
2090 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2091 {reset(__p.release()); return *this;}
2092 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2093 {reset(__p.release()); return *this;}
2094 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2095 {reset(__p.__ptr_); return *this;}
2096 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2097
2098 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2099 {return *__ptr_;}
2100 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2101 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2102 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2103 {
2104 _Tp* __t = __ptr_;
2105 __ptr_ = 0;
2106 return __t;
2107 }
2108 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2109 {
2110 if (__ptr_ != __p)
2111 delete __ptr_;
2112 __ptr_ = __p;
2113 }
2114
2115 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2116 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2117 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2118 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2119 {return auto_ptr<_Up>(release());}
2120};
2121
2122template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002123class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124{
2125public:
2126 typedef void element_type;
2127};
Marshall Clowb22274f2017-01-24 22:22:33 +00002128#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
Eric Fiselier9d355982017-04-12 23:45:53 +00002130template <class _Tp, int _Idx,
2131 bool _CanBeEmptyBase =
2132 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2133struct __compressed_pair_elem {
2134 typedef _Tp _ParamT;
2135 typedef _Tp& reference;
2136 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137
Eric Fiselier9d355982017-04-12 23:45:53 +00002138#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002139 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140
Eric Fiselier9d355982017-04-12 23:45:53 +00002141 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002142 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2143 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002144 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002145 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002146 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002147 : __value_(_VSTD::forward<_Up>(__u))
2148 {
2149 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150
Eric Fiselier9d355982017-04-12 23:45:53 +00002151 template <class... _Args, size_t... _Indexes>
2152 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2153 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2154 __tuple_indices<_Indexes...>)
2155 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2156#else
Alex Lorenz76132112017-11-09 17:54:49 +00002157 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2158 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002159 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2160#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161
Alex Lorenz76132112017-11-09 17:54:49 +00002162 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2163 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002164 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002167 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168};
2169
Eric Fiselier9d355982017-04-12 23:45:53 +00002170template <class _Tp, int _Idx>
2171struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2172 typedef _Tp _ParamT;
2173 typedef _Tp& reference;
2174 typedef const _Tp& const_reference;
2175 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176
Eric Fiselier9d355982017-04-12 23:45:53 +00002177#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002178 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179
Eric Fiselier9d355982017-04-12 23:45:53 +00002180 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002181 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2182 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002183 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002184 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002185 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002186 : __value_type(_VSTD::forward<_Up>(__u))
2187 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188
Eric Fiselier9d355982017-04-12 23:45:53 +00002189 template <class... _Args, size_t... _Indexes>
2190 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2191 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2192 __tuple_indices<_Indexes...>)
2193 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2194#else
Alex Lorenz76132112017-11-09 17:54:49 +00002195 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2196 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002197 __compressed_pair_elem(_ParamT __p)
2198 : __value_type(std::forward<_ParamT>(__p)) {}
2199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200
Alex Lorenz76132112017-11-09 17:54:49 +00002201 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2202 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002203 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204};
2205
Eric Fiselier9d355982017-04-12 23:45:53 +00002206// Tag used to construct the second element of the compressed pair.
2207struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208
2209template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002210class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2211 private __compressed_pair_elem<_T2, 1> {
2212 typedef __compressed_pair_elem<_T1, 0> _Base1;
2213 typedef __compressed_pair_elem<_T2, 1> _Base2;
2214
2215 // NOTE: This static assert should never fire because __compressed_pair
2216 // is *almost never* used in a scenario where it's possible for T1 == T2.
2217 // (The exception is std::function where it is possible that the function
2218 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002219 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002220 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2221 "The current implementation is NOT ABI-compatible with the previous "
2222 "implementation for this configuration");
2223
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002225#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002226 template <bool _Dummy = true,
2227 class = typename enable_if<
2228 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2229 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2230 >::type
2231 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002232 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002233 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234
Eric Fiselier9d355982017-04-12 23:45:53 +00002235 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2236 __compressed_pair>::value,
2237 bool>::type = true>
2238 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2239 __compressed_pair(_Tp&& __t)
2240 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241
Eric Fiselier9d355982017-04-12 23:45:53 +00002242 template <class _Tp>
2243 _LIBCPP_INLINE_VISIBILITY constexpr
2244 __compressed_pair(__second_tag, _Tp&& __t)
2245 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246
Eric Fiselier9d355982017-04-12 23:45:53 +00002247 template <class _U1, class _U2>
2248 _LIBCPP_INLINE_VISIBILITY constexpr
2249 __compressed_pair(_U1&& __t1, _U2&& __t2)
2250 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251
Eric Fiselier9d355982017-04-12 23:45:53 +00002252 template <class... _Args1, class... _Args2>
2253 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2254 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2255 tuple<_Args2...> __second_args)
2256 : _Base1(__pc, _VSTD::move(__first_args),
2257 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2258 _Base2(__pc, _VSTD::move(__second_args),
2259 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002260
Eric Fiselier9d355982017-04-12 23:45:53 +00002261#else
2262 _LIBCPP_INLINE_VISIBILITY
2263 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002264
Eric Fiselier9d355982017-04-12 23:45:53 +00002265 _LIBCPP_INLINE_VISIBILITY explicit
2266 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002267
Eric Fiselier9d355982017-04-12 23:45:53 +00002268 _LIBCPP_INLINE_VISIBILITY
2269 __compressed_pair(__second_tag, _T2 __t2)
2270 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Eric Fiselier9d355982017-04-12 23:45:53 +00002272 _LIBCPP_INLINE_VISIBILITY
2273 __compressed_pair(_T1 __t1, _T2 __t2)
2274 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276
Eric Fiselier9d355982017-04-12 23:45:53 +00002277 _LIBCPP_INLINE_VISIBILITY
2278 typename _Base1::reference first() _NOEXCEPT {
2279 return static_cast<_Base1&>(*this).__get();
2280 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
Eric Fiselier9d355982017-04-12 23:45:53 +00002282 _LIBCPP_INLINE_VISIBILITY
2283 typename _Base1::const_reference first() const _NOEXCEPT {
2284 return static_cast<_Base1 const&>(*this).__get();
2285 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286
Eric Fiselier9d355982017-04-12 23:45:53 +00002287 _LIBCPP_INLINE_VISIBILITY
2288 typename _Base2::reference second() _NOEXCEPT {
2289 return static_cast<_Base2&>(*this).__get();
2290 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291
Eric Fiselier9d355982017-04-12 23:45:53 +00002292 _LIBCPP_INLINE_VISIBILITY
2293 typename _Base2::const_reference second() const _NOEXCEPT {
2294 return static_cast<_Base2 const&>(*this).__get();
2295 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
Eric Fiselier9d355982017-04-12 23:45:53 +00002297 _LIBCPP_INLINE_VISIBILITY
2298 void swap(__compressed_pair& __x)
2299 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2300 __is_nothrow_swappable<_T2>::value)
2301 {
2302 using std::swap;
2303 swap(first(), __x.first());
2304 swap(second(), __x.second());
2305 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306};
2307
2308template <class _T1, class _T2>
2309inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002310void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2311 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2312 __is_nothrow_swappable<_T2>::value) {
2313 __x.swap(__y);
2314}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002316// default_delete
2317
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002319struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002320 static_assert(!is_function<_Tp>::value,
2321 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002322#ifndef _LIBCPP_CXX03_LANG
2323 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002324#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002325 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002326#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002327 template <class _Up>
2328 _LIBCPP_INLINE_VISIBILITY
2329 default_delete(const default_delete<_Up>&,
2330 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2331 0) _NOEXCEPT {}
2332
2333 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2334 static_assert(sizeof(_Tp) > 0,
2335 "default_delete can not delete incomplete type");
2336 static_assert(!is_void<_Tp>::value,
2337 "default_delete can not delete incomplete type");
2338 delete __ptr;
2339 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340};
2341
2342template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002343struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2344private:
2345 template <class _Up>
2346 struct _EnableIfConvertible
2347 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2348
Howard Hinnant4500ca52011-12-18 21:19:44 +00002349public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002350#ifndef _LIBCPP_CXX03_LANG
2351 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002352#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002353 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002354#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002355
2356 template <class _Up>
2357 _LIBCPP_INLINE_VISIBILITY
2358 default_delete(const default_delete<_Up[]>&,
2359 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2360
2361 template <class _Up>
2362 _LIBCPP_INLINE_VISIBILITY
2363 typename _EnableIfConvertible<_Up>::type
2364 operator()(_Up* __ptr) const _NOEXCEPT {
2365 static_assert(sizeof(_Tp) > 0,
2366 "default_delete can not delete incomplete type");
2367 static_assert(!is_void<_Tp>::value,
2368 "default_delete can not delete void type");
2369 delete[] __ptr;
2370 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371};
2372
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373
Eric Fiselier6779b062017-04-16 02:06:25 +00002374
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002375#ifndef _LIBCPP_CXX03_LANG
2376template <class _Deleter>
2377struct __unique_ptr_deleter_sfinae {
2378 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2379 typedef const _Deleter& __lval_ref_type;
2380 typedef _Deleter&& __good_rval_ref_type;
2381 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382};
2383
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002384template <class _Deleter>
2385struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2386 typedef const _Deleter& __lval_ref_type;
2387 typedef const _Deleter&& __bad_rval_ref_type;
2388 typedef false_type __enable_rval_overload;
2389};
2390
2391template <class _Deleter>
2392struct __unique_ptr_deleter_sfinae<_Deleter&> {
2393 typedef _Deleter& __lval_ref_type;
2394 typedef _Deleter&& __bad_rval_ref_type;
2395 typedef false_type __enable_rval_overload;
2396};
2397#endif // !defined(_LIBCPP_CXX03_LANG)
2398
2399template <class _Tp, class _Dp = default_delete<_Tp> >
2400class _LIBCPP_TEMPLATE_VIS unique_ptr {
2401public:
2402 typedef _Tp element_type;
2403 typedef _Dp deleter_type;
2404 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2405
2406 static_assert(!is_rvalue_reference<deleter_type>::value,
2407 "the specified deleter type cannot be an rvalue reference");
2408
2409private:
2410 __compressed_pair<pointer, deleter_type> __ptr_;
2411
2412 struct __nat { int __for_bool_; };
2413
2414#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002415 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002416
2417 template <bool _Dummy>
2418 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002419 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002420
2421 template <bool _Dummy>
2422 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002423 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002424
2425 template <bool _Dummy>
2426 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002427 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002428
2429 template <bool _Dummy, class _Deleter = typename __dependent_type<
2430 __identity<deleter_type>, _Dummy>::type>
2431 using _EnableIfDeleterDefaultConstructible =
2432 typename enable_if<is_default_constructible<_Deleter>::value &&
2433 !is_pointer<_Deleter>::value>::type;
2434
2435 template <class _ArgType>
2436 using _EnableIfDeleterConstructible =
2437 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2438
2439 template <class _UPtr, class _Up>
2440 using _EnableIfMoveConvertible = typename enable_if<
2441 is_convertible<typename _UPtr::pointer, pointer>::value &&
2442 !is_array<_Up>::value
2443 >::type;
2444
2445 template <class _UDel>
2446 using _EnableIfDeleterConvertible = typename enable_if<
2447 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2448 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2449 >::type;
2450
2451 template <class _UDel>
2452 using _EnableIfDeleterAssignable = typename enable_if<
2453 is_assignable<_Dp&, _UDel&&>::value
2454 >::type;
2455
2456public:
2457 template <bool _Dummy = true,
2458 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2459 _LIBCPP_INLINE_VISIBILITY
2460 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2461
2462 template <bool _Dummy = true,
2463 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2464 _LIBCPP_INLINE_VISIBILITY
2465 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2466
2467 template <bool _Dummy = true,
2468 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2469 _LIBCPP_INLINE_VISIBILITY
2470 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2471
2472 template <bool _Dummy = true,
2473 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2474 _LIBCPP_INLINE_VISIBILITY
2475 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2476 : __ptr_(__p, __d) {}
2477
2478 template <bool _Dummy = true,
2479 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2480 _LIBCPP_INLINE_VISIBILITY
2481 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2482 : __ptr_(__p, _VSTD::move(__d)) {
2483 static_assert(!is_reference<deleter_type>::value,
2484 "rvalue deleter bound to reference");
2485 }
2486
2487 template <bool _Dummy = true,
2488 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2489 _LIBCPP_INLINE_VISIBILITY
2490 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2491
2492 _LIBCPP_INLINE_VISIBILITY
2493 unique_ptr(unique_ptr&& __u) noexcept
2494 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2495 }
2496
2497 template <class _Up, class _Ep,
2498 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2499 class = _EnableIfDeleterConvertible<_Ep>
2500 >
2501 _LIBCPP_INLINE_VISIBILITY
2502 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2503 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2504
2505#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2506 template <class _Up>
2507 _LIBCPP_INLINE_VISIBILITY
2508 unique_ptr(auto_ptr<_Up>&& __p,
2509 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2510 is_same<_Dp, default_delete<_Tp>>::value,
2511 __nat>::type = __nat()) _NOEXCEPT
2512 : __ptr_(__p.release()) {}
2513#endif
2514
2515 _LIBCPP_INLINE_VISIBILITY
2516 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2517 reset(__u.release());
2518 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2519 return *this;
2520 }
2521
2522 template <class _Up, class _Ep,
2523 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2524 class = _EnableIfDeleterAssignable<_Ep>
2525 >
2526 _LIBCPP_INLINE_VISIBILITY
2527 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2528 reset(__u.release());
2529 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2530 return *this;
2531 }
2532
2533#else // _LIBCPP_CXX03_LANG
2534private:
2535 unique_ptr(unique_ptr&);
2536 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2537
2538 unique_ptr& operator=(unique_ptr&);
2539 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2540
2541public:
2542 _LIBCPP_INLINE_VISIBILITY
2543 unique_ptr() : __ptr_(pointer())
2544 {
2545 static_assert(!is_pointer<deleter_type>::value,
2546 "unique_ptr constructed with null function pointer deleter");
2547 static_assert(is_default_constructible<deleter_type>::value,
2548 "unique_ptr::deleter_type is not default constructible");
2549 }
2550 _LIBCPP_INLINE_VISIBILITY
2551 unique_ptr(nullptr_t) : __ptr_(pointer())
2552 {
2553 static_assert(!is_pointer<deleter_type>::value,
2554 "unique_ptr constructed with null function pointer deleter");
2555 }
2556 _LIBCPP_INLINE_VISIBILITY
2557 explicit unique_ptr(pointer __p)
2558 : __ptr_(_VSTD::move(__p)) {
2559 static_assert(!is_pointer<deleter_type>::value,
2560 "unique_ptr constructed with null function pointer deleter");
2561 }
2562
2563 _LIBCPP_INLINE_VISIBILITY
2564 operator __rv<unique_ptr>() {
2565 return __rv<unique_ptr>(*this);
2566 }
2567
2568 _LIBCPP_INLINE_VISIBILITY
2569 unique_ptr(__rv<unique_ptr> __u)
2570 : __ptr_(__u->release(),
2571 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2572
2573 template <class _Up, class _Ep>
2574 _LIBCPP_INLINE_VISIBILITY
2575 typename enable_if<
2576 !is_array<_Up>::value &&
2577 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2578 pointer>::value &&
2579 is_assignable<deleter_type&, _Ep&>::value,
2580 unique_ptr&>::type
2581 operator=(unique_ptr<_Up, _Ep> __u) {
2582 reset(__u.release());
2583 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2584 return *this;
2585 }
2586
2587 _LIBCPP_INLINE_VISIBILITY
2588 unique_ptr(pointer __p, deleter_type __d)
2589 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2590#endif // _LIBCPP_CXX03_LANG
2591
2592#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2593 template <class _Up>
2594 _LIBCPP_INLINE_VISIBILITY
2595 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2596 is_same<_Dp, default_delete<_Tp> >::value,
2597 unique_ptr&>::type
2598 operator=(auto_ptr<_Up> __p) {
2599 reset(__p.release());
2600 return *this;
2601 }
2602#endif
2603
2604 _LIBCPP_INLINE_VISIBILITY
2605 ~unique_ptr() { reset(); }
2606
2607 _LIBCPP_INLINE_VISIBILITY
2608 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2609 reset();
2610 return *this;
2611 }
2612
2613 _LIBCPP_INLINE_VISIBILITY
2614 typename add_lvalue_reference<_Tp>::type
2615 operator*() const {
2616 return *__ptr_.first();
2617 }
2618 _LIBCPP_INLINE_VISIBILITY
2619 pointer operator->() const _NOEXCEPT {
2620 return __ptr_.first();
2621 }
2622 _LIBCPP_INLINE_VISIBILITY
2623 pointer get() const _NOEXCEPT {
2624 return __ptr_.first();
2625 }
2626 _LIBCPP_INLINE_VISIBILITY
2627 deleter_type& get_deleter() _NOEXCEPT {
2628 return __ptr_.second();
2629 }
2630 _LIBCPP_INLINE_VISIBILITY
2631 const deleter_type& get_deleter() const _NOEXCEPT {
2632 return __ptr_.second();
2633 }
2634 _LIBCPP_INLINE_VISIBILITY
2635 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2636 return __ptr_.first() != nullptr;
2637 }
2638
2639 _LIBCPP_INLINE_VISIBILITY
2640 pointer release() _NOEXCEPT {
2641 pointer __t = __ptr_.first();
2642 __ptr_.first() = pointer();
2643 return __t;
2644 }
2645
2646 _LIBCPP_INLINE_VISIBILITY
2647 void reset(pointer __p = pointer()) _NOEXCEPT {
2648 pointer __tmp = __ptr_.first();
2649 __ptr_.first() = __p;
2650 if (__tmp)
2651 __ptr_.second()(__tmp);
2652 }
2653
2654 _LIBCPP_INLINE_VISIBILITY
2655 void swap(unique_ptr& __u) _NOEXCEPT {
2656 __ptr_.swap(__u.__ptr_);
2657 }
2658};
2659
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002660
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002662class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002664 typedef _Tp element_type;
2665 typedef _Dp deleter_type;
2666 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2667
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002669 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670
Eric Fiselier31127cd2017-04-16 02:14:31 +00002671 template <class _From>
2672 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673
Eric Fiselier31127cd2017-04-16 02:14:31 +00002674 template <class _FromElem>
2675 struct _CheckArrayPointerConversion<_FromElem*>
2676 : integral_constant<bool,
2677 is_same<_FromElem*, pointer>::value ||
2678 (is_same<pointer, element_type*>::value &&
2679 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2680 >
2681 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002683#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002684 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002685
2686 template <bool _Dummy>
2687 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002688 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002689
2690 template <bool _Dummy>
2691 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002692 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002693
2694 template <bool _Dummy>
2695 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002696 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002697
2698 template <bool _Dummy, class _Deleter = typename __dependent_type<
2699 __identity<deleter_type>, _Dummy>::type>
2700 using _EnableIfDeleterDefaultConstructible =
2701 typename enable_if<is_default_constructible<_Deleter>::value &&
2702 !is_pointer<_Deleter>::value>::type;
2703
2704 template <class _ArgType>
2705 using _EnableIfDeleterConstructible =
2706 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2707
2708 template <class _Pp>
2709 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002710 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002711 >::type;
2712
2713 template <class _UPtr, class _Up,
2714 class _ElemT = typename _UPtr::element_type>
2715 using _EnableIfMoveConvertible = typename enable_if<
2716 is_array<_Up>::value &&
2717 is_same<pointer, element_type*>::value &&
2718 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2719 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2720 >::type;
2721
2722 template <class _UDel>
2723 using _EnableIfDeleterConvertible = typename enable_if<
2724 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2725 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2726 >::type;
2727
2728 template <class _UDel>
2729 using _EnableIfDeleterAssignable = typename enable_if<
2730 is_assignable<_Dp&, _UDel&&>::value
2731 >::type;
2732
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002734 template <bool _Dummy = true,
2735 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2736 _LIBCPP_INLINE_VISIBILITY
2737 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002739 template <bool _Dummy = true,
2740 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2741 _LIBCPP_INLINE_VISIBILITY
2742 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 template <class _Pp, bool _Dummy = true,
2745 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2746 class = _EnableIfPointerConvertible<_Pp>>
2747 _LIBCPP_INLINE_VISIBILITY
2748 explicit unique_ptr(_Pp __p) noexcept
2749 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002751 template <class _Pp, bool _Dummy = true,
2752 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2753 class = _EnableIfPointerConvertible<_Pp>>
2754 _LIBCPP_INLINE_VISIBILITY
2755 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2756 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 template <bool _Dummy = true,
2759 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2760 _LIBCPP_INLINE_VISIBILITY
2761 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2762 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002764 template <class _Pp, bool _Dummy = true,
2765 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2766 class = _EnableIfPointerConvertible<_Pp>>
2767 _LIBCPP_INLINE_VISIBILITY
2768 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2769 : __ptr_(__p, _VSTD::move(__d)) {
2770 static_assert(!is_reference<deleter_type>::value,
2771 "rvalue deleter bound to reference");
2772 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002774 template <bool _Dummy = true,
2775 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2776 _LIBCPP_INLINE_VISIBILITY
2777 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2778 : __ptr_(nullptr, _VSTD::move(__d)) {
2779 static_assert(!is_reference<deleter_type>::value,
2780 "rvalue deleter bound to reference");
2781 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002782
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002783 template <class _Pp, bool _Dummy = true,
2784 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2785 class = _EnableIfPointerConvertible<_Pp>>
2786 _LIBCPP_INLINE_VISIBILITY
2787 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002788
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002789 _LIBCPP_INLINE_VISIBILITY
2790 unique_ptr(unique_ptr&& __u) noexcept
2791 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2792 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002793
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002794 _LIBCPP_INLINE_VISIBILITY
2795 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2796 reset(__u.release());
2797 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2798 return *this;
2799 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002801 template <class _Up, class _Ep,
2802 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2803 class = _EnableIfDeleterConvertible<_Ep>
2804 >
2805 _LIBCPP_INLINE_VISIBILITY
2806 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002807 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002808 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002810 template <class _Up, class _Ep,
2811 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2812 class = _EnableIfDeleterAssignable<_Ep>
2813 >
2814 _LIBCPP_INLINE_VISIBILITY
2815 unique_ptr&
2816 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2817 reset(__u.release());
2818 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2819 return *this;
2820 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002822#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002824 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002825
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002826 unique_ptr(unique_ptr&);
2827 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2828
2829 unique_ptr& operator=(unique_ptr&);
2830 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2831
2832 template <class _Up>
2833 unique_ptr(_Up __u,
2834 typename conditional<
2835 is_reference<deleter_type>::value, deleter_type,
2836 typename add_lvalue_reference<const deleter_type>::type>::type,
2837 typename enable_if<is_convertible<_Up, pointer>::value,
2838 __nat>::type = __nat());
2839public:
2840 _LIBCPP_INLINE_VISIBILITY
2841 unique_ptr() : __ptr_(pointer()) {
2842 static_assert(!is_pointer<deleter_type>::value,
2843 "unique_ptr constructed with null function pointer deleter");
2844 }
2845 _LIBCPP_INLINE_VISIBILITY
2846 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2847 static_assert(!is_pointer<deleter_type>::value,
2848 "unique_ptr constructed with null function pointer deleter");
2849 }
2850
2851 _LIBCPP_INLINE_VISIBILITY
2852 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2853 static_assert(!is_pointer<deleter_type>::value,
2854 "unique_ptr constructed with null function pointer deleter");
2855 }
2856
2857 _LIBCPP_INLINE_VISIBILITY
2858 unique_ptr(pointer __p, deleter_type __d)
2859 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2860
2861 _LIBCPP_INLINE_VISIBILITY
2862 unique_ptr(nullptr_t, deleter_type __d)
2863 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2864
2865 _LIBCPP_INLINE_VISIBILITY
2866 operator __rv<unique_ptr>() {
2867 return __rv<unique_ptr>(*this);
2868 }
2869
2870 _LIBCPP_INLINE_VISIBILITY
2871 unique_ptr(__rv<unique_ptr> __u)
2872 : __ptr_(__u->release(),
2873 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2874
2875 _LIBCPP_INLINE_VISIBILITY
2876 unique_ptr& operator=(__rv<unique_ptr> __u) {
2877 reset(__u->release());
2878 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2879 return *this;
2880 }
2881
2882#endif // _LIBCPP_CXX03_LANG
2883
2884public:
2885 _LIBCPP_INLINE_VISIBILITY
2886 ~unique_ptr() { reset(); }
2887
2888 _LIBCPP_INLINE_VISIBILITY
2889 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2890 reset();
2891 return *this;
2892 }
2893
2894 _LIBCPP_INLINE_VISIBILITY
2895 typename add_lvalue_reference<_Tp>::type
2896 operator[](size_t __i) const {
2897 return __ptr_.first()[__i];
2898 }
2899 _LIBCPP_INLINE_VISIBILITY
2900 pointer get() const _NOEXCEPT {
2901 return __ptr_.first();
2902 }
2903
2904 _LIBCPP_INLINE_VISIBILITY
2905 deleter_type& get_deleter() _NOEXCEPT {
2906 return __ptr_.second();
2907 }
2908
2909 _LIBCPP_INLINE_VISIBILITY
2910 const deleter_type& get_deleter() const _NOEXCEPT {
2911 return __ptr_.second();
2912 }
2913 _LIBCPP_INLINE_VISIBILITY
2914 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2915 return __ptr_.first() != nullptr;
2916 }
2917
2918 _LIBCPP_INLINE_VISIBILITY
2919 pointer release() _NOEXCEPT {
2920 pointer __t = __ptr_.first();
2921 __ptr_.first() = pointer();
2922 return __t;
2923 }
2924
2925 template <class _Pp>
2926 _LIBCPP_INLINE_VISIBILITY
2927 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002928 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002929 >::type
2930 reset(_Pp __p) _NOEXCEPT {
2931 pointer __tmp = __ptr_.first();
2932 __ptr_.first() = __p;
2933 if (__tmp)
2934 __ptr_.second()(__tmp);
2935 }
2936
2937 _LIBCPP_INLINE_VISIBILITY
2938 void reset(nullptr_t = nullptr) _NOEXCEPT {
2939 pointer __tmp = __ptr_.first();
2940 __ptr_.first() = nullptr;
2941 if (__tmp)
2942 __ptr_.second()(__tmp);
2943 }
2944
2945 _LIBCPP_INLINE_VISIBILITY
2946 void swap(unique_ptr& __u) _NOEXCEPT {
2947 __ptr_.swap(__u.__ptr_);
2948 }
2949
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950};
2951
2952template <class _Tp, class _Dp>
2953inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002954typename enable_if<
2955 __is_swappable<_Dp>::value,
2956 void
2957>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002958swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959
2960template <class _T1, class _D1, class _T2, class _D2>
2961inline _LIBCPP_INLINE_VISIBILITY
2962bool
2963operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2964
2965template <class _T1, class _D1, class _T2, class _D2>
2966inline _LIBCPP_INLINE_VISIBILITY
2967bool
2968operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2969
2970template <class _T1, class _D1, class _T2, class _D2>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002973operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2974{
2975 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2976 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002977 typedef typename common_type<_P1, _P2>::type _Vp;
2978 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002979}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980
2981template <class _T1, class _D1, class _T2, class _D2>
2982inline _LIBCPP_INLINE_VISIBILITY
2983bool
2984operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2985
2986template <class _T1, class _D1, class _T2, class _D2>
2987inline _LIBCPP_INLINE_VISIBILITY
2988bool
2989operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2990
2991template <class _T1, class _D1, class _T2, class _D2>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
2994operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2995
Howard Hinnantb17caf92012-02-21 21:02:58 +00002996template <class _T1, class _D1>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002999operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003000{
3001 return !__x;
3002}
3003
3004template <class _T1, class _D1>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003007operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003008{
3009 return !__x;
3010}
3011
3012template <class _T1, class _D1>
3013inline _LIBCPP_INLINE_VISIBILITY
3014bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003015operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003016{
3017 return static_cast<bool>(__x);
3018}
3019
3020template <class _T1, class _D1>
3021inline _LIBCPP_INLINE_VISIBILITY
3022bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003023operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003024{
3025 return static_cast<bool>(__x);
3026}
3027
3028template <class _T1, class _D1>
3029inline _LIBCPP_INLINE_VISIBILITY
3030bool
3031operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3032{
3033 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3034 return less<_P1>()(__x.get(), nullptr);
3035}
3036
3037template <class _T1, class _D1>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3041{
3042 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3043 return less<_P1>()(nullptr, __x.get());
3044}
3045
3046template <class _T1, class _D1>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
3049operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3050{
3051 return nullptr < __x;
3052}
3053
3054template <class _T1, class _D1>
3055inline _LIBCPP_INLINE_VISIBILITY
3056bool
3057operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3058{
3059 return __x < nullptr;
3060}
3061
3062template <class _T1, class _D1>
3063inline _LIBCPP_INLINE_VISIBILITY
3064bool
3065operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3066{
3067 return !(nullptr < __x);
3068}
3069
3070template <class _T1, class _D1>
3071inline _LIBCPP_INLINE_VISIBILITY
3072bool
3073operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3074{
3075 return !(__x < nullptr);
3076}
3077
3078template <class _T1, class _D1>
3079inline _LIBCPP_INLINE_VISIBILITY
3080bool
3081operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3082{
3083 return !(__x < nullptr);
3084}
3085
3086template <class _T1, class _D1>
3087inline _LIBCPP_INLINE_VISIBILITY
3088bool
3089operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3090{
3091 return !(nullptr < __x);
3092}
3093
Howard Hinnant9e028f12012-05-01 15:37:54 +00003094#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3095
3096template <class _Tp, class _Dp>
3097inline _LIBCPP_INLINE_VISIBILITY
3098unique_ptr<_Tp, _Dp>
3099move(unique_ptr<_Tp, _Dp>& __t)
3100{
3101 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3102}
3103
3104#endif
3105
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003106#if _LIBCPP_STD_VER > 11
3107
3108template<class _Tp>
3109struct __unique_if
3110{
3111 typedef unique_ptr<_Tp> __unique_single;
3112};
3113
3114template<class _Tp>
3115struct __unique_if<_Tp[]>
3116{
3117 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3118};
3119
3120template<class _Tp, size_t _Np>
3121struct __unique_if<_Tp[_Np]>
3122{
3123 typedef void __unique_array_known_bound;
3124};
3125
3126template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003127inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003128typename __unique_if<_Tp>::__unique_single
3129make_unique(_Args&&... __args)
3130{
3131 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3132}
3133
3134template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003135inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003136typename __unique_if<_Tp>::__unique_array_unknown_bound
3137make_unique(size_t __n)
3138{
3139 typedef typename remove_extent<_Tp>::type _Up;
3140 return unique_ptr<_Tp>(new _Up[__n]());
3141}
3142
3143template<class _Tp, class... _Args>
3144 typename __unique_if<_Tp>::__unique_array_known_bound
3145 make_unique(_Args&&...) = delete;
3146
3147#endif // _LIBCPP_STD_VER > 11
3148
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003149template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003150#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003151struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003152#else
3153struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3154 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3155#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003156{
3157 typedef unique_ptr<_Tp, _Dp> argument_type;
3158 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003159 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003160 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003161 {
3162 typedef typename argument_type::pointer pointer;
3163 return hash<pointer>()(__ptr.get());
3164 }
3165};
3166
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167struct __destruct_n
3168{
3169private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003170 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171
3172 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003173 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003174 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175
3176 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003177 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 {}
3179
Howard Hinnant719bda32011-05-28 14:41:13 +00003180 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003181 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003182 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 {}
3184
Howard Hinnant719bda32011-05-28 14:41:13 +00003185 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003186 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003187 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188 {}
3189public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003190 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003191 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192
3193 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003194 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003195 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196
3197 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003198 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003199 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200
3201 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003202 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003203 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204};
3205
3206template <class _Alloc>
3207class __allocator_destructor
3208{
3209 typedef allocator_traits<_Alloc> __alloc_traits;
3210public:
3211 typedef typename __alloc_traits::pointer pointer;
3212 typedef typename __alloc_traits::size_type size_type;
3213private:
3214 _Alloc& __alloc_;
3215 size_type __s_;
3216public:
3217 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003218 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003221 void operator()(pointer __p) _NOEXCEPT
3222 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223};
3224
3225template <class _InputIterator, class _ForwardIterator>
3226_ForwardIterator
3227uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3228{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003230#ifndef _LIBCPP_NO_EXCEPTIONS
3231 _ForwardIterator __s = __r;
3232 try
3233 {
3234#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003235 for (; __f != __l; ++__f, (void) ++__r)
3236 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003237#ifndef _LIBCPP_NO_EXCEPTIONS
3238 }
3239 catch (...)
3240 {
3241 for (; __s != __r; ++__s)
3242 __s->~value_type();
3243 throw;
3244 }
3245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246 return __r;
3247}
3248
3249template <class _InputIterator, class _Size, class _ForwardIterator>
3250_ForwardIterator
3251uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3252{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003254#ifndef _LIBCPP_NO_EXCEPTIONS
3255 _ForwardIterator __s = __r;
3256 try
3257 {
3258#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003259 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3260 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003261#ifndef _LIBCPP_NO_EXCEPTIONS
3262 }
3263 catch (...)
3264 {
3265 for (; __s != __r; ++__s)
3266 __s->~value_type();
3267 throw;
3268 }
3269#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 return __r;
3271}
3272
3273template <class _ForwardIterator, class _Tp>
3274void
3275uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3276{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003278#ifndef _LIBCPP_NO_EXCEPTIONS
3279 _ForwardIterator __s = __f;
3280 try
3281 {
3282#endif
3283 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003284 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003285#ifndef _LIBCPP_NO_EXCEPTIONS
3286 }
3287 catch (...)
3288 {
3289 for (; __s != __f; ++__s)
3290 __s->~value_type();
3291 throw;
3292 }
3293#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294}
3295
3296template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003297_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3299{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003301#ifndef _LIBCPP_NO_EXCEPTIONS
3302 _ForwardIterator __s = __f;
3303 try
3304 {
3305#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003306 for (; __n > 0; ++__f, (void) --__n)
3307 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003308#ifndef _LIBCPP_NO_EXCEPTIONS
3309 }
3310 catch (...)
3311 {
3312 for (; __s != __f; ++__s)
3313 __s->~value_type();
3314 throw;
3315 }
3316#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003317 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318}
3319
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003320#if _LIBCPP_STD_VER > 14
3321
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003322template <class _Tp>
3323inline _LIBCPP_INLINE_VISIBILITY
3324void destroy_at(_Tp* __loc) {
3325 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3326 __loc->~_Tp();
3327}
3328
3329template <class _ForwardIterator>
3330inline _LIBCPP_INLINE_VISIBILITY
3331void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3332 for (; __first != __last; ++__first)
3333 _VSTD::destroy_at(_VSTD::addressof(*__first));
3334}
3335
3336template <class _ForwardIterator, class _Size>
3337inline _LIBCPP_INLINE_VISIBILITY
3338_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3339 for (; __n > 0; (void)++__first, --__n)
3340 _VSTD::destroy_at(_VSTD::addressof(*__first));
3341 return __first;
3342}
3343
Eric Fiselier290c07c2016-10-11 21:13:44 +00003344template <class _ForwardIterator>
3345inline _LIBCPP_INLINE_VISIBILITY
3346void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3347 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3348 auto __idx = __first;
3349#ifndef _LIBCPP_NO_EXCEPTIONS
3350 try {
3351#endif
3352 for (; __idx != __last; ++__idx)
3353 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3354#ifndef _LIBCPP_NO_EXCEPTIONS
3355 } catch (...) {
3356 _VSTD::destroy(__first, __idx);
3357 throw;
3358 }
3359#endif
3360}
3361
3362template <class _ForwardIterator, class _Size>
3363inline _LIBCPP_INLINE_VISIBILITY
3364_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3365 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3366 auto __idx = __first;
3367#ifndef _LIBCPP_NO_EXCEPTIONS
3368 try {
3369#endif
3370 for (; __n > 0; (void)++__idx, --__n)
3371 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3372 return __idx;
3373#ifndef _LIBCPP_NO_EXCEPTIONS
3374 } catch (...) {
3375 _VSTD::destroy(__first, __idx);
3376 throw;
3377 }
3378#endif
3379}
3380
3381
3382template <class _ForwardIterator>
3383inline _LIBCPP_INLINE_VISIBILITY
3384void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3385 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3386 auto __idx = __first;
3387#ifndef _LIBCPP_NO_EXCEPTIONS
3388 try {
3389#endif
3390 for (; __idx != __last; ++__idx)
3391 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3392#ifndef _LIBCPP_NO_EXCEPTIONS
3393 } catch (...) {
3394 _VSTD::destroy(__first, __idx);
3395 throw;
3396 }
3397#endif
3398}
3399
3400template <class _ForwardIterator, class _Size>
3401inline _LIBCPP_INLINE_VISIBILITY
3402_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3403 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3404 auto __idx = __first;
3405#ifndef _LIBCPP_NO_EXCEPTIONS
3406 try {
3407#endif
3408 for (; __n > 0; (void)++__idx, --__n)
3409 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3410 return __idx;
3411#ifndef _LIBCPP_NO_EXCEPTIONS
3412 } catch (...) {
3413 _VSTD::destroy(__first, __idx);
3414 throw;
3415 }
3416#endif
3417}
3418
3419
3420template <class _InputIt, class _ForwardIt>
3421inline _LIBCPP_INLINE_VISIBILITY
3422_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3423 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3424 auto __idx = __first_res;
3425#ifndef _LIBCPP_NO_EXCEPTIONS
3426 try {
3427#endif
3428 for (; __first != __last; (void)++__idx, ++__first)
3429 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3430 return __idx;
3431#ifndef _LIBCPP_NO_EXCEPTIONS
3432 } catch (...) {
3433 _VSTD::destroy(__first_res, __idx);
3434 throw;
3435 }
3436#endif
3437}
3438
3439template <class _InputIt, class _Size, class _ForwardIt>
3440inline _LIBCPP_INLINE_VISIBILITY
3441pair<_InputIt, _ForwardIt>
3442uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3443 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3444 auto __idx = __first_res;
3445#ifndef _LIBCPP_NO_EXCEPTIONS
3446 try {
3447#endif
3448 for (; __n > 0; ++__idx, (void)++__first, --__n)
3449 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3450 return {__first, __idx};
3451#ifndef _LIBCPP_NO_EXCEPTIONS
3452 } catch (...) {
3453 _VSTD::destroy(__first_res, __idx);
3454 throw;
3455 }
3456#endif
3457}
3458
3459
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003460#endif // _LIBCPP_STD_VER > 14
3461
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003462// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3463// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003464// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003465#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3466 && defined(__ATOMIC_RELAXED) \
3467 && defined(__ATOMIC_ACQ_REL)
3468# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3469#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3470# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3471#endif
3472
3473template <class _Tp>
3474inline _LIBCPP_INLINE_VISIBILITY _Tp
3475__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3476{
3477#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3478 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3479#else
3480 return __t += 1;
3481#endif
3482}
3483
3484template <class _Tp>
3485inline _LIBCPP_INLINE_VISIBILITY _Tp
3486__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3487{
3488#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3489 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3490#else
3491 return __t -= 1;
3492#endif
3493}
3494
Howard Hinnant756c69b2010-09-22 16:48:34 +00003495class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496 : public std::exception
3497{
3498public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003499 virtual ~bad_weak_ptr() _NOEXCEPT;
3500 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501};
3502
Louis Dionne16fe2952018-07-11 23:14:33 +00003503_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003504void __throw_bad_weak_ptr()
3505{
3506#ifndef _LIBCPP_NO_EXCEPTIONS
3507 throw bad_weak_ptr();
3508#else
3509 _VSTD::abort();
3510#endif
3511}
3512
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003513template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003515class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516{
3517 __shared_count(const __shared_count&);
3518 __shared_count& operator=(const __shared_count&);
3519
3520protected:
3521 long __shared_owners_;
3522 virtual ~__shared_count();
3523private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003524 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525
3526public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003528 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529 : __shared_owners_(__refs) {}
3530
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003531#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003532 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003533 void __add_shared() _NOEXCEPT;
3534 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003535#else
3536 _LIBCPP_INLINE_VISIBILITY
3537 void __add_shared() _NOEXCEPT {
3538 __libcpp_atomic_refcount_increment(__shared_owners_);
3539 }
3540 _LIBCPP_INLINE_VISIBILITY
3541 bool __release_shared() _NOEXCEPT {
3542 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3543 __on_zero_shared();
3544 return true;
3545 }
3546 return false;
3547 }
3548#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003549 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003550 long use_count() const _NOEXCEPT {
3551 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3552 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553};
3554
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003555class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556 : private __shared_count
3557{
3558 long __shared_weak_owners_;
3559
3560public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003562 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563 : __shared_count(__refs),
3564 __shared_weak_owners_(__refs) {}
3565protected:
3566 virtual ~__shared_weak_count();
3567
3568public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003569#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003570 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003571 void __add_shared() _NOEXCEPT;
3572 void __add_weak() _NOEXCEPT;
3573 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003574#else
3575 _LIBCPP_INLINE_VISIBILITY
3576 void __add_shared() _NOEXCEPT {
3577 __shared_count::__add_shared();
3578 }
3579 _LIBCPP_INLINE_VISIBILITY
3580 void __add_weak() _NOEXCEPT {
3581 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3582 }
3583 _LIBCPP_INLINE_VISIBILITY
3584 void __release_shared() _NOEXCEPT {
3585 if (__shared_count::__release_shared())
3586 __release_weak();
3587 }
3588#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003589 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003591 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3592 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593
Howard Hinnant807d6332013-02-25 15:50:36 +00003594 // Define the function out only if we build static libc++ without RTTI.
3595 // Otherwise we may break clients who need to compile their projects with
3596 // -fno-rtti and yet link against a libc++.dylib compiled
3597 // without -fno-rtti.
3598#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003599 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003600#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003602 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603};
3604
3605template <class _Tp, class _Dp, class _Alloc>
3606class __shared_ptr_pointer
3607 : public __shared_weak_count
3608{
3609 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3610public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003613 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614
Howard Hinnant72f73582010-08-11 17:04:31 +00003615#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003616 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003617#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618
3619private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003620 virtual void __on_zero_shared() _NOEXCEPT;
3621 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622};
3623
Howard Hinnant72f73582010-08-11 17:04:31 +00003624#ifndef _LIBCPP_NO_RTTI
3625
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626template <class _Tp, class _Dp, class _Alloc>
3627const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003628__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003630 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631}
3632
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003633#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003634
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635template <class _Tp, class _Dp, class _Alloc>
3636void
Howard Hinnant719bda32011-05-28 14:41:13 +00003637__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
3639 __data_.first().second()(__data_.first().first());
3640 __data_.first().second().~_Dp();
3641}
3642
3643template <class _Tp, class _Dp, class _Alloc>
3644void
Howard Hinnant719bda32011-05-28 14:41:13 +00003645__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003647 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3648 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003649 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3650
Eric Fiselierf8898c82015-02-05 23:01:40 +00003651 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003653 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654}
3655
3656template <class _Tp, class _Alloc>
3657class __shared_ptr_emplace
3658 : public __shared_weak_count
3659{
3660 __compressed_pair<_Alloc, _Tp> __data_;
3661public:
3662#ifndef _LIBCPP_HAS_NO_VARIADICS
3663
Howard Hinnant756c69b2010-09-22 16:48:34 +00003664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003666 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667
3668 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003671 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3672 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673
3674#else // _LIBCPP_HAS_NO_VARIADICS
3675
Howard Hinnant756c69b2010-09-22 16:48:34 +00003676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677 __shared_ptr_emplace(_Alloc __a)
3678 : __data_(__a) {}
3679
3680 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3683 : __data_(__a, _Tp(__a0)) {}
3684
3685 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3688 : __data_(__a, _Tp(__a0, __a1)) {}
3689
3690 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3693 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3694
3695#endif // _LIBCPP_HAS_NO_VARIADICS
3696
3697private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003698 virtual void __on_zero_shared() _NOEXCEPT;
3699 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003701 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003702 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703};
3704
3705template <class _Tp, class _Alloc>
3706void
Howard Hinnant719bda32011-05-28 14:41:13 +00003707__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
3709 __data_.second().~_Tp();
3710}
3711
3712template <class _Tp, class _Alloc>
3713void
Howard Hinnant719bda32011-05-28 14:41:13 +00003714__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003716 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3717 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003718 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003719 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003721 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722}
3723
Erik Pilkington2a398762017-05-25 15:43:31 +00003724struct __shared_ptr_dummy_rebind_allocator_type;
3725template <>
3726class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3727{
3728public:
3729 template <class _Other>
3730 struct rebind
3731 {
3732 typedef allocator<_Other> other;
3733 };
3734};
3735
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003736template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737
3738template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003739class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003741public:
3742 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003743
Eric Fiselierae5b6672016-06-27 01:02:43 +00003744#if _LIBCPP_STD_VER > 14
3745 typedef weak_ptr<_Tp> weak_type;
3746#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747private:
3748 element_type* __ptr_;
3749 __shared_weak_count* __cntrl_;
3750
3751 struct __nat {int __for_bool_;};
3752public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003754 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003756 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003757 template<class _Yp>
3758 explicit shared_ptr(_Yp* __p,
3759 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3760 template<class _Yp, class _Dp>
3761 shared_ptr(_Yp* __p, _Dp __d,
3762 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3763 template<class _Yp, class _Dp, class _Alloc>
3764 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3765 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3767 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003768 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003770 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003774 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003775 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003776#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003778 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003779 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003780 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003781 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003782#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003784 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003785#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003786#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003787 template<class _Yp>
3788 shared_ptr(auto_ptr<_Yp>&& __r,
3789 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003790#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003791 template<class _Yp>
3792 shared_ptr(auto_ptr<_Yp> __r,
3793 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003795#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003796#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003797 template <class _Yp, class _Dp>
3798 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3799 typename enable_if
3800 <
3801 !is_lvalue_reference<_Dp>::value &&
3802 !is_array<_Yp>::value &&
3803 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3804 __nat
3805 >::type = __nat());
3806 template <class _Yp, class _Dp>
3807 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3808 typename enable_if
3809 <
3810 is_lvalue_reference<_Dp>::value &&
3811 !is_array<_Yp>::value &&
3812 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3813 __nat
3814 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003815#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003816 template <class _Yp, class _Dp>
3817 shared_ptr(unique_ptr<_Yp, _Dp>,
3818 typename enable_if
3819 <
3820 !is_lvalue_reference<_Dp>::value &&
3821 !is_array<_Yp>::value &&
3822 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3823 __nat
3824 >::type = __nat());
3825 template <class _Yp, class _Dp>
3826 shared_ptr(unique_ptr<_Yp, _Dp>,
3827 typename enable_if
3828 <
3829 is_lvalue_reference<_Dp>::value &&
3830 !is_array<_Yp>::value &&
3831 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3832 __nat
3833 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003834#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835
3836 ~shared_ptr();
3837
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003839 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003840 template<class _Yp>
3841 typename enable_if
3842 <
3843 is_convertible<_Yp*, element_type*>::value,
3844 shared_ptr&
3845 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003847 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003848#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003850 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003851 template<class _Yp>
3852 typename enable_if
3853 <
3854 is_convertible<_Yp*, element_type*>::value,
3855 shared_ptr<_Tp>&
3856 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003858 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003859#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003860 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003862 typename enable_if
3863 <
3864 !is_array<_Yp>::value &&
3865 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003866 shared_ptr
3867 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003868 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003869#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003870#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003871#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003872 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003874 typename enable_if
3875 <
3876 !is_array<_Yp>::value &&
3877 is_convertible<_Yp*, element_type*>::value,
3878 shared_ptr&
3879 >::type
3880 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003882#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003883 template <class _Yp, class _Dp>
3884 typename enable_if
3885 <
3886 !is_array<_Yp>::value &&
3887 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3888 shared_ptr&
3889 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003890#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003892 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003893#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003895 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896#endif
3897
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003899 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003901 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003902 template<class _Yp>
3903 typename enable_if
3904 <
3905 is_convertible<_Yp*, element_type*>::value,
3906 void
3907 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003909 reset(_Yp* __p);
3910 template<class _Yp, class _Dp>
3911 typename enable_if
3912 <
3913 is_convertible<_Yp*, element_type*>::value,
3914 void
3915 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003917 reset(_Yp* __p, _Dp __d);
3918 template<class _Yp, class _Dp, class _Alloc>
3919 typename enable_if
3920 <
3921 is_convertible<_Yp*, element_type*>::value,
3922 void
3923 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003925 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926
Howard Hinnant756c69b2010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003928 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003930 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3931 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003933 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003935 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003937 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003939 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003940 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003941 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003942 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003944 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003945 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003946 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003948 _LIBCPP_INLINE_VISIBILITY
3949 bool
3950 __owner_equivalent(const shared_ptr& __p) const
3951 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952
Howard Hinnant72f73582010-08-11 17:04:31 +00003953#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003956 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003957 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003958 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003959 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003960#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961
3962#ifndef _LIBCPP_HAS_NO_VARIADICS
3963
3964 template<class ..._Args>
3965 static
3966 shared_ptr<_Tp>
3967 make_shared(_Args&& ...__args);
3968
3969 template<class _Alloc, class ..._Args>
3970 static
3971 shared_ptr<_Tp>
3972 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3973
3974#else // _LIBCPP_HAS_NO_VARIADICS
3975
3976 static shared_ptr<_Tp> make_shared();
3977
3978 template<class _A0>
3979 static shared_ptr<_Tp> make_shared(_A0&);
3980
3981 template<class _A0, class _A1>
3982 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3983
3984 template<class _A0, class _A1, class _A2>
3985 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3986
3987 template<class _Alloc>
3988 static shared_ptr<_Tp>
3989 allocate_shared(const _Alloc& __a);
3990
3991 template<class _Alloc, class _A0>
3992 static shared_ptr<_Tp>
3993 allocate_shared(const _Alloc& __a, _A0& __a0);
3994
3995 template<class _Alloc, class _A0, class _A1>
3996 static shared_ptr<_Tp>
3997 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3998
3999 template<class _Alloc, class _A0, class _A1, class _A2>
4000 static shared_ptr<_Tp>
4001 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4002
4003#endif // _LIBCPP_HAS_NO_VARIADICS
4004
4005private:
Erik Pilkington2a398762017-05-25 15:43:31 +00004006 template <class _Yp, bool = is_function<_Yp>::value>
4007 struct __shared_ptr_default_allocator
4008 {
4009 typedef allocator<_Yp> type;
4010 };
4011
4012 template <class _Yp>
4013 struct __shared_ptr_default_allocator<_Yp, true>
4014 {
4015 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
4016 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004018 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004019 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004020 typename enable_if<is_convertible<_OrigPtr*,
4021 const enable_shared_from_this<_Yp>*
4022 >::value,
4023 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004024 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4025 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004027 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004028 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004029 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004030 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4031 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004032 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033 }
4034
Erik Pilkington2a398762017-05-25 15:43:31 +00004035 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004037 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4038 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039};
4040
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004041
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004043inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004044_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004045shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046 : __ptr_(0),
4047 __cntrl_(0)
4048{
4049}
4050
4051template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004052inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004053_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004054shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055 : __ptr_(0),
4056 __cntrl_(0)
4057{
4058}
4059
4060template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004061template<class _Yp>
4062shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4063 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064 : __ptr_(__p)
4065{
4066 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004067 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4068 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4069 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004071 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072}
4073
4074template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004075template<class _Yp, class _Dp>
4076shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4077 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078 : __ptr_(__p)
4079{
4080#ifndef _LIBCPP_NO_EXCEPTIONS
4081 try
4082 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004083#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004084 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4085 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4086 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004087 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088#ifndef _LIBCPP_NO_EXCEPTIONS
4089 }
4090 catch (...)
4091 {
4092 __d(__p);
4093 throw;
4094 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004095#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096}
4097
4098template<class _Tp>
4099template<class _Dp>
4100shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4101 : __ptr_(0)
4102{
4103#ifndef _LIBCPP_NO_EXCEPTIONS
4104 try
4105 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004106#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004107 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4108 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4109 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110#ifndef _LIBCPP_NO_EXCEPTIONS
4111 }
4112 catch (...)
4113 {
4114 __d(__p);
4115 throw;
4116 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004117#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118}
4119
4120template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004121template<class _Yp, class _Dp, class _Alloc>
4122shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4123 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124 : __ptr_(__p)
4125{
4126#ifndef _LIBCPP_NO_EXCEPTIONS
4127 try
4128 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004129#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004131 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132 typedef __allocator_destructor<_A2> _D2;
4133 _A2 __a2(__a);
4134 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004135 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4136 _CntrlBlk(__p, __d, __a);
4137 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004138 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139#ifndef _LIBCPP_NO_EXCEPTIONS
4140 }
4141 catch (...)
4142 {
4143 __d(__p);
4144 throw;
4145 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004146#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147}
4148
4149template<class _Tp>
4150template<class _Dp, class _Alloc>
4151shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4152 : __ptr_(0)
4153{
4154#ifndef _LIBCPP_NO_EXCEPTIONS
4155 try
4156 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004157#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004159 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160 typedef __allocator_destructor<_A2> _D2;
4161 _A2 __a2(__a);
4162 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004163 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4164 _CntrlBlk(__p, __d, __a);
4165 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166#ifndef _LIBCPP_NO_EXCEPTIONS
4167 }
4168 catch (...)
4169 {
4170 __d(__p);
4171 throw;
4172 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004173#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174}
4175
4176template<class _Tp>
4177template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004178inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004179shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180 : __ptr_(__p),
4181 __cntrl_(__r.__cntrl_)
4182{
4183 if (__cntrl_)
4184 __cntrl_->__add_shared();
4185}
4186
4187template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004188inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004189shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190 : __ptr_(__r.__ptr_),
4191 __cntrl_(__r.__cntrl_)
4192{
4193 if (__cntrl_)
4194 __cntrl_->__add_shared();
4195}
4196
4197template<class _Tp>
4198template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004199inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004201 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004202 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203 : __ptr_(__r.__ptr_),
4204 __cntrl_(__r.__cntrl_)
4205{
4206 if (__cntrl_)
4207 __cntrl_->__add_shared();
4208}
4209
Howard Hinnant74279a52010-09-04 23:28:19 +00004210#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211
4212template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004213inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004214shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215 : __ptr_(__r.__ptr_),
4216 __cntrl_(__r.__cntrl_)
4217{
4218 __r.__ptr_ = 0;
4219 __r.__cntrl_ = 0;
4220}
4221
4222template<class _Tp>
4223template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004224inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004226 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004227 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228 : __ptr_(__r.__ptr_),
4229 __cntrl_(__r.__cntrl_)
4230{
4231 __r.__ptr_ = 0;
4232 __r.__cntrl_ = 0;
4233}
4234
Howard Hinnant74279a52010-09-04 23:28:19 +00004235#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004236
Marshall Clowb22274f2017-01-24 22:22:33 +00004237#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004239template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004240#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004241shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004243shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004245 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246 : __ptr_(__r.get())
4247{
4248 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4249 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004250 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251 __r.release();
4252}
Marshall Clowb22274f2017-01-24 22:22:33 +00004253#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254
4255template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004256template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004257#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4259#else
4260shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4261#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004262 typename enable_if
4263 <
4264 !is_lvalue_reference<_Dp>::value &&
4265 !is_array<_Yp>::value &&
4266 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4267 __nat
4268 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269 : __ptr_(__r.get())
4270{
Marshall Clow35cde742015-05-10 13:59:45 +00004271#if _LIBCPP_STD_VER > 11
4272 if (__ptr_ == nullptr)
4273 __cntrl_ = nullptr;
4274 else
4275#endif
4276 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004277 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4278 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4279 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004280 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004281 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282 __r.release();
4283}
4284
4285template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004286template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004287#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4289#else
4290shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4291#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004292 typename enable_if
4293 <
4294 is_lvalue_reference<_Dp>::value &&
4295 !is_array<_Yp>::value &&
4296 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4297 __nat
4298 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299 : __ptr_(__r.get())
4300{
Marshall Clow35cde742015-05-10 13:59:45 +00004301#if _LIBCPP_STD_VER > 11
4302 if (__ptr_ == nullptr)
4303 __cntrl_ = nullptr;
4304 else
4305#endif
4306 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004307 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004308 typedef __shared_ptr_pointer<_Yp*,
4309 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004310 _AllocT > _CntrlBlk;
4311 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004312 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004313 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314 __r.release();
4315}
4316
4317#ifndef _LIBCPP_HAS_NO_VARIADICS
4318
4319template<class _Tp>
4320template<class ..._Args>
4321shared_ptr<_Tp>
4322shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4323{
Marshall Clowdec75c72017-12-05 04:09:49 +00004324 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4326 typedef allocator<_CntrlBlk> _A2;
4327 typedef __allocator_destructor<_A2> _D2;
4328 _A2 __a2;
4329 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004330 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331 shared_ptr<_Tp> __r;
4332 __r.__ptr_ = __hold2.get()->get();
4333 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004334 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335 return __r;
4336}
4337
4338template<class _Tp>
4339template<class _Alloc, class ..._Args>
4340shared_ptr<_Tp>
4341shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4342{
Marshall Clowdec75c72017-12-05 04:09:49 +00004343 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004345 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346 typedef __allocator_destructor<_A2> _D2;
4347 _A2 __a2(__a);
4348 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004349 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4350 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351 shared_ptr<_Tp> __r;
4352 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004353 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004354 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355 return __r;
4356}
4357
4358#else // _LIBCPP_HAS_NO_VARIADICS
4359
4360template<class _Tp>
4361shared_ptr<_Tp>
4362shared_ptr<_Tp>::make_shared()
4363{
Marshall Clowdec75c72017-12-05 04:09:49 +00004364 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4366 typedef allocator<_CntrlBlk> _Alloc2;
4367 typedef __allocator_destructor<_Alloc2> _D2;
4368 _Alloc2 __alloc2;
4369 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4370 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4371 shared_ptr<_Tp> __r;
4372 __r.__ptr_ = __hold2.get()->get();
4373 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004374 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004375 return __r;
4376}
4377
4378template<class _Tp>
4379template<class _A0>
4380shared_ptr<_Tp>
4381shared_ptr<_Tp>::make_shared(_A0& __a0)
4382{
Marshall Clowdec75c72017-12-05 04:09:49 +00004383 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4385 typedef allocator<_CntrlBlk> _Alloc2;
4386 typedef __allocator_destructor<_Alloc2> _D2;
4387 _Alloc2 __alloc2;
4388 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4389 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4390 shared_ptr<_Tp> __r;
4391 __r.__ptr_ = __hold2.get()->get();
4392 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004393 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004394 return __r;
4395}
4396
4397template<class _Tp>
4398template<class _A0, class _A1>
4399shared_ptr<_Tp>
4400shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4401{
Marshall Clowdec75c72017-12-05 04:09:49 +00004402 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4404 typedef allocator<_CntrlBlk> _Alloc2;
4405 typedef __allocator_destructor<_Alloc2> _D2;
4406 _Alloc2 __alloc2;
4407 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4408 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4409 shared_ptr<_Tp> __r;
4410 __r.__ptr_ = __hold2.get()->get();
4411 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004412 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413 return __r;
4414}
4415
4416template<class _Tp>
4417template<class _A0, class _A1, class _A2>
4418shared_ptr<_Tp>
4419shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4420{
Marshall Clowdec75c72017-12-05 04:09:49 +00004421 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4423 typedef allocator<_CntrlBlk> _Alloc2;
4424 typedef __allocator_destructor<_Alloc2> _D2;
4425 _Alloc2 __alloc2;
4426 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4427 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4428 shared_ptr<_Tp> __r;
4429 __r.__ptr_ = __hold2.get()->get();
4430 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004431 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432 return __r;
4433}
4434
4435template<class _Tp>
4436template<class _Alloc>
4437shared_ptr<_Tp>
4438shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4439{
Marshall Clowdec75c72017-12-05 04:09:49 +00004440 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004442 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443 typedef __allocator_destructor<_Alloc2> _D2;
4444 _Alloc2 __alloc2(__a);
4445 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004446 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4447 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448 shared_ptr<_Tp> __r;
4449 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004450 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004451 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452 return __r;
4453}
4454
4455template<class _Tp>
4456template<class _Alloc, class _A0>
4457shared_ptr<_Tp>
4458shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4459{
Marshall Clowdec75c72017-12-05 04:09:49 +00004460 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004462 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004463 typedef __allocator_destructor<_Alloc2> _D2;
4464 _Alloc2 __alloc2(__a);
4465 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004466 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4467 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004468 shared_ptr<_Tp> __r;
4469 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004470 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004471 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472 return __r;
4473}
4474
4475template<class _Tp>
4476template<class _Alloc, class _A0, class _A1>
4477shared_ptr<_Tp>
4478shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4479{
Marshall Clowdec75c72017-12-05 04:09:49 +00004480 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004482 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004483 typedef __allocator_destructor<_Alloc2> _D2;
4484 _Alloc2 __alloc2(__a);
4485 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004486 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4487 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004488 shared_ptr<_Tp> __r;
4489 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004490 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004491 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492 return __r;
4493}
4494
4495template<class _Tp>
4496template<class _Alloc, class _A0, class _A1, class _A2>
4497shared_ptr<_Tp>
4498shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4499{
Marshall Clowdec75c72017-12-05 04:09:49 +00004500 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004502 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503 typedef __allocator_destructor<_Alloc2> _D2;
4504 _Alloc2 __alloc2(__a);
4505 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004506 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4507 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004508 shared_ptr<_Tp> __r;
4509 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004510 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004511 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512 return __r;
4513}
4514
4515#endif // _LIBCPP_HAS_NO_VARIADICS
4516
4517template<class _Tp>
4518shared_ptr<_Tp>::~shared_ptr()
4519{
4520 if (__cntrl_)
4521 __cntrl_->__release_shared();
4522}
4523
4524template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004525inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004527shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528{
4529 shared_ptr(__r).swap(*this);
4530 return *this;
4531}
4532
4533template<class _Tp>
4534template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004535inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004536typename enable_if
4537<
Marshall Clow7e384b72017-01-10 16:59:33 +00004538 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004539 shared_ptr<_Tp>&
4540>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004541shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004542{
4543 shared_ptr(__r).swap(*this);
4544 return *this;
4545}
4546
Howard Hinnant74279a52010-09-04 23:28:19 +00004547#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548
4549template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004550inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004551shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004552shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004553{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004554 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004555 return *this;
4556}
4557
4558template<class _Tp>
4559template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004560inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004561typename enable_if
4562<
Marshall Clow7e384b72017-01-10 16:59:33 +00004563 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004564 shared_ptr<_Tp>&
4565>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4567{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004568 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004569 return *this;
4570}
4571
Marshall Clowb22274f2017-01-24 22:22:33 +00004572#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573template<class _Tp>
4574template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004575inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004576typename enable_if
4577<
4578 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004579 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004580 shared_ptr<_Tp>
4581>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004582shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4583{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004584 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004585 return *this;
4586}
Marshall Clowb22274f2017-01-24 22:22:33 +00004587#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004588
4589template<class _Tp>
4590template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004591inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004592typename enable_if
4593<
4594 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004595 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004596 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004597 shared_ptr<_Tp>&
4598>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004599shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4600{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004601 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004602 return *this;
4603}
4604
Howard Hinnant74279a52010-09-04 23:28:19 +00004605#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606
Marshall Clowb22274f2017-01-24 22:22:33 +00004607#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004608template<class _Tp>
4609template<class _Yp>
4610inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004611typename enable_if
4612<
4613 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004614 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004615 shared_ptr<_Tp>&
4616>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004617shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004618{
4619 shared_ptr(__r).swap(*this);
4620 return *this;
4621}
Marshall Clowb22274f2017-01-24 22:22:33 +00004622#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623
4624template<class _Tp>
4625template <class _Yp, class _Dp>
4626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004627typename enable_if
4628<
4629 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004630 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004631 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004632 shared_ptr<_Tp>&
4633>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004634shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4635{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004636 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637 return *this;
4638}
4639
Howard Hinnant74279a52010-09-04 23:28:19 +00004640#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641
4642template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004643inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644void
Howard Hinnant719bda32011-05-28 14:41:13 +00004645shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004646{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004647 _VSTD::swap(__ptr_, __r.__ptr_);
4648 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004649}
4650
4651template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004652inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653void
Howard Hinnant719bda32011-05-28 14:41:13 +00004654shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004655{
4656 shared_ptr().swap(*this);
4657}
4658
4659template<class _Tp>
4660template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004661inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004662typename enable_if
4663<
Marshall Clow7e384b72017-01-10 16:59:33 +00004664 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004665 void
4666>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004667shared_ptr<_Tp>::reset(_Yp* __p)
4668{
4669 shared_ptr(__p).swap(*this);
4670}
4671
4672template<class _Tp>
4673template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004674inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004675typename enable_if
4676<
Marshall Clow7e384b72017-01-10 16:59:33 +00004677 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004678 void
4679>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004680shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4681{
4682 shared_ptr(__p, __d).swap(*this);
4683}
4684
4685template<class _Tp>
4686template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004687inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004688typename enable_if
4689<
Marshall Clow7e384b72017-01-10 16:59:33 +00004690 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004691 void
4692>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4694{
4695 shared_ptr(__p, __d, __a).swap(*this);
4696}
4697
4698#ifndef _LIBCPP_HAS_NO_VARIADICS
4699
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004700template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004701inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004702typename enable_if
4703<
4704 !is_array<_Tp>::value,
4705 shared_ptr<_Tp>
4706>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004707make_shared(_Args&& ...__args)
4708{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004709 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004710}
4711
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004712template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004714typename enable_if
4715<
4716 !is_array<_Tp>::value,
4717 shared_ptr<_Tp>
4718>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004719allocate_shared(const _Alloc& __a, _Args&& ...__args)
4720{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004721 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004722}
4723
4724#else // _LIBCPP_HAS_NO_VARIADICS
4725
4726template<class _Tp>
4727inline _LIBCPP_INLINE_VISIBILITY
4728shared_ptr<_Tp>
4729make_shared()
4730{
4731 return shared_ptr<_Tp>::make_shared();
4732}
4733
4734template<class _Tp, class _A0>
4735inline _LIBCPP_INLINE_VISIBILITY
4736shared_ptr<_Tp>
4737make_shared(_A0& __a0)
4738{
4739 return shared_ptr<_Tp>::make_shared(__a0);
4740}
4741
4742template<class _Tp, class _A0, class _A1>
4743inline _LIBCPP_INLINE_VISIBILITY
4744shared_ptr<_Tp>
4745make_shared(_A0& __a0, _A1& __a1)
4746{
4747 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4748}
4749
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004750template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004751inline _LIBCPP_INLINE_VISIBILITY
4752shared_ptr<_Tp>
4753make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4754{
4755 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4756}
4757
4758template<class _Tp, class _Alloc>
4759inline _LIBCPP_INLINE_VISIBILITY
4760shared_ptr<_Tp>
4761allocate_shared(const _Alloc& __a)
4762{
4763 return shared_ptr<_Tp>::allocate_shared(__a);
4764}
4765
4766template<class _Tp, class _Alloc, class _A0>
4767inline _LIBCPP_INLINE_VISIBILITY
4768shared_ptr<_Tp>
4769allocate_shared(const _Alloc& __a, _A0& __a0)
4770{
4771 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4772}
4773
4774template<class _Tp, class _Alloc, class _A0, class _A1>
4775inline _LIBCPP_INLINE_VISIBILITY
4776shared_ptr<_Tp>
4777allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4778{
4779 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4780}
4781
4782template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4783inline _LIBCPP_INLINE_VISIBILITY
4784shared_ptr<_Tp>
4785allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4786{
4787 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4788}
4789
4790#endif // _LIBCPP_HAS_NO_VARIADICS
4791
4792template<class _Tp, class _Up>
4793inline _LIBCPP_INLINE_VISIBILITY
4794bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004795operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004796{
4797 return __x.get() == __y.get();
4798}
4799
4800template<class _Tp, class _Up>
4801inline _LIBCPP_INLINE_VISIBILITY
4802bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004803operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004804{
4805 return !(__x == __y);
4806}
4807
4808template<class _Tp, class _Up>
4809inline _LIBCPP_INLINE_VISIBILITY
4810bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004811operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004812{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004813#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004814 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4815 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004816#else
4817 return less<>()(__x.get(), __y.get());
4818#endif
4819
Howard Hinnantb17caf92012-02-21 21:02:58 +00004820}
4821
4822template<class _Tp, class _Up>
4823inline _LIBCPP_INLINE_VISIBILITY
4824bool
4825operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4826{
4827 return __y < __x;
4828}
4829
4830template<class _Tp, class _Up>
4831inline _LIBCPP_INLINE_VISIBILITY
4832bool
4833operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4834{
4835 return !(__y < __x);
4836}
4837
4838template<class _Tp, class _Up>
4839inline _LIBCPP_INLINE_VISIBILITY
4840bool
4841operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4842{
4843 return !(__x < __y);
4844}
4845
4846template<class _Tp>
4847inline _LIBCPP_INLINE_VISIBILITY
4848bool
4849operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4850{
4851 return !__x;
4852}
4853
4854template<class _Tp>
4855inline _LIBCPP_INLINE_VISIBILITY
4856bool
4857operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4858{
4859 return !__x;
4860}
4861
4862template<class _Tp>
4863inline _LIBCPP_INLINE_VISIBILITY
4864bool
4865operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4866{
4867 return static_cast<bool>(__x);
4868}
4869
4870template<class _Tp>
4871inline _LIBCPP_INLINE_VISIBILITY
4872bool
4873operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4874{
4875 return static_cast<bool>(__x);
4876}
4877
4878template<class _Tp>
4879inline _LIBCPP_INLINE_VISIBILITY
4880bool
4881operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4882{
4883 return less<_Tp*>()(__x.get(), nullptr);
4884}
4885
4886template<class _Tp>
4887inline _LIBCPP_INLINE_VISIBILITY
4888bool
4889operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4890{
4891 return less<_Tp*>()(nullptr, __x.get());
4892}
4893
4894template<class _Tp>
4895inline _LIBCPP_INLINE_VISIBILITY
4896bool
4897operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4898{
4899 return nullptr < __x;
4900}
4901
4902template<class _Tp>
4903inline _LIBCPP_INLINE_VISIBILITY
4904bool
4905operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4906{
4907 return __x < nullptr;
4908}
4909
4910template<class _Tp>
4911inline _LIBCPP_INLINE_VISIBILITY
4912bool
4913operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4914{
4915 return !(nullptr < __x);
4916}
4917
4918template<class _Tp>
4919inline _LIBCPP_INLINE_VISIBILITY
4920bool
4921operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4922{
4923 return !(__x < nullptr);
4924}
4925
4926template<class _Tp>
4927inline _LIBCPP_INLINE_VISIBILITY
4928bool
4929operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4930{
4931 return !(__x < nullptr);
4932}
4933
4934template<class _Tp>
4935inline _LIBCPP_INLINE_VISIBILITY
4936bool
4937operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4938{
4939 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004940}
4941
4942template<class _Tp>
4943inline _LIBCPP_INLINE_VISIBILITY
4944void
Howard Hinnant719bda32011-05-28 14:41:13 +00004945swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004946{
4947 __x.swap(__y);
4948}
4949
4950template<class _Tp, class _Up>
4951inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004952typename enable_if
4953<
4954 !is_array<_Tp>::value && !is_array<_Up>::value,
4955 shared_ptr<_Tp>
4956>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004957static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004958{
4959 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4960}
4961
4962template<class _Tp, class _Up>
4963inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004964typename enable_if
4965<
4966 !is_array<_Tp>::value && !is_array<_Up>::value,
4967 shared_ptr<_Tp>
4968>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004969dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004970{
4971 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4972 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4973}
4974
4975template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004976typename enable_if
4977<
4978 is_array<_Tp>::value == is_array<_Up>::value,
4979 shared_ptr<_Tp>
4980>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004981const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004983 typedef typename remove_extent<_Tp>::type _RTp;
4984 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985}
4986
Howard Hinnant72f73582010-08-11 17:04:31 +00004987#ifndef _LIBCPP_NO_RTTI
4988
Howard Hinnantc51e1022010-05-11 19:42:16 +00004989template<class _Dp, class _Tp>
4990inline _LIBCPP_INLINE_VISIBILITY
4991_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004992get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993{
4994 return __p.template __get_deleter<_Dp>();
4995}
4996
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004997#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004998
Howard Hinnantc51e1022010-05-11 19:42:16 +00004999template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005000class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005001{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005002public:
5003 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004private:
5005 element_type* __ptr_;
5006 __shared_weak_count* __cntrl_;
5007
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005008public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005010 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005011 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005012 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5013 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005015 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005016 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005017 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5018 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005019
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005022 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005023 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005024 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5025 _NOEXCEPT;
5026#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005027 ~weak_ptr();
5028
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005030 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005031 template<class _Yp>
5032 typename enable_if
5033 <
5034 is_convertible<_Yp*, element_type*>::value,
5035 weak_ptr&
5036 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005038 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5039
5040#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5041
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005043 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5044 template<class _Yp>
5045 typename enable_if
5046 <
5047 is_convertible<_Yp*, element_type*>::value,
5048 weak_ptr&
5049 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005051 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5052
5053#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5054
5055 template<class _Yp>
5056 typename enable_if
5057 <
5058 is_convertible<_Yp*, element_type*>::value,
5059 weak_ptr&
5060 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005062 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005063
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005065 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005067 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005068
Howard Hinnant756c69b2010-09-22 16:48:34 +00005069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005070 long use_count() const _NOEXCEPT
5071 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005073 bool expired() const _NOEXCEPT
5074 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5075 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005076 template<class _Up>
5077 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005078 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005079 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005080 template<class _Up>
5081 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005082 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005083 {return __cntrl_ < __r.__cntrl_;}
5084
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005085 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5086 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005087};
5088
5089template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005090inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005091_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005092weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005093 : __ptr_(0),
5094 __cntrl_(0)
5095{
5096}
5097
5098template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005099inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005100weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005101 : __ptr_(__r.__ptr_),
5102 __cntrl_(__r.__cntrl_)
5103{
5104 if (__cntrl_)
5105 __cntrl_->__add_weak();
5106}
5107
5108template<class _Tp>
5109template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005110inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005111weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005112 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005113 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005114 : __ptr_(__r.__ptr_),
5115 __cntrl_(__r.__cntrl_)
5116{
5117 if (__cntrl_)
5118 __cntrl_->__add_weak();
5119}
5120
5121template<class _Tp>
5122template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005123inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005124weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005125 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005126 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005127 : __ptr_(__r.__ptr_),
5128 __cntrl_(__r.__cntrl_)
5129{
5130 if (__cntrl_)
5131 __cntrl_->__add_weak();
5132}
5133
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005134#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5135
5136template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005137inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005138weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5139 : __ptr_(__r.__ptr_),
5140 __cntrl_(__r.__cntrl_)
5141{
5142 __r.__ptr_ = 0;
5143 __r.__cntrl_ = 0;
5144}
5145
5146template<class _Tp>
5147template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005148inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005149weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5150 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5151 _NOEXCEPT
5152 : __ptr_(__r.__ptr_),
5153 __cntrl_(__r.__cntrl_)
5154{
5155 __r.__ptr_ = 0;
5156 __r.__cntrl_ = 0;
5157}
5158
5159#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5160
Howard Hinnantc51e1022010-05-11 19:42:16 +00005161template<class _Tp>
5162weak_ptr<_Tp>::~weak_ptr()
5163{
5164 if (__cntrl_)
5165 __cntrl_->__release_weak();
5166}
5167
5168template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005169inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005171weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005172{
5173 weak_ptr(__r).swap(*this);
5174 return *this;
5175}
5176
5177template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005178template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005179inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005180typename enable_if
5181<
5182 is_convertible<_Yp*, _Tp*>::value,
5183 weak_ptr<_Tp>&
5184>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005185weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005186{
5187 weak_ptr(__r).swap(*this);
5188 return *this;
5189}
5190
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005191#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5192
5193template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005194inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005195weak_ptr<_Tp>&
5196weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5197{
5198 weak_ptr(_VSTD::move(__r)).swap(*this);
5199 return *this;
5200}
5201
Howard Hinnantc51e1022010-05-11 19:42:16 +00005202template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005203template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005204inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005205typename enable_if
5206<
5207 is_convertible<_Yp*, _Tp*>::value,
5208 weak_ptr<_Tp>&
5209>::type
5210weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5211{
5212 weak_ptr(_VSTD::move(__r)).swap(*this);
5213 return *this;
5214}
5215
5216#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5217
5218template<class _Tp>
5219template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005220inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005221typename enable_if
5222<
5223 is_convertible<_Yp*, _Tp*>::value,
5224 weak_ptr<_Tp>&
5225>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005226weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227{
5228 weak_ptr(__r).swap(*this);
5229 return *this;
5230}
5231
5232template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005234void
Howard Hinnant719bda32011-05-28 14:41:13 +00005235weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005236{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005237 _VSTD::swap(__ptr_, __r.__ptr_);
5238 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005239}
5240
5241template<class _Tp>
5242inline _LIBCPP_INLINE_VISIBILITY
5243void
Howard Hinnant719bda32011-05-28 14:41:13 +00005244swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005245{
5246 __x.swap(__y);
5247}
5248
5249template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005250inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005251void
Howard Hinnant719bda32011-05-28 14:41:13 +00005252weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005253{
5254 weak_ptr().swap(*this);
5255}
5256
5257template<class _Tp>
5258template<class _Yp>
5259shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005260 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005261 : __ptr_(__r.__ptr_),
5262 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5263{
5264 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005265 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005266}
5267
5268template<class _Tp>
5269shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005270weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005271{
5272 shared_ptr<_Tp> __r;
5273 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5274 if (__r.__cntrl_)
5275 __r.__ptr_ = __ptr_;
5276 return __r;
5277}
5278
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005279#if _LIBCPP_STD_VER > 14
5280template <class _Tp = void> struct owner_less;
5281#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005282template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005283#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005284
5285template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005286struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005287 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005288{
5289 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005290 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005291 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005292 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005293 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005294 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005295 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005296 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005297 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005298 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005299};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005300
5301template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005302struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005303 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5304{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005305 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005306 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005307 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005308 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005309 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005310 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005311 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005312 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005313 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005314 {return __x.owner_before(__y);}
5315};
5316
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005317#if _LIBCPP_STD_VER > 14
5318template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005319struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005320{
5321 template <class _Tp, class _Up>
5322 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005323 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005324 {return __x.owner_before(__y);}
5325 template <class _Tp, class _Up>
5326 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005327 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005328 {return __x.owner_before(__y);}
5329 template <class _Tp, class _Up>
5330 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005331 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005332 {return __x.owner_before(__y);}
5333 template <class _Tp, class _Up>
5334 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005335 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005336 {return __x.owner_before(__y);}
5337 typedef void is_transparent;
5338};
5339#endif
5340
Howard Hinnantc51e1022010-05-11 19:42:16 +00005341template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005342class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005343{
5344 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005345protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005346 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005347 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005349 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005351 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5352 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005354 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005355public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005357 shared_ptr<_Tp> shared_from_this()
5358 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005360 shared_ptr<_Tp const> shared_from_this() const
5361 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005362
Eric Fiselier84006862016-06-02 00:15:35 +00005363#if _LIBCPP_STD_VER > 14
5364 _LIBCPP_INLINE_VISIBILITY
5365 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5366 { return __weak_this_; }
5367
5368 _LIBCPP_INLINE_VISIBILITY
5369 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5370 { return __weak_this_; }
5371#endif // _LIBCPP_STD_VER > 14
5372
Howard Hinnantc51e1022010-05-11 19:42:16 +00005373 template <class _Up> friend class shared_ptr;
5374};
5375
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005376template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005377struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005378{
5379 typedef shared_ptr<_Tp> argument_type;
5380 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005381
Howard Hinnant756c69b2010-09-22 16:48:34 +00005382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005383 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005384 {
5385 return hash<_Tp*>()(__ptr.get());
5386 }
5387};
5388
Howard Hinnantc834c512011-11-29 18:15:50 +00005389template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005390inline _LIBCPP_INLINE_VISIBILITY
5391basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005392operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005393
Eric Fiselier9b492672016-06-18 02:12:53 +00005394
5395#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005396
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005397class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005398{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005399 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005400public:
5401 void lock() _NOEXCEPT;
5402 void unlock() _NOEXCEPT;
5403
5404private:
5405 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5406 __sp_mut(const __sp_mut&);
5407 __sp_mut& operator=(const __sp_mut&);
5408
Howard Hinnant8331b762013-03-06 23:30:19 +00005409 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005410};
5411
Mehdi Amini228053d2017-05-04 17:08:54 +00005412_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5413__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005414
5415template <class _Tp>
5416inline _LIBCPP_INLINE_VISIBILITY
5417bool
5418atomic_is_lock_free(const shared_ptr<_Tp>*)
5419{
5420 return false;
5421}
5422
5423template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005424_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005425shared_ptr<_Tp>
5426atomic_load(const shared_ptr<_Tp>* __p)
5427{
5428 __sp_mut& __m = __get_sp_mut(__p);
5429 __m.lock();
5430 shared_ptr<_Tp> __q = *__p;
5431 __m.unlock();
5432 return __q;
5433}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005434
Howard Hinnant9fa30202012-07-30 01:40:57 +00005435template <class _Tp>
5436inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005437_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005438shared_ptr<_Tp>
5439atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5440{
5441 return atomic_load(__p);
5442}
5443
5444template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005445_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005446void
5447atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5448{
5449 __sp_mut& __m = __get_sp_mut(__p);
5450 __m.lock();
5451 __p->swap(__r);
5452 __m.unlock();
5453}
5454
5455template <class _Tp>
5456inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005457_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005458void
5459atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5460{
5461 atomic_store(__p, __r);
5462}
5463
5464template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005465_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005466shared_ptr<_Tp>
5467atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5468{
5469 __sp_mut& __m = __get_sp_mut(__p);
5470 __m.lock();
5471 __p->swap(__r);
5472 __m.unlock();
5473 return __r;
5474}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005475
Howard Hinnant9fa30202012-07-30 01:40:57 +00005476template <class _Tp>
5477inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005478_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005479shared_ptr<_Tp>
5480atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5481{
5482 return atomic_exchange(__p, __r);
5483}
5484
5485template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005486_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005487bool
5488atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5489{
Marshall Clow4201ee82016-05-18 17:50:13 +00005490 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005491 __sp_mut& __m = __get_sp_mut(__p);
5492 __m.lock();
5493 if (__p->__owner_equivalent(*__v))
5494 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005495 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005496 *__p = __w;
5497 __m.unlock();
5498 return true;
5499 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005500 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005501 *__v = *__p;
5502 __m.unlock();
5503 return false;
5504}
5505
5506template <class _Tp>
5507inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005508_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005509bool
5510atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5511{
5512 return atomic_compare_exchange_strong(__p, __v, __w);
5513}
5514
5515template <class _Tp>
5516inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005517_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005518bool
5519atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5520 shared_ptr<_Tp> __w, memory_order, memory_order)
5521{
5522 return atomic_compare_exchange_strong(__p, __v, __w);
5523}
5524
5525template <class _Tp>
5526inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005527_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005528bool
5529atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5530 shared_ptr<_Tp> __w, memory_order, memory_order)
5531{
5532 return atomic_compare_exchange_weak(__p, __v, __w);
5533}
5534
Eric Fiselier9b492672016-06-18 02:12:53 +00005535#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005536
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005537//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005538#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5539# ifndef _LIBCPP_CXX03_LANG
5540enum class pointer_safety : unsigned char {
5541 relaxed,
5542 preferred,
5543 strict
5544};
5545# endif
5546#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005547struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005548{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005549 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005550 {
5551 relaxed,
5552 preferred,
5553 strict
5554 };
5555
Howard Hinnant49e145e2012-10-30 19:06:59 +00005556 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005557
Howard Hinnant756c69b2010-09-22 16:48:34 +00005558 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005559 pointer_safety() : __v_() {}
5560
5561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005562 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005564 operator int() const {return __v_;}
5565};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005566#endif
5567
5568#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005569 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005570_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5571#else
5572// This function is only offered in C++03 under ABI v1.
5573# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5574inline _LIBCPP_INLINE_VISIBILITY
5575pointer_safety get_pointer_safety() _NOEXCEPT {
5576 return pointer_safety::relaxed;
5577}
5578# endif
5579#endif
5580
Howard Hinnantc51e1022010-05-11 19:42:16 +00005581
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005582_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5583_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5584_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005585_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005586
5587template <class _Tp>
5588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005589_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005590undeclare_reachable(_Tp* __p)
5591{
5592 return static_cast<_Tp*>(__undeclare_reachable(__p));
5593}
5594
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005595_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005596
Marshall Clow8982dcd2015-07-13 20:04:56 +00005597// --- Helper for container swap --
5598template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005599inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005600void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5601#if _LIBCPP_STD_VER >= 14
5602 _NOEXCEPT
5603#else
5604 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5605#endif
5606{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005607 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005608 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5609}
5610
5611template <typename _Alloc>
5612_LIBCPP_INLINE_VISIBILITY
5613void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5614#if _LIBCPP_STD_VER >= 14
5615 _NOEXCEPT
5616#else
5617 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5618#endif
5619{
5620 using _VSTD::swap;
5621 swap(__a1, __a2);
5622}
5623
5624template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005625inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005626void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5627
Marshall Clowff91de82015-08-18 19:51:37 +00005628template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005629struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005630 _Traits::propagate_on_container_move_assignment::value
5631#if _LIBCPP_STD_VER > 14
5632 || _Traits::is_always_equal::value
5633#else
5634 && is_nothrow_move_assignable<_Alloc>::value
5635#endif
5636 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005637
Marshall Clowa591b9a2016-07-11 21:38:08 +00005638
5639#ifndef _LIBCPP_HAS_NO_VARIADICS
5640template <class _Tp, class _Alloc>
5641struct __temp_value {
5642 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005643
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00005644 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
Marshall Clowa591b9a2016-07-11 21:38:08 +00005645 _Alloc &__a;
5646
5647 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5648 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005649
Marshall Clowa591b9a2016-07-11 21:38:08 +00005650 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005651 _LIBCPP_NO_CFI
5652 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5653 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5654 _VSTD::forward<_Args>(__args)...);
5655 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005656
Marshall Clowa591b9a2016-07-11 21:38:08 +00005657 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5658 };
5659#endif
5660
Marshall Clowe46031a2018-07-02 18:41:15 +00005661template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005662struct __is_allocator : false_type {};
5663
5664template<typename _Alloc>
5665struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005666 typename __void_t<typename _Alloc::value_type>::type,
5667 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5668 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005669 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005670
Howard Hinnantc51e1022010-05-11 19:42:16 +00005671_LIBCPP_END_NAMESPACE_STD
5672
Eric Fiselierf4433a32017-05-31 22:07:49 +00005673_LIBCPP_POP_MACROS
5674
Howard Hinnantc51e1022010-05-11 19:42:16 +00005675#endif // _LIBCPP_MEMORY