blob: b012f5bce32409bb6209e3a3a2b7c3c0c9042868 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000021inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +000022
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
Louis Dionne44e1ea82018-11-13 17:04:05 +000046 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +000047};
48
Eric Fiselier45c9aac2017-11-22 19:49:21 +000049template <class T> constexpr T* to_address(T* p) noexcept; // C++20
50template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
51
Howard Hinnantc51e1022010-05-11 19:42:16 +000052template <class Alloc>
53struct allocator_traits
54{
55 typedef Alloc allocator_type;
56 typedef typename allocator_type::value_type
57 value_type;
58
59 typedef Alloc::pointer | value_type* pointer;
60 typedef Alloc::const_pointer
61 | pointer_traits<pointer>::rebind<const value_type>
62 const_pointer;
63 typedef Alloc::void_pointer
64 | pointer_traits<pointer>::rebind<void>
65 void_pointer;
66 typedef Alloc::const_void_pointer
67 | pointer_traits<pointer>::rebind<const void>
68 const_void_pointer;
69 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000070 | pointer_traits<pointer>::difference_type
71 difference_type;
72 typedef Alloc::size_type
73 | make_unsigned<difference_type>::type
74 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075 typedef Alloc::propagate_on_container_copy_assignment
76 | false_type propagate_on_container_copy_assignment;
77 typedef Alloc::propagate_on_container_move_assignment
78 | false_type propagate_on_container_move_assignment;
79 typedef Alloc::propagate_on_container_swap
80 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000081 typedef Alloc::is_always_equal
82 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083
84 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
85 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
86
Marshall Clow0e58cae2017-11-26 02:55:38 +000087 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
88 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
Howard Hinnant719bda32011-05-28 14:41:13 +000090 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000091
92 template <class T, class... Args>
93 static void construct(allocator_type& a, T* p, Args&&... args);
94
95 template <class T>
96 static void destroy(allocator_type& a, T* p);
97
Marshall Clow4f834b52013-08-27 20:22:15 +000098 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000099
100 static allocator_type
101 select_on_container_copy_construction(const allocator_type& a);
102};
103
104template <>
105class allocator<void>
106{
107public:
108 typedef void* pointer;
109 typedef const void* const_pointer;
110 typedef void value_type;
111
112 template <class _Up> struct rebind {typedef allocator<_Up> other;};
113};
114
115template <class T>
116class allocator
117{
118public:
119 typedef size_t size_type;
120 typedef ptrdiff_t difference_type;
121 typedef T* pointer;
122 typedef const T* const_pointer;
123 typedef typename add_lvalue_reference<T>::type reference;
124 typedef typename add_lvalue_reference<const T>::type const_reference;
125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;};
128
Marshall Clowbc759762018-03-20 23:02:53 +0000129 constexpr allocator() noexcept; // constexpr in C++20
130 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
131 template <class U>
132 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 ~allocator();
134 pointer address(reference x) const noexcept;
135 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000137 void deallocate(pointer p, size_type n) noexcept;
138 size_type max_size() const noexcept;
139 template<class U, class... Args>
140 void construct(U* p, Args&&... args);
141 template <class U>
142 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143};
144
145template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000146bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
148template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000149bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150
151template <class OutputIterator, class T>
152class raw_storage_iterator
153 : public iterator<output_iterator_tag,
154 T, // purposefully not C++03
155 ptrdiff_t, // purposefully not C++03
156 T*, // purposefully not C++03
157 raw_storage_iterator&> // purposefully not C++03
158{
159public:
160 explicit raw_storage_iterator(OutputIterator x);
161 raw_storage_iterator& operator*();
162 raw_storage_iterator& operator=(const T& element);
163 raw_storage_iterator& operator++();
164 raw_storage_iterator operator++(int);
165};
166
Howard Hinnant719bda32011-05-28 14:41:13 +0000167template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
168template <class T> void return_temporary_buffer(T* p) noexcept;
169
170template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000171template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172
173template <class InputIterator, class ForwardIterator>
174ForwardIterator
175uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
176
Howard Hinnant719bda32011-05-28 14:41:13 +0000177template <class InputIterator, class Size, class ForwardIterator>
178ForwardIterator
179uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
180
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181template <class ForwardIterator, class T>
182void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
183
184template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000185ForwardIterator
186uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000188template <class T>
189void destroy_at(T* location);
190
191template <class ForwardIterator>
192 void destroy(ForwardIterator first, ForwardIterator last);
193
194template <class ForwardIterator, class Size>
195 ForwardIterator destroy_n(ForwardIterator first, Size n);
196
197template <class InputIterator, class ForwardIterator>
198 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
199
200template <class InputIterator, class Size, class ForwardIterator>
201 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
202
203template <class ForwardIterator>
204 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
205
206template <class ForwardIterator, class Size>
207 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
208
209template <class ForwardIterator>
210 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
214
Louis Dionne481a2662018-09-23 18:35:00 +0000215template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
217template<class X>
Louis Dionne481a2662018-09-23 18:35:00 +0000218class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219{
220public:
221 typedef X element_type;
222
223 explicit auto_ptr(X* p =0) throw();
224 auto_ptr(auto_ptr&) throw();
225 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
226 auto_ptr& operator=(auto_ptr&) throw();
227 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
228 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
229 ~auto_ptr() throw();
230
231 typename add_lvalue_reference<X>::type operator*() const throw();
232 X* operator->() const throw();
233 X* get() const throw();
234 X* release() throw();
235 void reset(X* p =0) throw();
236
237 auto_ptr(auto_ptr_ref<X>) throw();
238 template<class Y> operator auto_ptr_ref<Y>() throw();
239 template<class Y> operator auto_ptr<Y>() throw();
240};
241
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000242template <class T>
243struct default_delete
244{
Howard Hinnant719bda32011-05-28 14:41:13 +0000245 constexpr default_delete() noexcept = default;
246 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000247
Howard Hinnant719bda32011-05-28 14:41:13 +0000248 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000249};
250
251template <class T>
252struct default_delete<T[]>
253{
Howard Hinnant719bda32011-05-28 14:41:13 +0000254 constexpr default_delete() noexcept = default;
255 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000256 template <class U> void operator()(U*) const = delete;
257};
258
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000259template <class T, class D = default_delete<T>>
260class unique_ptr
261{
262public:
263 typedef see below pointer;
264 typedef T element_type;
265 typedef D deleter_type;
266
267 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000268 constexpr unique_ptr() noexcept;
269 explicit unique_ptr(pointer p) noexcept;
270 unique_ptr(pointer p, see below d1) noexcept;
271 unique_ptr(pointer p, see below d2) noexcept;
272 unique_ptr(unique_ptr&& u) noexcept;
273 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000275 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000276 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000277 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000278
279 // destructor
280 ~unique_ptr();
281
282 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000283 unique_ptr& operator=(unique_ptr&& u) noexcept;
284 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
285 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000286
287 // observers
288 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000289 pointer operator->() const noexcept;
290 pointer get() const noexcept;
291 deleter_type& get_deleter() noexcept;
292 const deleter_type& get_deleter() const noexcept;
293 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000294
295 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000296 pointer release() noexcept;
297 void reset(pointer p = pointer()) noexcept;
298 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000299};
300
301template <class T, class D>
302class unique_ptr<T[], D>
303{
304public:
305 typedef implementation-defined pointer;
306 typedef T element_type;
307 typedef D deleter_type;
308
309 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000310 constexpr unique_ptr() noexcept;
311 explicit unique_ptr(pointer p) noexcept;
312 unique_ptr(pointer p, see below d) noexcept;
313 unique_ptr(pointer p, see below d) noexcept;
314 unique_ptr(unique_ptr&& u) noexcept;
315 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000316
317 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000318 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000321 unique_ptr& operator=(unique_ptr&& u) noexcept;
322 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000323
324 // observers
325 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000326 pointer get() const noexcept;
327 deleter_type& get_deleter() noexcept;
328 const deleter_type& get_deleter() const noexcept;
329 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330
331 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 pointer release() noexcept;
333 void reset(pointer p = pointer()) noexcept;
334 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000335 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337};
338
339template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000340 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000341
342template <class T1, class D1, class T2, class D2>
343 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354
Howard Hinnant719bda32011-05-28 14:41:13 +0000355template <class T, class D>
356 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359template <class T, class D>
360 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
361template <class T, class D>
362 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
363
364template <class T, class D>
365 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
376template <class T, class D>
377 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
378template <class T, class D>
379 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
380
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381class bad_weak_ptr
382 : public std::exception
383{
Howard Hinnant719bda32011-05-28 14:41:13 +0000384 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000385};
386
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000387template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
388template<class T> unique_ptr<T> make_unique(size_t n); // C++14
389template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
390
Marshall Clowe52a3242017-11-27 15:51:36 +0000391template<class E, class T, class Y, class D>
392 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
393
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000394template<class T>
395class shared_ptr
396{
397public:
398 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000399 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000400
401 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000402 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000403 template<class Y> explicit shared_ptr(Y* p);
404 template<class Y, class D> shared_ptr(Y* p, D d);
405 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
406 template <class D> shared_ptr(nullptr_t p, D d);
407 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000408 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
409 shared_ptr(const shared_ptr& r) noexcept;
410 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
411 shared_ptr(shared_ptr&& r) noexcept;
412 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000413 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000414 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000415 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
416 shared_ptr(nullptr_t) : shared_ptr() { }
417
418 // destructor:
419 ~shared_ptr();
420
421 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000422 shared_ptr& operator=(const shared_ptr& r) noexcept;
423 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
424 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000426 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000427 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
428
429 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000430 void swap(shared_ptr& r) noexcept;
431 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000432 template<class Y> void reset(Y* p);
433 template<class Y, class D> void reset(Y* p, D d);
434 template<class Y, class D, class A> void reset(Y* p, D d, A a);
435
Howard Hinnant719bda32011-05-28 14:41:13 +0000436 // observers:
437 T* get() const noexcept;
438 T& operator*() const noexcept;
439 T* operator->() const noexcept;
440 long use_count() const noexcept;
441 bool unique() const noexcept;
442 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000443 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
444 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445};
446
447// shared_ptr comparisons:
448template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000449 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000451 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000453 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000457 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000459 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
460
461template <class T>
462 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
463template <class T>
464 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
465template <class T>
466 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
467template <class T>
468 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
469template <class T>
470 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
471template <class T>
472bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
473template <class T>
474 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485
486// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000487template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000488
489// shared_ptr casts:
490template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000491 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000492template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000493 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000494template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000495 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497// shared_ptr I/O:
498template<class E, class T, class Y>
499 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
500
501// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000502template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000503
504template<class T, class... Args>
505 shared_ptr<T> make_shared(Args&&... args);
506template<class T, class A, class... Args>
507 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
508
509template<class T>
510class weak_ptr
511{
512public:
513 typedef T element_type;
514
515 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000516 constexpr weak_ptr() noexcept;
517 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
518 weak_ptr(weak_ptr const& r) noexcept;
519 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000520 weak_ptr(weak_ptr&& r) noexcept; // C++14
521 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000522
523 // destructor
524 ~weak_ptr();
525
526 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 weak_ptr& operator=(weak_ptr const& r) noexcept;
528 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
529 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000530 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
531 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000532
533 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000534 void swap(weak_ptr& r) noexcept;
535 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536
537 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000538 long use_count() const noexcept;
539 bool expired() const noexcept;
540 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000541 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
542 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543};
544
545// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000546template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000547
548// class owner_less:
549template<class T> struct owner_less;
550
551template<class T>
552struct owner_less<shared_ptr<T>>
553 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
554{
555 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000556 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
557 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
558 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000559};
560
561template<class T>
562struct owner_less<weak_ptr<T>>
563 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
564{
565 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000566 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
567 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
568 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
569};
570
571template <> // Added in C++14
572struct owner_less<void>
573{
574 template <class _Tp, class _Up>
575 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
576 template <class _Tp, class _Up>
577 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
578 template <class _Tp, class _Up>
579 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
580 template <class _Tp, class _Up>
581 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
582
583 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000584};
585
586template<class T>
587class enable_shared_from_this
588{
589protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000590 constexpr enable_shared_from_this() noexcept;
591 enable_shared_from_this(enable_shared_from_this const&) noexcept;
592 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000593 ~enable_shared_from_this();
594public:
595 shared_ptr<T> shared_from_this();
596 shared_ptr<T const> shared_from_this() const;
597};
598
599template<class T>
600 bool atomic_is_lock_free(const shared_ptr<T>* p);
601template<class T>
602 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
603template<class T>
604 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
605template<class T>
606 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
607template<class T>
608 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
609template<class T>
610 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
611template<class T>
612 shared_ptr<T>
613 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
614template<class T>
615 bool
616 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
617template<class T>
618 bool
619 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
620template<class T>
621 bool
622 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
623 shared_ptr<T> w, memory_order success,
624 memory_order failure);
625template<class T>
626 bool
627 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
628 shared_ptr<T> w, memory_order success,
629 memory_order failure);
630// Hash support
631template <class T> struct hash;
632template <class T, class D> struct hash<unique_ptr<T, D> >;
633template <class T> struct hash<shared_ptr<T> >;
634
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000635template <class T, class Alloc>
636 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
637
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000638// Pointer safety
639enum class pointer_safety { relaxed, preferred, strict };
640void declare_reachable(void *p);
641template <class T> T *undeclare_reachable(T *p);
642void declare_no_pointers(char *p, size_t n);
643void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000644pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000645
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
647
648} // std
649
650*/
651
652#include <__config>
653#include <type_traits>
654#include <typeinfo>
655#include <cstddef>
656#include <cstdint>
657#include <new>
658#include <utility>
659#include <limits>
660#include <iterator>
661#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000662#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000663#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000664#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000665#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000666#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000667#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000668# include <atomic>
669#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000670#include <version>
Howard Hinnant9fa30202012-07-30 01:40:57 +0000671
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000672#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000674#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675
Eric Fiselierf4433a32017-05-31 22:07:49 +0000676_LIBCPP_PUSH_MACROS
677#include <__undef_macros>
678
679
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680_LIBCPP_BEGIN_NAMESPACE_STD
681
Eric Fiselier89659d12015-07-07 00:27:16 +0000682template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000683inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +0000684_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
685#if !defined(_LIBCPP_HAS_NO_THREADS) && \
686 defined(__ATOMIC_RELAXED) && \
687 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
688 return __atomic_load_n(__value, __ATOMIC_RELAXED);
689#else
690 return *__value;
691#endif
692}
693
Kuba Breckade9d6792016-09-04 09:55:12 +0000694template <class _ValueType>
Louis Dionne16fe2952018-07-11 23:14:33 +0000695inline _LIBCPP_INLINE_VISIBILITY
Kuba Breckade9d6792016-09-04 09:55:12 +0000696_ValueType __libcpp_acquire_load(_ValueType const* __value) {
697#if !defined(_LIBCPP_HAS_NO_THREADS) && \
698 defined(__ATOMIC_ACQUIRE) && \
699 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
700 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
701#else
702 return *__value;
703#endif
704}
705
Marshall Clow78dbe462016-11-14 18:22:19 +0000706// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000707
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708template <class _Tp> class allocator;
709
710template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000711class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712{
713public:
714 typedef void* pointer;
715 typedef const void* const_pointer;
716 typedef void value_type;
717
718 template <class _Up> struct rebind {typedef allocator<_Up> other;};
719};
720
Howard Hinnant9f771052012-01-19 23:15:22 +0000721template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000722class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000723{
724public:
725 typedef const void* pointer;
726 typedef const void* const_pointer;
727 typedef const void value_type;
728
729 template <class _Up> struct rebind {typedef allocator<_Up> other;};
730};
731
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732// pointer_traits
733
Marshall Clow0be70c32017-06-14 21:23:57 +0000734template <class _Tp, class = void>
735struct __has_element_type : false_type {};
736
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000738struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000739 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740
741template <class _Ptr, bool = __has_element_type<_Ptr>::value>
742struct __pointer_traits_element_type;
743
744template <class _Ptr>
745struct __pointer_traits_element_type<_Ptr, true>
746{
747 typedef typename _Ptr::element_type type;
748};
749
750#ifndef _LIBCPP_HAS_NO_VARIADICS
751
752template <template <class, class...> class _Sp, class _Tp, class ..._Args>
753struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
754{
755 typedef typename _Sp<_Tp, _Args...>::element_type type;
756};
757
758template <template <class, class...> class _Sp, class _Tp, class ..._Args>
759struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
760{
761 typedef _Tp type;
762};
763
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000764#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765
766template <template <class> class _Sp, class _Tp>
767struct __pointer_traits_element_type<_Sp<_Tp>, true>
768{
769 typedef typename _Sp<_Tp>::element_type type;
770};
771
772template <template <class> class _Sp, class _Tp>
773struct __pointer_traits_element_type<_Sp<_Tp>, false>
774{
775 typedef _Tp type;
776};
777
778template <template <class, class> class _Sp, class _Tp, class _A0>
779struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
780{
781 typedef typename _Sp<_Tp, _A0>::element_type type;
782};
783
784template <template <class, class> class _Sp, class _Tp, class _A0>
785struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
786{
787 typedef _Tp type;
788};
789
790template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
791struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
792{
793 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
794};
795
796template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
797struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
798{
799 typedef _Tp type;
800};
801
802template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
803 class _A1, class _A2>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
805{
806 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
807};
808
809template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
810 class _A1, class _A2>
811struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
812{
813 typedef _Tp type;
814};
815
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000816#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817
Marshall Clow0be70c32017-06-14 21:23:57 +0000818template <class _Tp, class = void>
819struct __has_difference_type : false_type {};
820
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000822struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000823 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
825template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
826struct __pointer_traits_difference_type
827{
828 typedef ptrdiff_t type;
829};
830
831template <class _Ptr>
832struct __pointer_traits_difference_type<_Ptr, true>
833{
834 typedef typename _Ptr::difference_type type;
835};
836
837template <class _Tp, class _Up>
838struct __has_rebind
839{
840private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000841 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 template <class _Xp> static __two __test(...);
843 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
844public:
845 static const bool value = sizeof(__test<_Tp>(0)) == 1;
846};
847
848template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
849struct __pointer_traits_rebind
850{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000851#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852 typedef typename _Tp::template rebind<_Up> type;
853#else
854 typedef typename _Tp::template rebind<_Up>::other type;
855#endif
856};
857
858#ifndef _LIBCPP_HAS_NO_VARIADICS
859
860template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
861struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
862{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000863#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
865#else
866 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
867#endif
868};
869
870template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
871struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
872{
873 typedef _Sp<_Up, _Args...> type;
874};
875
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000876#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877
878template <template <class> class _Sp, class _Tp, class _Up>
879struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
880{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000881#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882 typedef typename _Sp<_Tp>::template rebind<_Up> type;
883#else
884 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
885#endif
886};
887
888template <template <class> class _Sp, class _Tp, class _Up>
889struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
890{
891 typedef _Sp<_Up> type;
892};
893
894template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
895struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
896{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000897#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
899#else
900 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
901#endif
902};
903
904template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
905struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
906{
907 typedef _Sp<_Up, _A0> type;
908};
909
910template <template <class, class, class> class _Sp, class _Tp, class _A0,
911 class _A1, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
913{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000914#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
916#else
917 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
918#endif
919};
920
921template <template <class, class, class> class _Sp, class _Tp, class _A0,
922 class _A1, class _Up>
923struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
924{
925 typedef _Sp<_Up, _A0, _A1> type;
926};
927
928template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
929 class _A1, class _A2, class _Up>
930struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
931{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000932#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
934#else
935 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
936#endif
937};
938
939template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
940 class _A1, class _A2, class _Up>
941struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
942{
943 typedef _Sp<_Up, _A0, _A1, _A2> type;
944};
945
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000946#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947
948template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000949struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950{
951 typedef _Ptr pointer;
952 typedef typename __pointer_traits_element_type<pointer>::type element_type;
953 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
954
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000956 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957#else
958 template <class _Up> struct rebind
959 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000960#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
962private:
963 struct __nat {};
964public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 static pointer pointer_to(typename conditional<is_void<element_type>::value,
967 __nat, element_type>::type& __r)
968 {return pointer::pointer_to(__r);}
969};
970
971template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000972struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973{
974 typedef _Tp* pointer;
975 typedef _Tp element_type;
976 typedef ptrdiff_t difference_type;
977
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000978#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 template <class _Up> using rebind = _Up*;
980#else
981 template <class _Up> struct rebind {typedef _Up* other;};
982#endif
983
984private:
985 struct __nat {};
986public:
Louis Dionne44e1ea82018-11-13 17:04:05 +0000987 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000989 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000990 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991};
992
Eric Fiselierae3ab842015-08-23 02:56:05 +0000993template <class _From, class _To>
994struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000995#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000996 typedef typename pointer_traits<_From>::template rebind<_To> type;
997#else
998 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
999#endif
1000};
1001
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002// allocator_traits
1003
Marshall Clow0be70c32017-06-14 21:23:57 +00001004template <class _Tp, class = void>
1005struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006
1007template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001008struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001009 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
Marshall Clow0be70c32017-06-14 21:23:57 +00001034template <class _Tp, class = void>
1035struct __has_const_pointer : false_type {};
1036
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001038struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001039 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040
1041template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1042struct __const_pointer
1043{
1044 typedef typename _Alloc::const_pointer type;
1045};
1046
1047template <class _Tp, class _Ptr, class _Alloc>
1048struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1049{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001050#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1052#else
1053 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1054#endif
1055};
1056
Marshall Clow0be70c32017-06-14 21:23:57 +00001057template <class _Tp, class = void>
1058struct __has_void_pointer : false_type {};
1059
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001061struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001062 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063
1064template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1065struct __void_pointer
1066{
1067 typedef typename _Alloc::void_pointer type;
1068};
1069
1070template <class _Ptr, class _Alloc>
1071struct __void_pointer<_Ptr, _Alloc, false>
1072{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001073#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1075#else
1076 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1077#endif
1078};
1079
Marshall Clow0be70c32017-06-14 21:23:57 +00001080template <class _Tp, class = void>
1081struct __has_const_void_pointer : false_type {};
1082
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001084struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001085 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086
1087template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1088struct __const_void_pointer
1089{
1090 typedef typename _Alloc::const_void_pointer type;
1091};
1092
1093template <class _Ptr, class _Alloc>
1094struct __const_void_pointer<_Ptr, _Alloc, false>
1095{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001096#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1098#else
1099 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1100#endif
1101};
1102
Howard Hinnantc834c512011-11-29 18:15:50 +00001103template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001104inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001105_Tp*
1106__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107{
1108 return __p;
1109}
1110
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001111#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112template <class _Pointer>
1113inline _LIBCPP_INLINE_VISIBILITY
1114typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001115__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001117 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001119#else
1120template <class _Pointer>
1121inline _LIBCPP_INLINE_VISIBILITY
1122auto
1123__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1124-> decltype(pointer_traits<_Pointer>::to_address(__p))
1125{
1126 return pointer_traits<_Pointer>::to_address(__p);
1127}
1128
1129template <class _Pointer, class... _None>
1130inline _LIBCPP_INLINE_VISIBILITY
1131auto
1132__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1133{
1134 return _VSTD::__to_raw_pointer(__p.operator->());
1135}
1136
1137template <class _Tp>
1138inline _LIBCPP_INLINE_VISIBILITY constexpr
1139_Tp*
1140to_address(_Tp* __p) _NOEXCEPT
1141{
1142 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1143 return __p;
1144}
1145
1146template <class _Pointer>
1147inline _LIBCPP_INLINE_VISIBILITY
1148auto
1149to_address(const _Pointer& __p) _NOEXCEPT
1150{
1151 return _VSTD::__to_raw_pointer(__p);
1152}
1153#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154
Marshall Clow0be70c32017-06-14 21:23:57 +00001155template <class _Tp, class = void>
1156struct __has_size_type : false_type {};
1157
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001159struct __has_size_type<_Tp,
1160 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001162template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163struct __size_type
1164{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001165 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166};
1167
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001168template <class _Alloc, class _DiffType>
1169struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170{
1171 typedef typename _Alloc::size_type type;
1172};
1173
Marshall Clow0be70c32017-06-14 21:23:57 +00001174template <class _Tp, class = void>
1175struct __has_propagate_on_container_copy_assignment : false_type {};
1176
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001178struct __has_propagate_on_container_copy_assignment<_Tp,
1179 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1180 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181
1182template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1183struct __propagate_on_container_copy_assignment
1184{
1185 typedef false_type type;
1186};
1187
1188template <class _Alloc>
1189struct __propagate_on_container_copy_assignment<_Alloc, true>
1190{
1191 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1192};
1193
Marshall Clow0be70c32017-06-14 21:23:57 +00001194template <class _Tp, class = void>
1195struct __has_propagate_on_container_move_assignment : false_type {};
1196
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001198struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001199 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1200 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201
1202template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1203struct __propagate_on_container_move_assignment
1204{
1205 typedef false_type type;
1206};
1207
1208template <class _Alloc>
1209struct __propagate_on_container_move_assignment<_Alloc, true>
1210{
1211 typedef typename _Alloc::propagate_on_container_move_assignment type;
1212};
1213
Marshall Clow0be70c32017-06-14 21:23:57 +00001214template <class _Tp, class = void>
1215struct __has_propagate_on_container_swap : false_type {};
1216
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001218struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001219 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1220 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
1222template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1223struct __propagate_on_container_swap
1224{
1225 typedef false_type type;
1226};
1227
1228template <class _Alloc>
1229struct __propagate_on_container_swap<_Alloc, true>
1230{
1231 typedef typename _Alloc::propagate_on_container_swap type;
1232};
1233
Marshall Clow0be70c32017-06-14 21:23:57 +00001234template <class _Tp, class = void>
1235struct __has_is_always_equal : false_type {};
1236
Marshall Clow0b587562015-06-02 16:34:03 +00001237template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001238struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001239 typename __void_t<typename _Tp::is_always_equal>::type>
1240 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001241
1242template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1243struct __is_always_equal
1244{
1245 typedef typename _VSTD::is_empty<_Alloc>::type type;
1246};
1247
1248template <class _Alloc>
1249struct __is_always_equal<_Alloc, true>
1250{
1251 typedef typename _Alloc::is_always_equal type;
1252};
1253
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1255struct __has_rebind_other
1256{
1257private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001258 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 template <class _Xp> static __two __test(...);
1260 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1261public:
1262 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1263};
1264
1265template <class _Tp, class _Up>
1266struct __has_rebind_other<_Tp, _Up, false>
1267{
1268 static const bool value = false;
1269};
1270
1271template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1272struct __allocator_traits_rebind
1273{
1274 typedef typename _Tp::template rebind<_Up>::other type;
1275};
1276
1277#ifndef _LIBCPP_HAS_NO_VARIADICS
1278
1279template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1280struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1281{
1282 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1283};
1284
1285template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1287{
1288 typedef _Alloc<_Up, _Args...> type;
1289};
1290
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001291#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292
1293template <template <class> class _Alloc, class _Tp, class _Up>
1294struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1295{
1296 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1297};
1298
1299template <template <class> class _Alloc, class _Tp, class _Up>
1300struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1301{
1302 typedef _Alloc<_Up> type;
1303};
1304
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1306struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1307{
1308 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1309};
1310
1311template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1312struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1313{
1314 typedef _Alloc<_Up, _A0> type;
1315};
1316
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1318 class _A1, class _Up>
1319struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1320{
1321 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1322};
1323
1324template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1325 class _A1, class _Up>
1326struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1327{
1328 typedef _Alloc<_Up, _A0, _A1> type;
1329};
1330
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1332 class _A1, class _A2, class _Up>
1333struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1334{
1335 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1336};
1337
1338template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1339 class _A1, class _A2, class _Up>
1340struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1341{
1342 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1343};
1344
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001345#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001347#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1350auto
1351__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001352 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353
1354template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1355auto
1356__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1357 -> false_type;
1358
1359template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1360struct __has_allocate_hint
1361 : integral_constant<bool,
1362 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001363 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 declval<_SizeType>(),
1365 declval<_ConstVoidPtr>())),
1366 true_type>::value>
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
1372template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1373struct __has_allocate_hint
1374 : true_type
1375{
1376};
1377
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001378#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001380#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381
1382template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001383decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1384 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 true_type())
1386__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1387
1388template <class _Alloc, class _Pointer, class ..._Args>
1389false_type
1390__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1391
1392template <class _Alloc, class _Pointer, class ..._Args>
1393struct __has_construct
1394 : integral_constant<bool,
1395 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001396 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 declval<_Pointer>(),
1398 declval<_Args>()...)),
1399 true_type>::value>
1400{
1401};
1402
1403template <class _Alloc, class _Pointer>
1404auto
1405__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1406 -> decltype(__a.destroy(__p), true_type());
1407
1408template <class _Alloc, class _Pointer>
1409auto
1410__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1411 -> false_type;
1412
1413template <class _Alloc, class _Pointer>
1414struct __has_destroy
1415 : integral_constant<bool,
1416 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001417 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 declval<_Pointer>())),
1419 true_type>::value>
1420{
1421};
1422
1423template <class _Alloc>
1424auto
1425__has_max_size_test(_Alloc&& __a)
1426 -> decltype(__a.max_size(), true_type());
1427
1428template <class _Alloc>
1429auto
1430__has_max_size_test(const volatile _Alloc& __a)
1431 -> false_type;
1432
1433template <class _Alloc>
1434struct __has_max_size
1435 : integral_constant<bool,
1436 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001437 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 true_type>::value>
1439{
1440};
1441
1442template <class _Alloc>
1443auto
1444__has_select_on_container_copy_construction_test(_Alloc&& __a)
1445 -> decltype(__a.select_on_container_copy_construction(), true_type());
1446
1447template <class _Alloc>
1448auto
1449__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1450 -> false_type;
1451
1452template <class _Alloc>
1453struct __has_select_on_container_copy_construction
1454 : integral_constant<bool,
1455 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001456 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 true_type>::value>
1458{
1459};
1460
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001461#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001463template <class _Alloc, class _Pointer, class _Tp, class = void>
1464struct __has_construct : std::false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001466template <class _Alloc, class _Pointer, class _Tp>
1467struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1468 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1469>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001471template <class _Alloc, class _Pointer, class = void>
1472struct __has_destroy : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473
1474template <class _Alloc, class _Pointer>
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001475struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1476 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1477>::type> : std::true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Marshall Clow0e58cae2017-11-26 02:55:38 +00001539 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001542 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001544 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001566 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001568 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1569 __a, __p, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 }
1571 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001572 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001573 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 const _A1& __a1)
1575 {
1576 ::new ((void*)__p) _Tp(__a0, __a1);
1577 }
1578 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001579 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001580 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 const _A1& __a1, const _A2& __a2)
1582 {
1583 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1584 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001585#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
1587 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 static void destroy(allocator_type& __a, _Tp* __p)
1590 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1591
Howard Hinnant756c69b2010-09-22 16:48:34 +00001592 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001593 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1595
Howard Hinnant756c69b2010-09-22 16:48:34 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 static allocator_type
1598 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001599 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 __has_select_on_container_copy_construction<const allocator_type>(),
1601 __a);}
1602
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001603 template <class _Ptr>
1604 _LIBCPP_INLINE_VISIBILITY
1605 static
1606 void
1607 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1608 {
Louis Dionne301a6772018-12-07 16:42:28 +00001609 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001610 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1611 }
1612
1613 template <class _Tp>
1614 _LIBCPP_INLINE_VISIBILITY
1615 static
1616 typename enable_if
1617 <
1618 (is_same<allocator_type, allocator<_Tp> >::value
1619 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1620 is_trivially_move_constructible<_Tp>::value,
1621 void
1622 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001623 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001624 {
1625 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001626 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001627 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001628 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001629 __begin2 += _Np;
1630 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001631 }
1632
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001633 template <class _Iter, class _Ptr>
1634 _LIBCPP_INLINE_VISIBILITY
1635 static
1636 void
1637 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1638 {
1639 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1640 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1641 }
1642
1643 template <class _Tp>
1644 _LIBCPP_INLINE_VISIBILITY
1645 static
1646 typename enable_if
1647 <
1648 (is_same<allocator_type, allocator<_Tp> >::value
1649 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1650 is_trivially_move_constructible<_Tp>::value,
1651 void
1652 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001653 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001654 {
1655 typedef typename remove_const<_Tp>::type _Vp;
1656 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001657 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001658 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001659 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001660 __begin2 += _Np;
1661 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001662 }
1663
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001664 template <class _Ptr>
1665 _LIBCPP_INLINE_VISIBILITY
1666 static
1667 void
1668 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1669 {
1670 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001671 {
1672 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1673 --__end2;
1674 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001675 }
1676
1677 template <class _Tp>
1678 _LIBCPP_INLINE_VISIBILITY
1679 static
1680 typename enable_if
1681 <
1682 (is_same<allocator_type, allocator<_Tp> >::value
1683 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1684 is_trivially_move_constructible<_Tp>::value,
1685 void
1686 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001687 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001688 {
1689 ptrdiff_t _Np = __end1 - __begin1;
1690 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001691 if (_Np > 0)
1692 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001693 }
1694
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695private:
1696
Howard Hinnant756c69b2010-09-22 16:48:34 +00001697 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001698 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699 const_void_pointer __hint, true_type)
1700 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001701 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001702 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001703 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704 {return __a.allocate(__n);}
1705
1706#ifndef _LIBCPP_HAS_NO_VARIADICS
1707 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001710 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1714 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001715 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 }
Volodymyr Sapsai2b1eb842018-12-19 20:08:43 +00001717#else // _LIBCPP_HAS_NO_VARIADICS
1718 template <class _Tp, class _A0>
1719 _LIBCPP_INLINE_VISIBILITY
1720 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1721 const _A0& __a0)
1722 {__a.construct(__p, __a0);}
1723 template <class _Tp, class _A0>
1724 _LIBCPP_INLINE_VISIBILITY
1725 static void __construct(false_type, allocator_type&, _Tp* __p,
1726 const _A0& __a0)
1727 {
1728 ::new ((void*)__p) _Tp(__a0);
1729 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001730#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731
1732 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1735 {__a.destroy(__p);}
1736 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 static void __destroy(false_type, allocator_type&, _Tp* __p)
1739 {
1740 __p->~_Tp();
1741 }
1742
Howard Hinnant756c69b2010-09-22 16:48:34 +00001743 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001744 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001746 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001747 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001748 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749
Howard Hinnant756c69b2010-09-22 16:48:34 +00001750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001752 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001756 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 {return __a;}
1758};
1759
Marshall Clow940e01c2015-04-07 05:21:38 +00001760template <class _Traits, class _Tp>
1761struct __rebind_alloc_helper
1762{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001763#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001764 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001765#else
1766 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1767#endif
1768};
1769
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770// allocator
1771
1772template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001773class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774{
1775public:
1776 typedef size_t size_type;
1777 typedef ptrdiff_t difference_type;
1778 typedef _Tp* pointer;
1779 typedef const _Tp* const_pointer;
1780 typedef _Tp& reference;
1781 typedef const _Tp& const_reference;
1782 typedef _Tp value_type;
1783
Howard Hinnant4931e092011-06-02 21:38:57 +00001784 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001785 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001786
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1788
Marshall Clowbc759762018-03-20 23:02:53 +00001789 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1790 allocator() _NOEXCEPT {}
1791
Louis Dionne481a2662018-09-23 18:35:00 +00001792 template <class _Up>
Marshall Clowbc759762018-03-20 23:02:53 +00001793 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1794 allocator(const allocator<_Up>&) _NOEXCEPT {}
1795
Howard Hinnant719bda32011-05-28 14:41:13 +00001796 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001797 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001798 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001799 {return _VSTD::addressof(__x);}
Louis Dionne481a2662018-09-23 18:35:00 +00001800 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0e58cae2017-11-26 02:55:38 +00001801 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001802 {
1803 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001804 __throw_length_error("allocator<T>::allocate(size_t n)"
1805 " 'n' exceeds maximum supported size");
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00001806 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001807 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001808 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00001809 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), __alignof(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001810 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1811 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001812#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 template <class _Up, class... _Args>
1814 _LIBCPP_INLINE_VISIBILITY
1815 void
1816 construct(_Up* __p, _Args&&... __args)
1817 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001818 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001820#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 _LIBCPP_INLINE_VISIBILITY
1822 void
1823 construct(pointer __p)
1824 {
1825 ::new((void*)__p) _Tp();
1826 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001827# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001828
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 template <class _A0>
1830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001831 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 construct(pointer __p, _A0& __a0)
1833 {
1834 ::new((void*)__p) _Tp(__a0);
1835 }
1836 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, const _A0& __a0)
1840 {
1841 ::new((void*)__p) _Tp(__a0);
1842 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001843# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844 template <class _A0, class _A1>
1845 _LIBCPP_INLINE_VISIBILITY
1846 void
1847 construct(pointer __p, _A0& __a0, _A1& __a1)
1848 {
1849 ::new((void*)__p) _Tp(__a0, __a1);
1850 }
1851 template <class _A0, class _A1>
1852 _LIBCPP_INLINE_VISIBILITY
1853 void
1854 construct(pointer __p, const _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, _A0& __a0, const _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, const _A0& __a0, const _A1& __a1)
1869 {
1870 ::new((void*)__p) _Tp(__a0, __a1);
1871 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001872#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1874};
1875
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001876template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001877class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001878{
1879public:
1880 typedef size_t size_type;
1881 typedef ptrdiff_t difference_type;
1882 typedef const _Tp* pointer;
1883 typedef const _Tp* const_pointer;
1884 typedef const _Tp& reference;
1885 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001886 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001887
1888 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001889 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001890
1891 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1892
Marshall Clowbc759762018-03-20 23:02:53 +00001893 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1894 allocator() _NOEXCEPT {}
1895
1896 template <class _Up>
Louis Dionne481a2662018-09-23 18:35:00 +00001897 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowbc759762018-03-20 23:02:53 +00001898 allocator(const allocator<_Up>&) _NOEXCEPT {}
1899
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001900 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1901 {return _VSTD::addressof(__x);}
1902 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001903 {
1904 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001905 __throw_length_error("allocator<const T>::allocate(size_t n)"
1906 " 'n' exceeds maximum supported size");
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00001907 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001908 }
Eric Fiselier2856ef82018-10-25 17:21:30 +00001909 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00001910 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), __alignof(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001911 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1912 {return size_type(~0) / sizeof(_Tp);}
1913#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1914 template <class _Up, class... _Args>
1915 _LIBCPP_INLINE_VISIBILITY
1916 void
1917 construct(_Up* __p, _Args&&... __args)
1918 {
1919 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1920 }
1921#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1922 _LIBCPP_INLINE_VISIBILITY
1923 void
1924 construct(pointer __p)
1925 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001926 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001927 }
1928# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001929
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001930 template <class _A0>
1931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001932 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001933 construct(pointer __p, _A0& __a0)
1934 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001935 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001936 }
1937 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, const _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 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001944# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1945 template <class _A0, class _A1>
1946 _LIBCPP_INLINE_VISIBILITY
1947 void
1948 construct(pointer __p, _A0& __a0, _A1& __a1)
1949 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001950 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001951 }
1952 template <class _A0, class _A1>
1953 _LIBCPP_INLINE_VISIBILITY
1954 void
1955 construct(pointer __p, const _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, _A0& __a0, const _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, const _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#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1974 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1975};
1976
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977template <class _Tp, class _Up>
1978inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001979bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980
1981template <class _Tp, class _Up>
1982inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001983bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984
1985template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001986class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 : public iterator<output_iterator_tag,
1988 _Tp, // purposefully not C++03
1989 ptrdiff_t, // purposefully not C++03
1990 _Tp*, // purposefully not C++03
1991 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1992{
1993private:
1994 _OutputIterator __x_;
1995public:
1996 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1997 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1998 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00001999 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002000#if _LIBCPP_STD_VER >= 14
2001 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowbdfdea82018-08-28 13:29:30 +00002002 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00002003#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2005 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2006 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002007#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002008 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002009#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010};
2011
2012template <class _Tp>
Roman Lebedevb5959fa2018-09-22 17:54:48 +00002013_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002015get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016{
2017 pair<_Tp*, ptrdiff_t> __r(0, 0);
2018 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2019 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2020 / sizeof(_Tp);
2021 if (__n > __m)
2022 __n = __m;
2023 while (__n > 0)
2024 {
Eric Fiselier6a07b342018-10-26 17:12:32 +00002025#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00002026 if (__is_overaligned_for_new(__alignof(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002027 {
2028 std::align_val_t __al =
2029 std::align_val_t(std::alignment_of<_Tp>::value);
2030 __r.first = static_cast<_Tp*>(::operator new(
2031 __n * sizeof(_Tp), __al, nothrow));
2032 } else {
2033 __r.first = static_cast<_Tp*>(::operator new(
2034 __n * sizeof(_Tp), nothrow));
2035 }
2036#else
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00002037 if (__is_overaligned_for_new(__alignof(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002038 {
2039 // Since aligned operator new is unavailable, return an empty
2040 // buffer rather than one with invalid alignment.
2041 return __r;
2042 }
2043
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002045#endif
2046
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047 if (__r.first)
2048 {
2049 __r.second = __n;
2050 break;
2051 }
2052 __n /= 2;
2053 }
2054 return __r;
2055}
2056
2057template <class _Tp>
2058inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002059void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2060{
Eric Fiselierc7c7ab82018-11-28 22:24:19 +00002061 _VSTD::__libcpp_deallocate_unsized((void*)__p, __alignof(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002062}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063
Marshall Clowb22274f2017-01-24 22:22:33 +00002064#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065template <class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002066struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068 _Tp* __ptr_;
2069};
2070
2071template<class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00002072class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073{
2074private:
2075 _Tp* __ptr_;
2076public:
2077 typedef _Tp element_type;
2078
2079 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2080 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2081 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2082 : __ptr_(__p.release()) {}
2083 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2084 {reset(__p.release()); return *this;}
2085 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2086 {reset(__p.release()); return *this;}
2087 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2088 {reset(__p.__ptr_); return *this;}
2089 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2090
2091 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2092 {return *__ptr_;}
2093 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2094 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2095 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2096 {
2097 _Tp* __t = __ptr_;
2098 __ptr_ = 0;
2099 return __t;
2100 }
2101 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2102 {
2103 if (__ptr_ != __p)
2104 delete __ptr_;
2105 __ptr_ = __p;
2106 }
2107
2108 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2109 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2110 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2111 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2112 {return auto_ptr<_Up>(release());}
2113};
2114
2115template <>
Louis Dionne481a2662018-09-23 18:35:00 +00002116class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117{
2118public:
2119 typedef void element_type;
2120};
Marshall Clowb22274f2017-01-24 22:22:33 +00002121#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
Eric Fiselier9d355982017-04-12 23:45:53 +00002123template <class _Tp, int _Idx,
2124 bool _CanBeEmptyBase =
2125 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2126struct __compressed_pair_elem {
2127 typedef _Tp _ParamT;
2128 typedef _Tp& reference;
2129 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130
Eric Fiselier9d355982017-04-12 23:45:53 +00002131#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002132 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133
Eric Fiselier9d355982017-04-12 23:45:53 +00002134 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002135 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2136 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002137 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002138 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002139 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002140 : __value_(_VSTD::forward<_Up>(__u))
2141 {
2142 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143
Eric Fiselier9d355982017-04-12 23:45:53 +00002144 template <class... _Args, size_t... _Indexes>
2145 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2146 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2147 __tuple_indices<_Indexes...>)
2148 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2149#else
Alex Lorenz76132112017-11-09 17:54:49 +00002150 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2151 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002152 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2153#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154
Alex Lorenz76132112017-11-09 17:54:49 +00002155 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2156 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002157 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002160 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161};
2162
Eric Fiselier9d355982017-04-12 23:45:53 +00002163template <class _Tp, int _Idx>
2164struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2165 typedef _Tp _ParamT;
2166 typedef _Tp& reference;
2167 typedef const _Tp& const_reference;
2168 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169
Eric Fiselier9d355982017-04-12 23:45:53 +00002170#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002171 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172
Eric Fiselier9d355982017-04-12 23:45:53 +00002173 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002174 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2175 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002176 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002177 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002178 __compressed_pair_elem(_Up&& __u)
Eric Fiselierb41db9a2018-10-01 01:59:37 +00002179 : __value_type(_VSTD::forward<_Up>(__u))
2180 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181
Eric Fiselier9d355982017-04-12 23:45:53 +00002182 template <class... _Args, size_t... _Indexes>
2183 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2184 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2185 __tuple_indices<_Indexes...>)
2186 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2187#else
Alex Lorenz76132112017-11-09 17:54:49 +00002188 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2189 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002190 __compressed_pair_elem(_ParamT __p)
2191 : __value_type(std::forward<_ParamT>(__p)) {}
2192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193
Alex Lorenz76132112017-11-09 17:54:49 +00002194 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2195 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002196 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197};
2198
Eric Fiselier9d355982017-04-12 23:45:53 +00002199// Tag used to construct the second element of the compressed pair.
2200struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201
2202template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002203class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2204 private __compressed_pair_elem<_T2, 1> {
2205 typedef __compressed_pair_elem<_T1, 0> _Base1;
2206 typedef __compressed_pair_elem<_T2, 1> _Base2;
2207
2208 // NOTE: This static assert should never fire because __compressed_pair
2209 // is *almost never* used in a scenario where it's possible for T1 == T2.
2210 // (The exception is std::function where it is possible that the function
2211 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002212 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002213 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2214 "The current implementation is NOT ABI-compatible with the previous "
2215 "implementation for this configuration");
2216
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002218#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002219 template <bool _Dummy = true,
2220 class = typename enable_if<
2221 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2222 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2223 >::type
2224 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002225 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002226 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227
Eric Fiselier9d355982017-04-12 23:45:53 +00002228 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2229 __compressed_pair>::value,
2230 bool>::type = true>
2231 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2232 __compressed_pair(_Tp&& __t)
2233 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234
Eric Fiselier9d355982017-04-12 23:45:53 +00002235 template <class _Tp>
2236 _LIBCPP_INLINE_VISIBILITY constexpr
2237 __compressed_pair(__second_tag, _Tp&& __t)
2238 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239
Eric Fiselier9d355982017-04-12 23:45:53 +00002240 template <class _U1, class _U2>
2241 _LIBCPP_INLINE_VISIBILITY constexpr
2242 __compressed_pair(_U1&& __t1, _U2&& __t2)
2243 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244
Eric Fiselier9d355982017-04-12 23:45:53 +00002245 template <class... _Args1, class... _Args2>
2246 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2247 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2248 tuple<_Args2...> __second_args)
2249 : _Base1(__pc, _VSTD::move(__first_args),
2250 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2251 _Base2(__pc, _VSTD::move(__second_args),
2252 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002253
Eric Fiselier9d355982017-04-12 23:45:53 +00002254#else
2255 _LIBCPP_INLINE_VISIBILITY
2256 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002257
Eric Fiselier9d355982017-04-12 23:45:53 +00002258 _LIBCPP_INLINE_VISIBILITY explicit
2259 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002260
Eric Fiselier9d355982017-04-12 23:45:53 +00002261 _LIBCPP_INLINE_VISIBILITY
2262 __compressed_pair(__second_tag, _T2 __t2)
2263 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264
Eric Fiselier9d355982017-04-12 23:45:53 +00002265 _LIBCPP_INLINE_VISIBILITY
2266 __compressed_pair(_T1 __t1, _T2 __t2)
2267 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2268#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Eric Fiselier9d355982017-04-12 23:45:53 +00002270 _LIBCPP_INLINE_VISIBILITY
2271 typename _Base1::reference first() _NOEXCEPT {
2272 return static_cast<_Base1&>(*this).__get();
2273 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274
Eric Fiselier9d355982017-04-12 23:45:53 +00002275 _LIBCPP_INLINE_VISIBILITY
2276 typename _Base1::const_reference first() const _NOEXCEPT {
2277 return static_cast<_Base1 const&>(*this).__get();
2278 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279
Eric Fiselier9d355982017-04-12 23:45:53 +00002280 _LIBCPP_INLINE_VISIBILITY
2281 typename _Base2::reference second() _NOEXCEPT {
2282 return static_cast<_Base2&>(*this).__get();
2283 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284
Eric Fiselier9d355982017-04-12 23:45:53 +00002285 _LIBCPP_INLINE_VISIBILITY
2286 typename _Base2::const_reference second() const _NOEXCEPT {
2287 return static_cast<_Base2 const&>(*this).__get();
2288 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289
Eric Fiselier9d355982017-04-12 23:45:53 +00002290 _LIBCPP_INLINE_VISIBILITY
2291 void swap(__compressed_pair& __x)
2292 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2293 __is_nothrow_swappable<_T2>::value)
2294 {
2295 using std::swap;
2296 swap(first(), __x.first());
2297 swap(second(), __x.second());
2298 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299};
2300
2301template <class _T1, class _T2>
2302inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002303void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2304 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2305 __is_nothrow_swappable<_T2>::value) {
2306 __x.swap(__y);
2307}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002309// default_delete
2310
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002312struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002313 static_assert(!is_function<_Tp>::value,
2314 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002315#ifndef _LIBCPP_CXX03_LANG
2316 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002317#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002318 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002319#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002320 template <class _Up>
2321 _LIBCPP_INLINE_VISIBILITY
2322 default_delete(const default_delete<_Up>&,
2323 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2324 0) _NOEXCEPT {}
2325
2326 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2327 static_assert(sizeof(_Tp) > 0,
2328 "default_delete can not delete incomplete type");
2329 static_assert(!is_void<_Tp>::value,
2330 "default_delete can not delete incomplete type");
2331 delete __ptr;
2332 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333};
2334
2335template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002336struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2337private:
2338 template <class _Up>
2339 struct _EnableIfConvertible
2340 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2341
Howard Hinnant4500ca52011-12-18 21:19:44 +00002342public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002343#ifndef _LIBCPP_CXX03_LANG
2344 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002345#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002346 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002347#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002348
2349 template <class _Up>
2350 _LIBCPP_INLINE_VISIBILITY
2351 default_delete(const default_delete<_Up[]>&,
2352 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2353
2354 template <class _Up>
2355 _LIBCPP_INLINE_VISIBILITY
2356 typename _EnableIfConvertible<_Up>::type
2357 operator()(_Up* __ptr) const _NOEXCEPT {
2358 static_assert(sizeof(_Tp) > 0,
2359 "default_delete can not delete incomplete type");
2360 static_assert(!is_void<_Tp>::value,
2361 "default_delete can not delete void type");
2362 delete[] __ptr;
2363 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364};
2365
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366
Eric Fiselier6779b062017-04-16 02:06:25 +00002367
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002368#ifndef _LIBCPP_CXX03_LANG
2369template <class _Deleter>
2370struct __unique_ptr_deleter_sfinae {
2371 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2372 typedef const _Deleter& __lval_ref_type;
2373 typedef _Deleter&& __good_rval_ref_type;
2374 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375};
2376
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002377template <class _Deleter>
2378struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2379 typedef const _Deleter& __lval_ref_type;
2380 typedef const _Deleter&& __bad_rval_ref_type;
2381 typedef false_type __enable_rval_overload;
2382};
2383
2384template <class _Deleter>
2385struct __unique_ptr_deleter_sfinae<_Deleter&> {
2386 typedef _Deleter& __lval_ref_type;
2387 typedef _Deleter&& __bad_rval_ref_type;
2388 typedef false_type __enable_rval_overload;
2389};
2390#endif // !defined(_LIBCPP_CXX03_LANG)
2391
2392template <class _Tp, class _Dp = default_delete<_Tp> >
2393class _LIBCPP_TEMPLATE_VIS unique_ptr {
2394public:
2395 typedef _Tp element_type;
2396 typedef _Dp deleter_type;
2397 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2398
2399 static_assert(!is_rvalue_reference<deleter_type>::value,
2400 "the specified deleter type cannot be an rvalue reference");
2401
2402private:
2403 __compressed_pair<pointer, deleter_type> __ptr_;
2404
2405 struct __nat { int __for_bool_; };
2406
2407#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002408 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002409
2410 template <bool _Dummy>
2411 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002412 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002413
2414 template <bool _Dummy>
2415 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002416 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002417
2418 template <bool _Dummy>
2419 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002420 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002421
2422 template <bool _Dummy, class _Deleter = typename __dependent_type<
2423 __identity<deleter_type>, _Dummy>::type>
2424 using _EnableIfDeleterDefaultConstructible =
2425 typename enable_if<is_default_constructible<_Deleter>::value &&
2426 !is_pointer<_Deleter>::value>::type;
2427
2428 template <class _ArgType>
2429 using _EnableIfDeleterConstructible =
2430 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2431
2432 template <class _UPtr, class _Up>
2433 using _EnableIfMoveConvertible = typename enable_if<
2434 is_convertible<typename _UPtr::pointer, pointer>::value &&
2435 !is_array<_Up>::value
2436 >::type;
2437
2438 template <class _UDel>
2439 using _EnableIfDeleterConvertible = typename enable_if<
2440 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2441 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2442 >::type;
2443
2444 template <class _UDel>
2445 using _EnableIfDeleterAssignable = typename enable_if<
2446 is_assignable<_Dp&, _UDel&&>::value
2447 >::type;
2448
2449public:
2450 template <bool _Dummy = true,
2451 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2452 _LIBCPP_INLINE_VISIBILITY
2453 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2454
2455 template <bool _Dummy = true,
2456 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2457 _LIBCPP_INLINE_VISIBILITY
2458 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2459
2460 template <bool _Dummy = true,
2461 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2462 _LIBCPP_INLINE_VISIBILITY
2463 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2464
2465 template <bool _Dummy = true,
2466 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2467 _LIBCPP_INLINE_VISIBILITY
2468 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2469 : __ptr_(__p, __d) {}
2470
2471 template <bool _Dummy = true,
2472 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2473 _LIBCPP_INLINE_VISIBILITY
2474 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2475 : __ptr_(__p, _VSTD::move(__d)) {
2476 static_assert(!is_reference<deleter_type>::value,
2477 "rvalue deleter bound to reference");
2478 }
2479
2480 template <bool _Dummy = true,
2481 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2482 _LIBCPP_INLINE_VISIBILITY
2483 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2484
2485 _LIBCPP_INLINE_VISIBILITY
2486 unique_ptr(unique_ptr&& __u) noexcept
2487 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2488 }
2489
2490 template <class _Up, class _Ep,
2491 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2492 class = _EnableIfDeleterConvertible<_Ep>
2493 >
2494 _LIBCPP_INLINE_VISIBILITY
2495 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2496 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2497
2498#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2499 template <class _Up>
2500 _LIBCPP_INLINE_VISIBILITY
2501 unique_ptr(auto_ptr<_Up>&& __p,
2502 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2503 is_same<_Dp, default_delete<_Tp>>::value,
2504 __nat>::type = __nat()) _NOEXCEPT
2505 : __ptr_(__p.release()) {}
2506#endif
2507
2508 _LIBCPP_INLINE_VISIBILITY
2509 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2510 reset(__u.release());
2511 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2512 return *this;
2513 }
2514
2515 template <class _Up, class _Ep,
2516 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2517 class = _EnableIfDeleterAssignable<_Ep>
2518 >
2519 _LIBCPP_INLINE_VISIBILITY
2520 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2521 reset(__u.release());
2522 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2523 return *this;
2524 }
2525
2526#else // _LIBCPP_CXX03_LANG
2527private:
2528 unique_ptr(unique_ptr&);
2529 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2530
2531 unique_ptr& operator=(unique_ptr&);
2532 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2533
2534public:
2535 _LIBCPP_INLINE_VISIBILITY
2536 unique_ptr() : __ptr_(pointer())
2537 {
2538 static_assert(!is_pointer<deleter_type>::value,
2539 "unique_ptr constructed with null function pointer deleter");
2540 static_assert(is_default_constructible<deleter_type>::value,
2541 "unique_ptr::deleter_type is not default constructible");
2542 }
2543 _LIBCPP_INLINE_VISIBILITY
2544 unique_ptr(nullptr_t) : __ptr_(pointer())
2545 {
2546 static_assert(!is_pointer<deleter_type>::value,
2547 "unique_ptr constructed with null function pointer deleter");
2548 }
2549 _LIBCPP_INLINE_VISIBILITY
2550 explicit unique_ptr(pointer __p)
2551 : __ptr_(_VSTD::move(__p)) {
2552 static_assert(!is_pointer<deleter_type>::value,
2553 "unique_ptr constructed with null function pointer deleter");
2554 }
2555
2556 _LIBCPP_INLINE_VISIBILITY
2557 operator __rv<unique_ptr>() {
2558 return __rv<unique_ptr>(*this);
2559 }
2560
2561 _LIBCPP_INLINE_VISIBILITY
2562 unique_ptr(__rv<unique_ptr> __u)
2563 : __ptr_(__u->release(),
2564 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2565
2566 template <class _Up, class _Ep>
2567 _LIBCPP_INLINE_VISIBILITY
2568 typename enable_if<
2569 !is_array<_Up>::value &&
2570 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2571 pointer>::value &&
2572 is_assignable<deleter_type&, _Ep&>::value,
2573 unique_ptr&>::type
2574 operator=(unique_ptr<_Up, _Ep> __u) {
2575 reset(__u.release());
2576 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2577 return *this;
2578 }
2579
2580 _LIBCPP_INLINE_VISIBILITY
2581 unique_ptr(pointer __p, deleter_type __d)
2582 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2583#endif // _LIBCPP_CXX03_LANG
2584
2585#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2586 template <class _Up>
2587 _LIBCPP_INLINE_VISIBILITY
2588 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2589 is_same<_Dp, default_delete<_Tp> >::value,
2590 unique_ptr&>::type
2591 operator=(auto_ptr<_Up> __p) {
2592 reset(__p.release());
2593 return *this;
2594 }
2595#endif
2596
2597 _LIBCPP_INLINE_VISIBILITY
2598 ~unique_ptr() { reset(); }
2599
2600 _LIBCPP_INLINE_VISIBILITY
2601 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2602 reset();
2603 return *this;
2604 }
2605
2606 _LIBCPP_INLINE_VISIBILITY
2607 typename add_lvalue_reference<_Tp>::type
2608 operator*() const {
2609 return *__ptr_.first();
2610 }
2611 _LIBCPP_INLINE_VISIBILITY
2612 pointer operator->() const _NOEXCEPT {
2613 return __ptr_.first();
2614 }
2615 _LIBCPP_INLINE_VISIBILITY
2616 pointer get() const _NOEXCEPT {
2617 return __ptr_.first();
2618 }
2619 _LIBCPP_INLINE_VISIBILITY
2620 deleter_type& get_deleter() _NOEXCEPT {
2621 return __ptr_.second();
2622 }
2623 _LIBCPP_INLINE_VISIBILITY
2624 const deleter_type& get_deleter() const _NOEXCEPT {
2625 return __ptr_.second();
2626 }
2627 _LIBCPP_INLINE_VISIBILITY
2628 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2629 return __ptr_.first() != nullptr;
2630 }
2631
2632 _LIBCPP_INLINE_VISIBILITY
2633 pointer release() _NOEXCEPT {
2634 pointer __t = __ptr_.first();
2635 __ptr_.first() = pointer();
2636 return __t;
2637 }
2638
2639 _LIBCPP_INLINE_VISIBILITY
2640 void reset(pointer __p = pointer()) _NOEXCEPT {
2641 pointer __tmp = __ptr_.first();
2642 __ptr_.first() = __p;
2643 if (__tmp)
2644 __ptr_.second()(__tmp);
2645 }
2646
2647 _LIBCPP_INLINE_VISIBILITY
2648 void swap(unique_ptr& __u) _NOEXCEPT {
2649 __ptr_.swap(__u.__ptr_);
2650 }
2651};
2652
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002653
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002655class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002657 typedef _Tp element_type;
2658 typedef _Dp deleter_type;
2659 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2660
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002662 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663
Eric Fiselier31127cd2017-04-16 02:14:31 +00002664 template <class _From>
2665 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666
Eric Fiselier31127cd2017-04-16 02:14:31 +00002667 template <class _FromElem>
2668 struct _CheckArrayPointerConversion<_FromElem*>
2669 : integral_constant<bool,
2670 is_same<_FromElem*, pointer>::value ||
2671 (is_same<pointer, element_type*>::value &&
2672 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2673 >
2674 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002676#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002677 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002678
2679 template <bool _Dummy>
2680 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002681 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002682
2683 template <bool _Dummy>
2684 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002685 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002686
2687 template <bool _Dummy>
2688 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002689 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002690
2691 template <bool _Dummy, class _Deleter = typename __dependent_type<
2692 __identity<deleter_type>, _Dummy>::type>
2693 using _EnableIfDeleterDefaultConstructible =
2694 typename enable_if<is_default_constructible<_Deleter>::value &&
2695 !is_pointer<_Deleter>::value>::type;
2696
2697 template <class _ArgType>
2698 using _EnableIfDeleterConstructible =
2699 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2700
2701 template <class _Pp>
2702 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002703 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002704 >::type;
2705
2706 template <class _UPtr, class _Up,
2707 class _ElemT = typename _UPtr::element_type>
2708 using _EnableIfMoveConvertible = typename enable_if<
2709 is_array<_Up>::value &&
2710 is_same<pointer, element_type*>::value &&
2711 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2712 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2713 >::type;
2714
2715 template <class _UDel>
2716 using _EnableIfDeleterConvertible = typename enable_if<
2717 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2718 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2719 >::type;
2720
2721 template <class _UDel>
2722 using _EnableIfDeleterAssignable = typename enable_if<
2723 is_assignable<_Dp&, _UDel&&>::value
2724 >::type;
2725
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727 template <bool _Dummy = true,
2728 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2729 _LIBCPP_INLINE_VISIBILITY
2730 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002732 template <bool _Dummy = true,
2733 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2734 _LIBCPP_INLINE_VISIBILITY
2735 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002737 template <class _Pp, bool _Dummy = true,
2738 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2739 class = _EnableIfPointerConvertible<_Pp>>
2740 _LIBCPP_INLINE_VISIBILITY
2741 explicit unique_ptr(_Pp __p) noexcept
2742 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002744 template <class _Pp, bool _Dummy = true,
2745 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2746 class = _EnableIfPointerConvertible<_Pp>>
2747 _LIBCPP_INLINE_VISIBILITY
2748 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2749 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002751 template <bool _Dummy = true,
2752 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2753 _LIBCPP_INLINE_VISIBILITY
2754 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2755 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002757 template <class _Pp, bool _Dummy = true,
2758 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2759 class = _EnableIfPointerConvertible<_Pp>>
2760 _LIBCPP_INLINE_VISIBILITY
2761 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2762 : __ptr_(__p, _VSTD::move(__d)) {
2763 static_assert(!is_reference<deleter_type>::value,
2764 "rvalue deleter bound to reference");
2765 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002767 template <bool _Dummy = true,
2768 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2769 _LIBCPP_INLINE_VISIBILITY
2770 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2771 : __ptr_(nullptr, _VSTD::move(__d)) {
2772 static_assert(!is_reference<deleter_type>::value,
2773 "rvalue deleter bound to reference");
2774 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002775
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002776 template <class _Pp, bool _Dummy = true,
2777 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2778 class = _EnableIfPointerConvertible<_Pp>>
2779 _LIBCPP_INLINE_VISIBILITY
2780 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002781
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002782 _LIBCPP_INLINE_VISIBILITY
2783 unique_ptr(unique_ptr&& __u) noexcept
2784 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2785 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002786
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002787 _LIBCPP_INLINE_VISIBILITY
2788 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2789 reset(__u.release());
2790 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2791 return *this;
2792 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002794 template <class _Up, class _Ep,
2795 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2796 class = _EnableIfDeleterConvertible<_Ep>
2797 >
2798 _LIBCPP_INLINE_VISIBILITY
2799 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002800 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002801 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002803 template <class _Up, class _Ep,
2804 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2805 class = _EnableIfDeleterAssignable<_Ep>
2806 >
2807 _LIBCPP_INLINE_VISIBILITY
2808 unique_ptr&
2809 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2810 reset(__u.release());
2811 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2812 return *this;
2813 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002815#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002817 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002818
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002819 unique_ptr(unique_ptr&);
2820 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2821
2822 unique_ptr& operator=(unique_ptr&);
2823 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2824
2825 template <class _Up>
2826 unique_ptr(_Up __u,
2827 typename conditional<
2828 is_reference<deleter_type>::value, deleter_type,
2829 typename add_lvalue_reference<const deleter_type>::type>::type,
2830 typename enable_if<is_convertible<_Up, pointer>::value,
2831 __nat>::type = __nat());
2832public:
2833 _LIBCPP_INLINE_VISIBILITY
2834 unique_ptr() : __ptr_(pointer()) {
2835 static_assert(!is_pointer<deleter_type>::value,
2836 "unique_ptr constructed with null function pointer deleter");
2837 }
2838 _LIBCPP_INLINE_VISIBILITY
2839 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2840 static_assert(!is_pointer<deleter_type>::value,
2841 "unique_ptr constructed with null function pointer deleter");
2842 }
2843
2844 _LIBCPP_INLINE_VISIBILITY
2845 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2846 static_assert(!is_pointer<deleter_type>::value,
2847 "unique_ptr constructed with null function pointer deleter");
2848 }
2849
2850 _LIBCPP_INLINE_VISIBILITY
2851 unique_ptr(pointer __p, deleter_type __d)
2852 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2853
2854 _LIBCPP_INLINE_VISIBILITY
2855 unique_ptr(nullptr_t, deleter_type __d)
2856 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2857
2858 _LIBCPP_INLINE_VISIBILITY
2859 operator __rv<unique_ptr>() {
2860 return __rv<unique_ptr>(*this);
2861 }
2862
2863 _LIBCPP_INLINE_VISIBILITY
2864 unique_ptr(__rv<unique_ptr> __u)
2865 : __ptr_(__u->release(),
2866 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2867
2868 _LIBCPP_INLINE_VISIBILITY
2869 unique_ptr& operator=(__rv<unique_ptr> __u) {
2870 reset(__u->release());
2871 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2872 return *this;
2873 }
2874
2875#endif // _LIBCPP_CXX03_LANG
2876
2877public:
2878 _LIBCPP_INLINE_VISIBILITY
2879 ~unique_ptr() { reset(); }
2880
2881 _LIBCPP_INLINE_VISIBILITY
2882 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2883 reset();
2884 return *this;
2885 }
2886
2887 _LIBCPP_INLINE_VISIBILITY
2888 typename add_lvalue_reference<_Tp>::type
2889 operator[](size_t __i) const {
2890 return __ptr_.first()[__i];
2891 }
2892 _LIBCPP_INLINE_VISIBILITY
2893 pointer get() const _NOEXCEPT {
2894 return __ptr_.first();
2895 }
2896
2897 _LIBCPP_INLINE_VISIBILITY
2898 deleter_type& get_deleter() _NOEXCEPT {
2899 return __ptr_.second();
2900 }
2901
2902 _LIBCPP_INLINE_VISIBILITY
2903 const deleter_type& get_deleter() const _NOEXCEPT {
2904 return __ptr_.second();
2905 }
2906 _LIBCPP_INLINE_VISIBILITY
2907 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2908 return __ptr_.first() != nullptr;
2909 }
2910
2911 _LIBCPP_INLINE_VISIBILITY
2912 pointer release() _NOEXCEPT {
2913 pointer __t = __ptr_.first();
2914 __ptr_.first() = pointer();
2915 return __t;
2916 }
2917
2918 template <class _Pp>
2919 _LIBCPP_INLINE_VISIBILITY
2920 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002921 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002922 >::type
2923 reset(_Pp __p) _NOEXCEPT {
2924 pointer __tmp = __ptr_.first();
2925 __ptr_.first() = __p;
2926 if (__tmp)
2927 __ptr_.second()(__tmp);
2928 }
2929
2930 _LIBCPP_INLINE_VISIBILITY
2931 void reset(nullptr_t = nullptr) _NOEXCEPT {
2932 pointer __tmp = __ptr_.first();
2933 __ptr_.first() = nullptr;
2934 if (__tmp)
2935 __ptr_.second()(__tmp);
2936 }
2937
2938 _LIBCPP_INLINE_VISIBILITY
2939 void swap(unique_ptr& __u) _NOEXCEPT {
2940 __ptr_.swap(__u.__ptr_);
2941 }
2942
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943};
2944
2945template <class _Tp, class _Dp>
2946inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002947typename enable_if<
2948 __is_swappable<_Dp>::value,
2949 void
2950>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002951swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952
2953template <class _T1, class _D1, class _T2, class _D2>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
2956operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2957
2958template <class _T1, class _D1, class _T2, class _D2>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
2961operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2962
2963template <class _T1, class _D1, class _T2, class _D2>
2964inline _LIBCPP_INLINE_VISIBILITY
2965bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002966operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2967{
2968 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2969 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002970 typedef typename common_type<_P1, _P2>::type _Vp;
2971 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002972}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973
2974template <class _T1, class _D1, class _T2, class _D2>
2975inline _LIBCPP_INLINE_VISIBILITY
2976bool
2977operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2978
2979template <class _T1, class _D1, class _T2, class _D2>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
2982operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2983
2984template <class _T1, class _D1, class _T2, class _D2>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
2987operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2988
Howard Hinnantb17caf92012-02-21 21:02:58 +00002989template <class _T1, class _D1>
2990inline _LIBCPP_INLINE_VISIBILITY
2991bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002992operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002993{
2994 return !__x;
2995}
2996
2997template <class _T1, class _D1>
2998inline _LIBCPP_INLINE_VISIBILITY
2999bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003000operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003001{
3002 return !__x;
3003}
3004
3005template <class _T1, class _D1>
3006inline _LIBCPP_INLINE_VISIBILITY
3007bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003008operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003009{
3010 return static_cast<bool>(__x);
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003016operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003017{
3018 return static_cast<bool>(__x);
3019}
3020
3021template <class _T1, class _D1>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
3024operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3025{
3026 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3027 return less<_P1>()(__x.get(), nullptr);
3028}
3029
3030template <class _T1, class _D1>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
3033operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3034{
3035 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3036 return less<_P1>()(nullptr, __x.get());
3037}
3038
3039template <class _T1, class _D1>
3040inline _LIBCPP_INLINE_VISIBILITY
3041bool
3042operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3043{
3044 return nullptr < __x;
3045}
3046
3047template <class _T1, class _D1>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3051{
3052 return __x < nullptr;
3053}
3054
3055template <class _T1, class _D1>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3059{
3060 return !(nullptr < __x);
3061}
3062
3063template <class _T1, class _D1>
3064inline _LIBCPP_INLINE_VISIBILITY
3065bool
3066operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3067{
3068 return !(__x < nullptr);
3069}
3070
3071template <class _T1, class _D1>
3072inline _LIBCPP_INLINE_VISIBILITY
3073bool
3074operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3075{
3076 return !(__x < nullptr);
3077}
3078
3079template <class _T1, class _D1>
3080inline _LIBCPP_INLINE_VISIBILITY
3081bool
3082operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3083{
3084 return !(nullptr < __x);
3085}
3086
Howard Hinnant9e028f12012-05-01 15:37:54 +00003087#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3088
3089template <class _Tp, class _Dp>
3090inline _LIBCPP_INLINE_VISIBILITY
3091unique_ptr<_Tp, _Dp>
3092move(unique_ptr<_Tp, _Dp>& __t)
3093{
3094 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3095}
3096
3097#endif
3098
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003099#if _LIBCPP_STD_VER > 11
3100
3101template<class _Tp>
3102struct __unique_if
3103{
3104 typedef unique_ptr<_Tp> __unique_single;
3105};
3106
3107template<class _Tp>
3108struct __unique_if<_Tp[]>
3109{
3110 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3111};
3112
3113template<class _Tp, size_t _Np>
3114struct __unique_if<_Tp[_Np]>
3115{
3116 typedef void __unique_array_known_bound;
3117};
3118
3119template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003120inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003121typename __unique_if<_Tp>::__unique_single
3122make_unique(_Args&&... __args)
3123{
3124 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3125}
3126
3127template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003128inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003129typename __unique_if<_Tp>::__unique_array_unknown_bound
3130make_unique(size_t __n)
3131{
3132 typedef typename remove_extent<_Tp>::type _Up;
3133 return unique_ptr<_Tp>(new _Up[__n]());
3134}
3135
3136template<class _Tp, class... _Args>
3137 typename __unique_if<_Tp>::__unique_array_known_bound
3138 make_unique(_Args&&...) = delete;
3139
3140#endif // _LIBCPP_STD_VER > 11
3141
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003142template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003143#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003144struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003145#else
3146struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3147 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3148#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003149{
3150 typedef unique_ptr<_Tp, _Dp> argument_type;
3151 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003152 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003153 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003154 {
3155 typedef typename argument_type::pointer pointer;
3156 return hash<pointer>()(__ptr.get());
3157 }
3158};
3159
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160struct __destruct_n
3161{
3162private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003163 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164
3165 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003166 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003167 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168
3169 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003170 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 {}
3172
Howard Hinnant719bda32011-05-28 14:41:13 +00003173 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003174 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003175 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 {}
3177
Howard Hinnant719bda32011-05-28 14:41:13 +00003178 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003179 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003180 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181 {}
3182public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003183 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003184 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185
3186 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003187 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003188 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003189
3190 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003191 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003192 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193
3194 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003195 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003196 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197};
3198
3199template <class _Alloc>
3200class __allocator_destructor
3201{
3202 typedef allocator_traits<_Alloc> __alloc_traits;
3203public:
3204 typedef typename __alloc_traits::pointer pointer;
3205 typedef typename __alloc_traits::size_type size_type;
3206private:
3207 _Alloc& __alloc_;
3208 size_type __s_;
3209public:
3210 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003211 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003214 void operator()(pointer __p) _NOEXCEPT
3215 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216};
3217
3218template <class _InputIterator, class _ForwardIterator>
3219_ForwardIterator
3220uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3221{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003223#ifndef _LIBCPP_NO_EXCEPTIONS
3224 _ForwardIterator __s = __r;
3225 try
3226 {
3227#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003228 for (; __f != __l; ++__f, (void) ++__r)
3229 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003230#ifndef _LIBCPP_NO_EXCEPTIONS
3231 }
3232 catch (...)
3233 {
3234 for (; __s != __r; ++__s)
3235 __s->~value_type();
3236 throw;
3237 }
3238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239 return __r;
3240}
3241
3242template <class _InputIterator, class _Size, class _ForwardIterator>
3243_ForwardIterator
3244uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3245{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003247#ifndef _LIBCPP_NO_EXCEPTIONS
3248 _ForwardIterator __s = __r;
3249 try
3250 {
3251#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003252 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3253 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003254#ifndef _LIBCPP_NO_EXCEPTIONS
3255 }
3256 catch (...)
3257 {
3258 for (; __s != __r; ++__s)
3259 __s->~value_type();
3260 throw;
3261 }
3262#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 return __r;
3264}
3265
3266template <class _ForwardIterator, class _Tp>
3267void
3268uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3269{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003271#ifndef _LIBCPP_NO_EXCEPTIONS
3272 _ForwardIterator __s = __f;
3273 try
3274 {
3275#endif
3276 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003277 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003278#ifndef _LIBCPP_NO_EXCEPTIONS
3279 }
3280 catch (...)
3281 {
3282 for (; __s != __f; ++__s)
3283 __s->~value_type();
3284 throw;
3285 }
3286#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003287}
3288
3289template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003290_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3292{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003293 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003294#ifndef _LIBCPP_NO_EXCEPTIONS
3295 _ForwardIterator __s = __f;
3296 try
3297 {
3298#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003299 for (; __n > 0; ++__f, (void) --__n)
3300 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003301#ifndef _LIBCPP_NO_EXCEPTIONS
3302 }
3303 catch (...)
3304 {
3305 for (; __s != __f; ++__s)
3306 __s->~value_type();
3307 throw;
3308 }
3309#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003310 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311}
3312
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003313#if _LIBCPP_STD_VER > 14
3314
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003315template <class _Tp>
3316inline _LIBCPP_INLINE_VISIBILITY
3317void destroy_at(_Tp* __loc) {
3318 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3319 __loc->~_Tp();
3320}
3321
3322template <class _ForwardIterator>
3323inline _LIBCPP_INLINE_VISIBILITY
3324void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3325 for (; __first != __last; ++__first)
3326 _VSTD::destroy_at(_VSTD::addressof(*__first));
3327}
3328
3329template <class _ForwardIterator, class _Size>
3330inline _LIBCPP_INLINE_VISIBILITY
3331_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3332 for (; __n > 0; (void)++__first, --__n)
3333 _VSTD::destroy_at(_VSTD::addressof(*__first));
3334 return __first;
3335}
3336
Eric Fiselier290c07c2016-10-11 21:13:44 +00003337template <class _ForwardIterator>
3338inline _LIBCPP_INLINE_VISIBILITY
3339void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3340 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3341 auto __idx = __first;
3342#ifndef _LIBCPP_NO_EXCEPTIONS
3343 try {
3344#endif
3345 for (; __idx != __last; ++__idx)
3346 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3347#ifndef _LIBCPP_NO_EXCEPTIONS
3348 } catch (...) {
3349 _VSTD::destroy(__first, __idx);
3350 throw;
3351 }
3352#endif
3353}
3354
3355template <class _ForwardIterator, class _Size>
3356inline _LIBCPP_INLINE_VISIBILITY
3357_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3358 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3359 auto __idx = __first;
3360#ifndef _LIBCPP_NO_EXCEPTIONS
3361 try {
3362#endif
3363 for (; __n > 0; (void)++__idx, --__n)
3364 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3365 return __idx;
3366#ifndef _LIBCPP_NO_EXCEPTIONS
3367 } catch (...) {
3368 _VSTD::destroy(__first, __idx);
3369 throw;
3370 }
3371#endif
3372}
3373
3374
3375template <class _ForwardIterator>
3376inline _LIBCPP_INLINE_VISIBILITY
3377void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3378 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3379 auto __idx = __first;
3380#ifndef _LIBCPP_NO_EXCEPTIONS
3381 try {
3382#endif
3383 for (; __idx != __last; ++__idx)
3384 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3385#ifndef _LIBCPP_NO_EXCEPTIONS
3386 } catch (...) {
3387 _VSTD::destroy(__first, __idx);
3388 throw;
3389 }
3390#endif
3391}
3392
3393template <class _ForwardIterator, class _Size>
3394inline _LIBCPP_INLINE_VISIBILITY
3395_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3396 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3397 auto __idx = __first;
3398#ifndef _LIBCPP_NO_EXCEPTIONS
3399 try {
3400#endif
3401 for (; __n > 0; (void)++__idx, --__n)
3402 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3403 return __idx;
3404#ifndef _LIBCPP_NO_EXCEPTIONS
3405 } catch (...) {
3406 _VSTD::destroy(__first, __idx);
3407 throw;
3408 }
3409#endif
3410}
3411
3412
3413template <class _InputIt, class _ForwardIt>
3414inline _LIBCPP_INLINE_VISIBILITY
3415_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3416 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3417 auto __idx = __first_res;
3418#ifndef _LIBCPP_NO_EXCEPTIONS
3419 try {
3420#endif
3421 for (; __first != __last; (void)++__idx, ++__first)
3422 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3423 return __idx;
3424#ifndef _LIBCPP_NO_EXCEPTIONS
3425 } catch (...) {
3426 _VSTD::destroy(__first_res, __idx);
3427 throw;
3428 }
3429#endif
3430}
3431
3432template <class _InputIt, class _Size, class _ForwardIt>
3433inline _LIBCPP_INLINE_VISIBILITY
3434pair<_InputIt, _ForwardIt>
3435uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3436 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3437 auto __idx = __first_res;
3438#ifndef _LIBCPP_NO_EXCEPTIONS
3439 try {
3440#endif
3441 for (; __n > 0; ++__idx, (void)++__first, --__n)
3442 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3443 return {__first, __idx};
3444#ifndef _LIBCPP_NO_EXCEPTIONS
3445 } catch (...) {
3446 _VSTD::destroy(__first_res, __idx);
3447 throw;
3448 }
3449#endif
3450}
3451
3452
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003453#endif // _LIBCPP_STD_VER > 14
3454
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003455// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3456// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003457// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003458#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3459 && defined(__ATOMIC_RELAXED) \
3460 && defined(__ATOMIC_ACQ_REL)
3461# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3462#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3463# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3464#endif
3465
3466template <class _Tp>
3467inline _LIBCPP_INLINE_VISIBILITY _Tp
3468__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3469{
3470#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3471 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3472#else
3473 return __t += 1;
3474#endif
3475}
3476
3477template <class _Tp>
3478inline _LIBCPP_INLINE_VISIBILITY _Tp
3479__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3480{
3481#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3482 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3483#else
3484 return __t -= 1;
3485#endif
3486}
3487
Howard Hinnant756c69b2010-09-22 16:48:34 +00003488class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489 : public std::exception
3490{
3491public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003492 virtual ~bad_weak_ptr() _NOEXCEPT;
3493 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494};
3495
Louis Dionne16fe2952018-07-11 23:14:33 +00003496_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00003497void __throw_bad_weak_ptr()
3498{
3499#ifndef _LIBCPP_NO_EXCEPTIONS
3500 throw bad_weak_ptr();
3501#else
3502 _VSTD::abort();
3503#endif
3504}
3505
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003506template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003508class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509{
3510 __shared_count(const __shared_count&);
3511 __shared_count& operator=(const __shared_count&);
3512
3513protected:
3514 long __shared_owners_;
3515 virtual ~__shared_count();
3516private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003517 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518
3519public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003521 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 : __shared_owners_(__refs) {}
3523
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003524#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003525 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003526 void __add_shared() _NOEXCEPT;
3527 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003528#else
3529 _LIBCPP_INLINE_VISIBILITY
3530 void __add_shared() _NOEXCEPT {
3531 __libcpp_atomic_refcount_increment(__shared_owners_);
3532 }
3533 _LIBCPP_INLINE_VISIBILITY
3534 bool __release_shared() _NOEXCEPT {
3535 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3536 __on_zero_shared();
3537 return true;
3538 }
3539 return false;
3540 }
3541#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003542 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003543 long use_count() const _NOEXCEPT {
3544 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3545 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546};
3547
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003548class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549 : private __shared_count
3550{
3551 long __shared_weak_owners_;
3552
3553public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003555 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556 : __shared_count(__refs),
3557 __shared_weak_owners_(__refs) {}
3558protected:
3559 virtual ~__shared_weak_count();
3560
3561public:
Louis Dionne5e0eadd2018-08-01 02:08:59 +00003562#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiselier98848572017-01-17 03:16:26 +00003563 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003564 void __add_shared() _NOEXCEPT;
3565 void __add_weak() _NOEXCEPT;
3566 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003567#else
3568 _LIBCPP_INLINE_VISIBILITY
3569 void __add_shared() _NOEXCEPT {
3570 __shared_count::__add_shared();
3571 }
3572 _LIBCPP_INLINE_VISIBILITY
3573 void __add_weak() _NOEXCEPT {
3574 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3575 }
3576 _LIBCPP_INLINE_VISIBILITY
3577 void __release_shared() _NOEXCEPT {
3578 if (__shared_count::__release_shared())
3579 __release_weak();
3580 }
3581#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003582 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003584 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3585 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586
Howard Hinnant807d6332013-02-25 15:50:36 +00003587 // Define the function out only if we build static libc++ without RTTI.
3588 // Otherwise we may break clients who need to compile their projects with
3589 // -fno-rtti and yet link against a libc++.dylib compiled
3590 // without -fno-rtti.
3591#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003592 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003593#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003595 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596};
3597
3598template <class _Tp, class _Dp, class _Alloc>
3599class __shared_ptr_pointer
3600 : public __shared_weak_count
3601{
3602 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3603public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003606 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607
Howard Hinnant72f73582010-08-11 17:04:31 +00003608#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003609 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003610#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611
3612private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003613 virtual void __on_zero_shared() _NOEXCEPT;
3614 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615};
3616
Howard Hinnant72f73582010-08-11 17:04:31 +00003617#ifndef _LIBCPP_NO_RTTI
3618
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619template <class _Tp, class _Dp, class _Alloc>
3620const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003621__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003623 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624}
3625
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003626#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003627
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628template <class _Tp, class _Dp, class _Alloc>
3629void
Howard Hinnant719bda32011-05-28 14:41:13 +00003630__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631{
3632 __data_.first().second()(__data_.first().first());
3633 __data_.first().second().~_Dp();
3634}
3635
3636template <class _Tp, class _Dp, class _Alloc>
3637void
Howard Hinnant719bda32011-05-28 14:41:13 +00003638__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003640 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3641 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003642 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3643
Eric Fiselierf8898c82015-02-05 23:01:40 +00003644 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003646 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647}
3648
3649template <class _Tp, class _Alloc>
3650class __shared_ptr_emplace
3651 : public __shared_weak_count
3652{
3653 __compressed_pair<_Alloc, _Tp> __data_;
3654public:
3655#ifndef _LIBCPP_HAS_NO_VARIADICS
3656
Howard Hinnant756c69b2010-09-22 16:48:34 +00003657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003659 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660
3661 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003664 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3665 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666
3667#else // _LIBCPP_HAS_NO_VARIADICS
3668
Howard Hinnant756c69b2010-09-22 16:48:34 +00003669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670 __shared_ptr_emplace(_Alloc __a)
3671 : __data_(__a) {}
3672
3673 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3676 : __data_(__a, _Tp(__a0)) {}
3677
3678 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3681 : __data_(__a, _Tp(__a0, __a1)) {}
3682
3683 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3686 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3687
3688#endif // _LIBCPP_HAS_NO_VARIADICS
3689
3690private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003691 virtual void __on_zero_shared() _NOEXCEPT;
3692 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003694 _LIBCPP_INLINE_VISIBILITY
Marshall Clowbdfdea82018-08-28 13:29:30 +00003695 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696};
3697
3698template <class _Tp, class _Alloc>
3699void
Howard Hinnant719bda32011-05-28 14:41:13 +00003700__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701{
3702 __data_.second().~_Tp();
3703}
3704
3705template <class _Tp, class _Alloc>
3706void
Howard Hinnant719bda32011-05-28 14:41:13 +00003707__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003709 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3710 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003711 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003712 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003714 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715}
3716
Erik Pilkington2a398762017-05-25 15:43:31 +00003717struct __shared_ptr_dummy_rebind_allocator_type;
3718template <>
3719class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3720{
3721public:
3722 template <class _Other>
3723 struct rebind
3724 {
3725 typedef allocator<_Other> other;
3726 };
3727};
3728
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003729template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730
3731template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003732class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003734public:
3735 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003736
Eric Fiselierae5b6672016-06-27 01:02:43 +00003737#if _LIBCPP_STD_VER > 14
3738 typedef weak_ptr<_Tp> weak_type;
3739#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740private:
3741 element_type* __ptr_;
3742 __shared_weak_count* __cntrl_;
3743
3744 struct __nat {int __for_bool_;};
3745public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003747 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003749 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003750 template<class _Yp>
3751 explicit shared_ptr(_Yp* __p,
3752 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3753 template<class _Yp, class _Dp>
3754 shared_ptr(_Yp* __p, _Dp __d,
3755 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3756 template<class _Yp, class _Dp, class _Alloc>
3757 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3758 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3760 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003761 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003763 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003767 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003768 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003769#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003771 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003772 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003773 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003774 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003775#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003777 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003778#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003779#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003780 template<class _Yp>
3781 shared_ptr(auto_ptr<_Yp>&& __r,
3782 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003783#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003784 template<class _Yp>
3785 shared_ptr(auto_ptr<_Yp> __r,
3786 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003788#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003790 template <class _Yp, class _Dp>
3791 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3792 typename enable_if
3793 <
3794 !is_lvalue_reference<_Dp>::value &&
3795 !is_array<_Yp>::value &&
3796 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3797 __nat
3798 >::type = __nat());
3799 template <class _Yp, class _Dp>
3800 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3801 typename enable_if
3802 <
3803 is_lvalue_reference<_Dp>::value &&
3804 !is_array<_Yp>::value &&
3805 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3806 __nat
3807 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003808#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003809 template <class _Yp, class _Dp>
3810 shared_ptr(unique_ptr<_Yp, _Dp>,
3811 typename enable_if
3812 <
3813 !is_lvalue_reference<_Dp>::value &&
3814 !is_array<_Yp>::value &&
3815 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3816 __nat
3817 >::type = __nat());
3818 template <class _Yp, class _Dp>
3819 shared_ptr(unique_ptr<_Yp, _Dp>,
3820 typename enable_if
3821 <
3822 is_lvalue_reference<_Dp>::value &&
3823 !is_array<_Yp>::value &&
3824 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3825 __nat
3826 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003827#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828
3829 ~shared_ptr();
3830
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003832 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003833 template<class _Yp>
3834 typename enable_if
3835 <
3836 is_convertible<_Yp*, element_type*>::value,
3837 shared_ptr&
3838 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003840 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003841#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003843 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003844 template<class _Yp>
3845 typename enable_if
3846 <
3847 is_convertible<_Yp*, element_type*>::value,
3848 shared_ptr<_Tp>&
3849 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003851 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003852#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003853 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003855 typename enable_if
3856 <
3857 !is_array<_Yp>::value &&
3858 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003859 shared_ptr
3860 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003861 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003862#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003863#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003864#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003865 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003867 typename enable_if
3868 <
3869 !is_array<_Yp>::value &&
3870 is_convertible<_Yp*, element_type*>::value,
3871 shared_ptr&
3872 >::type
3873 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003875#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003876 template <class _Yp, class _Dp>
3877 typename enable_if
3878 <
3879 !is_array<_Yp>::value &&
3880 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3881 shared_ptr&
3882 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003883#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003885 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003886#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003888 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889#endif
3890
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003892 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003894 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003895 template<class _Yp>
3896 typename enable_if
3897 <
3898 is_convertible<_Yp*, element_type*>::value,
3899 void
3900 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003902 reset(_Yp* __p);
3903 template<class _Yp, class _Dp>
3904 typename enable_if
3905 <
3906 is_convertible<_Yp*, element_type*>::value,
3907 void
3908 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003910 reset(_Yp* __p, _Dp __d);
3911 template<class _Yp, class _Dp, class _Alloc>
3912 typename enable_if
3913 <
3914 is_convertible<_Yp*, element_type*>::value,
3915 void
3916 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003918 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919
Howard Hinnant756c69b2010-09-22 16:48:34 +00003920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003921 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003923 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3924 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003926 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003928 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003930 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003932 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003933 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003934 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003935 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003937 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003938 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003939 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003941 _LIBCPP_INLINE_VISIBILITY
3942 bool
3943 __owner_equivalent(const shared_ptr& __p) const
3944 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945
Howard Hinnant72f73582010-08-11 17:04:31 +00003946#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003949 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003950 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003951 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003952 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003953#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954
3955#ifndef _LIBCPP_HAS_NO_VARIADICS
3956
3957 template<class ..._Args>
3958 static
3959 shared_ptr<_Tp>
3960 make_shared(_Args&& ...__args);
3961
3962 template<class _Alloc, class ..._Args>
3963 static
3964 shared_ptr<_Tp>
3965 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3966
3967#else // _LIBCPP_HAS_NO_VARIADICS
3968
3969 static shared_ptr<_Tp> make_shared();
3970
3971 template<class _A0>
3972 static shared_ptr<_Tp> make_shared(_A0&);
3973
3974 template<class _A0, class _A1>
3975 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3976
3977 template<class _A0, class _A1, class _A2>
3978 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3979
3980 template<class _Alloc>
3981 static shared_ptr<_Tp>
3982 allocate_shared(const _Alloc& __a);
3983
3984 template<class _Alloc, class _A0>
3985 static shared_ptr<_Tp>
3986 allocate_shared(const _Alloc& __a, _A0& __a0);
3987
3988 template<class _Alloc, class _A0, class _A1>
3989 static shared_ptr<_Tp>
3990 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3991
3992 template<class _Alloc, class _A0, class _A1, class _A2>
3993 static shared_ptr<_Tp>
3994 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3995
3996#endif // _LIBCPP_HAS_NO_VARIADICS
3997
3998private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003999 template <class _Yp, bool = is_function<_Yp>::value>
4000 struct __shared_ptr_default_allocator
4001 {
4002 typedef allocator<_Yp> type;
4003 };
4004
4005 template <class _Yp>
4006 struct __shared_ptr_default_allocator<_Yp, true>
4007 {
4008 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
4009 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004011 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004012 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004013 typename enable_if<is_convertible<_OrigPtr*,
4014 const enable_shared_from_this<_Yp>*
4015 >::value,
4016 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004017 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4018 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004020 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004021 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004022 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004023 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4024 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004025 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026 }
4027
Erik Pilkington2a398762017-05-25 15:43:31 +00004028 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004030 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4031 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032};
4033
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004034
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004036inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004037_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004038shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039 : __ptr_(0),
4040 __cntrl_(0)
4041{
4042}
4043
4044template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004045inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004046_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004047shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048 : __ptr_(0),
4049 __cntrl_(0)
4050{
4051}
4052
4053template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004054template<class _Yp>
4055shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4056 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057 : __ptr_(__p)
4058{
4059 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004060 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4061 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4062 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004064 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065}
4066
4067template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004068template<class _Yp, class _Dp>
4069shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4070 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 : __ptr_(__p)
4072{
4073#ifndef _LIBCPP_NO_EXCEPTIONS
4074 try
4075 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004076#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004077 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4078 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4079 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004080 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081#ifndef _LIBCPP_NO_EXCEPTIONS
4082 }
4083 catch (...)
4084 {
4085 __d(__p);
4086 throw;
4087 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004088#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089}
4090
4091template<class _Tp>
4092template<class _Dp>
4093shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4094 : __ptr_(0)
4095{
4096#ifndef _LIBCPP_NO_EXCEPTIONS
4097 try
4098 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004099#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004100 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4101 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4102 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103#ifndef _LIBCPP_NO_EXCEPTIONS
4104 }
4105 catch (...)
4106 {
4107 __d(__p);
4108 throw;
4109 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004110#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111}
4112
4113template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004114template<class _Yp, class _Dp, class _Alloc>
4115shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4116 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117 : __ptr_(__p)
4118{
4119#ifndef _LIBCPP_NO_EXCEPTIONS
4120 try
4121 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004122#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004124 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125 typedef __allocator_destructor<_A2> _D2;
4126 _A2 __a2(__a);
4127 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004128 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4129 _CntrlBlk(__p, __d, __a);
4130 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004131 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132#ifndef _LIBCPP_NO_EXCEPTIONS
4133 }
4134 catch (...)
4135 {
4136 __d(__p);
4137 throw;
4138 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004139#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140}
4141
4142template<class _Tp>
4143template<class _Dp, class _Alloc>
4144shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4145 : __ptr_(0)
4146{
4147#ifndef _LIBCPP_NO_EXCEPTIONS
4148 try
4149 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004150#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004152 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153 typedef __allocator_destructor<_A2> _D2;
4154 _A2 __a2(__a);
4155 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004156 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4157 _CntrlBlk(__p, __d, __a);
4158 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159#ifndef _LIBCPP_NO_EXCEPTIONS
4160 }
4161 catch (...)
4162 {
4163 __d(__p);
4164 throw;
4165 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004166#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167}
4168
4169template<class _Tp>
4170template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004171inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004172shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173 : __ptr_(__p),
4174 __cntrl_(__r.__cntrl_)
4175{
4176 if (__cntrl_)
4177 __cntrl_->__add_shared();
4178}
4179
4180template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004181inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004182shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183 : __ptr_(__r.__ptr_),
4184 __cntrl_(__r.__cntrl_)
4185{
4186 if (__cntrl_)
4187 __cntrl_->__add_shared();
4188}
4189
4190template<class _Tp>
4191template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004194 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004195 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196 : __ptr_(__r.__ptr_),
4197 __cntrl_(__r.__cntrl_)
4198{
4199 if (__cntrl_)
4200 __cntrl_->__add_shared();
4201}
4202
Howard Hinnant74279a52010-09-04 23:28:19 +00004203#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204
4205template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004206inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004207shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208 : __ptr_(__r.__ptr_),
4209 __cntrl_(__r.__cntrl_)
4210{
4211 __r.__ptr_ = 0;
4212 __r.__cntrl_ = 0;
4213}
4214
4215template<class _Tp>
4216template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004217inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004219 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004220 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221 : __ptr_(__r.__ptr_),
4222 __cntrl_(__r.__cntrl_)
4223{
4224 __r.__ptr_ = 0;
4225 __r.__cntrl_ = 0;
4226}
4227
Howard Hinnant74279a52010-09-04 23:28:19 +00004228#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229
Marshall Clowb22274f2017-01-24 22:22:33 +00004230#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004232template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004233#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004234shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004236shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004238 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239 : __ptr_(__r.get())
4240{
4241 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4242 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004243 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244 __r.release();
4245}
Marshall Clowb22274f2017-01-24 22:22:33 +00004246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247
4248template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004249template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004250#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4252#else
4253shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4254#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004255 typename enable_if
4256 <
4257 !is_lvalue_reference<_Dp>::value &&
4258 !is_array<_Yp>::value &&
4259 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4260 __nat
4261 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262 : __ptr_(__r.get())
4263{
Marshall Clow35cde742015-05-10 13:59:45 +00004264#if _LIBCPP_STD_VER > 11
4265 if (__ptr_ == nullptr)
4266 __cntrl_ = nullptr;
4267 else
4268#endif
4269 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004270 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4271 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4272 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004273 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004274 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275 __r.release();
4276}
4277
4278template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004279template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4282#else
4283shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4284#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004285 typename enable_if
4286 <
4287 is_lvalue_reference<_Dp>::value &&
4288 !is_array<_Yp>::value &&
4289 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4290 __nat
4291 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 : __ptr_(__r.get())
4293{
Marshall Clow35cde742015-05-10 13:59:45 +00004294#if _LIBCPP_STD_VER > 11
4295 if (__ptr_ == nullptr)
4296 __cntrl_ = nullptr;
4297 else
4298#endif
4299 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004300 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004301 typedef __shared_ptr_pointer<_Yp*,
4302 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004303 _AllocT > _CntrlBlk;
4304 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004305 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004306 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 __r.release();
4308}
4309
4310#ifndef _LIBCPP_HAS_NO_VARIADICS
4311
4312template<class _Tp>
4313template<class ..._Args>
4314shared_ptr<_Tp>
4315shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4316{
Marshall Clowdec75c72017-12-05 04:09:49 +00004317 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4319 typedef allocator<_CntrlBlk> _A2;
4320 typedef __allocator_destructor<_A2> _D2;
4321 _A2 __a2;
4322 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004323 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324 shared_ptr<_Tp> __r;
4325 __r.__ptr_ = __hold2.get()->get();
4326 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004327 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328 return __r;
4329}
4330
4331template<class _Tp>
4332template<class _Alloc, class ..._Args>
4333shared_ptr<_Tp>
4334shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4335{
Marshall Clowdec75c72017-12-05 04:09:49 +00004336 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004338 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339 typedef __allocator_destructor<_A2> _D2;
4340 _A2 __a2(__a);
4341 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004342 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4343 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344 shared_ptr<_Tp> __r;
4345 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004346 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004347 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 return __r;
4349}
4350
4351#else // _LIBCPP_HAS_NO_VARIADICS
4352
4353template<class _Tp>
4354shared_ptr<_Tp>
4355shared_ptr<_Tp>::make_shared()
4356{
Marshall Clowdec75c72017-12-05 04:09:49 +00004357 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4359 typedef allocator<_CntrlBlk> _Alloc2;
4360 typedef __allocator_destructor<_Alloc2> _D2;
4361 _Alloc2 __alloc2;
4362 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4363 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4364 shared_ptr<_Tp> __r;
4365 __r.__ptr_ = __hold2.get()->get();
4366 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004367 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368 return __r;
4369}
4370
4371template<class _Tp>
4372template<class _A0>
4373shared_ptr<_Tp>
4374shared_ptr<_Tp>::make_shared(_A0& __a0)
4375{
Marshall Clowdec75c72017-12-05 04:09:49 +00004376 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4378 typedef allocator<_CntrlBlk> _Alloc2;
4379 typedef __allocator_destructor<_Alloc2> _D2;
4380 _Alloc2 __alloc2;
4381 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4382 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4383 shared_ptr<_Tp> __r;
4384 __r.__ptr_ = __hold2.get()->get();
4385 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004386 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004387 return __r;
4388}
4389
4390template<class _Tp>
4391template<class _A0, class _A1>
4392shared_ptr<_Tp>
4393shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4394{
Marshall Clowdec75c72017-12-05 04:09:49 +00004395 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4397 typedef allocator<_CntrlBlk> _Alloc2;
4398 typedef __allocator_destructor<_Alloc2> _D2;
4399 _Alloc2 __alloc2;
4400 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4401 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4402 shared_ptr<_Tp> __r;
4403 __r.__ptr_ = __hold2.get()->get();
4404 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004405 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406 return __r;
4407}
4408
4409template<class _Tp>
4410template<class _A0, class _A1, class _A2>
4411shared_ptr<_Tp>
4412shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4413{
Marshall Clowdec75c72017-12-05 04:09:49 +00004414 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4416 typedef allocator<_CntrlBlk> _Alloc2;
4417 typedef __allocator_destructor<_Alloc2> _D2;
4418 _Alloc2 __alloc2;
4419 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4420 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4421 shared_ptr<_Tp> __r;
4422 __r.__ptr_ = __hold2.get()->get();
4423 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004424 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425 return __r;
4426}
4427
4428template<class _Tp>
4429template<class _Alloc>
4430shared_ptr<_Tp>
4431shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4432{
Marshall Clowdec75c72017-12-05 04:09:49 +00004433 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004435 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436 typedef __allocator_destructor<_Alloc2> _D2;
4437 _Alloc2 __alloc2(__a);
4438 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004439 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4440 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004441 shared_ptr<_Tp> __r;
4442 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004443 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004444 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445 return __r;
4446}
4447
4448template<class _Tp>
4449template<class _Alloc, class _A0>
4450shared_ptr<_Tp>
4451shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4452{
Marshall Clowdec75c72017-12-05 04:09:49 +00004453 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004454 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004455 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456 typedef __allocator_destructor<_Alloc2> _D2;
4457 _Alloc2 __alloc2(__a);
4458 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004459 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4460 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461 shared_ptr<_Tp> __r;
4462 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004463 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004464 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004465 return __r;
4466}
4467
4468template<class _Tp>
4469template<class _Alloc, class _A0, class _A1>
4470shared_ptr<_Tp>
4471shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4472{
Marshall Clowdec75c72017-12-05 04:09:49 +00004473 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004475 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476 typedef __allocator_destructor<_Alloc2> _D2;
4477 _Alloc2 __alloc2(__a);
4478 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004479 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4480 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481 shared_ptr<_Tp> __r;
4482 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004483 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004484 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485 return __r;
4486}
4487
4488template<class _Tp>
4489template<class _Alloc, class _A0, class _A1, class _A2>
4490shared_ptr<_Tp>
4491shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4492{
Marshall Clowdec75c72017-12-05 04:09:49 +00004493 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004495 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496 typedef __allocator_destructor<_Alloc2> _D2;
4497 _Alloc2 __alloc2(__a);
4498 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004499 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4500 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501 shared_ptr<_Tp> __r;
4502 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004503 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004504 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004505 return __r;
4506}
4507
4508#endif // _LIBCPP_HAS_NO_VARIADICS
4509
4510template<class _Tp>
4511shared_ptr<_Tp>::~shared_ptr()
4512{
4513 if (__cntrl_)
4514 __cntrl_->__release_shared();
4515}
4516
4517template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004518inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004520shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521{
4522 shared_ptr(__r).swap(*this);
4523 return *this;
4524}
4525
4526template<class _Tp>
4527template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004528inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004529typename enable_if
4530<
Marshall Clow7e384b72017-01-10 16:59:33 +00004531 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004532 shared_ptr<_Tp>&
4533>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004534shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004535{
4536 shared_ptr(__r).swap(*this);
4537 return *this;
4538}
4539
Howard Hinnant74279a52010-09-04 23:28:19 +00004540#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004541
4542template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004543inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004545shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004547 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548 return *this;
4549}
4550
4551template<class _Tp>
4552template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004553inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004554typename enable_if
4555<
Marshall Clow7e384b72017-01-10 16:59:33 +00004556 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004557 shared_ptr<_Tp>&
4558>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4560{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004561 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004562 return *this;
4563}
4564
Marshall Clowb22274f2017-01-24 22:22:33 +00004565#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566template<class _Tp>
4567template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004568inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004569typename enable_if
4570<
4571 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004572 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004573 shared_ptr<_Tp>
4574>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004575shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4576{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004577 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578 return *this;
4579}
Marshall Clowb22274f2017-01-24 22:22:33 +00004580#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004581
4582template<class _Tp>
4583template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004584inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004585typename enable_if
4586<
4587 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004588 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004589 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004590 shared_ptr<_Tp>&
4591>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4593{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004594 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595 return *this;
4596}
4597
Howard Hinnant74279a52010-09-04 23:28:19 +00004598#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004599
Marshall Clowb22274f2017-01-24 22:22:33 +00004600#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004601template<class _Tp>
4602template<class _Yp>
4603inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004604typename enable_if
4605<
4606 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004607 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004608 shared_ptr<_Tp>&
4609>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004610shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611{
4612 shared_ptr(__r).swap(*this);
4613 return *this;
4614}
Marshall Clowb22274f2017-01-24 22:22:33 +00004615#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004616
4617template<class _Tp>
4618template <class _Yp, class _Dp>
4619inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004620typename enable_if
4621<
4622 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004623 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004624 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004625 shared_ptr<_Tp>&
4626>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004627shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4628{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004629 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004630 return *this;
4631}
4632
Howard Hinnant74279a52010-09-04 23:28:19 +00004633#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004634
4635template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004636inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004637void
Howard Hinnant719bda32011-05-28 14:41:13 +00004638shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004639{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004640 _VSTD::swap(__ptr_, __r.__ptr_);
4641 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004642}
4643
4644template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004645inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004646void
Howard Hinnant719bda32011-05-28 14:41:13 +00004647shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004648{
4649 shared_ptr().swap(*this);
4650}
4651
4652template<class _Tp>
4653template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004654inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004655typename enable_if
4656<
Marshall Clow7e384b72017-01-10 16:59:33 +00004657 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004658 void
4659>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004660shared_ptr<_Tp>::reset(_Yp* __p)
4661{
4662 shared_ptr(__p).swap(*this);
4663}
4664
4665template<class _Tp>
4666template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004667inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004668typename enable_if
4669<
Marshall Clow7e384b72017-01-10 16:59:33 +00004670 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004671 void
4672>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004673shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4674{
4675 shared_ptr(__p, __d).swap(*this);
4676}
4677
4678template<class _Tp>
4679template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004680inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004681typename enable_if
4682<
Marshall Clow7e384b72017-01-10 16:59:33 +00004683 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004684 void
4685>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004686shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4687{
4688 shared_ptr(__p, __d, __a).swap(*this);
4689}
4690
4691#ifndef _LIBCPP_HAS_NO_VARIADICS
4692
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004693template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004694inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004695typename enable_if
4696<
4697 !is_array<_Tp>::value,
4698 shared_ptr<_Tp>
4699>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004700make_shared(_Args&& ...__args)
4701{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004702 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004703}
4704
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004705template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004706inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004707typename enable_if
4708<
4709 !is_array<_Tp>::value,
4710 shared_ptr<_Tp>
4711>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004712allocate_shared(const _Alloc& __a, _Args&& ...__args)
4713{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004714 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004715}
4716
4717#else // _LIBCPP_HAS_NO_VARIADICS
4718
4719template<class _Tp>
4720inline _LIBCPP_INLINE_VISIBILITY
4721shared_ptr<_Tp>
4722make_shared()
4723{
4724 return shared_ptr<_Tp>::make_shared();
4725}
4726
4727template<class _Tp, class _A0>
4728inline _LIBCPP_INLINE_VISIBILITY
4729shared_ptr<_Tp>
4730make_shared(_A0& __a0)
4731{
4732 return shared_ptr<_Tp>::make_shared(__a0);
4733}
4734
4735template<class _Tp, class _A0, class _A1>
4736inline _LIBCPP_INLINE_VISIBILITY
4737shared_ptr<_Tp>
4738make_shared(_A0& __a0, _A1& __a1)
4739{
4740 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4741}
4742
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004743template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004744inline _LIBCPP_INLINE_VISIBILITY
4745shared_ptr<_Tp>
4746make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4747{
4748 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4749}
4750
4751template<class _Tp, class _Alloc>
4752inline _LIBCPP_INLINE_VISIBILITY
4753shared_ptr<_Tp>
4754allocate_shared(const _Alloc& __a)
4755{
4756 return shared_ptr<_Tp>::allocate_shared(__a);
4757}
4758
4759template<class _Tp, class _Alloc, class _A0>
4760inline _LIBCPP_INLINE_VISIBILITY
4761shared_ptr<_Tp>
4762allocate_shared(const _Alloc& __a, _A0& __a0)
4763{
4764 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4765}
4766
4767template<class _Tp, class _Alloc, class _A0, class _A1>
4768inline _LIBCPP_INLINE_VISIBILITY
4769shared_ptr<_Tp>
4770allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4771{
4772 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4773}
4774
4775template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4776inline _LIBCPP_INLINE_VISIBILITY
4777shared_ptr<_Tp>
4778allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4779{
4780 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4781}
4782
4783#endif // _LIBCPP_HAS_NO_VARIADICS
4784
4785template<class _Tp, class _Up>
4786inline _LIBCPP_INLINE_VISIBILITY
4787bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004788operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004789{
4790 return __x.get() == __y.get();
4791}
4792
4793template<class _Tp, class _Up>
4794inline _LIBCPP_INLINE_VISIBILITY
4795bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004796operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004797{
4798 return !(__x == __y);
4799}
4800
4801template<class _Tp, class _Up>
4802inline _LIBCPP_INLINE_VISIBILITY
4803bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004804operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004805{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004806#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004807 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4808 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004809#else
4810 return less<>()(__x.get(), __y.get());
4811#endif
4812
Howard Hinnantb17caf92012-02-21 21:02:58 +00004813}
4814
4815template<class _Tp, class _Up>
4816inline _LIBCPP_INLINE_VISIBILITY
4817bool
4818operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4819{
4820 return __y < __x;
4821}
4822
4823template<class _Tp, class _Up>
4824inline _LIBCPP_INLINE_VISIBILITY
4825bool
4826operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4827{
4828 return !(__y < __x);
4829}
4830
4831template<class _Tp, class _Up>
4832inline _LIBCPP_INLINE_VISIBILITY
4833bool
4834operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4835{
4836 return !(__x < __y);
4837}
4838
4839template<class _Tp>
4840inline _LIBCPP_INLINE_VISIBILITY
4841bool
4842operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4843{
4844 return !__x;
4845}
4846
4847template<class _Tp>
4848inline _LIBCPP_INLINE_VISIBILITY
4849bool
4850operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4851{
4852 return !__x;
4853}
4854
4855template<class _Tp>
4856inline _LIBCPP_INLINE_VISIBILITY
4857bool
4858operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4859{
4860 return static_cast<bool>(__x);
4861}
4862
4863template<class _Tp>
4864inline _LIBCPP_INLINE_VISIBILITY
4865bool
4866operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4867{
4868 return static_cast<bool>(__x);
4869}
4870
4871template<class _Tp>
4872inline _LIBCPP_INLINE_VISIBILITY
4873bool
4874operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4875{
4876 return less<_Tp*>()(__x.get(), nullptr);
4877}
4878
4879template<class _Tp>
4880inline _LIBCPP_INLINE_VISIBILITY
4881bool
4882operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4883{
4884 return less<_Tp*>()(nullptr, __x.get());
4885}
4886
4887template<class _Tp>
4888inline _LIBCPP_INLINE_VISIBILITY
4889bool
4890operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4891{
4892 return nullptr < __x;
4893}
4894
4895template<class _Tp>
4896inline _LIBCPP_INLINE_VISIBILITY
4897bool
4898operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4899{
4900 return __x < nullptr;
4901}
4902
4903template<class _Tp>
4904inline _LIBCPP_INLINE_VISIBILITY
4905bool
4906operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4907{
4908 return !(nullptr < __x);
4909}
4910
4911template<class _Tp>
4912inline _LIBCPP_INLINE_VISIBILITY
4913bool
4914operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4915{
4916 return !(__x < nullptr);
4917}
4918
4919template<class _Tp>
4920inline _LIBCPP_INLINE_VISIBILITY
4921bool
4922operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4923{
4924 return !(__x < nullptr);
4925}
4926
4927template<class _Tp>
4928inline _LIBCPP_INLINE_VISIBILITY
4929bool
4930operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4931{
4932 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004933}
4934
4935template<class _Tp>
4936inline _LIBCPP_INLINE_VISIBILITY
4937void
Howard Hinnant719bda32011-05-28 14:41:13 +00004938swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004939{
4940 __x.swap(__y);
4941}
4942
4943template<class _Tp, class _Up>
4944inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004945typename enable_if
4946<
4947 !is_array<_Tp>::value && !is_array<_Up>::value,
4948 shared_ptr<_Tp>
4949>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004950static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004951{
4952 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4953}
4954
4955template<class _Tp, class _Up>
4956inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004957typename enable_if
4958<
4959 !is_array<_Tp>::value && !is_array<_Up>::value,
4960 shared_ptr<_Tp>
4961>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004962dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963{
4964 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4965 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4966}
4967
4968template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004969typename enable_if
4970<
4971 is_array<_Tp>::value == is_array<_Up>::value,
4972 shared_ptr<_Tp>
4973>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004974const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004976 typedef typename remove_extent<_Tp>::type _RTp;
4977 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004978}
4979
Howard Hinnant72f73582010-08-11 17:04:31 +00004980#ifndef _LIBCPP_NO_RTTI
4981
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982template<class _Dp, class _Tp>
4983inline _LIBCPP_INLINE_VISIBILITY
4984_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004985get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986{
4987 return __p.template __get_deleter<_Dp>();
4988}
4989
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004990#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004991
Howard Hinnantc51e1022010-05-11 19:42:16 +00004992template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004993class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004994{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004995public:
4996 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004997private:
4998 element_type* __ptr_;
4999 __shared_weak_count* __cntrl_;
5000
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005001public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005003 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005004 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005005 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5006 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005008 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005009 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005010 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5011 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005012
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005015 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005016 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005017 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5018 _NOEXCEPT;
5019#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005020 ~weak_ptr();
5021
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005023 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005024 template<class _Yp>
5025 typename enable_if
5026 <
5027 is_convertible<_Yp*, element_type*>::value,
5028 weak_ptr&
5029 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005031 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5032
5033#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5034
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005036 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5037 template<class _Yp>
5038 typename enable_if
5039 <
5040 is_convertible<_Yp*, element_type*>::value,
5041 weak_ptr&
5042 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005044 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5045
5046#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5047
5048 template<class _Yp>
5049 typename enable_if
5050 <
5051 is_convertible<_Yp*, element_type*>::value,
5052 weak_ptr&
5053 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005055 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005056
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005058 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005060 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005061
Howard Hinnant756c69b2010-09-22 16:48:34 +00005062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005063 long use_count() const _NOEXCEPT
5064 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005066 bool expired() const _NOEXCEPT
5067 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5068 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005069 template<class _Up>
5070 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005071 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005072 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005073 template<class _Up>
5074 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005075 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005076 {return __cntrl_ < __r.__cntrl_;}
5077
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005078 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5079 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005080};
5081
5082template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005083inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005084_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005085weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005086 : __ptr_(0),
5087 __cntrl_(0)
5088{
5089}
5090
5091template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005092inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005093weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005094 : __ptr_(__r.__ptr_),
5095 __cntrl_(__r.__cntrl_)
5096{
5097 if (__cntrl_)
5098 __cntrl_->__add_weak();
5099}
5100
5101template<class _Tp>
5102template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005103inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005104weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005105 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005106 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005107 : __ptr_(__r.__ptr_),
5108 __cntrl_(__r.__cntrl_)
5109{
5110 if (__cntrl_)
5111 __cntrl_->__add_weak();
5112}
5113
5114template<class _Tp>
5115template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005116inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005117weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005118 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005119 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005120 : __ptr_(__r.__ptr_),
5121 __cntrl_(__r.__cntrl_)
5122{
5123 if (__cntrl_)
5124 __cntrl_->__add_weak();
5125}
5126
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005127#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5128
5129template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005130inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005131weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5132 : __ptr_(__r.__ptr_),
5133 __cntrl_(__r.__cntrl_)
5134{
5135 __r.__ptr_ = 0;
5136 __r.__cntrl_ = 0;
5137}
5138
5139template<class _Tp>
5140template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005141inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005142weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5143 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5144 _NOEXCEPT
5145 : __ptr_(__r.__ptr_),
5146 __cntrl_(__r.__cntrl_)
5147{
5148 __r.__ptr_ = 0;
5149 __r.__cntrl_ = 0;
5150}
5151
5152#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5153
Howard Hinnantc51e1022010-05-11 19:42:16 +00005154template<class _Tp>
5155weak_ptr<_Tp>::~weak_ptr()
5156{
5157 if (__cntrl_)
5158 __cntrl_->__release_weak();
5159}
5160
5161template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005162inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005163weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005164weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005165{
5166 weak_ptr(__r).swap(*this);
5167 return *this;
5168}
5169
5170template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005171template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005172inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005173typename enable_if
5174<
5175 is_convertible<_Yp*, _Tp*>::value,
5176 weak_ptr<_Tp>&
5177>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005178weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005179{
5180 weak_ptr(__r).swap(*this);
5181 return *this;
5182}
5183
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005184#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5185
5186template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005187inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005188weak_ptr<_Tp>&
5189weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5190{
5191 weak_ptr(_VSTD::move(__r)).swap(*this);
5192 return *this;
5193}
5194
Howard Hinnantc51e1022010-05-11 19:42:16 +00005195template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005196template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005197inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005198typename enable_if
5199<
5200 is_convertible<_Yp*, _Tp*>::value,
5201 weak_ptr<_Tp>&
5202>::type
5203weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5204{
5205 weak_ptr(_VSTD::move(__r)).swap(*this);
5206 return *this;
5207}
5208
5209#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5210
5211template<class _Tp>
5212template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005213inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005214typename enable_if
5215<
5216 is_convertible<_Yp*, _Tp*>::value,
5217 weak_ptr<_Tp>&
5218>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005219weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005220{
5221 weak_ptr(__r).swap(*this);
5222 return *this;
5223}
5224
5225template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005226inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227void
Howard Hinnant719bda32011-05-28 14:41:13 +00005228weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005229{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005230 _VSTD::swap(__ptr_, __r.__ptr_);
5231 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005232}
5233
5234template<class _Tp>
5235inline _LIBCPP_INLINE_VISIBILITY
5236void
Howard Hinnant719bda32011-05-28 14:41:13 +00005237swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005238{
5239 __x.swap(__y);
5240}
5241
5242template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005243inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005244void
Howard Hinnant719bda32011-05-28 14:41:13 +00005245weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005246{
5247 weak_ptr().swap(*this);
5248}
5249
5250template<class _Tp>
5251template<class _Yp>
5252shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005253 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005254 : __ptr_(__r.__ptr_),
5255 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5256{
5257 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005258 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005259}
5260
5261template<class _Tp>
5262shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005263weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005264{
5265 shared_ptr<_Tp> __r;
5266 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5267 if (__r.__cntrl_)
5268 __r.__ptr_ = __ptr_;
5269 return __r;
5270}
5271
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005272#if _LIBCPP_STD_VER > 14
5273template <class _Tp = void> struct owner_less;
5274#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005275template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005277
5278template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005279struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005280 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005281{
5282 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005283 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005284 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005285 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005286 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005287 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005288 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005289 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005290 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005291 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005292};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005293
5294template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005295struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005296 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5297{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005298 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005299 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005300 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005301 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005302 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005303 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005304 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005305 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005306 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005307 {return __x.owner_before(__y);}
5308};
5309
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005310#if _LIBCPP_STD_VER > 14
5311template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005312struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005313{
5314 template <class _Tp, class _Up>
5315 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005316 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005317 {return __x.owner_before(__y);}
5318 template <class _Tp, class _Up>
5319 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005320 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005321 {return __x.owner_before(__y);}
5322 template <class _Tp, class _Up>
5323 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005324 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005325 {return __x.owner_before(__y);}
5326 template <class _Tp, class _Up>
5327 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005328 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005329 {return __x.owner_before(__y);}
5330 typedef void is_transparent;
5331};
5332#endif
5333
Howard Hinnantc51e1022010-05-11 19:42:16 +00005334template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005335class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005336{
5337 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005338protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005340 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005342 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005344 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5345 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005347 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005348public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005350 shared_ptr<_Tp> shared_from_this()
5351 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005353 shared_ptr<_Tp const> shared_from_this() const
5354 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005355
Eric Fiselier84006862016-06-02 00:15:35 +00005356#if _LIBCPP_STD_VER > 14
5357 _LIBCPP_INLINE_VISIBILITY
5358 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5359 { return __weak_this_; }
5360
5361 _LIBCPP_INLINE_VISIBILITY
5362 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5363 { return __weak_this_; }
5364#endif // _LIBCPP_STD_VER > 14
5365
Howard Hinnantc51e1022010-05-11 19:42:16 +00005366 template <class _Up> friend class shared_ptr;
5367};
5368
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005369template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005370struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005371{
5372 typedef shared_ptr<_Tp> argument_type;
5373 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005374
Howard Hinnant756c69b2010-09-22 16:48:34 +00005375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005376 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005377 {
5378 return hash<_Tp*>()(__ptr.get());
5379 }
5380};
5381
Howard Hinnantc834c512011-11-29 18:15:50 +00005382template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005383inline _LIBCPP_INLINE_VISIBILITY
5384basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005385operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005386
Eric Fiselier9b492672016-06-18 02:12:53 +00005387
5388#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005389
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005390class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005391{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005392 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005393public:
5394 void lock() _NOEXCEPT;
5395 void unlock() _NOEXCEPT;
5396
5397private:
5398 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5399 __sp_mut(const __sp_mut&);
5400 __sp_mut& operator=(const __sp_mut&);
5401
Howard Hinnant8331b762013-03-06 23:30:19 +00005402 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005403};
5404
Mehdi Amini228053d2017-05-04 17:08:54 +00005405_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5406__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005407
5408template <class _Tp>
5409inline _LIBCPP_INLINE_VISIBILITY
5410bool
5411atomic_is_lock_free(const shared_ptr<_Tp>*)
5412{
5413 return false;
5414}
5415
5416template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005417_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005418shared_ptr<_Tp>
5419atomic_load(const shared_ptr<_Tp>* __p)
5420{
5421 __sp_mut& __m = __get_sp_mut(__p);
5422 __m.lock();
5423 shared_ptr<_Tp> __q = *__p;
5424 __m.unlock();
5425 return __q;
5426}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005427
Howard Hinnant9fa30202012-07-30 01:40:57 +00005428template <class _Tp>
5429inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005430_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005431shared_ptr<_Tp>
5432atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5433{
5434 return atomic_load(__p);
5435}
5436
5437template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005438_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005439void
5440atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5441{
5442 __sp_mut& __m = __get_sp_mut(__p);
5443 __m.lock();
5444 __p->swap(__r);
5445 __m.unlock();
5446}
5447
5448template <class _Tp>
5449inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005450_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005451void
5452atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5453{
5454 atomic_store(__p, __r);
5455}
5456
5457template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005458_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005459shared_ptr<_Tp>
5460atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5461{
5462 __sp_mut& __m = __get_sp_mut(__p);
5463 __m.lock();
5464 __p->swap(__r);
5465 __m.unlock();
5466 return __r;
5467}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005468
Howard Hinnant9fa30202012-07-30 01:40:57 +00005469template <class _Tp>
5470inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005471_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005472shared_ptr<_Tp>
5473atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5474{
5475 return atomic_exchange(__p, __r);
5476}
5477
5478template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005479_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005480bool
5481atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5482{
Marshall Clow4201ee82016-05-18 17:50:13 +00005483 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005484 __sp_mut& __m = __get_sp_mut(__p);
5485 __m.lock();
5486 if (__p->__owner_equivalent(*__v))
5487 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005488 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005489 *__p = __w;
5490 __m.unlock();
5491 return true;
5492 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005493 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005494 *__v = *__p;
5495 __m.unlock();
5496 return false;
5497}
5498
5499template <class _Tp>
5500inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005501_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005502bool
5503atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5504{
5505 return atomic_compare_exchange_strong(__p, __v, __w);
5506}
5507
5508template <class _Tp>
5509inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005510_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005511bool
5512atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5513 shared_ptr<_Tp> __w, memory_order, memory_order)
5514{
5515 return atomic_compare_exchange_strong(__p, __v, __w);
5516}
5517
5518template <class _Tp>
5519inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005520_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005521bool
5522atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5523 shared_ptr<_Tp> __w, memory_order, memory_order)
5524{
5525 return atomic_compare_exchange_weak(__p, __v, __w);
5526}
5527
Eric Fiselier9b492672016-06-18 02:12:53 +00005528#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005529
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005530//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005531#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5532# ifndef _LIBCPP_CXX03_LANG
5533enum class pointer_safety : unsigned char {
5534 relaxed,
5535 preferred,
5536 strict
5537};
5538# endif
5539#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005540struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005541{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005542 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005543 {
5544 relaxed,
5545 preferred,
5546 strict
5547 };
5548
Howard Hinnant49e145e2012-10-30 19:06:59 +00005549 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005550
Howard Hinnant756c69b2010-09-22 16:48:34 +00005551 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005552 pointer_safety() : __v_() {}
5553
5554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005555 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005557 operator int() const {return __v_;}
5558};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005559#endif
5560
5561#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne5e0eadd2018-08-01 02:08:59 +00005562 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierab6bb302017-01-05 01:15:42 +00005563_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5564#else
5565// This function is only offered in C++03 under ABI v1.
5566# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5567inline _LIBCPP_INLINE_VISIBILITY
5568pointer_safety get_pointer_safety() _NOEXCEPT {
5569 return pointer_safety::relaxed;
5570}
5571# endif
5572#endif
5573
Howard Hinnantc51e1022010-05-11 19:42:16 +00005574
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005575_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5576_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5577_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005578_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005579
5580template <class _Tp>
5581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005582_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005583undeclare_reachable(_Tp* __p)
5584{
5585 return static_cast<_Tp*>(__undeclare_reachable(__p));
5586}
5587
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005588_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005589
Marshall Clow8982dcd2015-07-13 20:04:56 +00005590// --- Helper for container swap --
5591template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005592inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005593void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5594#if _LIBCPP_STD_VER >= 14
5595 _NOEXCEPT
5596#else
5597 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5598#endif
5599{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005600 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005601 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5602}
5603
5604template <typename _Alloc>
5605_LIBCPP_INLINE_VISIBILITY
5606void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5607#if _LIBCPP_STD_VER >= 14
5608 _NOEXCEPT
5609#else
5610 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5611#endif
5612{
5613 using _VSTD::swap;
5614 swap(__a1, __a2);
5615}
5616
5617template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005618inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005619void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5620
Marshall Clowff91de82015-08-18 19:51:37 +00005621template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005622struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005623 _Traits::propagate_on_container_move_assignment::value
5624#if _LIBCPP_STD_VER > 14
5625 || _Traits::is_always_equal::value
5626#else
5627 && is_nothrow_move_assignable<_Alloc>::value
5628#endif
5629 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005630
Marshall Clowa591b9a2016-07-11 21:38:08 +00005631
5632#ifndef _LIBCPP_HAS_NO_VARIADICS
5633template <class _Tp, class _Alloc>
5634struct __temp_value {
5635 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005636
Marshall Clowa591b9a2016-07-11 21:38:08 +00005637 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5638 _Alloc &__a;
5639
5640 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5641 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005642
Marshall Clowa591b9a2016-07-11 21:38:08 +00005643 template<class... _Args>
Peter Collingbournecb126152018-08-15 17:49:30 +00005644 _LIBCPP_NO_CFI
5645 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5646 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5647 _VSTD::forward<_Args>(__args)...);
5648 }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005649
Marshall Clowa591b9a2016-07-11 21:38:08 +00005650 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5651 };
5652#endif
5653
Marshall Clowe46031a2018-07-02 18:41:15 +00005654template<typename _Alloc, typename = void, typename = void>
Marshall Clow82c90aa2018-01-11 19:36:22 +00005655struct __is_allocator : false_type {};
5656
5657template<typename _Alloc>
5658struct __is_allocator<_Alloc,
Marshall Clowe46031a2018-07-02 18:41:15 +00005659 typename __void_t<typename _Alloc::value_type>::type,
5660 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5661 >
Marshall Clow82c90aa2018-01-11 19:36:22 +00005662 : true_type {};
Marshall Clow82c90aa2018-01-11 19:36:22 +00005663
Howard Hinnantc51e1022010-05-11 19:42:16 +00005664_LIBCPP_END_NAMESPACE_STD
5665
Eric Fiselierf4433a32017-05-31 22:07:49 +00005666_LIBCPP_POP_MACROS
5667
Howard Hinnantc51e1022010-05-11 19:42:16 +00005668#endif // _LIBCPP_MEMORY