blob: 021bd136af4c5250b90aac652ee84f9083e9ee0b [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
46 static pointer pointer_to(<details>) noexcept;
47};
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
Marshall Clowb22274f2017-01-24 22:22:33 +0000215template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
217template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000218class auto_ptr // 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
670
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000671#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000673#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674
Eric Fiselierf4433a32017-05-31 22:07:49 +0000675_LIBCPP_PUSH_MACROS
676#include <__undef_macros>
677
678
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679_LIBCPP_BEGIN_NAMESPACE_STD
680
Eric Fiselier89659d12015-07-07 00:27:16 +0000681template <class _ValueType>
682inline _LIBCPP_ALWAYS_INLINE
683_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_RELAXED) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_RELAXED);
688#else
689 return *__value;
690#endif
691}
692
Kuba Breckade9d6792016-09-04 09:55:12 +0000693template <class _ValueType>
694inline _LIBCPP_ALWAYS_INLINE
695_ValueType __libcpp_acquire_load(_ValueType const* __value) {
696#if !defined(_LIBCPP_HAS_NO_THREADS) && \
697 defined(__ATOMIC_ACQUIRE) && \
698 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
699 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
700#else
701 return *__value;
702#endif
703}
704
Marshall Clow78dbe462016-11-14 18:22:19 +0000705// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707template <class _Tp> class allocator;
708
709template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000710class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711{
712public:
713 typedef void* pointer;
714 typedef const void* const_pointer;
715 typedef void value_type;
716
717 template <class _Up> struct rebind {typedef allocator<_Up> other;};
718};
719
Howard Hinnant9f771052012-01-19 23:15:22 +0000720template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000721class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000722{
723public:
724 typedef const void* pointer;
725 typedef const void* const_pointer;
726 typedef const void value_type;
727
728 template <class _Up> struct rebind {typedef allocator<_Up> other;};
729};
730
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731// pointer_traits
732
Marshall Clow0be70c32017-06-14 21:23:57 +0000733template <class _Tp, class = void>
734struct __has_element_type : false_type {};
735
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000737struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000738 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
740template <class _Ptr, bool = __has_element_type<_Ptr>::value>
741struct __pointer_traits_element_type;
742
743template <class _Ptr>
744struct __pointer_traits_element_type<_Ptr, true>
745{
746 typedef typename _Ptr::element_type type;
747};
748
749#ifndef _LIBCPP_HAS_NO_VARIADICS
750
751template <template <class, class...> class _Sp, class _Tp, class ..._Args>
752struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
753{
754 typedef typename _Sp<_Tp, _Args...>::element_type type;
755};
756
757template <template <class, class...> class _Sp, class _Tp, class ..._Args>
758struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
759{
760 typedef _Tp type;
761};
762
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000763#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
765template <template <class> class _Sp, class _Tp>
766struct __pointer_traits_element_type<_Sp<_Tp>, true>
767{
768 typedef typename _Sp<_Tp>::element_type type;
769};
770
771template <template <class> class _Sp, class _Tp>
772struct __pointer_traits_element_type<_Sp<_Tp>, false>
773{
774 typedef _Tp type;
775};
776
777template <template <class, class> class _Sp, class _Tp, class _A0>
778struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
779{
780 typedef typename _Sp<_Tp, _A0>::element_type type;
781};
782
783template <template <class, class> class _Sp, class _Tp, class _A0>
784struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
785{
786 typedef _Tp type;
787};
788
789template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
790struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
791{
792 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
793};
794
795template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
796struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
797{
798 typedef _Tp type;
799};
800
801template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
802 class _A1, class _A2>
803struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
804{
805 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
806};
807
808template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
809 class _A1, class _A2>
810struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
811{
812 typedef _Tp type;
813};
814
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000815#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
Marshall Clow0be70c32017-06-14 21:23:57 +0000817template <class _Tp, class = void>
818struct __has_difference_type : false_type {};
819
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000821struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000822 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
824template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
825struct __pointer_traits_difference_type
826{
827 typedef ptrdiff_t type;
828};
829
830template <class _Ptr>
831struct __pointer_traits_difference_type<_Ptr, true>
832{
833 typedef typename _Ptr::difference_type type;
834};
835
836template <class _Tp, class _Up>
837struct __has_rebind
838{
839private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000840 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 template <class _Xp> static __two __test(...);
842 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
843public:
844 static const bool value = sizeof(__test<_Tp>(0)) == 1;
845};
846
847template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
848struct __pointer_traits_rebind
849{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000850#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851 typedef typename _Tp::template rebind<_Up> type;
852#else
853 typedef typename _Tp::template rebind<_Up>::other type;
854#endif
855};
856
857#ifndef _LIBCPP_HAS_NO_VARIADICS
858
859template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
861{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000862#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
864#else
865 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
866#endif
867};
868
869template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
870struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
871{
872 typedef _Sp<_Up, _Args...> type;
873};
874
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000875#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876
877template <template <class> class _Sp, class _Tp, class _Up>
878struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
879{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000880#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881 typedef typename _Sp<_Tp>::template rebind<_Up> type;
882#else
883 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
884#endif
885};
886
887template <template <class> class _Sp, class _Tp, class _Up>
888struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
889{
890 typedef _Sp<_Up> type;
891};
892
893template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
894struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
895{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000896#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
898#else
899 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
900#endif
901};
902
903template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
905{
906 typedef _Sp<_Up, _A0> type;
907};
908
909template <template <class, class, class> class _Sp, class _Tp, class _A0,
910 class _A1, class _Up>
911struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
912{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000913#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
915#else
916 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
917#endif
918};
919
920template <template <class, class, class> class _Sp, class _Tp, class _A0,
921 class _A1, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
923{
924 typedef _Sp<_Up, _A0, _A1> type;
925};
926
927template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
928 class _A1, class _A2, class _Up>
929struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
930{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
933#else
934 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
935#endif
936};
937
938template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
939 class _A1, class _A2, class _Up>
940struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
941{
942 typedef _Sp<_Up, _A0, _A1, _A2> type;
943};
944
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000945#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
947template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000948struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949{
950 typedef _Ptr pointer;
951 typedef typename __pointer_traits_element_type<pointer>::type element_type;
952 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
953
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000954#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000955 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956#else
957 template <class _Up> struct rebind
958 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000959#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960
961private:
962 struct __nat {};
963public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 static pointer pointer_to(typename conditional<is_void<element_type>::value,
966 __nat, element_type>::type& __r)
967 {return pointer::pointer_to(__r);}
968};
969
970template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000971struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972{
973 typedef _Tp* pointer;
974 typedef _Tp element_type;
975 typedef ptrdiff_t difference_type;
976
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000977#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 template <class _Up> using rebind = _Up*;
979#else
980 template <class _Up> struct rebind {typedef _Up* other;};
981#endif
982
983private:
984 struct __nat {};
985public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000988 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000989 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990};
991
Eric Fiselierae3ab842015-08-23 02:56:05 +0000992template <class _From, class _To>
993struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000994#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000995 typedef typename pointer_traits<_From>::template rebind<_To> type;
996#else
997 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
998#endif
999};
1000
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001// allocator_traits
1002
Marshall Clow0be70c32017-06-14 21:23:57 +00001003template <class _Tp, class = void>
1004struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005
1006template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001007struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001008 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009
1010namespace __pointer_type_imp
1011{
1012
1013template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1014struct __pointer_type
1015{
1016 typedef typename _Dp::pointer type;
1017};
1018
1019template <class _Tp, class _Dp>
1020struct __pointer_type<_Tp, _Dp, false>
1021{
1022 typedef _Tp* type;
1023};
1024
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001025} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026
1027template <class _Tp, class _Dp>
1028struct __pointer_type
1029{
1030 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1031};
1032
Marshall Clow0be70c32017-06-14 21:23:57 +00001033template <class _Tp, class = void>
1034struct __has_const_pointer : false_type {};
1035
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001037struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001038 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039
1040template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1041struct __const_pointer
1042{
1043 typedef typename _Alloc::const_pointer type;
1044};
1045
1046template <class _Tp, class _Ptr, class _Alloc>
1047struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1048{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001049#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1051#else
1052 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1053#endif
1054};
1055
Marshall Clow0be70c32017-06-14 21:23:57 +00001056template <class _Tp, class = void>
1057struct __has_void_pointer : false_type {};
1058
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001060struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001061 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062
1063template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1064struct __void_pointer
1065{
1066 typedef typename _Alloc::void_pointer type;
1067};
1068
1069template <class _Ptr, class _Alloc>
1070struct __void_pointer<_Ptr, _Alloc, false>
1071{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001072#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1074#else
1075 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1076#endif
1077};
1078
Marshall Clow0be70c32017-06-14 21:23:57 +00001079template <class _Tp, class = void>
1080struct __has_const_void_pointer : false_type {};
1081
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001083struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001084 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
1086template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1087struct __const_void_pointer
1088{
1089 typedef typename _Alloc::const_void_pointer type;
1090};
1091
1092template <class _Ptr, class _Alloc>
1093struct __const_void_pointer<_Ptr, _Alloc, false>
1094{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1097#else
1098 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1099#endif
1100};
1101
Howard Hinnantc834c512011-11-29 18:15:50 +00001102template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001103inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001104_Tp*
1105__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106{
1107 return __p;
1108}
1109
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001110#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111template <class _Pointer>
1112inline _LIBCPP_INLINE_VISIBILITY
1113typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001114__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001116 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001118#else
1119template <class _Pointer>
1120inline _LIBCPP_INLINE_VISIBILITY
1121auto
1122__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1123-> decltype(pointer_traits<_Pointer>::to_address(__p))
1124{
1125 return pointer_traits<_Pointer>::to_address(__p);
1126}
1127
1128template <class _Pointer, class... _None>
1129inline _LIBCPP_INLINE_VISIBILITY
1130auto
1131__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1132{
1133 return _VSTD::__to_raw_pointer(__p.operator->());
1134}
1135
1136template <class _Tp>
1137inline _LIBCPP_INLINE_VISIBILITY constexpr
1138_Tp*
1139to_address(_Tp* __p) _NOEXCEPT
1140{
1141 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1142 return __p;
1143}
1144
1145template <class _Pointer>
1146inline _LIBCPP_INLINE_VISIBILITY
1147auto
1148to_address(const _Pointer& __p) _NOEXCEPT
1149{
1150 return _VSTD::__to_raw_pointer(__p);
1151}
1152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153
Marshall Clow0be70c32017-06-14 21:23:57 +00001154template <class _Tp, class = void>
1155struct __has_size_type : false_type {};
1156
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001158struct __has_size_type<_Tp,
1159 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001161template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162struct __size_type
1163{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001164 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165};
1166
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001167template <class _Alloc, class _DiffType>
1168struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169{
1170 typedef typename _Alloc::size_type type;
1171};
1172
Marshall Clow0be70c32017-06-14 21:23:57 +00001173template <class _Tp, class = void>
1174struct __has_propagate_on_container_copy_assignment : false_type {};
1175
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001177struct __has_propagate_on_container_copy_assignment<_Tp,
1178 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1179 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
1181template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1182struct __propagate_on_container_copy_assignment
1183{
1184 typedef false_type type;
1185};
1186
1187template <class _Alloc>
1188struct __propagate_on_container_copy_assignment<_Alloc, true>
1189{
1190 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1191};
1192
Marshall Clow0be70c32017-06-14 21:23:57 +00001193template <class _Tp, class = void>
1194struct __has_propagate_on_container_move_assignment : false_type {};
1195
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001197struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001198 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1199 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200
1201template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1202struct __propagate_on_container_move_assignment
1203{
1204 typedef false_type type;
1205};
1206
1207template <class _Alloc>
1208struct __propagate_on_container_move_assignment<_Alloc, true>
1209{
1210 typedef typename _Alloc::propagate_on_container_move_assignment type;
1211};
1212
Marshall Clow0be70c32017-06-14 21:23:57 +00001213template <class _Tp, class = void>
1214struct __has_propagate_on_container_swap : false_type {};
1215
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001217struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001218 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1219 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
1221template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1222struct __propagate_on_container_swap
1223{
1224 typedef false_type type;
1225};
1226
1227template <class _Alloc>
1228struct __propagate_on_container_swap<_Alloc, true>
1229{
1230 typedef typename _Alloc::propagate_on_container_swap type;
1231};
1232
Marshall Clow0be70c32017-06-14 21:23:57 +00001233template <class _Tp, class = void>
1234struct __has_is_always_equal : false_type {};
1235
Marshall Clow0b587562015-06-02 16:34:03 +00001236template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001237struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001238 typename __void_t<typename _Tp::is_always_equal>::type>
1239 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001240
1241template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1242struct __is_always_equal
1243{
1244 typedef typename _VSTD::is_empty<_Alloc>::type type;
1245};
1246
1247template <class _Alloc>
1248struct __is_always_equal<_Alloc, true>
1249{
1250 typedef typename _Alloc::is_always_equal type;
1251};
1252
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1254struct __has_rebind_other
1255{
1256private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001257 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 template <class _Xp> static __two __test(...);
1259 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1260public:
1261 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1262};
1263
1264template <class _Tp, class _Up>
1265struct __has_rebind_other<_Tp, _Up, false>
1266{
1267 static const bool value = false;
1268};
1269
1270template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1271struct __allocator_traits_rebind
1272{
1273 typedef typename _Tp::template rebind<_Up>::other type;
1274};
1275
1276#ifndef _LIBCPP_HAS_NO_VARIADICS
1277
1278template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1279struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1280{
1281 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1282};
1283
1284template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1285struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1286{
1287 typedef _Alloc<_Up, _Args...> type;
1288};
1289
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001290#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
1292template <template <class> class _Alloc, class _Tp, class _Up>
1293struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1294{
1295 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1296};
1297
1298template <template <class> class _Alloc, class _Tp, class _Up>
1299struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1300{
1301 typedef _Alloc<_Up> type;
1302};
1303
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1305struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1306{
1307 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1308};
1309
1310template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1312{
1313 typedef _Alloc<_Up, _A0> type;
1314};
1315
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1319{
1320 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1321};
1322
1323template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1326{
1327 typedef _Alloc<_Up, _A0, _A1> type;
1328};
1329
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1333{
1334 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1335};
1336
1337template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1338 class _A1, class _A2, class _Up>
1339struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1340{
1341 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1342};
1343
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001344#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001346#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347
1348template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1349auto
1350__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001351 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352
1353template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1354auto
1355__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1356 -> false_type;
1357
1358template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1359struct __has_allocate_hint
1360 : integral_constant<bool,
1361 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001362 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 declval<_SizeType>(),
1364 declval<_ConstVoidPtr>())),
1365 true_type>::value>
1366{
1367};
1368
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001369#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370
1371template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1372struct __has_allocate_hint
1373 : true_type
1374{
1375};
1376
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001377#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001379#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
1381template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001382decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1383 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 true_type())
1385__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1386
1387template <class _Alloc, class _Pointer, class ..._Args>
1388false_type
1389__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1390
1391template <class _Alloc, class _Pointer, class ..._Args>
1392struct __has_construct
1393 : integral_constant<bool,
1394 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001395 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 declval<_Pointer>(),
1397 declval<_Args>()...)),
1398 true_type>::value>
1399{
1400};
1401
1402template <class _Alloc, class _Pointer>
1403auto
1404__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1405 -> decltype(__a.destroy(__p), true_type());
1406
1407template <class _Alloc, class _Pointer>
1408auto
1409__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1410 -> false_type;
1411
1412template <class _Alloc, class _Pointer>
1413struct __has_destroy
1414 : integral_constant<bool,
1415 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001416 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 declval<_Pointer>())),
1418 true_type>::value>
1419{
1420};
1421
1422template <class _Alloc>
1423auto
1424__has_max_size_test(_Alloc&& __a)
1425 -> decltype(__a.max_size(), true_type());
1426
1427template <class _Alloc>
1428auto
1429__has_max_size_test(const volatile _Alloc& __a)
1430 -> false_type;
1431
1432template <class _Alloc>
1433struct __has_max_size
1434 : integral_constant<bool,
1435 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001436 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 true_type>::value>
1438{
1439};
1440
1441template <class _Alloc>
1442auto
1443__has_select_on_container_copy_construction_test(_Alloc&& __a)
1444 -> decltype(__a.select_on_container_copy_construction(), true_type());
1445
1446template <class _Alloc>
1447auto
1448__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1449 -> false_type;
1450
1451template <class _Alloc>
1452struct __has_select_on_container_copy_construction
1453 : integral_constant<bool,
1454 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001455 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 true_type>::value>
1457{
1458};
1459
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001460#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461
1462#ifndef _LIBCPP_HAS_NO_VARIADICS
1463
1464template <class _Alloc, class _Pointer, class ..._Args>
1465struct __has_construct
1466 : false_type
1467{
1468};
1469
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001470#else // _LIBCPP_HAS_NO_VARIADICS
1471
1472template <class _Alloc, class _Pointer, class _Args>
1473struct __has_construct
1474 : false_type
1475{
1476};
1477
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001478#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479
1480template <class _Alloc, class _Pointer>
1481struct __has_destroy
1482 : false_type
1483{
1484};
1485
1486template <class _Alloc>
1487struct __has_max_size
1488 : true_type
1489{
1490};
1491
1492template <class _Alloc>
1493struct __has_select_on_container_copy_construction
1494 : false_type
1495{
1496};
1497
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001498#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001500template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1501struct __alloc_traits_difference_type
1502{
1503 typedef typename pointer_traits<_Ptr>::difference_type type;
1504};
1505
1506template <class _Alloc, class _Ptr>
1507struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1508{
1509 typedef typename _Alloc::difference_type type;
1510};
1511
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001513struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514{
1515 typedef _Alloc allocator_type;
1516 typedef typename allocator_type::value_type value_type;
1517
1518 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1519 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1520 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1521 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1522
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001523 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1524 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525
1526 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1527 propagate_on_container_copy_assignment;
1528 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1529 propagate_on_container_move_assignment;
1530 typedef typename __propagate_on_container_swap<allocator_type>::type
1531 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001532 typedef typename __is_always_equal<allocator_type>::type
1533 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001535#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001537 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001539#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 template <class _Tp> struct rebind_alloc
1541 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1542 template <class _Tp> struct rebind_traits
1543 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001544#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545
Marshall Clow0e58cae2017-11-26 02:55:38 +00001546 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 static pointer allocate(allocator_type& __a, size_type __n)
1548 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001549 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001551 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1553
Howard Hinnant756c69b2010-09-22 16:48:34 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001555 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 {__a.deallocate(__p, __n);}
1557
1558#ifndef _LIBCPP_HAS_NO_VARIADICS
1559 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001562 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001563 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001564#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001566 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001567 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 {
1569 ::new ((void*)__p) _Tp();
1570 }
1571 template <class _Tp, class _A0>
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 {
1575 ::new ((void*)__p) _Tp(__a0);
1576 }
1577 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1);
1583 }
1584 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001585 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001586 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 const _A1& __a1, const _A2& __a2)
1588 {
1589 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1590 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001591#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592
1593 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 static void destroy(allocator_type& __a, _Tp* __p)
1596 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1597
Howard Hinnant756c69b2010-09-22 16:48:34 +00001598 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001599 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1601
Howard Hinnant756c69b2010-09-22 16:48:34 +00001602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 static allocator_type
1604 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001605 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 __has_select_on_container_copy_construction<const allocator_type>(),
1607 __a);}
1608
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001609 template <class _Ptr>
1610 _LIBCPP_INLINE_VISIBILITY
1611 static
1612 void
1613 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1614 {
1615 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1616 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1617 }
1618
1619 template <class _Tp>
1620 _LIBCPP_INLINE_VISIBILITY
1621 static
1622 typename enable_if
1623 <
1624 (is_same<allocator_type, allocator<_Tp> >::value
1625 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1626 is_trivially_move_constructible<_Tp>::value,
1627 void
1628 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001629 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 {
1631 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001632 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001633 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001634 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001635 __begin2 += _Np;
1636 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001637 }
1638
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001639 template <class _Iter, class _Ptr>
1640 _LIBCPP_INLINE_VISIBILITY
1641 static
1642 void
1643 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1644 {
1645 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1646 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1647 }
1648
1649 template <class _Tp>
1650 _LIBCPP_INLINE_VISIBILITY
1651 static
1652 typename enable_if
1653 <
1654 (is_same<allocator_type, allocator<_Tp> >::value
1655 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1656 is_trivially_move_constructible<_Tp>::value,
1657 void
1658 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001659 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001660 {
1661 typedef typename remove_const<_Tp>::type _Vp;
1662 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001663 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001664 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001665 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001666 __begin2 += _Np;
1667 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001668 }
1669
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001670 template <class _Ptr>
1671 _LIBCPP_INLINE_VISIBILITY
1672 static
1673 void
1674 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1675 {
1676 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001677 {
1678 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1679 --__end2;
1680 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001681 }
1682
1683 template <class _Tp>
1684 _LIBCPP_INLINE_VISIBILITY
1685 static
1686 typename enable_if
1687 <
1688 (is_same<allocator_type, allocator<_Tp> >::value
1689 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1690 is_trivially_move_constructible<_Tp>::value,
1691 void
1692 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001693 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001694 {
1695 ptrdiff_t _Np = __end1 - __begin1;
1696 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001697 if (_Np > 0)
1698 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001699 }
1700
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701private:
1702
Howard Hinnant756c69b2010-09-22 16:48:34 +00001703 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001704 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 const_void_pointer __hint, true_type)
1706 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001708 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001709 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 {return __a.allocate(__n);}
1711
1712#ifndef _LIBCPP_HAS_NO_VARIADICS
1713 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001716 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1720 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001721 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001723#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724
1725 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1728 {__a.destroy(__p);}
1729 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 static void __destroy(false_type, allocator_type&, _Tp* __p)
1732 {
1733 __p->~_Tp();
1734 }
1735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001737 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001739 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf817a352017-12-05 15:56:26 +00001740 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow33daa162015-10-25 19:34:04 +00001741 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742
Howard Hinnant756c69b2010-09-22 16:48:34 +00001743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001745 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001749 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 {return __a;}
1751};
1752
Marshall Clow940e01c2015-04-07 05:21:38 +00001753template <class _Traits, class _Tp>
1754struct __rebind_alloc_helper
1755{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001756#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001757 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001758#else
1759 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1760#endif
1761};
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763// allocator
1764
1765template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001766class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767{
1768public:
1769 typedef size_t size_type;
1770 typedef ptrdiff_t difference_type;
1771 typedef _Tp* pointer;
1772 typedef const _Tp* const_pointer;
1773 typedef _Tp& reference;
1774 typedef const _Tp& const_reference;
1775 typedef _Tp value_type;
1776
Howard Hinnant4931e092011-06-02 21:38:57 +00001777 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001778 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001779
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1781
Marshall Clowbc759762018-03-20 23:02:53 +00001782 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1783 allocator() _NOEXCEPT {}
1784
1785 template <class _Up>
1786 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1787 allocator(const allocator<_Up>&) _NOEXCEPT {}
1788
Howard Hinnant719bda32011-05-28 14:41:13 +00001789 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001790 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001791 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001792 {return _VSTD::addressof(__x);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001793 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1794 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001795 {
1796 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001797 __throw_length_error("allocator<T>::allocate(size_t n)"
1798 " 'n' exceeds maximum supported size");
Eric Fiselier06548ab2018-03-22 04:42:56 +00001799 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Marshall Clow813ffb32016-03-03 12:04:39 +00001800 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001801 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier06548ab2018-03-22 04:42:56 +00001802 {_VSTD::__libcpp_deallocate((void*)__p, __alignof(_Tp));}
Howard Hinnant719bda32011-05-28 14:41:13 +00001803 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1804 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001805#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 template <class _Up, class... _Args>
1807 _LIBCPP_INLINE_VISIBILITY
1808 void
1809 construct(_Up* __p, _Args&&... __args)
1810 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001811 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001813#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 _LIBCPP_INLINE_VISIBILITY
1815 void
1816 construct(pointer __p)
1817 {
1818 ::new((void*)__p) _Tp();
1819 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001820# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001821
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 template <class _A0>
1823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001824 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 construct(pointer __p, _A0& __a0)
1826 {
1827 ::new((void*)__p) _Tp(__a0);
1828 }
1829 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, const _A0& __a0)
1833 {
1834 ::new((void*)__p) _Tp(__a0);
1835 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001836# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 template <class _A0, class _A1>
1838 _LIBCPP_INLINE_VISIBILITY
1839 void
1840 construct(pointer __p, _A0& __a0, _A1& __a1)
1841 {
1842 ::new((void*)__p) _Tp(__a0, __a1);
1843 }
1844 template <class _A0, class _A1>
1845 _LIBCPP_INLINE_VISIBILITY
1846 void
1847 construct(pointer __p, const _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, _A0& __a0, const _A1& __a1)
1855 {
1856 ::new((void*)__p) _Tp(__a0, __a1);
1857 }
1858 template <class _A0, class _A1>
1859 _LIBCPP_INLINE_VISIBILITY
1860 void
1861 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1862 {
1863 ::new((void*)__p) _Tp(__a0, __a1);
1864 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001865#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1867};
1868
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001870class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871{
1872public:
1873 typedef size_t size_type;
1874 typedef ptrdiff_t difference_type;
1875 typedef const _Tp* pointer;
1876 typedef const _Tp* const_pointer;
1877 typedef const _Tp& reference;
1878 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001879 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001880
1881 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001882 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001883
1884 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1885
Marshall Clowbc759762018-03-20 23:02:53 +00001886 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1887 allocator() _NOEXCEPT {}
1888
1889 template <class _Up>
1890 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1891 allocator(const allocator<_Up>&) _NOEXCEPT {}
1892
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001893 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1894 {return _VSTD::addressof(__x);}
1895 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001896 {
1897 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001898 __throw_length_error("allocator<const T>::allocate(size_t n)"
1899 " 'n' exceeds maximum supported size");
Eric Fiselier06548ab2018-03-22 04:42:56 +00001900 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001901 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001902 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier06548ab2018-03-22 04:42:56 +00001903 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __alignof(_Tp));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001904 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1905 {return size_type(~0) / sizeof(_Tp);}
1906#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1907 template <class _Up, class... _Args>
1908 _LIBCPP_INLINE_VISIBILITY
1909 void
1910 construct(_Up* __p, _Args&&... __args)
1911 {
1912 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1913 }
1914#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1915 _LIBCPP_INLINE_VISIBILITY
1916 void
1917 construct(pointer __p)
1918 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001919 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001920 }
1921# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001922
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001923 template <class _A0>
1924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001925 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001926 construct(pointer __p, _A0& __a0)
1927 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001928 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001929 }
1930 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, const _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 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001937# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1938 template <class _A0, class _A1>
1939 _LIBCPP_INLINE_VISIBILITY
1940 void
1941 construct(pointer __p, _A0& __a0, _A1& __a1)
1942 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001943 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001944 }
1945 template <class _A0, class _A1>
1946 _LIBCPP_INLINE_VISIBILITY
1947 void
1948 construct(pointer __p, const _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, _A0& __a0, const _A1& __a1)
1956 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001957 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001958 }
1959 template <class _A0, class _A1>
1960 _LIBCPP_INLINE_VISIBILITY
1961 void
1962 construct(pointer __p, const _A0& __a0, 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#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1967 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1968};
1969
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970template <class _Tp, class _Up>
1971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001972bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973
1974template <class _Tp, class _Up>
1975inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001976bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977
1978template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001979class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980 : public iterator<output_iterator_tag,
1981 _Tp, // purposefully not C++03
1982 ptrdiff_t, // purposefully not C++03
1983 _Tp*, // purposefully not C++03
1984 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1985{
1986private:
1987 _OutputIterator __x_;
1988public:
1989 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1990 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1991 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1992 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001993#if _LIBCPP_STD_VER >= 14
1994 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1995 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1996#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1998 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1999 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00002000#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00002001 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00002002#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003};
2004
2005template <class _Tp>
Peter Collingbournecd000192018-01-17 19:32:35 +00002006_LIBCPP_NO_CFI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00002008get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
2010 pair<_Tp*, ptrdiff_t> __r(0, 0);
2011 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2012 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2013 / sizeof(_Tp);
2014 if (__n > __m)
2015 __n = __m;
2016 while (__n > 0)
2017 {
Richard Smith0657be12018-02-01 22:24:45 +00002018#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier06548ab2018-03-22 04:42:56 +00002019 if (__is_overaligned_for_new(__alignof(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002020 {
2021 std::align_val_t __al =
2022 std::align_val_t(std::alignment_of<_Tp>::value);
2023 __r.first = static_cast<_Tp*>(::operator new(
2024 __n * sizeof(_Tp), __al, nothrow));
2025 } else {
2026 __r.first = static_cast<_Tp*>(::operator new(
2027 __n * sizeof(_Tp), nothrow));
2028 }
2029#else
Eric Fiselier06548ab2018-03-22 04:42:56 +00002030 if (__is_overaligned_for_new(__alignof(_Tp)))
Richard Smith0657be12018-02-01 22:24:45 +00002031 {
2032 // Since aligned operator new is unavailable, return an empty
2033 // buffer rather than one with invalid alignment.
2034 return __r;
2035 }
2036
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smith0657be12018-02-01 22:24:45 +00002038#endif
2039
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040 if (__r.first)
2041 {
2042 __r.second = __n;
2043 break;
2044 }
2045 __n /= 2;
2046 }
2047 return __r;
2048}
2049
2050template <class _Tp>
2051inline _LIBCPP_INLINE_VISIBILITY
Richard Smith0657be12018-02-01 22:24:45 +00002052void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2053{
Eric Fiselier06548ab2018-03-22 04:42:56 +00002054 _VSTD::__libcpp_deallocate((void*)__p, __alignof(_Tp));
Richard Smith0657be12018-02-01 22:24:45 +00002055}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056
Marshall Clowb22274f2017-01-24 22:22:33 +00002057#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058template <class _Tp>
2059struct auto_ptr_ref
2060{
2061 _Tp* __ptr_;
2062};
2063
2064template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002065class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066{
2067private:
2068 _Tp* __ptr_;
2069public:
2070 typedef _Tp element_type;
2071
2072 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2073 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2074 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2075 : __ptr_(__p.release()) {}
2076 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2077 {reset(__p.release()); return *this;}
2078 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2079 {reset(__p.release()); return *this;}
2080 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2081 {reset(__p.__ptr_); return *this;}
2082 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2083
2084 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2085 {return *__ptr_;}
2086 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2087 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2088 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2089 {
2090 _Tp* __t = __ptr_;
2091 __ptr_ = 0;
2092 return __t;
2093 }
2094 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2095 {
2096 if (__ptr_ != __p)
2097 delete __ptr_;
2098 __ptr_ = __p;
2099 }
2100
2101 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2102 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2103 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2104 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2105 {return auto_ptr<_Up>(release());}
2106};
2107
2108template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002109class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110{
2111public:
2112 typedef void element_type;
2113};
Marshall Clowb22274f2017-01-24 22:22:33 +00002114#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115
Eric Fiselier9d355982017-04-12 23:45:53 +00002116template <class _Tp, int _Idx,
2117 bool _CanBeEmptyBase =
2118 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2119struct __compressed_pair_elem {
2120 typedef _Tp _ParamT;
2121 typedef _Tp& reference;
2122 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123
Eric Fiselier9d355982017-04-12 23:45:53 +00002124#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002125 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126
Eric Fiselier9d355982017-04-12 23:45:53 +00002127 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002128 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2129 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002130 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002131 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002132 __compressed_pair_elem(_Up&& __u)
2133 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134
Eric Fiselier9d355982017-04-12 23:45:53 +00002135 template <class... _Args, size_t... _Indexes>
2136 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2137 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2138 __tuple_indices<_Indexes...>)
2139 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2140#else
Alex Lorenz76132112017-11-09 17:54:49 +00002141 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2142 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002143 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2144#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145
Alex Lorenz76132112017-11-09 17:54:49 +00002146 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2147 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002148 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002151 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152};
2153
Eric Fiselier9d355982017-04-12 23:45:53 +00002154template <class _Tp, int _Idx>
2155struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2156 typedef _Tp _ParamT;
2157 typedef _Tp& reference;
2158 typedef const _Tp& const_reference;
2159 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160
Eric Fiselier9d355982017-04-12 23:45:53 +00002161#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002162 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163
Eric Fiselier9d355982017-04-12 23:45:53 +00002164 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002165 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2166 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002167 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002168 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002169 __compressed_pair_elem(_Up&& __u)
2170 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171
Eric Fiselier9d355982017-04-12 23:45:53 +00002172 template <class... _Args, size_t... _Indexes>
2173 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2174 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2175 __tuple_indices<_Indexes...>)
2176 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2177#else
Alex Lorenz76132112017-11-09 17:54:49 +00002178 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2179 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002180 __compressed_pair_elem(_ParamT __p)
2181 : __value_type(std::forward<_ParamT>(__p)) {}
2182#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183
Alex Lorenz76132112017-11-09 17:54:49 +00002184 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2185 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002186 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187};
2188
Eric Fiselier9d355982017-04-12 23:45:53 +00002189// Tag used to construct the second element of the compressed pair.
2190struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191
2192template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002193class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2194 private __compressed_pair_elem<_T2, 1> {
2195 typedef __compressed_pair_elem<_T1, 0> _Base1;
2196 typedef __compressed_pair_elem<_T2, 1> _Base2;
2197
2198 // NOTE: This static assert should never fire because __compressed_pair
2199 // is *almost never* used in a scenario where it's possible for T1 == T2.
2200 // (The exception is std::function where it is possible that the function
2201 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002202 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002203 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2204 "The current implementation is NOT ABI-compatible with the previous "
2205 "implementation for this configuration");
2206
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002208#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002209 template <bool _Dummy = true,
2210 class = typename enable_if<
2211 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2212 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2213 >::type
2214 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002215 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002216 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2219 __compressed_pair>::value,
2220 bool>::type = true>
2221 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2222 __compressed_pair(_Tp&& __t)
2223 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224
Eric Fiselier9d355982017-04-12 23:45:53 +00002225 template <class _Tp>
2226 _LIBCPP_INLINE_VISIBILITY constexpr
2227 __compressed_pair(__second_tag, _Tp&& __t)
2228 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229
Eric Fiselier9d355982017-04-12 23:45:53 +00002230 template <class _U1, class _U2>
2231 _LIBCPP_INLINE_VISIBILITY constexpr
2232 __compressed_pair(_U1&& __t1, _U2&& __t2)
2233 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234
Eric Fiselier9d355982017-04-12 23:45:53 +00002235 template <class... _Args1, class... _Args2>
2236 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2237 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2238 tuple<_Args2...> __second_args)
2239 : _Base1(__pc, _VSTD::move(__first_args),
2240 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2241 _Base2(__pc, _VSTD::move(__second_args),
2242 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002243
Eric Fiselier9d355982017-04-12 23:45:53 +00002244#else
2245 _LIBCPP_INLINE_VISIBILITY
2246 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002247
Eric Fiselier9d355982017-04-12 23:45:53 +00002248 _LIBCPP_INLINE_VISIBILITY explicit
2249 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002250
Eric Fiselier9d355982017-04-12 23:45:53 +00002251 _LIBCPP_INLINE_VISIBILITY
2252 __compressed_pair(__second_tag, _T2 __t2)
2253 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254
Eric Fiselier9d355982017-04-12 23:45:53 +00002255 _LIBCPP_INLINE_VISIBILITY
2256 __compressed_pair(_T1 __t1, _T2 __t2)
2257 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2258#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259
Eric Fiselier9d355982017-04-12 23:45:53 +00002260 _LIBCPP_INLINE_VISIBILITY
2261 typename _Base1::reference first() _NOEXCEPT {
2262 return static_cast<_Base1&>(*this).__get();
2263 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264
Eric Fiselier9d355982017-04-12 23:45:53 +00002265 _LIBCPP_INLINE_VISIBILITY
2266 typename _Base1::const_reference first() const _NOEXCEPT {
2267 return static_cast<_Base1 const&>(*this).__get();
2268 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269
Eric Fiselier9d355982017-04-12 23:45:53 +00002270 _LIBCPP_INLINE_VISIBILITY
2271 typename _Base2::reference second() _NOEXCEPT {
2272 return static_cast<_Base2&>(*this).__get();
2273 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274
Eric Fiselier9d355982017-04-12 23:45:53 +00002275 _LIBCPP_INLINE_VISIBILITY
2276 typename _Base2::const_reference second() const _NOEXCEPT {
2277 return static_cast<_Base2 const&>(*this).__get();
2278 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279
Eric Fiselier9d355982017-04-12 23:45:53 +00002280 _LIBCPP_INLINE_VISIBILITY
2281 void swap(__compressed_pair& __x)
2282 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2283 __is_nothrow_swappable<_T2>::value)
2284 {
2285 using std::swap;
2286 swap(first(), __x.first());
2287 swap(second(), __x.second());
2288 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289};
2290
2291template <class _T1, class _T2>
2292inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002293void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2294 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2295 __is_nothrow_swappable<_T2>::value) {
2296 __x.swap(__y);
2297}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002299// default_delete
2300
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002302struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002303 static_assert(!is_function<_Tp>::value,
2304 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002305#ifndef _LIBCPP_CXX03_LANG
2306 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002307#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002308 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002309#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002310 template <class _Up>
2311 _LIBCPP_INLINE_VISIBILITY
2312 default_delete(const default_delete<_Up>&,
2313 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2314 0) _NOEXCEPT {}
2315
2316 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2317 static_assert(sizeof(_Tp) > 0,
2318 "default_delete can not delete incomplete type");
2319 static_assert(!is_void<_Tp>::value,
2320 "default_delete can not delete incomplete type");
2321 delete __ptr;
2322 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323};
2324
2325template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002326struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2327private:
2328 template <class _Up>
2329 struct _EnableIfConvertible
2330 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2331
Howard Hinnant4500ca52011-12-18 21:19:44 +00002332public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002333#ifndef _LIBCPP_CXX03_LANG
2334 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002335#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002336 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002337#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002338
2339 template <class _Up>
2340 _LIBCPP_INLINE_VISIBILITY
2341 default_delete(const default_delete<_Up[]>&,
2342 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2343
2344 template <class _Up>
2345 _LIBCPP_INLINE_VISIBILITY
2346 typename _EnableIfConvertible<_Up>::type
2347 operator()(_Up* __ptr) const _NOEXCEPT {
2348 static_assert(sizeof(_Tp) > 0,
2349 "default_delete can not delete incomplete type");
2350 static_assert(!is_void<_Tp>::value,
2351 "default_delete can not delete void type");
2352 delete[] __ptr;
2353 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354};
2355
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356
Eric Fiselier6779b062017-04-16 02:06:25 +00002357
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002358#ifndef _LIBCPP_CXX03_LANG
2359template <class _Deleter>
2360struct __unique_ptr_deleter_sfinae {
2361 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2362 typedef const _Deleter& __lval_ref_type;
2363 typedef _Deleter&& __good_rval_ref_type;
2364 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365};
2366
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002367template <class _Deleter>
2368struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2369 typedef const _Deleter& __lval_ref_type;
2370 typedef const _Deleter&& __bad_rval_ref_type;
2371 typedef false_type __enable_rval_overload;
2372};
2373
2374template <class _Deleter>
2375struct __unique_ptr_deleter_sfinae<_Deleter&> {
2376 typedef _Deleter& __lval_ref_type;
2377 typedef _Deleter&& __bad_rval_ref_type;
2378 typedef false_type __enable_rval_overload;
2379};
2380#endif // !defined(_LIBCPP_CXX03_LANG)
2381
2382template <class _Tp, class _Dp = default_delete<_Tp> >
2383class _LIBCPP_TEMPLATE_VIS unique_ptr {
2384public:
2385 typedef _Tp element_type;
2386 typedef _Dp deleter_type;
2387 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2388
2389 static_assert(!is_rvalue_reference<deleter_type>::value,
2390 "the specified deleter type cannot be an rvalue reference");
2391
2392private:
2393 __compressed_pair<pointer, deleter_type> __ptr_;
2394
2395 struct __nat { int __for_bool_; };
2396
2397#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002398 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002399
2400 template <bool _Dummy>
2401 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002402 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002403
2404 template <bool _Dummy>
2405 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002406 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002407
2408 template <bool _Dummy>
2409 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002410 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002411
2412 template <bool _Dummy, class _Deleter = typename __dependent_type<
2413 __identity<deleter_type>, _Dummy>::type>
2414 using _EnableIfDeleterDefaultConstructible =
2415 typename enable_if<is_default_constructible<_Deleter>::value &&
2416 !is_pointer<_Deleter>::value>::type;
2417
2418 template <class _ArgType>
2419 using _EnableIfDeleterConstructible =
2420 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2421
2422 template <class _UPtr, class _Up>
2423 using _EnableIfMoveConvertible = typename enable_if<
2424 is_convertible<typename _UPtr::pointer, pointer>::value &&
2425 !is_array<_Up>::value
2426 >::type;
2427
2428 template <class _UDel>
2429 using _EnableIfDeleterConvertible = typename enable_if<
2430 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2431 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2432 >::type;
2433
2434 template <class _UDel>
2435 using _EnableIfDeleterAssignable = typename enable_if<
2436 is_assignable<_Dp&, _UDel&&>::value
2437 >::type;
2438
2439public:
2440 template <bool _Dummy = true,
2441 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2442 _LIBCPP_INLINE_VISIBILITY
2443 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2444
2445 template <bool _Dummy = true,
2446 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2447 _LIBCPP_INLINE_VISIBILITY
2448 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2449
2450 template <bool _Dummy = true,
2451 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2452 _LIBCPP_INLINE_VISIBILITY
2453 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2454
2455 template <bool _Dummy = true,
2456 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2457 _LIBCPP_INLINE_VISIBILITY
2458 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2459 : __ptr_(__p, __d) {}
2460
2461 template <bool _Dummy = true,
2462 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2463 _LIBCPP_INLINE_VISIBILITY
2464 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2465 : __ptr_(__p, _VSTD::move(__d)) {
2466 static_assert(!is_reference<deleter_type>::value,
2467 "rvalue deleter bound to reference");
2468 }
2469
2470 template <bool _Dummy = true,
2471 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2472 _LIBCPP_INLINE_VISIBILITY
2473 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2474
2475 _LIBCPP_INLINE_VISIBILITY
2476 unique_ptr(unique_ptr&& __u) noexcept
2477 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2478 }
2479
2480 template <class _Up, class _Ep,
2481 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2482 class = _EnableIfDeleterConvertible<_Ep>
2483 >
2484 _LIBCPP_INLINE_VISIBILITY
2485 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2486 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2487
2488#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2489 template <class _Up>
2490 _LIBCPP_INLINE_VISIBILITY
2491 unique_ptr(auto_ptr<_Up>&& __p,
2492 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2493 is_same<_Dp, default_delete<_Tp>>::value,
2494 __nat>::type = __nat()) _NOEXCEPT
2495 : __ptr_(__p.release()) {}
2496#endif
2497
2498 _LIBCPP_INLINE_VISIBILITY
2499 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2500 reset(__u.release());
2501 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2502 return *this;
2503 }
2504
2505 template <class _Up, class _Ep,
2506 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2507 class = _EnableIfDeleterAssignable<_Ep>
2508 >
2509 _LIBCPP_INLINE_VISIBILITY
2510 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2511 reset(__u.release());
2512 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2513 return *this;
2514 }
2515
2516#else // _LIBCPP_CXX03_LANG
2517private:
2518 unique_ptr(unique_ptr&);
2519 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2520
2521 unique_ptr& operator=(unique_ptr&);
2522 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2523
2524public:
2525 _LIBCPP_INLINE_VISIBILITY
2526 unique_ptr() : __ptr_(pointer())
2527 {
2528 static_assert(!is_pointer<deleter_type>::value,
2529 "unique_ptr constructed with null function pointer deleter");
2530 static_assert(is_default_constructible<deleter_type>::value,
2531 "unique_ptr::deleter_type is not default constructible");
2532 }
2533 _LIBCPP_INLINE_VISIBILITY
2534 unique_ptr(nullptr_t) : __ptr_(pointer())
2535 {
2536 static_assert(!is_pointer<deleter_type>::value,
2537 "unique_ptr constructed with null function pointer deleter");
2538 }
2539 _LIBCPP_INLINE_VISIBILITY
2540 explicit unique_ptr(pointer __p)
2541 : __ptr_(_VSTD::move(__p)) {
2542 static_assert(!is_pointer<deleter_type>::value,
2543 "unique_ptr constructed with null function pointer deleter");
2544 }
2545
2546 _LIBCPP_INLINE_VISIBILITY
2547 operator __rv<unique_ptr>() {
2548 return __rv<unique_ptr>(*this);
2549 }
2550
2551 _LIBCPP_INLINE_VISIBILITY
2552 unique_ptr(__rv<unique_ptr> __u)
2553 : __ptr_(__u->release(),
2554 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2555
2556 template <class _Up, class _Ep>
2557 _LIBCPP_INLINE_VISIBILITY
2558 typename enable_if<
2559 !is_array<_Up>::value &&
2560 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2561 pointer>::value &&
2562 is_assignable<deleter_type&, _Ep&>::value,
2563 unique_ptr&>::type
2564 operator=(unique_ptr<_Up, _Ep> __u) {
2565 reset(__u.release());
2566 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2567 return *this;
2568 }
2569
2570 _LIBCPP_INLINE_VISIBILITY
2571 unique_ptr(pointer __p, deleter_type __d)
2572 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2573#endif // _LIBCPP_CXX03_LANG
2574
2575#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2576 template <class _Up>
2577 _LIBCPP_INLINE_VISIBILITY
2578 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2579 is_same<_Dp, default_delete<_Tp> >::value,
2580 unique_ptr&>::type
2581 operator=(auto_ptr<_Up> __p) {
2582 reset(__p.release());
2583 return *this;
2584 }
2585#endif
2586
2587 _LIBCPP_INLINE_VISIBILITY
2588 ~unique_ptr() { reset(); }
2589
2590 _LIBCPP_INLINE_VISIBILITY
2591 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2592 reset();
2593 return *this;
2594 }
2595
2596 _LIBCPP_INLINE_VISIBILITY
2597 typename add_lvalue_reference<_Tp>::type
2598 operator*() const {
2599 return *__ptr_.first();
2600 }
2601 _LIBCPP_INLINE_VISIBILITY
2602 pointer operator->() const _NOEXCEPT {
2603 return __ptr_.first();
2604 }
2605 _LIBCPP_INLINE_VISIBILITY
2606 pointer get() const _NOEXCEPT {
2607 return __ptr_.first();
2608 }
2609 _LIBCPP_INLINE_VISIBILITY
2610 deleter_type& get_deleter() _NOEXCEPT {
2611 return __ptr_.second();
2612 }
2613 _LIBCPP_INLINE_VISIBILITY
2614 const deleter_type& get_deleter() const _NOEXCEPT {
2615 return __ptr_.second();
2616 }
2617 _LIBCPP_INLINE_VISIBILITY
2618 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2619 return __ptr_.first() != nullptr;
2620 }
2621
2622 _LIBCPP_INLINE_VISIBILITY
2623 pointer release() _NOEXCEPT {
2624 pointer __t = __ptr_.first();
2625 __ptr_.first() = pointer();
2626 return __t;
2627 }
2628
2629 _LIBCPP_INLINE_VISIBILITY
2630 void reset(pointer __p = pointer()) _NOEXCEPT {
2631 pointer __tmp = __ptr_.first();
2632 __ptr_.first() = __p;
2633 if (__tmp)
2634 __ptr_.second()(__tmp);
2635 }
2636
2637 _LIBCPP_INLINE_VISIBILITY
2638 void swap(unique_ptr& __u) _NOEXCEPT {
2639 __ptr_.swap(__u.__ptr_);
2640 }
2641};
2642
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002643
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002645class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002647 typedef _Tp element_type;
2648 typedef _Dp deleter_type;
2649 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2650
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002652 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653
Eric Fiselier31127cd2017-04-16 02:14:31 +00002654 template <class _From>
2655 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656
Eric Fiselier31127cd2017-04-16 02:14:31 +00002657 template <class _FromElem>
2658 struct _CheckArrayPointerConversion<_FromElem*>
2659 : integral_constant<bool,
2660 is_same<_FromElem*, pointer>::value ||
2661 (is_same<pointer, element_type*>::value &&
2662 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2663 >
2664 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002666#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002667 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002668
2669 template <bool _Dummy>
2670 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002671 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002672
2673 template <bool _Dummy>
2674 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002675 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002676
2677 template <bool _Dummy>
2678 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002679 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002680
2681 template <bool _Dummy, class _Deleter = typename __dependent_type<
2682 __identity<deleter_type>, _Dummy>::type>
2683 using _EnableIfDeleterDefaultConstructible =
2684 typename enable_if<is_default_constructible<_Deleter>::value &&
2685 !is_pointer<_Deleter>::value>::type;
2686
2687 template <class _ArgType>
2688 using _EnableIfDeleterConstructible =
2689 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2690
2691 template <class _Pp>
2692 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002693 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002694 >::type;
2695
2696 template <class _UPtr, class _Up,
2697 class _ElemT = typename _UPtr::element_type>
2698 using _EnableIfMoveConvertible = typename enable_if<
2699 is_array<_Up>::value &&
2700 is_same<pointer, element_type*>::value &&
2701 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2702 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2703 >::type;
2704
2705 template <class _UDel>
2706 using _EnableIfDeleterConvertible = typename enable_if<
2707 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2708 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2709 >::type;
2710
2711 template <class _UDel>
2712 using _EnableIfDeleterAssignable = typename enable_if<
2713 is_assignable<_Dp&, _UDel&&>::value
2714 >::type;
2715
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002717 template <bool _Dummy = true,
2718 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2719 _LIBCPP_INLINE_VISIBILITY
2720 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002722 template <bool _Dummy = true,
2723 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2724 _LIBCPP_INLINE_VISIBILITY
2725 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727 template <class _Pp, bool _Dummy = true,
2728 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2729 class = _EnableIfPointerConvertible<_Pp>>
2730 _LIBCPP_INLINE_VISIBILITY
2731 explicit unique_ptr(_Pp __p) noexcept
2732 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002734 template <class _Pp, bool _Dummy = true,
2735 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2736 class = _EnableIfPointerConvertible<_Pp>>
2737 _LIBCPP_INLINE_VISIBILITY
2738 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2739 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002741 template <bool _Dummy = true,
2742 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2743 _LIBCPP_INLINE_VISIBILITY
2744 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2745 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002747 template <class _Pp, bool _Dummy = true,
2748 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2749 class = _EnableIfPointerConvertible<_Pp>>
2750 _LIBCPP_INLINE_VISIBILITY
2751 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2752 : __ptr_(__p, _VSTD::move(__d)) {
2753 static_assert(!is_reference<deleter_type>::value,
2754 "rvalue deleter bound to reference");
2755 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002757 template <bool _Dummy = true,
2758 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2759 _LIBCPP_INLINE_VISIBILITY
2760 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2761 : __ptr_(nullptr, _VSTD::move(__d)) {
2762 static_assert(!is_reference<deleter_type>::value,
2763 "rvalue deleter bound to reference");
2764 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002765
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002766 template <class _Pp, bool _Dummy = true,
2767 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2768 class = _EnableIfPointerConvertible<_Pp>>
2769 _LIBCPP_INLINE_VISIBILITY
2770 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002771
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002772 _LIBCPP_INLINE_VISIBILITY
2773 unique_ptr(unique_ptr&& __u) noexcept
2774 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2775 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002776
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002777 _LIBCPP_INLINE_VISIBILITY
2778 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2779 reset(__u.release());
2780 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2781 return *this;
2782 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002784 template <class _Up, class _Ep,
2785 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2786 class = _EnableIfDeleterConvertible<_Ep>
2787 >
2788 _LIBCPP_INLINE_VISIBILITY
2789 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002790 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002791 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002793 template <class _Up, class _Ep,
2794 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2795 class = _EnableIfDeleterAssignable<_Ep>
2796 >
2797 _LIBCPP_INLINE_VISIBILITY
2798 unique_ptr&
2799 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2800 reset(__u.release());
2801 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2802 return *this;
2803 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002805#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002807 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002808
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002809 unique_ptr(unique_ptr&);
2810 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2811
2812 unique_ptr& operator=(unique_ptr&);
2813 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2814
2815 template <class _Up>
2816 unique_ptr(_Up __u,
2817 typename conditional<
2818 is_reference<deleter_type>::value, deleter_type,
2819 typename add_lvalue_reference<const deleter_type>::type>::type,
2820 typename enable_if<is_convertible<_Up, pointer>::value,
2821 __nat>::type = __nat());
2822public:
2823 _LIBCPP_INLINE_VISIBILITY
2824 unique_ptr() : __ptr_(pointer()) {
2825 static_assert(!is_pointer<deleter_type>::value,
2826 "unique_ptr constructed with null function pointer deleter");
2827 }
2828 _LIBCPP_INLINE_VISIBILITY
2829 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2830 static_assert(!is_pointer<deleter_type>::value,
2831 "unique_ptr constructed with null function pointer deleter");
2832 }
2833
2834 _LIBCPP_INLINE_VISIBILITY
2835 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2836 static_assert(!is_pointer<deleter_type>::value,
2837 "unique_ptr constructed with null function pointer deleter");
2838 }
2839
2840 _LIBCPP_INLINE_VISIBILITY
2841 unique_ptr(pointer __p, deleter_type __d)
2842 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2843
2844 _LIBCPP_INLINE_VISIBILITY
2845 unique_ptr(nullptr_t, deleter_type __d)
2846 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2847
2848 _LIBCPP_INLINE_VISIBILITY
2849 operator __rv<unique_ptr>() {
2850 return __rv<unique_ptr>(*this);
2851 }
2852
2853 _LIBCPP_INLINE_VISIBILITY
2854 unique_ptr(__rv<unique_ptr> __u)
2855 : __ptr_(__u->release(),
2856 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2857
2858 _LIBCPP_INLINE_VISIBILITY
2859 unique_ptr& operator=(__rv<unique_ptr> __u) {
2860 reset(__u->release());
2861 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2862 return *this;
2863 }
2864
2865#endif // _LIBCPP_CXX03_LANG
2866
2867public:
2868 _LIBCPP_INLINE_VISIBILITY
2869 ~unique_ptr() { reset(); }
2870
2871 _LIBCPP_INLINE_VISIBILITY
2872 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2873 reset();
2874 return *this;
2875 }
2876
2877 _LIBCPP_INLINE_VISIBILITY
2878 typename add_lvalue_reference<_Tp>::type
2879 operator[](size_t __i) const {
2880 return __ptr_.first()[__i];
2881 }
2882 _LIBCPP_INLINE_VISIBILITY
2883 pointer get() const _NOEXCEPT {
2884 return __ptr_.first();
2885 }
2886
2887 _LIBCPP_INLINE_VISIBILITY
2888 deleter_type& get_deleter() _NOEXCEPT {
2889 return __ptr_.second();
2890 }
2891
2892 _LIBCPP_INLINE_VISIBILITY
2893 const deleter_type& get_deleter() const _NOEXCEPT {
2894 return __ptr_.second();
2895 }
2896 _LIBCPP_INLINE_VISIBILITY
2897 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2898 return __ptr_.first() != nullptr;
2899 }
2900
2901 _LIBCPP_INLINE_VISIBILITY
2902 pointer release() _NOEXCEPT {
2903 pointer __t = __ptr_.first();
2904 __ptr_.first() = pointer();
2905 return __t;
2906 }
2907
2908 template <class _Pp>
2909 _LIBCPP_INLINE_VISIBILITY
2910 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002911 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002912 >::type
2913 reset(_Pp __p) _NOEXCEPT {
2914 pointer __tmp = __ptr_.first();
2915 __ptr_.first() = __p;
2916 if (__tmp)
2917 __ptr_.second()(__tmp);
2918 }
2919
2920 _LIBCPP_INLINE_VISIBILITY
2921 void reset(nullptr_t = nullptr) _NOEXCEPT {
2922 pointer __tmp = __ptr_.first();
2923 __ptr_.first() = nullptr;
2924 if (__tmp)
2925 __ptr_.second()(__tmp);
2926 }
2927
2928 _LIBCPP_INLINE_VISIBILITY
2929 void swap(unique_ptr& __u) _NOEXCEPT {
2930 __ptr_.swap(__u.__ptr_);
2931 }
2932
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933};
2934
2935template <class _Tp, class _Dp>
2936inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002937typename enable_if<
2938 __is_swappable<_Dp>::value,
2939 void
2940>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002941swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942
2943template <class _T1, class _D1, class _T2, class _D2>
2944inline _LIBCPP_INLINE_VISIBILITY
2945bool
2946operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2947
2948template <class _T1, class _D1, class _T2, class _D2>
2949inline _LIBCPP_INLINE_VISIBILITY
2950bool
2951operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2952
2953template <class _T1, class _D1, class _T2, class _D2>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002956operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2957{
2958 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2959 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002960 typedef typename common_type<_P1, _P2>::type _Vp;
2961 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002962}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963
2964template <class _T1, class _D1, class _T2, class _D2>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
2967operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2968
2969template <class _T1, class _D1, class _T2, class _D2>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
2972operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2973
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 !(__x < __y);}
2978
Howard Hinnantb17caf92012-02-21 21:02:58 +00002979template <class _T1, class _D1>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002982operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002983{
2984 return !__x;
2985}
2986
2987template <class _T1, class _D1>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002990operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002991{
2992 return !__x;
2993}
2994
2995template <class _T1, class _D1>
2996inline _LIBCPP_INLINE_VISIBILITY
2997bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002998operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002999{
3000 return static_cast<bool>(__x);
3001}
3002
3003template <class _T1, class _D1>
3004inline _LIBCPP_INLINE_VISIBILITY
3005bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003006operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003007{
3008 return static_cast<bool>(__x);
3009}
3010
3011template <class _T1, class _D1>
3012inline _LIBCPP_INLINE_VISIBILITY
3013bool
3014operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3015{
3016 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3017 return less<_P1>()(__x.get(), nullptr);
3018}
3019
3020template <class _T1, class _D1>
3021inline _LIBCPP_INLINE_VISIBILITY
3022bool
3023operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3024{
3025 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3026 return less<_P1>()(nullptr, __x.get());
3027}
3028
3029template <class _T1, class _D1>
3030inline _LIBCPP_INLINE_VISIBILITY
3031bool
3032operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3033{
3034 return nullptr < __x;
3035}
3036
3037template <class _T1, class _D1>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3041{
3042 return __x < nullptr;
3043}
3044
3045template <class _T1, class _D1>
3046inline _LIBCPP_INLINE_VISIBILITY
3047bool
3048operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3049{
3050 return !(nullptr < __x);
3051}
3052
3053template <class _T1, class _D1>
3054inline _LIBCPP_INLINE_VISIBILITY
3055bool
3056operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3057{
3058 return !(__x < nullptr);
3059}
3060
3061template <class _T1, class _D1>
3062inline _LIBCPP_INLINE_VISIBILITY
3063bool
3064operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3065{
3066 return !(__x < nullptr);
3067}
3068
3069template <class _T1, class _D1>
3070inline _LIBCPP_INLINE_VISIBILITY
3071bool
3072operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3073{
3074 return !(nullptr < __x);
3075}
3076
Howard Hinnant9e028f12012-05-01 15:37:54 +00003077#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3078
3079template <class _Tp, class _Dp>
3080inline _LIBCPP_INLINE_VISIBILITY
3081unique_ptr<_Tp, _Dp>
3082move(unique_ptr<_Tp, _Dp>& __t)
3083{
3084 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3085}
3086
3087#endif
3088
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003089#if _LIBCPP_STD_VER > 11
3090
3091template<class _Tp>
3092struct __unique_if
3093{
3094 typedef unique_ptr<_Tp> __unique_single;
3095};
3096
3097template<class _Tp>
3098struct __unique_if<_Tp[]>
3099{
3100 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3101};
3102
3103template<class _Tp, size_t _Np>
3104struct __unique_if<_Tp[_Np]>
3105{
3106 typedef void __unique_array_known_bound;
3107};
3108
3109template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003110inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003111typename __unique_if<_Tp>::__unique_single
3112make_unique(_Args&&... __args)
3113{
3114 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3115}
3116
3117template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003118inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003119typename __unique_if<_Tp>::__unique_array_unknown_bound
3120make_unique(size_t __n)
3121{
3122 typedef typename remove_extent<_Tp>::type _Up;
3123 return unique_ptr<_Tp>(new _Up[__n]());
3124}
3125
3126template<class _Tp, class... _Args>
3127 typename __unique_if<_Tp>::__unique_array_known_bound
3128 make_unique(_Args&&...) = delete;
3129
3130#endif // _LIBCPP_STD_VER > 11
3131
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003132template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003133#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003134struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003135#else
3136struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3137 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3138#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003139{
3140 typedef unique_ptr<_Tp, _Dp> argument_type;
3141 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003142 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003143 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003144 {
3145 typedef typename argument_type::pointer pointer;
3146 return hash<pointer>()(__ptr.get());
3147 }
3148};
3149
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150struct __destruct_n
3151{
3152private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003153 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154
3155 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003156 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003157 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158
3159 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003160 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 {}
3162
Howard Hinnant719bda32011-05-28 14:41:13 +00003163 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003164 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003165 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 {}
3167
Howard Hinnant719bda32011-05-28 14:41:13 +00003168 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003169 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003170 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171 {}
3172public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003173 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003174 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175
3176 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003177 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003178 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179
3180 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003181 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003182 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183
3184 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003185 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003186 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187};
3188
3189template <class _Alloc>
3190class __allocator_destructor
3191{
3192 typedef allocator_traits<_Alloc> __alloc_traits;
3193public:
3194 typedef typename __alloc_traits::pointer pointer;
3195 typedef typename __alloc_traits::size_type size_type;
3196private:
3197 _Alloc& __alloc_;
3198 size_type __s_;
3199public:
3200 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003201 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003204 void operator()(pointer __p) _NOEXCEPT
3205 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206};
3207
3208template <class _InputIterator, class _ForwardIterator>
3209_ForwardIterator
3210uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3211{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003213#ifndef _LIBCPP_NO_EXCEPTIONS
3214 _ForwardIterator __s = __r;
3215 try
3216 {
3217#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003218 for (; __f != __l; ++__f, (void) ++__r)
3219 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003220#ifndef _LIBCPP_NO_EXCEPTIONS
3221 }
3222 catch (...)
3223 {
3224 for (; __s != __r; ++__s)
3225 __s->~value_type();
3226 throw;
3227 }
3228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 return __r;
3230}
3231
3232template <class _InputIterator, class _Size, class _ForwardIterator>
3233_ForwardIterator
3234uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3235{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003237#ifndef _LIBCPP_NO_EXCEPTIONS
3238 _ForwardIterator __s = __r;
3239 try
3240 {
3241#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003242 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3243 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003244#ifndef _LIBCPP_NO_EXCEPTIONS
3245 }
3246 catch (...)
3247 {
3248 for (; __s != __r; ++__s)
3249 __s->~value_type();
3250 throw;
3251 }
3252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253 return __r;
3254}
3255
3256template <class _ForwardIterator, class _Tp>
3257void
3258uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3259{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003261#ifndef _LIBCPP_NO_EXCEPTIONS
3262 _ForwardIterator __s = __f;
3263 try
3264 {
3265#endif
3266 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003267 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003268#ifndef _LIBCPP_NO_EXCEPTIONS
3269 }
3270 catch (...)
3271 {
3272 for (; __s != __f; ++__s)
3273 __s->~value_type();
3274 throw;
3275 }
3276#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277}
3278
3279template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003280_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003281uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3282{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003284#ifndef _LIBCPP_NO_EXCEPTIONS
3285 _ForwardIterator __s = __f;
3286 try
3287 {
3288#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003289 for (; __n > 0; ++__f, (void) --__n)
3290 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003291#ifndef _LIBCPP_NO_EXCEPTIONS
3292 }
3293 catch (...)
3294 {
3295 for (; __s != __f; ++__s)
3296 __s->~value_type();
3297 throw;
3298 }
3299#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003300 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301}
3302
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003303#if _LIBCPP_STD_VER > 14
3304
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003305template <class _Tp>
3306inline _LIBCPP_INLINE_VISIBILITY
3307void destroy_at(_Tp* __loc) {
3308 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3309 __loc->~_Tp();
3310}
3311
3312template <class _ForwardIterator>
3313inline _LIBCPP_INLINE_VISIBILITY
3314void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3315 for (; __first != __last; ++__first)
3316 _VSTD::destroy_at(_VSTD::addressof(*__first));
3317}
3318
3319template <class _ForwardIterator, class _Size>
3320inline _LIBCPP_INLINE_VISIBILITY
3321_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3322 for (; __n > 0; (void)++__first, --__n)
3323 _VSTD::destroy_at(_VSTD::addressof(*__first));
3324 return __first;
3325}
3326
Eric Fiselier290c07c2016-10-11 21:13:44 +00003327template <class _ForwardIterator>
3328inline _LIBCPP_INLINE_VISIBILITY
3329void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3330 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3331 auto __idx = __first;
3332#ifndef _LIBCPP_NO_EXCEPTIONS
3333 try {
3334#endif
3335 for (; __idx != __last; ++__idx)
3336 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3337#ifndef _LIBCPP_NO_EXCEPTIONS
3338 } catch (...) {
3339 _VSTD::destroy(__first, __idx);
3340 throw;
3341 }
3342#endif
3343}
3344
3345template <class _ForwardIterator, class _Size>
3346inline _LIBCPP_INLINE_VISIBILITY
3347_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3348 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3349 auto __idx = __first;
3350#ifndef _LIBCPP_NO_EXCEPTIONS
3351 try {
3352#endif
3353 for (; __n > 0; (void)++__idx, --__n)
3354 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3355 return __idx;
3356#ifndef _LIBCPP_NO_EXCEPTIONS
3357 } catch (...) {
3358 _VSTD::destroy(__first, __idx);
3359 throw;
3360 }
3361#endif
3362}
3363
3364
3365template <class _ForwardIterator>
3366inline _LIBCPP_INLINE_VISIBILITY
3367void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3368 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3369 auto __idx = __first;
3370#ifndef _LIBCPP_NO_EXCEPTIONS
3371 try {
3372#endif
3373 for (; __idx != __last; ++__idx)
3374 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3375#ifndef _LIBCPP_NO_EXCEPTIONS
3376 } catch (...) {
3377 _VSTD::destroy(__first, __idx);
3378 throw;
3379 }
3380#endif
3381}
3382
3383template <class _ForwardIterator, class _Size>
3384inline _LIBCPP_INLINE_VISIBILITY
3385_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3386 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3387 auto __idx = __first;
3388#ifndef _LIBCPP_NO_EXCEPTIONS
3389 try {
3390#endif
3391 for (; __n > 0; (void)++__idx, --__n)
3392 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3393 return __idx;
3394#ifndef _LIBCPP_NO_EXCEPTIONS
3395 } catch (...) {
3396 _VSTD::destroy(__first, __idx);
3397 throw;
3398 }
3399#endif
3400}
3401
3402
3403template <class _InputIt, class _ForwardIt>
3404inline _LIBCPP_INLINE_VISIBILITY
3405_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3406 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3407 auto __idx = __first_res;
3408#ifndef _LIBCPP_NO_EXCEPTIONS
3409 try {
3410#endif
3411 for (; __first != __last; (void)++__idx, ++__first)
3412 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3413 return __idx;
3414#ifndef _LIBCPP_NO_EXCEPTIONS
3415 } catch (...) {
3416 _VSTD::destroy(__first_res, __idx);
3417 throw;
3418 }
3419#endif
3420}
3421
3422template <class _InputIt, class _Size, class _ForwardIt>
3423inline _LIBCPP_INLINE_VISIBILITY
3424pair<_InputIt, _ForwardIt>
3425uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3426 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3427 auto __idx = __first_res;
3428#ifndef _LIBCPP_NO_EXCEPTIONS
3429 try {
3430#endif
3431 for (; __n > 0; ++__idx, (void)++__first, --__n)
3432 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3433 return {__first, __idx};
3434#ifndef _LIBCPP_NO_EXCEPTIONS
3435 } catch (...) {
3436 _VSTD::destroy(__first_res, __idx);
3437 throw;
3438 }
3439#endif
3440}
3441
3442
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003443#endif // _LIBCPP_STD_VER > 14
3444
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003445// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3446// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003447// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003448#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3449 && defined(__ATOMIC_RELAXED) \
3450 && defined(__ATOMIC_ACQ_REL)
3451# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3452#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3453# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3454#endif
3455
3456template <class _Tp>
3457inline _LIBCPP_INLINE_VISIBILITY _Tp
3458__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3459{
3460#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3461 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3462#else
3463 return __t += 1;
3464#endif
3465}
3466
3467template <class _Tp>
3468inline _LIBCPP_INLINE_VISIBILITY _Tp
3469__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3470{
3471#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3472 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3473#else
3474 return __t -= 1;
3475#endif
3476}
3477
Howard Hinnant756c69b2010-09-22 16:48:34 +00003478class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479 : public std::exception
3480{
3481public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003482 virtual ~bad_weak_ptr() _NOEXCEPT;
3483 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484};
3485
Marshall Clow8fea1612016-08-25 15:09:01 +00003486_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3487void __throw_bad_weak_ptr()
3488{
3489#ifndef _LIBCPP_NO_EXCEPTIONS
3490 throw bad_weak_ptr();
3491#else
3492 _VSTD::abort();
3493#endif
3494}
3495
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003496template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003498class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499{
3500 __shared_count(const __shared_count&);
3501 __shared_count& operator=(const __shared_count&);
3502
3503protected:
3504 long __shared_owners_;
3505 virtual ~__shared_count();
3506private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003507 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508
3509public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003511 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512 : __shared_owners_(__refs) {}
3513
Eric Fiselier98848572017-01-17 03:16:26 +00003514#if defined(_LIBCPP_BUILDING_MEMORY) && \
3515 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003516 void __add_shared() _NOEXCEPT;
3517 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003518#else
3519 _LIBCPP_INLINE_VISIBILITY
3520 void __add_shared() _NOEXCEPT {
3521 __libcpp_atomic_refcount_increment(__shared_owners_);
3522 }
3523 _LIBCPP_INLINE_VISIBILITY
3524 bool __release_shared() _NOEXCEPT {
3525 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3526 __on_zero_shared();
3527 return true;
3528 }
3529 return false;
3530 }
3531#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003532 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003533 long use_count() const _NOEXCEPT {
3534 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3535 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536};
3537
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003538class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539 : private __shared_count
3540{
3541 long __shared_weak_owners_;
3542
3543public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003545 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546 : __shared_count(__refs),
3547 __shared_weak_owners_(__refs) {}
3548protected:
3549 virtual ~__shared_weak_count();
3550
3551public:
Eric Fiselier98848572017-01-17 03:16:26 +00003552#if defined(_LIBCPP_BUILDING_MEMORY) && \
3553 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003554 void __add_shared() _NOEXCEPT;
3555 void __add_weak() _NOEXCEPT;
3556 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003557#else
3558 _LIBCPP_INLINE_VISIBILITY
3559 void __add_shared() _NOEXCEPT {
3560 __shared_count::__add_shared();
3561 }
3562 _LIBCPP_INLINE_VISIBILITY
3563 void __add_weak() _NOEXCEPT {
3564 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3565 }
3566 _LIBCPP_INLINE_VISIBILITY
3567 void __release_shared() _NOEXCEPT {
3568 if (__shared_count::__release_shared())
3569 __release_weak();
3570 }
3571#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003572 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003574 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3575 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576
Howard Hinnant807d6332013-02-25 15:50:36 +00003577 // Define the function out only if we build static libc++ without RTTI.
3578 // Otherwise we may break clients who need to compile their projects with
3579 // -fno-rtti and yet link against a libc++.dylib compiled
3580 // without -fno-rtti.
3581#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003582 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003583#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003585 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586};
3587
3588template <class _Tp, class _Dp, class _Alloc>
3589class __shared_ptr_pointer
3590 : public __shared_weak_count
3591{
3592 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3593public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003596 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597
Howard Hinnant72f73582010-08-11 17:04:31 +00003598#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003599 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003600#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601
3602private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003603 virtual void __on_zero_shared() _NOEXCEPT;
3604 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605};
3606
Howard Hinnant72f73582010-08-11 17:04:31 +00003607#ifndef _LIBCPP_NO_RTTI
3608
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609template <class _Tp, class _Dp, class _Alloc>
3610const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003611__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003613 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614}
3615
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003616#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003617
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618template <class _Tp, class _Dp, class _Alloc>
3619void
Howard Hinnant719bda32011-05-28 14:41:13 +00003620__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621{
3622 __data_.first().second()(__data_.first().first());
3623 __data_.first().second().~_Dp();
3624}
3625
3626template <class _Tp, class _Dp, class _Alloc>
3627void
Howard Hinnant719bda32011-05-28 14:41:13 +00003628__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003630 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3631 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003632 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3633
Eric Fiselierf8898c82015-02-05 23:01:40 +00003634 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003636 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637}
3638
3639template <class _Tp, class _Alloc>
3640class __shared_ptr_emplace
3641 : public __shared_weak_count
3642{
3643 __compressed_pair<_Alloc, _Tp> __data_;
3644public:
3645#ifndef _LIBCPP_HAS_NO_VARIADICS
3646
Howard Hinnant756c69b2010-09-22 16:48:34 +00003647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003649 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650
3651 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003654 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3655 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656
3657#else // _LIBCPP_HAS_NO_VARIADICS
3658
Howard Hinnant756c69b2010-09-22 16:48:34 +00003659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660 __shared_ptr_emplace(_Alloc __a)
3661 : __data_(__a) {}
3662
3663 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3666 : __data_(__a, _Tp(__a0)) {}
3667
3668 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3671 : __data_(__a, _Tp(__a0, __a1)) {}
3672
3673 template <class _A0, class _A1, class _A2>
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, _A1& __a1, _A2& __a2)
3676 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3677
3678#endif // _LIBCPP_HAS_NO_VARIADICS
3679
3680private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003681 virtual void __on_zero_shared() _NOEXCEPT;
3682 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003685 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686};
3687
3688template <class _Tp, class _Alloc>
3689void
Howard Hinnant719bda32011-05-28 14:41:13 +00003690__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691{
3692 __data_.second().~_Tp();
3693}
3694
3695template <class _Tp, class _Alloc>
3696void
Howard Hinnant719bda32011-05-28 14:41:13 +00003697__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003699 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3700 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003701 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003702 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003704 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705}
3706
Erik Pilkington2a398762017-05-25 15:43:31 +00003707struct __shared_ptr_dummy_rebind_allocator_type;
3708template <>
3709class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3710{
3711public:
3712 template <class _Other>
3713 struct rebind
3714 {
3715 typedef allocator<_Other> other;
3716 };
3717};
3718
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003719template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720
3721template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003722class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003724public:
3725 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003726
Eric Fiselierae5b6672016-06-27 01:02:43 +00003727#if _LIBCPP_STD_VER > 14
3728 typedef weak_ptr<_Tp> weak_type;
3729#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730private:
3731 element_type* __ptr_;
3732 __shared_weak_count* __cntrl_;
3733
3734 struct __nat {int __for_bool_;};
3735public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003737 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003739 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003740 template<class _Yp>
3741 explicit shared_ptr(_Yp* __p,
3742 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3743 template<class _Yp, class _Dp>
3744 shared_ptr(_Yp* __p, _Dp __d,
3745 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3746 template<class _Yp, class _Dp, class _Alloc>
3747 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3748 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3750 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003751 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003753 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003757 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003758 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003759#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003761 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003762 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003763 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003764 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003765#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003767 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003768#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003769#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003770 template<class _Yp>
3771 shared_ptr(auto_ptr<_Yp>&& __r,
3772 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003773#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003774 template<class _Yp>
3775 shared_ptr(auto_ptr<_Yp> __r,
3776 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003778#endif
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, class _Dp>
3781 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3782 typename enable_if
3783 <
3784 !is_lvalue_reference<_Dp>::value &&
3785 !is_array<_Yp>::value &&
3786 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3787 __nat
3788 >::type = __nat());
3789 template <class _Yp, class _Dp>
3790 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3791 typename enable_if
3792 <
3793 is_lvalue_reference<_Dp>::value &&
3794 !is_array<_Yp>::value &&
3795 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3796 __nat
3797 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003798#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003799 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());
3808 template <class _Yp, class _Dp>
3809 shared_ptr(unique_ptr<_Yp, _Dp>,
3810 typename enable_if
3811 <
3812 is_lvalue_reference<_Dp>::value &&
3813 !is_array<_Yp>::value &&
3814 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3815 __nat
3816 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003817#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818
3819 ~shared_ptr();
3820
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003822 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003823 template<class _Yp>
3824 typename enable_if
3825 <
3826 is_convertible<_Yp*, element_type*>::value,
3827 shared_ptr&
3828 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003830 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003831#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003833 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003834 template<class _Yp>
3835 typename enable_if
3836 <
3837 is_convertible<_Yp*, element_type*>::value,
3838 shared_ptr<_Tp>&
3839 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003841 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003842#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003843 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003845 typename enable_if
3846 <
3847 !is_array<_Yp>::value &&
3848 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003849 shared_ptr
3850 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003851 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003852#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003853#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003854#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003855 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003857 typename enable_if
3858 <
3859 !is_array<_Yp>::value &&
3860 is_convertible<_Yp*, element_type*>::value,
3861 shared_ptr&
3862 >::type
3863 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003865#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003866 template <class _Yp, class _Dp>
3867 typename enable_if
3868 <
3869 !is_array<_Yp>::value &&
3870 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3871 shared_ptr&
3872 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003873#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003875 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003876#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003878 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879#endif
3880
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003882 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003884 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003885 template<class _Yp>
3886 typename enable_if
3887 <
3888 is_convertible<_Yp*, element_type*>::value,
3889 void
3890 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003892 reset(_Yp* __p);
3893 template<class _Yp, class _Dp>
3894 typename enable_if
3895 <
3896 is_convertible<_Yp*, element_type*>::value,
3897 void
3898 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003900 reset(_Yp* __p, _Dp __d);
3901 template<class _Yp, class _Dp, class _Alloc>
3902 typename enable_if
3903 <
3904 is_convertible<_Yp*, element_type*>::value,
3905 void
3906 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003908 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909
Howard Hinnant756c69b2010-09-22 16:48:34 +00003910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003911 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003913 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3914 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003916 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003918 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003920 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003922 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003923 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003924 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003925 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003927 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003928 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003929 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003931 _LIBCPP_INLINE_VISIBILITY
3932 bool
3933 __owner_equivalent(const shared_ptr& __p) const
3934 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935
Howard Hinnant72f73582010-08-11 17:04:31 +00003936#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003939 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003940 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003941 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003942 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003943#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944
3945#ifndef _LIBCPP_HAS_NO_VARIADICS
3946
3947 template<class ..._Args>
3948 static
3949 shared_ptr<_Tp>
3950 make_shared(_Args&& ...__args);
3951
3952 template<class _Alloc, class ..._Args>
3953 static
3954 shared_ptr<_Tp>
3955 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3956
3957#else // _LIBCPP_HAS_NO_VARIADICS
3958
3959 static shared_ptr<_Tp> make_shared();
3960
3961 template<class _A0>
3962 static shared_ptr<_Tp> make_shared(_A0&);
3963
3964 template<class _A0, class _A1>
3965 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3966
3967 template<class _A0, class _A1, class _A2>
3968 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3969
3970 template<class _Alloc>
3971 static shared_ptr<_Tp>
3972 allocate_shared(const _Alloc& __a);
3973
3974 template<class _Alloc, class _A0>
3975 static shared_ptr<_Tp>
3976 allocate_shared(const _Alloc& __a, _A0& __a0);
3977
3978 template<class _Alloc, class _A0, class _A1>
3979 static shared_ptr<_Tp>
3980 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3981
3982 template<class _Alloc, class _A0, class _A1, class _A2>
3983 static shared_ptr<_Tp>
3984 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3985
3986#endif // _LIBCPP_HAS_NO_VARIADICS
3987
3988private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003989 template <class _Yp, bool = is_function<_Yp>::value>
3990 struct __shared_ptr_default_allocator
3991 {
3992 typedef allocator<_Yp> type;
3993 };
3994
3995 template <class _Yp>
3996 struct __shared_ptr_default_allocator<_Yp, true>
3997 {
3998 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3999 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004001 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004002 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004003 typename enable_if<is_convertible<_OrigPtr*,
4004 const enable_shared_from_this<_Yp>*
4005 >::value,
4006 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004007 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4008 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004010 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004011 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004012 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004013 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4014 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004015 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016 }
4017
Erik Pilkington2a398762017-05-25 15:43:31 +00004018 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004020 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4021 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022};
4023
Eric Fiselier30d5ac62017-05-10 19:35:49 +00004024
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004026inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004027_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004028shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029 : __ptr_(0),
4030 __cntrl_(0)
4031{
4032}
4033
4034template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004035inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004036_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004037shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038 : __ptr_(0),
4039 __cntrl_(0)
4040{
4041}
4042
4043template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004044template<class _Yp>
4045shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4046 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047 : __ptr_(__p)
4048{
4049 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004050 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4051 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4052 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004054 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055}
4056
4057template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004058template<class _Yp, class _Dp>
4059shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4060 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061 : __ptr_(__p)
4062{
4063#ifndef _LIBCPP_NO_EXCEPTIONS
4064 try
4065 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004066#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004067 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4068 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4069 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004070 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071#ifndef _LIBCPP_NO_EXCEPTIONS
4072 }
4073 catch (...)
4074 {
4075 __d(__p);
4076 throw;
4077 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004078#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079}
4080
4081template<class _Tp>
4082template<class _Dp>
4083shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4084 : __ptr_(0)
4085{
4086#ifndef _LIBCPP_NO_EXCEPTIONS
4087 try
4088 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004089#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004090 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4091 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4092 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093#ifndef _LIBCPP_NO_EXCEPTIONS
4094 }
4095 catch (...)
4096 {
4097 __d(__p);
4098 throw;
4099 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004100#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101}
4102
4103template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004104template<class _Yp, class _Dp, class _Alloc>
4105shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4106 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107 : __ptr_(__p)
4108{
4109#ifndef _LIBCPP_NO_EXCEPTIONS
4110 try
4111 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004112#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004114 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115 typedef __allocator_destructor<_A2> _D2;
4116 _A2 __a2(__a);
4117 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004118 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4119 _CntrlBlk(__p, __d, __a);
4120 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004121 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122#ifndef _LIBCPP_NO_EXCEPTIONS
4123 }
4124 catch (...)
4125 {
4126 __d(__p);
4127 throw;
4128 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004129#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130}
4131
4132template<class _Tp>
4133template<class _Dp, class _Alloc>
4134shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4135 : __ptr_(0)
4136{
4137#ifndef _LIBCPP_NO_EXCEPTIONS
4138 try
4139 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004140#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004142 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143 typedef __allocator_destructor<_A2> _D2;
4144 _A2 __a2(__a);
4145 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004146 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4147 _CntrlBlk(__p, __d, __a);
4148 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149#ifndef _LIBCPP_NO_EXCEPTIONS
4150 }
4151 catch (...)
4152 {
4153 __d(__p);
4154 throw;
4155 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004156#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157}
4158
4159template<class _Tp>
4160template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004161inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004162shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163 : __ptr_(__p),
4164 __cntrl_(__r.__cntrl_)
4165{
4166 if (__cntrl_)
4167 __cntrl_->__add_shared();
4168}
4169
4170template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004171inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004172shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173 : __ptr_(__r.__ptr_),
4174 __cntrl_(__r.__cntrl_)
4175{
4176 if (__cntrl_)
4177 __cntrl_->__add_shared();
4178}
4179
4180template<class _Tp>
4181template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004182inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004184 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004185 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186 : __ptr_(__r.__ptr_),
4187 __cntrl_(__r.__cntrl_)
4188{
4189 if (__cntrl_)
4190 __cntrl_->__add_shared();
4191}
4192
Howard Hinnant74279a52010-09-04 23:28:19 +00004193#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194
4195template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004196inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004197shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198 : __ptr_(__r.__ptr_),
4199 __cntrl_(__r.__cntrl_)
4200{
4201 __r.__ptr_ = 0;
4202 __r.__cntrl_ = 0;
4203}
4204
4205template<class _Tp>
4206template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004207inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004209 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004210 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211 : __ptr_(__r.__ptr_),
4212 __cntrl_(__r.__cntrl_)
4213{
4214 __r.__ptr_ = 0;
4215 __r.__cntrl_ = 0;
4216}
4217
Howard Hinnant74279a52010-09-04 23:28:19 +00004218#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219
Marshall Clowb22274f2017-01-24 22:22:33 +00004220#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004222template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004223#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004224shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004226shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004228 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229 : __ptr_(__r.get())
4230{
4231 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4232 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004233 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234 __r.release();
4235}
Marshall Clowb22274f2017-01-24 22:22:33 +00004236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237
4238template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004239template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004240#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4242#else
4243shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4244#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004245 typename enable_if
4246 <
4247 !is_lvalue_reference<_Dp>::value &&
4248 !is_array<_Yp>::value &&
4249 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4250 __nat
4251 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252 : __ptr_(__r.get())
4253{
Marshall Clow35cde742015-05-10 13:59:45 +00004254#if _LIBCPP_STD_VER > 11
4255 if (__ptr_ == nullptr)
4256 __cntrl_ = nullptr;
4257 else
4258#endif
4259 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004260 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4261 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4262 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004263 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004264 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265 __r.release();
4266}
4267
4268template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004269template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004270#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4272#else
4273shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4274#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004275 typename enable_if
4276 <
4277 is_lvalue_reference<_Dp>::value &&
4278 !is_array<_Yp>::value &&
4279 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4280 __nat
4281 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282 : __ptr_(__r.get())
4283{
Marshall Clow35cde742015-05-10 13:59:45 +00004284#if _LIBCPP_STD_VER > 11
4285 if (__ptr_ == nullptr)
4286 __cntrl_ = nullptr;
4287 else
4288#endif
4289 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004290 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004291 typedef __shared_ptr_pointer<_Yp*,
4292 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004293 _AllocT > _CntrlBlk;
4294 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004295 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004296 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297 __r.release();
4298}
4299
4300#ifndef _LIBCPP_HAS_NO_VARIADICS
4301
4302template<class _Tp>
4303template<class ..._Args>
4304shared_ptr<_Tp>
4305shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4306{
Marshall Clowdec75c72017-12-05 04:09:49 +00004307 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4309 typedef allocator<_CntrlBlk> _A2;
4310 typedef __allocator_destructor<_A2> _D2;
4311 _A2 __a2;
4312 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004313 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314 shared_ptr<_Tp> __r;
4315 __r.__ptr_ = __hold2.get()->get();
4316 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004317 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318 return __r;
4319}
4320
4321template<class _Tp>
4322template<class _Alloc, class ..._Args>
4323shared_ptr<_Tp>
4324shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4325{
Marshall Clowdec75c72017-12-05 04:09:49 +00004326 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004328 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329 typedef __allocator_destructor<_A2> _D2;
4330 _A2 __a2(__a);
4331 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004332 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4333 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334 shared_ptr<_Tp> __r;
4335 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004336 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004337 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338 return __r;
4339}
4340
4341#else // _LIBCPP_HAS_NO_VARIADICS
4342
4343template<class _Tp>
4344shared_ptr<_Tp>
4345shared_ptr<_Tp>::make_shared()
4346{
Marshall Clowdec75c72017-12-05 04:09:49 +00004347 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4349 typedef allocator<_CntrlBlk> _Alloc2;
4350 typedef __allocator_destructor<_Alloc2> _D2;
4351 _Alloc2 __alloc2;
4352 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4353 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4354 shared_ptr<_Tp> __r;
4355 __r.__ptr_ = __hold2.get()->get();
4356 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004357 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358 return __r;
4359}
4360
4361template<class _Tp>
4362template<class _A0>
4363shared_ptr<_Tp>
4364shared_ptr<_Tp>::make_shared(_A0& __a0)
4365{
Marshall Clowdec75c72017-12-05 04:09:49 +00004366 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4368 typedef allocator<_CntrlBlk> _Alloc2;
4369 typedef __allocator_destructor<_Alloc2> _D2;
4370 _Alloc2 __alloc2;
4371 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4372 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4373 shared_ptr<_Tp> __r;
4374 __r.__ptr_ = __hold2.get()->get();
4375 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004376 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 return __r;
4378}
4379
4380template<class _Tp>
4381template<class _A0, class _A1>
4382shared_ptr<_Tp>
4383shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4384{
Marshall Clowdec75c72017-12-05 04:09:49 +00004385 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4387 typedef allocator<_CntrlBlk> _Alloc2;
4388 typedef __allocator_destructor<_Alloc2> _D2;
4389 _Alloc2 __alloc2;
4390 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4391 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4392 shared_ptr<_Tp> __r;
4393 __r.__ptr_ = __hold2.get()->get();
4394 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004395 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396 return __r;
4397}
4398
4399template<class _Tp>
4400template<class _A0, class _A1, class _A2>
4401shared_ptr<_Tp>
4402shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4403{
Marshall Clowdec75c72017-12-05 04:09:49 +00004404 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4406 typedef allocator<_CntrlBlk> _Alloc2;
4407 typedef __allocator_destructor<_Alloc2> _D2;
4408 _Alloc2 __alloc2;
4409 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4410 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4411 shared_ptr<_Tp> __r;
4412 __r.__ptr_ = __hold2.get()->get();
4413 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004414 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 return __r;
4416}
4417
4418template<class _Tp>
4419template<class _Alloc>
4420shared_ptr<_Tp>
4421shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4422{
Marshall Clowdec75c72017-12-05 04:09:49 +00004423 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004425 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426 typedef __allocator_destructor<_Alloc2> _D2;
4427 _Alloc2 __alloc2(__a);
4428 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004429 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4430 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431 shared_ptr<_Tp> __r;
4432 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004433 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004434 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435 return __r;
4436}
4437
4438template<class _Tp>
4439template<class _Alloc, class _A0>
4440shared_ptr<_Tp>
4441shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4442{
Marshall Clowdec75c72017-12-05 04:09:49 +00004443 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004445 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446 typedef __allocator_destructor<_Alloc2> _D2;
4447 _Alloc2 __alloc2(__a);
4448 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004449 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4450 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004451 shared_ptr<_Tp> __r;
4452 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004453 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004454 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455 return __r;
4456}
4457
4458template<class _Tp>
4459template<class _Alloc, class _A0, class _A1>
4460shared_ptr<_Tp>
4461shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4462{
Marshall Clowdec75c72017-12-05 04:09:49 +00004463 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004464 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004465 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466 typedef __allocator_destructor<_Alloc2> _D2;
4467 _Alloc2 __alloc2(__a);
4468 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004469 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4470 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471 shared_ptr<_Tp> __r;
4472 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004473 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004474 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475 return __r;
4476}
4477
4478template<class _Tp>
4479template<class _Alloc, class _A0, class _A1, class _A2>
4480shared_ptr<_Tp>
4481shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4482{
Marshall Clowdec75c72017-12-05 04:09:49 +00004483 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004485 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486 typedef __allocator_destructor<_Alloc2> _D2;
4487 _Alloc2 __alloc2(__a);
4488 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004489 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4490 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491 shared_ptr<_Tp> __r;
4492 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004493 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004494 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495 return __r;
4496}
4497
4498#endif // _LIBCPP_HAS_NO_VARIADICS
4499
4500template<class _Tp>
4501shared_ptr<_Tp>::~shared_ptr()
4502{
4503 if (__cntrl_)
4504 __cntrl_->__release_shared();
4505}
4506
4507template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004508inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004510shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511{
4512 shared_ptr(__r).swap(*this);
4513 return *this;
4514}
4515
4516template<class _Tp>
4517template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004518inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004519typename enable_if
4520<
Marshall Clow7e384b72017-01-10 16:59:33 +00004521 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004522 shared_ptr<_Tp>&
4523>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004524shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004525{
4526 shared_ptr(__r).swap(*this);
4527 return *this;
4528}
4529
Howard Hinnant74279a52010-09-04 23:28:19 +00004530#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531
4532template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004533inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004534shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004535shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004537 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538 return *this;
4539}
4540
4541template<class _Tp>
4542template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004543inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004544typename enable_if
4545<
Marshall Clow7e384b72017-01-10 16:59:33 +00004546 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004547 shared_ptr<_Tp>&
4548>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4550{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004551 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004552 return *this;
4553}
4554
Marshall Clowb22274f2017-01-24 22:22:33 +00004555#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556template<class _Tp>
4557template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004558inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004559typename enable_if
4560<
4561 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004562 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004563 shared_ptr<_Tp>
4564>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4566{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004567 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568 return *this;
4569}
Marshall Clowb22274f2017-01-24 22:22:33 +00004570#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571
4572template<class _Tp>
4573template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004574inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004575typename enable_if
4576<
4577 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004578 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004579 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004580 shared_ptr<_Tp>&
4581>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004582shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4583{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004584 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004585 return *this;
4586}
4587
Howard Hinnant74279a52010-09-04 23:28:19 +00004588#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004589
Marshall Clowb22274f2017-01-24 22:22:33 +00004590#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591template<class _Tp>
4592template<class _Yp>
4593inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004594typename enable_if
4595<
4596 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004597 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004598 shared_ptr<_Tp>&
4599>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004600shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004601{
4602 shared_ptr(__r).swap(*this);
4603 return *this;
4604}
Marshall Clowb22274f2017-01-24 22:22:33 +00004605#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606
4607template<class _Tp>
4608template <class _Yp, class _Dp>
4609inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004610typename enable_if
4611<
4612 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004613 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004614 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004615 shared_ptr<_Tp>&
4616>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004617shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4618{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004619 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004620 return *this;
4621}
4622
Howard Hinnant74279a52010-09-04 23:28:19 +00004623#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004624
4625template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004626inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004627void
Howard Hinnant719bda32011-05-28 14:41:13 +00004628shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004629{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004630 _VSTD::swap(__ptr_, __r.__ptr_);
4631 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632}
4633
4634template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004635inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004636void
Howard Hinnant719bda32011-05-28 14:41:13 +00004637shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004638{
4639 shared_ptr().swap(*this);
4640}
4641
4642template<class _Tp>
4643template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004644inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004645typename enable_if
4646<
Marshall Clow7e384b72017-01-10 16:59:33 +00004647 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004648 void
4649>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004650shared_ptr<_Tp>::reset(_Yp* __p)
4651{
4652 shared_ptr(__p).swap(*this);
4653}
4654
4655template<class _Tp>
4656template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004657inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004658typename enable_if
4659<
Marshall Clow7e384b72017-01-10 16:59:33 +00004660 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004661 void
4662>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004663shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4664{
4665 shared_ptr(__p, __d).swap(*this);
4666}
4667
4668template<class _Tp>
4669template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004670inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004671typename enable_if
4672<
Marshall Clow7e384b72017-01-10 16:59:33 +00004673 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004674 void
4675>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004676shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4677{
4678 shared_ptr(__p, __d, __a).swap(*this);
4679}
4680
4681#ifndef _LIBCPP_HAS_NO_VARIADICS
4682
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004683template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004684inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004685typename enable_if
4686<
4687 !is_array<_Tp>::value,
4688 shared_ptr<_Tp>
4689>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004690make_shared(_Args&& ...__args)
4691{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004692 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693}
4694
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004695template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004696inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004697typename enable_if
4698<
4699 !is_array<_Tp>::value,
4700 shared_ptr<_Tp>
4701>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004702allocate_shared(const _Alloc& __a, _Args&& ...__args)
4703{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004704 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004705}
4706
4707#else // _LIBCPP_HAS_NO_VARIADICS
4708
4709template<class _Tp>
4710inline _LIBCPP_INLINE_VISIBILITY
4711shared_ptr<_Tp>
4712make_shared()
4713{
4714 return shared_ptr<_Tp>::make_shared();
4715}
4716
4717template<class _Tp, class _A0>
4718inline _LIBCPP_INLINE_VISIBILITY
4719shared_ptr<_Tp>
4720make_shared(_A0& __a0)
4721{
4722 return shared_ptr<_Tp>::make_shared(__a0);
4723}
4724
4725template<class _Tp, class _A0, class _A1>
4726inline _LIBCPP_INLINE_VISIBILITY
4727shared_ptr<_Tp>
4728make_shared(_A0& __a0, _A1& __a1)
4729{
4730 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4731}
4732
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004733template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004734inline _LIBCPP_INLINE_VISIBILITY
4735shared_ptr<_Tp>
4736make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4737{
4738 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4739}
4740
4741template<class _Tp, class _Alloc>
4742inline _LIBCPP_INLINE_VISIBILITY
4743shared_ptr<_Tp>
4744allocate_shared(const _Alloc& __a)
4745{
4746 return shared_ptr<_Tp>::allocate_shared(__a);
4747}
4748
4749template<class _Tp, class _Alloc, class _A0>
4750inline _LIBCPP_INLINE_VISIBILITY
4751shared_ptr<_Tp>
4752allocate_shared(const _Alloc& __a, _A0& __a0)
4753{
4754 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4755}
4756
4757template<class _Tp, class _Alloc, class _A0, class _A1>
4758inline _LIBCPP_INLINE_VISIBILITY
4759shared_ptr<_Tp>
4760allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4761{
4762 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4763}
4764
4765template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4766inline _LIBCPP_INLINE_VISIBILITY
4767shared_ptr<_Tp>
4768allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4769{
4770 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4771}
4772
4773#endif // _LIBCPP_HAS_NO_VARIADICS
4774
4775template<class _Tp, class _Up>
4776inline _LIBCPP_INLINE_VISIBILITY
4777bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004778operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004779{
4780 return __x.get() == __y.get();
4781}
4782
4783template<class _Tp, class _Up>
4784inline _LIBCPP_INLINE_VISIBILITY
4785bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004786operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004787{
4788 return !(__x == __y);
4789}
4790
4791template<class _Tp, class _Up>
4792inline _LIBCPP_INLINE_VISIBILITY
4793bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004794operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004795{
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004796#if _LIBCPP_STD_VER <= 11
Eric Fiselierf8898c82015-02-05 23:01:40 +00004797 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4798 return less<_Vp>()(__x.get(), __y.get());
Marshall Clow8afdf7a2018-02-12 17:26:40 +00004799#else
4800 return less<>()(__x.get(), __y.get());
4801#endif
4802
Howard Hinnantb17caf92012-02-21 21:02:58 +00004803}
4804
4805template<class _Tp, class _Up>
4806inline _LIBCPP_INLINE_VISIBILITY
4807bool
4808operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4809{
4810 return __y < __x;
4811}
4812
4813template<class _Tp, class _Up>
4814inline _LIBCPP_INLINE_VISIBILITY
4815bool
4816operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4817{
4818 return !(__y < __x);
4819}
4820
4821template<class _Tp, class _Up>
4822inline _LIBCPP_INLINE_VISIBILITY
4823bool
4824operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4825{
4826 return !(__x < __y);
4827}
4828
4829template<class _Tp>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
4832operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4833{
4834 return !__x;
4835}
4836
4837template<class _Tp>
4838inline _LIBCPP_INLINE_VISIBILITY
4839bool
4840operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4841{
4842 return !__x;
4843}
4844
4845template<class _Tp>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4849{
4850 return static_cast<bool>(__x);
4851}
4852
4853template<class _Tp>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4857{
4858 return static_cast<bool>(__x);
4859}
4860
4861template<class _Tp>
4862inline _LIBCPP_INLINE_VISIBILITY
4863bool
4864operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4865{
4866 return less<_Tp*>()(__x.get(), nullptr);
4867}
4868
4869template<class _Tp>
4870inline _LIBCPP_INLINE_VISIBILITY
4871bool
4872operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4873{
4874 return less<_Tp*>()(nullptr, __x.get());
4875}
4876
4877template<class _Tp>
4878inline _LIBCPP_INLINE_VISIBILITY
4879bool
4880operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4881{
4882 return nullptr < __x;
4883}
4884
4885template<class _Tp>
4886inline _LIBCPP_INLINE_VISIBILITY
4887bool
4888operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4889{
4890 return __x < nullptr;
4891}
4892
4893template<class _Tp>
4894inline _LIBCPP_INLINE_VISIBILITY
4895bool
4896operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4897{
4898 return !(nullptr < __x);
4899}
4900
4901template<class _Tp>
4902inline _LIBCPP_INLINE_VISIBILITY
4903bool
4904operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4905{
4906 return !(__x < nullptr);
4907}
4908
4909template<class _Tp>
4910inline _LIBCPP_INLINE_VISIBILITY
4911bool
4912operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4913{
4914 return !(__x < nullptr);
4915}
4916
4917template<class _Tp>
4918inline _LIBCPP_INLINE_VISIBILITY
4919bool
4920operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4921{
4922 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004923}
4924
4925template<class _Tp>
4926inline _LIBCPP_INLINE_VISIBILITY
4927void
Howard Hinnant719bda32011-05-28 14:41:13 +00004928swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004929{
4930 __x.swap(__y);
4931}
4932
4933template<class _Tp, class _Up>
4934inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004935typename enable_if
4936<
4937 !is_array<_Tp>::value && !is_array<_Up>::value,
4938 shared_ptr<_Tp>
4939>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004940static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004941{
4942 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4943}
4944
4945template<class _Tp, class _Up>
4946inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004947typename enable_if
4948<
4949 !is_array<_Tp>::value && !is_array<_Up>::value,
4950 shared_ptr<_Tp>
4951>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004952dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004953{
4954 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4955 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4956}
4957
4958template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004959typename enable_if
4960<
4961 is_array<_Tp>::value == is_array<_Up>::value,
4962 shared_ptr<_Tp>
4963>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004964const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004965{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004966 typedef typename remove_extent<_Tp>::type _RTp;
4967 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004968}
4969
Howard Hinnant72f73582010-08-11 17:04:31 +00004970#ifndef _LIBCPP_NO_RTTI
4971
Howard Hinnantc51e1022010-05-11 19:42:16 +00004972template<class _Dp, class _Tp>
4973inline _LIBCPP_INLINE_VISIBILITY
4974_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004975get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004976{
4977 return __p.template __get_deleter<_Dp>();
4978}
4979
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004980#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004981
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004983class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004984{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004985public:
4986 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004987private:
4988 element_type* __ptr_;
4989 __shared_weak_count* __cntrl_;
4990
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004991public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004993 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004994 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004995 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4996 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004998 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004999 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005000 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5001 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005002
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005003#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005005 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005006 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005007 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5008 _NOEXCEPT;
5009#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005010 ~weak_ptr();
5011
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005013 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005014 template<class _Yp>
5015 typename enable_if
5016 <
5017 is_convertible<_Yp*, element_type*>::value,
5018 weak_ptr&
5019 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005021 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5022
5023#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5024
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005026 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5027 template<class _Yp>
5028 typename enable_if
5029 <
5030 is_convertible<_Yp*, element_type*>::value,
5031 weak_ptr&
5032 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005034 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5035
5036#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5037
5038 template<class _Yp>
5039 typename enable_if
5040 <
5041 is_convertible<_Yp*, element_type*>::value,
5042 weak_ptr&
5043 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005045 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005046
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005048 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005050 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005051
Howard Hinnant756c69b2010-09-22 16:48:34 +00005052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005053 long use_count() const _NOEXCEPT
5054 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005056 bool expired() const _NOEXCEPT
5057 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5058 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005059 template<class _Up>
5060 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005061 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005062 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005063 template<class _Up>
5064 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005065 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005066 {return __cntrl_ < __r.__cntrl_;}
5067
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005068 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5069 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005070};
5071
5072template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005073inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005074_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005075weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005076 : __ptr_(0),
5077 __cntrl_(0)
5078{
5079}
5080
5081template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005082inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005083weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005084 : __ptr_(__r.__ptr_),
5085 __cntrl_(__r.__cntrl_)
5086{
5087 if (__cntrl_)
5088 __cntrl_->__add_weak();
5089}
5090
5091template<class _Tp>
5092template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005093inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005094weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005095 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005096 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005097 : __ptr_(__r.__ptr_),
5098 __cntrl_(__r.__cntrl_)
5099{
5100 if (__cntrl_)
5101 __cntrl_->__add_weak();
5102}
5103
5104template<class _Tp>
5105template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005106inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005107weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005108 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005109 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005110 : __ptr_(__r.__ptr_),
5111 __cntrl_(__r.__cntrl_)
5112{
5113 if (__cntrl_)
5114 __cntrl_->__add_weak();
5115}
5116
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005117#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5118
5119template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005120inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005121weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5122 : __ptr_(__r.__ptr_),
5123 __cntrl_(__r.__cntrl_)
5124{
5125 __r.__ptr_ = 0;
5126 __r.__cntrl_ = 0;
5127}
5128
5129template<class _Tp>
5130template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005131inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005132weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5133 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5134 _NOEXCEPT
5135 : __ptr_(__r.__ptr_),
5136 __cntrl_(__r.__cntrl_)
5137{
5138 __r.__ptr_ = 0;
5139 __r.__cntrl_ = 0;
5140}
5141
5142#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5143
Howard Hinnantc51e1022010-05-11 19:42:16 +00005144template<class _Tp>
5145weak_ptr<_Tp>::~weak_ptr()
5146{
5147 if (__cntrl_)
5148 __cntrl_->__release_weak();
5149}
5150
5151template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005152inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005153weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005154weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005155{
5156 weak_ptr(__r).swap(*this);
5157 return *this;
5158}
5159
5160template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005161template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005162inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005163typename enable_if
5164<
5165 is_convertible<_Yp*, _Tp*>::value,
5166 weak_ptr<_Tp>&
5167>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005168weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005169{
5170 weak_ptr(__r).swap(*this);
5171 return *this;
5172}
5173
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005174#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5175
5176template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005177inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005178weak_ptr<_Tp>&
5179weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5180{
5181 weak_ptr(_VSTD::move(__r)).swap(*this);
5182 return *this;
5183}
5184
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005186template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005187inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005188typename enable_if
5189<
5190 is_convertible<_Yp*, _Tp*>::value,
5191 weak_ptr<_Tp>&
5192>::type
5193weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5194{
5195 weak_ptr(_VSTD::move(__r)).swap(*this);
5196 return *this;
5197}
5198
5199#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5200
5201template<class _Tp>
5202template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005203inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005204typename enable_if
5205<
5206 is_convertible<_Yp*, _Tp*>::value,
5207 weak_ptr<_Tp>&
5208>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005209weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005210{
5211 weak_ptr(__r).swap(*this);
5212 return *this;
5213}
5214
5215template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005216inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005217void
Howard Hinnant719bda32011-05-28 14:41:13 +00005218weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005219{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005220 _VSTD::swap(__ptr_, __r.__ptr_);
5221 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005222}
5223
5224template<class _Tp>
5225inline _LIBCPP_INLINE_VISIBILITY
5226void
Howard Hinnant719bda32011-05-28 14:41:13 +00005227swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005228{
5229 __x.swap(__y);
5230}
5231
5232template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005234void
Howard Hinnant719bda32011-05-28 14:41:13 +00005235weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005236{
5237 weak_ptr().swap(*this);
5238}
5239
5240template<class _Tp>
5241template<class _Yp>
5242shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005243 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005244 : __ptr_(__r.__ptr_),
5245 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5246{
5247 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005248 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005249}
5250
5251template<class _Tp>
5252shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005253weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005254{
5255 shared_ptr<_Tp> __r;
5256 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5257 if (__r.__cntrl_)
5258 __r.__ptr_ = __ptr_;
5259 return __r;
5260}
5261
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005262#if _LIBCPP_STD_VER > 14
5263template <class _Tp = void> struct owner_less;
5264#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005265template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005267
5268template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005269struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005270 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005271{
5272 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005273 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005274 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005275 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005276 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005277 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005278 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005279 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005280 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005281 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005282};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005283
5284template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005285struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005286 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5287{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005288 typedef bool result_type;
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, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005291 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005292 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005293 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005294 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005295 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005296 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005297 {return __x.owner_before(__y);}
5298};
5299
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005300#if _LIBCPP_STD_VER > 14
5301template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005302struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005303{
5304 template <class _Tp, class _Up>
5305 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005306 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005307 {return __x.owner_before(__y);}
5308 template <class _Tp, class _Up>
5309 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005310 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005311 {return __x.owner_before(__y);}
5312 template <class _Tp, class _Up>
5313 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005314 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005315 {return __x.owner_before(__y);}
5316 template <class _Tp, class _Up>
5317 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005318 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005319 {return __x.owner_before(__y);}
5320 typedef void is_transparent;
5321};
5322#endif
5323
Howard Hinnantc51e1022010-05-11 19:42:16 +00005324template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005325class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005326{
5327 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005328protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005329 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005330 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005332 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005334 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5335 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005337 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005338public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005340 shared_ptr<_Tp> shared_from_this()
5341 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005343 shared_ptr<_Tp const> shared_from_this() const
5344 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005345
Eric Fiselier84006862016-06-02 00:15:35 +00005346#if _LIBCPP_STD_VER > 14
5347 _LIBCPP_INLINE_VISIBILITY
5348 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5349 { return __weak_this_; }
5350
5351 _LIBCPP_INLINE_VISIBILITY
5352 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5353 { return __weak_this_; }
5354#endif // _LIBCPP_STD_VER > 14
5355
Howard Hinnantc51e1022010-05-11 19:42:16 +00005356 template <class _Up> friend class shared_ptr;
5357};
5358
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005359template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005360struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005361{
5362 typedef shared_ptr<_Tp> argument_type;
5363 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005364
Howard Hinnant756c69b2010-09-22 16:48:34 +00005365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005366 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005367 {
5368 return hash<_Tp*>()(__ptr.get());
5369 }
5370};
5371
Howard Hinnantc834c512011-11-29 18:15:50 +00005372template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005373inline _LIBCPP_INLINE_VISIBILITY
5374basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005375operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005376
Eric Fiselier9b492672016-06-18 02:12:53 +00005377
5378#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005379
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005380class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005381{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005382 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005383public:
5384 void lock() _NOEXCEPT;
5385 void unlock() _NOEXCEPT;
5386
5387private:
5388 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5389 __sp_mut(const __sp_mut&);
5390 __sp_mut& operator=(const __sp_mut&);
5391
Howard Hinnant8331b762013-03-06 23:30:19 +00005392 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005393};
5394
Mehdi Amini228053d2017-05-04 17:08:54 +00005395_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5396__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005397
5398template <class _Tp>
5399inline _LIBCPP_INLINE_VISIBILITY
5400bool
5401atomic_is_lock_free(const shared_ptr<_Tp>*)
5402{
5403 return false;
5404}
5405
5406template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005407_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005408shared_ptr<_Tp>
5409atomic_load(const shared_ptr<_Tp>* __p)
5410{
5411 __sp_mut& __m = __get_sp_mut(__p);
5412 __m.lock();
5413 shared_ptr<_Tp> __q = *__p;
5414 __m.unlock();
5415 return __q;
5416}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005417
Howard Hinnant9fa30202012-07-30 01:40:57 +00005418template <class _Tp>
5419inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005420_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005421shared_ptr<_Tp>
5422atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5423{
5424 return atomic_load(__p);
5425}
5426
5427template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005428_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005429void
5430atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5431{
5432 __sp_mut& __m = __get_sp_mut(__p);
5433 __m.lock();
5434 __p->swap(__r);
5435 __m.unlock();
5436}
5437
5438template <class _Tp>
5439inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005440_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005441void
5442atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5443{
5444 atomic_store(__p, __r);
5445}
5446
5447template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005448_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005449shared_ptr<_Tp>
5450atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5451{
5452 __sp_mut& __m = __get_sp_mut(__p);
5453 __m.lock();
5454 __p->swap(__r);
5455 __m.unlock();
5456 return __r;
5457}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005458
Howard Hinnant9fa30202012-07-30 01:40:57 +00005459template <class _Tp>
5460inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005461_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005462shared_ptr<_Tp>
5463atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5464{
5465 return atomic_exchange(__p, __r);
5466}
5467
5468template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005469_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005470bool
5471atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5472{
Marshall Clow4201ee82016-05-18 17:50:13 +00005473 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005474 __sp_mut& __m = __get_sp_mut(__p);
5475 __m.lock();
5476 if (__p->__owner_equivalent(*__v))
5477 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005478 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005479 *__p = __w;
5480 __m.unlock();
5481 return true;
5482 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005483 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005484 *__v = *__p;
5485 __m.unlock();
5486 return false;
5487}
5488
5489template <class _Tp>
5490inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005491_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005492bool
5493atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5494{
5495 return atomic_compare_exchange_strong(__p, __v, __w);
5496}
5497
5498template <class _Tp>
5499inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005500_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005501bool
5502atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5503 shared_ptr<_Tp> __w, memory_order, memory_order)
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_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5513 shared_ptr<_Tp> __w, memory_order, memory_order)
5514{
5515 return atomic_compare_exchange_weak(__p, __v, __w);
5516}
5517
Eric Fiselier9b492672016-06-18 02:12:53 +00005518#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005519
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005520//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005521#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5522# ifndef _LIBCPP_CXX03_LANG
5523enum class pointer_safety : unsigned char {
5524 relaxed,
5525 preferred,
5526 strict
5527};
5528# endif
5529#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005530struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005531{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005532 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005533 {
5534 relaxed,
5535 preferred,
5536 strict
5537 };
5538
Howard Hinnant49e145e2012-10-30 19:06:59 +00005539 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005540
Howard Hinnant756c69b2010-09-22 16:48:34 +00005541 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005542 pointer_safety() : __v_() {}
5543
5544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005545 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005547 operator int() const {return __v_;}
5548};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005549#endif
5550
5551#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5552 defined(_LIBCPP_BUILDING_MEMORY)
5553_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5554#else
5555// This function is only offered in C++03 under ABI v1.
5556# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5557inline _LIBCPP_INLINE_VISIBILITY
5558pointer_safety get_pointer_safety() _NOEXCEPT {
5559 return pointer_safety::relaxed;
5560}
5561# endif
5562#endif
5563
Howard Hinnantc51e1022010-05-11 19:42:16 +00005564
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005565_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5566_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5567_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005568_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005569
5570template <class _Tp>
5571inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005572_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005573undeclare_reachable(_Tp* __p)
5574{
5575 return static_cast<_Tp*>(__undeclare_reachable(__p));
5576}
5577
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005578_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005579
Marshall Clow8982dcd2015-07-13 20:04:56 +00005580// --- Helper for container swap --
5581template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005582inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005583void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5584#if _LIBCPP_STD_VER >= 14
5585 _NOEXCEPT
5586#else
5587 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5588#endif
5589{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005590 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005591 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5592}
5593
5594template <typename _Alloc>
5595_LIBCPP_INLINE_VISIBILITY
5596void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5597#if _LIBCPP_STD_VER >= 14
5598 _NOEXCEPT
5599#else
5600 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5601#endif
5602{
5603 using _VSTD::swap;
5604 swap(__a1, __a2);
5605}
5606
5607template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005608inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005609void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5610
Marshall Clowff91de82015-08-18 19:51:37 +00005611template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005612struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005613 _Traits::propagate_on_container_move_assignment::value
5614#if _LIBCPP_STD_VER > 14
5615 || _Traits::is_always_equal::value
5616#else
5617 && is_nothrow_move_assignable<_Alloc>::value
5618#endif
5619 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005620
Marshall Clowa591b9a2016-07-11 21:38:08 +00005621
5622#ifndef _LIBCPP_HAS_NO_VARIADICS
5623template <class _Tp, class _Alloc>
5624struct __temp_value {
5625 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005626
Marshall Clowa591b9a2016-07-11 21:38:08 +00005627 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5628 _Alloc &__a;
5629
5630 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5631 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005632
Marshall Clowa591b9a2016-07-11 21:38:08 +00005633 template<class... _Args>
5634 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5635 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005636
Marshall Clowa591b9a2016-07-11 21:38:08 +00005637 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5638 };
5639#endif
5640
Marshall Clow82c90aa2018-01-11 19:36:22 +00005641#if _LIBCPP_STD_VER > 14
5642template<typename _Alloc, typename = void>
5643struct __is_allocator : false_type {};
5644
5645template<typename _Alloc>
5646struct __is_allocator<_Alloc,
5647 void_t<typename _Alloc::value_type, decltype(_VSTD::declval<_Alloc&>().allocate(size_t{}))>>
5648 : true_type {};
5649#endif
5650
Howard Hinnantc51e1022010-05-11 19:42:16 +00005651_LIBCPP_END_NAMESPACE_STD
5652
Eric Fiselierf4433a32017-05-31 22:07:49 +00005653_LIBCPP_POP_MACROS
5654
Howard Hinnantc51e1022010-05-11 19:42:16 +00005655#endif // _LIBCPP_MEMORY