blob: 5c109df1cafe9ef97c223cd03807ce1ea55d7b6b [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 { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
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
Howard Hinnant719bda32011-05-28 14:41:13 +0000129 allocator() noexcept;
130 allocator(const allocator&) noexcept;
131 template <class U> allocator(const allocator<U>&) noexcept;
132 ~allocator();
133 pointer address(reference x) const noexcept;
134 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000136 void deallocate(pointer p, size_type n) noexcept;
137 size_type max_size() const noexcept;
138 template<class U, class... Args>
139 void construct(U* p, Args&&... args);
140 template <class U>
141 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142};
143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000148bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
150template <class OutputIterator, class T>
151class raw_storage_iterator
152 : public iterator<output_iterator_tag,
153 T, // purposefully not C++03
154 ptrdiff_t, // purposefully not C++03
155 T*, // purposefully not C++03
156 raw_storage_iterator&> // purposefully not C++03
157{
158public:
159 explicit raw_storage_iterator(OutputIterator x);
160 raw_storage_iterator& operator*();
161 raw_storage_iterator& operator=(const T& element);
162 raw_storage_iterator& operator++();
163 raw_storage_iterator operator++(int);
164};
165
Howard Hinnant719bda32011-05-28 14:41:13 +0000166template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
167template <class T> void return_temporary_buffer(T* p) noexcept;
168
169template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000170template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class InputIterator, class ForwardIterator>
173ForwardIterator
174uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
175
Howard Hinnant719bda32011-05-28 14:41:13 +0000176template <class InputIterator, class Size, class ForwardIterator>
177ForwardIterator
178uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
179
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180template <class ForwardIterator, class T>
181void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
182
183template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000184ForwardIterator
185uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000187template <class T>
188void destroy_at(T* location);
189
190template <class ForwardIterator>
191 void destroy(ForwardIterator first, ForwardIterator last);
192
193template <class ForwardIterator, class Size>
194 ForwardIterator destroy_n(ForwardIterator first, Size n);
195
196template <class InputIterator, class ForwardIterator>
197 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
198
199template <class InputIterator, class Size, class ForwardIterator>
200 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
201
202template <class ForwardIterator>
203 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
204
205template <class ForwardIterator, class Size>
206 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
207
208template <class ForwardIterator>
209 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
210
211template <class ForwardIterator, class Size>
212 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
213
Marshall Clowb22274f2017-01-24 22:22:33 +0000214template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
216template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000217class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218{
219public:
220 typedef X element_type;
221
222 explicit auto_ptr(X* p =0) throw();
223 auto_ptr(auto_ptr&) throw();
224 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
225 auto_ptr& operator=(auto_ptr&) throw();
226 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
227 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
228 ~auto_ptr() throw();
229
230 typename add_lvalue_reference<X>::type operator*() const throw();
231 X* operator->() const throw();
232 X* get() const throw();
233 X* release() throw();
234 void reset(X* p =0) throw();
235
236 auto_ptr(auto_ptr_ref<X>) throw();
237 template<class Y> operator auto_ptr_ref<Y>() throw();
238 template<class Y> operator auto_ptr<Y>() throw();
239};
240
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000241template <class T>
242struct default_delete
243{
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 constexpr default_delete() noexcept = default;
245 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000246
Howard Hinnant719bda32011-05-28 14:41:13 +0000247 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000248};
249
250template <class T>
251struct default_delete<T[]>
252{
Howard Hinnant719bda32011-05-28 14:41:13 +0000253 constexpr default_delete() noexcept = default;
254 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255 template <class U> void operator()(U*) const = delete;
256};
257
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000258template <class T, class D = default_delete<T>>
259class unique_ptr
260{
261public:
262 typedef see below pointer;
263 typedef T element_type;
264 typedef D deleter_type;
265
266 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000267 constexpr unique_ptr() noexcept;
268 explicit unique_ptr(pointer p) noexcept;
269 unique_ptr(pointer p, see below d1) noexcept;
270 unique_ptr(pointer p, see below d2) noexcept;
271 unique_ptr(unique_ptr&& u) noexcept;
272 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000273 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000274 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000275 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000276 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000277
278 // destructor
279 ~unique_ptr();
280
281 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000282 unique_ptr& operator=(unique_ptr&& u) noexcept;
283 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
284 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000285
286 // observers
287 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000288 pointer operator->() const noexcept;
289 pointer get() const noexcept;
290 deleter_type& get_deleter() noexcept;
291 const deleter_type& get_deleter() const noexcept;
292 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000293
294 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000295 pointer release() noexcept;
296 void reset(pointer p = pointer()) noexcept;
297 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000298};
299
300template <class T, class D>
301class unique_ptr<T[], D>
302{
303public:
304 typedef implementation-defined pointer;
305 typedef T element_type;
306 typedef D deleter_type;
307
308 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000309 constexpr unique_ptr() noexcept;
310 explicit unique_ptr(pointer p) noexcept;
311 unique_ptr(pointer p, see below d) noexcept;
312 unique_ptr(pointer p, see below d) noexcept;
313 unique_ptr(unique_ptr&& u) noexcept;
314 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000317 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000318
319 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000320 unique_ptr& operator=(unique_ptr&& u) noexcept;
321 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000322
323 // observers
324 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000325 pointer get() const noexcept;
326 deleter_type& get_deleter() noexcept;
327 const deleter_type& get_deleter() const noexcept;
328 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000329
330 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000331 pointer release() noexcept;
332 void reset(pointer p = pointer()) noexcept;
333 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000334 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000335 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336};
337
338template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000339 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000340
341template <class T1, class D1, class T2, class D2>
342 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
343template <class T1, class D1, class T2, class D2>
344 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
345template <class T1, class D1, class T2, class D2>
346 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
347template <class T1, class D1, class T2, class D2>
348 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
349template <class T1, class D1, class T2, class D2>
350 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
351template <class T1, class D1, class T2, class D2>
352 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
353
Howard Hinnant719bda32011-05-28 14:41:13 +0000354template <class T, class D>
355 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
356template <class T, class D>
357 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
358template <class T, class D>
359 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
360template <class T, class D>
361 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
362
363template <class T, class D>
364 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
365template <class T, class D>
366 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
367template <class T, class D>
368 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
369template <class T, class D>
370 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
371template <class T, class D>
372 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
375template <class T, class D>
376 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
377template <class T, class D>
378 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
379
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000380class bad_weak_ptr
381 : public std::exception
382{
Howard Hinnant719bda32011-05-28 14:41:13 +0000383 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000384};
385
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000386template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
387template<class T> unique_ptr<T> make_unique(size_t n); // C++14
388template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
389
Marshall Clowe52a3242017-11-27 15:51:36 +0000390template<class E, class T, class Y, class D>
391 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
392
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393template<class T>
394class shared_ptr
395{
396public:
397 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000398 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000399
400 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000402 template<class Y> explicit shared_ptr(Y* p);
403 template<class Y, class D> shared_ptr(Y* p, D d);
404 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
405 template <class D> shared_ptr(nullptr_t p, D d);
406 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000407 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
408 shared_ptr(const shared_ptr& r) noexcept;
409 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
410 shared_ptr(shared_ptr&& r) noexcept;
411 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000412 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000413 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000414 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
415 shared_ptr(nullptr_t) : shared_ptr() { }
416
417 // destructor:
418 ~shared_ptr();
419
420 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000421 shared_ptr& operator=(const shared_ptr& r) noexcept;
422 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
423 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000424 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000425 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000426 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
427
428 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 void swap(shared_ptr& r) noexcept;
430 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000431 template<class Y> void reset(Y* p);
432 template<class Y, class D> void reset(Y* p, D d);
433 template<class Y, class D, class A> void reset(Y* p, D d, A a);
434
Howard Hinnant719bda32011-05-28 14:41:13 +0000435 // observers:
436 T* get() const noexcept;
437 T& operator*() const noexcept;
438 T* operator->() const noexcept;
439 long use_count() const noexcept;
440 bool unique() const noexcept;
441 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000442 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
443 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000444};
445
446// shared_ptr comparisons:
447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000453template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000454 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000455template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000456 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000457template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000458 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
459
460template <class T>
461 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
462template <class T>
463 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
464template <class T>
465 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
466template <class T>
467 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
468template <class T>
469 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
470template <class T>
471bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
472template <class T>
473 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
474template <class T>
475 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
476template <class T>
477 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
478template <class T>
479 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
480template <class T>
481 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
482template <class T>
483 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484
485// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000486template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487
488// shared_ptr casts:
489template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000490 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000491template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000492 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000493template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000494 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000495
496// shared_ptr I/O:
497template<class E, class T, class Y>
498 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
499
500// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000501template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000502
503template<class T, class... Args>
504 shared_ptr<T> make_shared(Args&&... args);
505template<class T, class A, class... Args>
506 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
507
508template<class T>
509class weak_ptr
510{
511public:
512 typedef T element_type;
513
514 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000515 constexpr weak_ptr() noexcept;
516 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
517 weak_ptr(weak_ptr const& r) noexcept;
518 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000519 weak_ptr(weak_ptr&& r) noexcept; // C++14
520 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000521
522 // destructor
523 ~weak_ptr();
524
525 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000526 weak_ptr& operator=(weak_ptr const& r) noexcept;
527 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
528 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000529 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
530 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000531
532 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000533 void swap(weak_ptr& r) noexcept;
534 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000535
536 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000537 long use_count() const noexcept;
538 bool expired() const noexcept;
539 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000540 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
541 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000542};
543
544// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000545template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000546
547// class owner_less:
548template<class T> struct owner_less;
549
550template<class T>
551struct owner_less<shared_ptr<T>>
552 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
553{
554 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000555 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
556 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
557 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000558};
559
560template<class T>
561struct owner_less<weak_ptr<T>>
562 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
563{
564 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000565 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
566 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
567 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
568};
569
570template <> // Added in C++14
571struct owner_less<void>
572{
573 template <class _Tp, class _Up>
574 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
575 template <class _Tp, class _Up>
576 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
577 template <class _Tp, class _Up>
578 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
579 template <class _Tp, class _Up>
580 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
581
582 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000583};
584
585template<class T>
586class enable_shared_from_this
587{
588protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000589 constexpr enable_shared_from_this() noexcept;
590 enable_shared_from_this(enable_shared_from_this const&) noexcept;
591 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000592 ~enable_shared_from_this();
593public:
594 shared_ptr<T> shared_from_this();
595 shared_ptr<T const> shared_from_this() const;
596};
597
598template<class T>
599 bool atomic_is_lock_free(const shared_ptr<T>* p);
600template<class T>
601 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
602template<class T>
603 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
604template<class T>
605 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
606template<class T>
607 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
608template<class T>
609 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
610template<class T>
611 shared_ptr<T>
612 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
613template<class T>
614 bool
615 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
616template<class T>
617 bool
618 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
619template<class T>
620 bool
621 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
622 shared_ptr<T> w, memory_order success,
623 memory_order failure);
624template<class T>
625 bool
626 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
627 shared_ptr<T> w, memory_order success,
628 memory_order failure);
629// Hash support
630template <class T> struct hash;
631template <class T, class D> struct hash<unique_ptr<T, D> >;
632template <class T> struct hash<shared_ptr<T> >;
633
634// Pointer safety
635enum class pointer_safety { relaxed, preferred, strict };
636void declare_reachable(void *p);
637template <class T> T *undeclare_reachable(T *p);
638void declare_no_pointers(char *p, size_t n);
639void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000640pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000641
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
643
644} // std
645
646*/
647
648#include <__config>
649#include <type_traits>
650#include <typeinfo>
651#include <cstddef>
652#include <cstdint>
653#include <new>
654#include <utility>
655#include <limits>
656#include <iterator>
657#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000658#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000659#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000660#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000661#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000662#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000663#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000664# include <atomic>
665#endif
666
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000667#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000669#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670
Eric Fiselierf4433a32017-05-31 22:07:49 +0000671_LIBCPP_PUSH_MACROS
672#include <__undef_macros>
673
674
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675_LIBCPP_BEGIN_NAMESPACE_STD
676
Eric Fiselier89659d12015-07-07 00:27:16 +0000677template <class _ValueType>
678inline _LIBCPP_ALWAYS_INLINE
679_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
680#if !defined(_LIBCPP_HAS_NO_THREADS) && \
681 defined(__ATOMIC_RELAXED) && \
682 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
683 return __atomic_load_n(__value, __ATOMIC_RELAXED);
684#else
685 return *__value;
686#endif
687}
688
Kuba Breckade9d6792016-09-04 09:55:12 +0000689template <class _ValueType>
690inline _LIBCPP_ALWAYS_INLINE
691_ValueType __libcpp_acquire_load(_ValueType const* __value) {
692#if !defined(_LIBCPP_HAS_NO_THREADS) && \
693 defined(__ATOMIC_ACQUIRE) && \
694 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
695 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
696#else
697 return *__value;
698#endif
699}
700
Marshall Clow78dbe462016-11-14 18:22:19 +0000701// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000702
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703template <class _Tp> class allocator;
704
705template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000706class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707{
708public:
709 typedef void* pointer;
710 typedef const void* const_pointer;
711 typedef void value_type;
712
713 template <class _Up> struct rebind {typedef allocator<_Up> other;};
714};
715
Howard Hinnant9f771052012-01-19 23:15:22 +0000716template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000717class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000718{
719public:
720 typedef const void* pointer;
721 typedef const void* const_pointer;
722 typedef const void value_type;
723
724 template <class _Up> struct rebind {typedef allocator<_Up> other;};
725};
726
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727// pointer_traits
728
Marshall Clow0be70c32017-06-14 21:23:57 +0000729template <class _Tp, class = void>
730struct __has_element_type : false_type {};
731
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000733struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000734 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735
736template <class _Ptr, bool = __has_element_type<_Ptr>::value>
737struct __pointer_traits_element_type;
738
739template <class _Ptr>
740struct __pointer_traits_element_type<_Ptr, true>
741{
742 typedef typename _Ptr::element_type type;
743};
744
745#ifndef _LIBCPP_HAS_NO_VARIADICS
746
747template <template <class, class...> class _Sp, class _Tp, class ..._Args>
748struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
749{
750 typedef typename _Sp<_Tp, _Args...>::element_type type;
751};
752
753template <template <class, class...> class _Sp, class _Tp, class ..._Args>
754struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
755{
756 typedef _Tp type;
757};
758
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000759#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760
761template <template <class> class _Sp, class _Tp>
762struct __pointer_traits_element_type<_Sp<_Tp>, true>
763{
764 typedef typename _Sp<_Tp>::element_type type;
765};
766
767template <template <class> class _Sp, class _Tp>
768struct __pointer_traits_element_type<_Sp<_Tp>, false>
769{
770 typedef _Tp type;
771};
772
773template <template <class, class> class _Sp, class _Tp, class _A0>
774struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
775{
776 typedef typename _Sp<_Tp, _A0>::element_type type;
777};
778
779template <template <class, class> class _Sp, class _Tp, class _A0>
780struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
781{
782 typedef _Tp type;
783};
784
785template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
786struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
787{
788 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
789};
790
791template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
792struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
793{
794 typedef _Tp type;
795};
796
797template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
798 class _A1, class _A2>
799struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
800{
801 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
802};
803
804template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
805 class _A1, class _A2>
806struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
807{
808 typedef _Tp type;
809};
810
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000811#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812
Marshall Clow0be70c32017-06-14 21:23:57 +0000813template <class _Tp, class = void>
814struct __has_difference_type : false_type {};
815
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000817struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000818 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819
820template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
821struct __pointer_traits_difference_type
822{
823 typedef ptrdiff_t type;
824};
825
826template <class _Ptr>
827struct __pointer_traits_difference_type<_Ptr, true>
828{
829 typedef typename _Ptr::difference_type type;
830};
831
832template <class _Tp, class _Up>
833struct __has_rebind
834{
835private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000836 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 template <class _Xp> static __two __test(...);
838 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
839public:
840 static const bool value = sizeof(__test<_Tp>(0)) == 1;
841};
842
843template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
844struct __pointer_traits_rebind
845{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 typedef typename _Tp::template rebind<_Up> type;
848#else
849 typedef typename _Tp::template rebind<_Up>::other type;
850#endif
851};
852
853#ifndef _LIBCPP_HAS_NO_VARIADICS
854
855template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
867{
868 typedef _Sp<_Up, _Args...> type;
869};
870
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000871#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
873template <template <class> class _Sp, class _Tp, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
875{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 typedef typename _Sp<_Tp>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class> class _Sp, class _Tp, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
885{
886 typedef _Sp<_Up> type;
887};
888
889template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
891{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000892#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
894#else
895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
896#endif
897};
898
899template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
900struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
901{
902 typedef _Sp<_Up, _A0> type;
903};
904
905template <template <class, class, class> class _Sp, class _Tp, class _A0,
906 class _A1, class _Up>
907struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
908{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000909#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
911#else
912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
913#endif
914};
915
916template <template <class, class, class> class _Sp, class _Tp, class _A0,
917 class _A1, class _Up>
918struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
919{
920 typedef _Sp<_Up, _A0, _A1> type;
921};
922
923template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
924 class _A1, class _A2, class _Up>
925struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
926{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000927#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
929#else
930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
931#endif
932};
933
934template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
935 class _A1, class _A2, class _Up>
936struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
937{
938 typedef _Sp<_Up, _A0, _A1, _A2> type;
939};
940
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000941#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000944struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
946 typedef _Ptr pointer;
947 typedef typename __pointer_traits_element_type<pointer>::type element_type;
948 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
949
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000951 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952#else
953 template <class _Up> struct rebind
954 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957private:
958 struct __nat {};
959public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 static pointer pointer_to(typename conditional<is_void<element_type>::value,
962 __nat, element_type>::type& __r)
963 {return pointer::pointer_to(__r);}
964};
965
966template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
969 typedef _Tp* pointer;
970 typedef _Tp element_type;
971 typedef ptrdiff_t difference_type;
972
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000973#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 template <class _Up> using rebind = _Up*;
975#else
976 template <class _Up> struct rebind {typedef _Up* other;};
977#endif
978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000984 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000985 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988template <class _From, class _To>
989struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991 typedef typename pointer_traits<_From>::template rebind<_To> type;
992#else
993 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
994#endif
995};
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// allocator_traits
998
Marshall Clow0be70c32017-06-14 21:23:57 +0000999template <class _Tp, class = void>
1000struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001
1002template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001003struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001004 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005
1006namespace __pointer_type_imp
1007{
1008
1009template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1010struct __pointer_type
1011{
1012 typedef typename _Dp::pointer type;
1013};
1014
1015template <class _Tp, class _Dp>
1016struct __pointer_type<_Tp, _Dp, false>
1017{
1018 typedef _Tp* type;
1019};
1020
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001021} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022
1023template <class _Tp, class _Dp>
1024struct __pointer_type
1025{
1026 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1027};
1028
Marshall Clow0be70c32017-06-14 21:23:57 +00001029template <class _Tp, class = void>
1030struct __has_const_pointer : false_type {};
1031
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001033struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001034 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035
1036template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1037struct __const_pointer
1038{
1039 typedef typename _Alloc::const_pointer type;
1040};
1041
1042template <class _Tp, class _Ptr, class _Alloc>
1043struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1044{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001045#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1047#else
1048 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1049#endif
1050};
1051
Marshall Clow0be70c32017-06-14 21:23:57 +00001052template <class _Tp, class = void>
1053struct __has_void_pointer : false_type {};
1054
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001056struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001057 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058
1059template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1060struct __void_pointer
1061{
1062 typedef typename _Alloc::void_pointer type;
1063};
1064
1065template <class _Ptr, class _Alloc>
1066struct __void_pointer<_Ptr, _Alloc, false>
1067{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001068#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1070#else
1071 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1072#endif
1073};
1074
Marshall Clow0be70c32017-06-14 21:23:57 +00001075template <class _Tp, class = void>
1076struct __has_const_void_pointer : false_type {};
1077
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001079struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001080 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081
1082template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1083struct __const_void_pointer
1084{
1085 typedef typename _Alloc::const_void_pointer type;
1086};
1087
1088template <class _Ptr, class _Alloc>
1089struct __const_void_pointer<_Ptr, _Alloc, false>
1090{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001091#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1093#else
1094 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1095#endif
1096};
1097
Howard Hinnantc834c512011-11-29 18:15:50 +00001098template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001099inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001100_Tp*
1101__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102{
1103 return __p;
1104}
1105
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001106#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107template <class _Pointer>
1108inline _LIBCPP_INLINE_VISIBILITY
1109typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001110__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001112 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001114#else
1115template <class _Pointer>
1116inline _LIBCPP_INLINE_VISIBILITY
1117auto
1118__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1119-> decltype(pointer_traits<_Pointer>::to_address(__p))
1120{
1121 return pointer_traits<_Pointer>::to_address(__p);
1122}
1123
1124template <class _Pointer, class... _None>
1125inline _LIBCPP_INLINE_VISIBILITY
1126auto
1127__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1128{
1129 return _VSTD::__to_raw_pointer(__p.operator->());
1130}
1131
1132template <class _Tp>
1133inline _LIBCPP_INLINE_VISIBILITY constexpr
1134_Tp*
1135to_address(_Tp* __p) _NOEXCEPT
1136{
1137 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1138 return __p;
1139}
1140
1141template <class _Pointer>
1142inline _LIBCPP_INLINE_VISIBILITY
1143auto
1144to_address(const _Pointer& __p) _NOEXCEPT
1145{
1146 return _VSTD::__to_raw_pointer(__p);
1147}
1148#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149
Marshall Clow0be70c32017-06-14 21:23:57 +00001150template <class _Tp, class = void>
1151struct __has_size_type : false_type {};
1152
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001154struct __has_size_type<_Tp,
1155 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001157template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158struct __size_type
1159{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001160 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161};
1162
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001163template <class _Alloc, class _DiffType>
1164struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165{
1166 typedef typename _Alloc::size_type type;
1167};
1168
Marshall Clow0be70c32017-06-14 21:23:57 +00001169template <class _Tp, class = void>
1170struct __has_propagate_on_container_copy_assignment : false_type {};
1171
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001173struct __has_propagate_on_container_copy_assignment<_Tp,
1174 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1175 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176
1177template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1178struct __propagate_on_container_copy_assignment
1179{
1180 typedef false_type type;
1181};
1182
1183template <class _Alloc>
1184struct __propagate_on_container_copy_assignment<_Alloc, true>
1185{
1186 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1187};
1188
Marshall Clow0be70c32017-06-14 21:23:57 +00001189template <class _Tp, class = void>
1190struct __has_propagate_on_container_move_assignment : false_type {};
1191
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001193struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001194 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1195 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196
1197template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1198struct __propagate_on_container_move_assignment
1199{
1200 typedef false_type type;
1201};
1202
1203template <class _Alloc>
1204struct __propagate_on_container_move_assignment<_Alloc, true>
1205{
1206 typedef typename _Alloc::propagate_on_container_move_assignment type;
1207};
1208
Marshall Clow0be70c32017-06-14 21:23:57 +00001209template <class _Tp, class = void>
1210struct __has_propagate_on_container_swap : false_type {};
1211
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001213struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001214 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1215 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216
1217template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1218struct __propagate_on_container_swap
1219{
1220 typedef false_type type;
1221};
1222
1223template <class _Alloc>
1224struct __propagate_on_container_swap<_Alloc, true>
1225{
1226 typedef typename _Alloc::propagate_on_container_swap type;
1227};
1228
Marshall Clow0be70c32017-06-14 21:23:57 +00001229template <class _Tp, class = void>
1230struct __has_is_always_equal : false_type {};
1231
Marshall Clow0b587562015-06-02 16:34:03 +00001232template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001233struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001234 typename __void_t<typename _Tp::is_always_equal>::type>
1235 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001236
1237template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1238struct __is_always_equal
1239{
1240 typedef typename _VSTD::is_empty<_Alloc>::type type;
1241};
1242
1243template <class _Alloc>
1244struct __is_always_equal<_Alloc, true>
1245{
1246 typedef typename _Alloc::is_always_equal type;
1247};
1248
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1250struct __has_rebind_other
1251{
1252private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001253 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 template <class _Xp> static __two __test(...);
1255 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1256public:
1257 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1258};
1259
1260template <class _Tp, class _Up>
1261struct __has_rebind_other<_Tp, _Up, false>
1262{
1263 static const bool value = false;
1264};
1265
1266template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1267struct __allocator_traits_rebind
1268{
1269 typedef typename _Tp::template rebind<_Up>::other type;
1270};
1271
1272#ifndef _LIBCPP_HAS_NO_VARIADICS
1273
1274template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1275struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1276{
1277 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1278};
1279
1280template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1281struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1282{
1283 typedef _Alloc<_Up, _Args...> type;
1284};
1285
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001286#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
1288template <template <class> class _Alloc, class _Tp, class _Up>
1289struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1290{
1291 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1292};
1293
1294template <template <class> class _Alloc, class _Tp, class _Up>
1295struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1296{
1297 typedef _Alloc<_Up> type;
1298};
1299
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1301struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1302{
1303 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1304};
1305
1306template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1307struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1308{
1309 typedef _Alloc<_Up, _A0> type;
1310};
1311
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1313 class _A1, class _Up>
1314struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1315{
1316 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1317};
1318
1319template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1320 class _A1, class _Up>
1321struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1322{
1323 typedef _Alloc<_Up, _A0, _A1> type;
1324};
1325
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1327 class _A1, class _A2, class _Up>
1328struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1329{
1330 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1331};
1332
1333template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1334 class _A1, class _A2, class _Up>
1335struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1336{
1337 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1338};
1339
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001340#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001342#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343
1344template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1345auto
1346__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001347 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1350auto
1351__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1352 -> false_type;
1353
1354template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1355struct __has_allocate_hint
1356 : integral_constant<bool,
1357 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001358 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 declval<_SizeType>(),
1360 declval<_ConstVoidPtr>())),
1361 true_type>::value>
1362{
1363};
1364
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001365#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366
1367template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1368struct __has_allocate_hint
1369 : true_type
1370{
1371};
1372
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001373#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001375#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376
1377template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001378decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1379 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 true_type())
1381__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1382
1383template <class _Alloc, class _Pointer, class ..._Args>
1384false_type
1385__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1386
1387template <class _Alloc, class _Pointer, class ..._Args>
1388struct __has_construct
1389 : integral_constant<bool,
1390 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001391 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 declval<_Pointer>(),
1393 declval<_Args>()...)),
1394 true_type>::value>
1395{
1396};
1397
1398template <class _Alloc, class _Pointer>
1399auto
1400__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1401 -> decltype(__a.destroy(__p), true_type());
1402
1403template <class _Alloc, class _Pointer>
1404auto
1405__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1406 -> false_type;
1407
1408template <class _Alloc, class _Pointer>
1409struct __has_destroy
1410 : integral_constant<bool,
1411 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001412 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413 declval<_Pointer>())),
1414 true_type>::value>
1415{
1416};
1417
1418template <class _Alloc>
1419auto
1420__has_max_size_test(_Alloc&& __a)
1421 -> decltype(__a.max_size(), true_type());
1422
1423template <class _Alloc>
1424auto
1425__has_max_size_test(const volatile _Alloc& __a)
1426 -> false_type;
1427
1428template <class _Alloc>
1429struct __has_max_size
1430 : integral_constant<bool,
1431 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001432 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 true_type>::value>
1434{
1435};
1436
1437template <class _Alloc>
1438auto
1439__has_select_on_container_copy_construction_test(_Alloc&& __a)
1440 -> decltype(__a.select_on_container_copy_construction(), true_type());
1441
1442template <class _Alloc>
1443auto
1444__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1445 -> false_type;
1446
1447template <class _Alloc>
1448struct __has_select_on_container_copy_construction
1449 : integral_constant<bool,
1450 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001451 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 true_type>::value>
1453{
1454};
1455
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001456#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458#ifndef _LIBCPP_HAS_NO_VARIADICS
1459
1460template <class _Alloc, class _Pointer, class ..._Args>
1461struct __has_construct
1462 : false_type
1463{
1464};
1465
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001466#else // _LIBCPP_HAS_NO_VARIADICS
1467
1468template <class _Alloc, class _Pointer, class _Args>
1469struct __has_construct
1470 : false_type
1471{
1472};
1473
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001474#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475
1476template <class _Alloc, class _Pointer>
1477struct __has_destroy
1478 : false_type
1479{
1480};
1481
1482template <class _Alloc>
1483struct __has_max_size
1484 : true_type
1485{
1486};
1487
1488template <class _Alloc>
1489struct __has_select_on_container_copy_construction
1490 : false_type
1491{
1492};
1493
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001494#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001496template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1497struct __alloc_traits_difference_type
1498{
1499 typedef typename pointer_traits<_Ptr>::difference_type type;
1500};
1501
1502template <class _Alloc, class _Ptr>
1503struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1504{
1505 typedef typename _Alloc::difference_type type;
1506};
1507
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001509struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510{
1511 typedef _Alloc allocator_type;
1512 typedef typename allocator_type::value_type value_type;
1513
1514 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1515 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1516 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1517 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1518
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001519 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1520 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521
1522 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1523 propagate_on_container_copy_assignment;
1524 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1525 propagate_on_container_move_assignment;
1526 typedef typename __propagate_on_container_swap<allocator_type>::type
1527 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001528 typedef typename __is_always_equal<allocator_type>::type
1529 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001531#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001533 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001535#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 template <class _Tp> struct rebind_alloc
1537 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1538 template <class _Tp> struct rebind_traits
1539 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001540#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
Marshall Clow0e58cae2017-11-26 02:55:38 +00001542 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n)
1544 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001545 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001547 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1549
Howard Hinnant756c69b2010-09-22 16:48:34 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001551 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 {__a.deallocate(__p, __n);}
1553
1554#ifndef _LIBCPP_HAS_NO_VARIADICS
1555 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001558 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001559 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001560#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001562 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001563 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 {
1565 ::new ((void*)__p) _Tp();
1566 }
1567 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001568 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001569 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 {
1571 ::new ((void*)__p) _Tp(__a0);
1572 }
1573 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001574 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001575 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 const _A1& __a1)
1577 {
1578 ::new ((void*)__p) _Tp(__a0, __a1);
1579 }
1580 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001581 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001582 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 const _A1& __a1, const _A2& __a2)
1584 {
1585 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1586 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001587#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588
1589 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 static void destroy(allocator_type& __a, _Tp* __p)
1592 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1593
Howard Hinnant756c69b2010-09-22 16:48:34 +00001594 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001595 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1597
Howard Hinnant756c69b2010-09-22 16:48:34 +00001598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 static allocator_type
1600 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001601 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 __has_select_on_container_copy_construction<const allocator_type>(),
1603 __a);}
1604
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001605 template <class _Ptr>
1606 _LIBCPP_INLINE_VISIBILITY
1607 static
1608 void
1609 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1610 {
1611 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1612 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1613 }
1614
1615 template <class _Tp>
1616 _LIBCPP_INLINE_VISIBILITY
1617 static
1618 typename enable_if
1619 <
1620 (is_same<allocator_type, allocator<_Tp> >::value
1621 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1622 is_trivially_move_constructible<_Tp>::value,
1623 void
1624 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001625 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001626 {
1627 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001628 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001629 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001630 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001631 __begin2 += _Np;
1632 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001633 }
1634
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001635 template <class _Iter, class _Ptr>
1636 _LIBCPP_INLINE_VISIBILITY
1637 static
1638 void
1639 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1640 {
1641 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1642 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1643 }
1644
1645 template <class _Tp>
1646 _LIBCPP_INLINE_VISIBILITY
1647 static
1648 typename enable_if
1649 <
1650 (is_same<allocator_type, allocator<_Tp> >::value
1651 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1652 is_trivially_move_constructible<_Tp>::value,
1653 void
1654 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001655 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001656 {
1657 typedef typename remove_const<_Tp>::type _Vp;
1658 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001659 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001660 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001661 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001662 __begin2 += _Np;
1663 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001664 }
1665
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001666 template <class _Ptr>
1667 _LIBCPP_INLINE_VISIBILITY
1668 static
1669 void
1670 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1671 {
1672 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001673 {
1674 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1675 --__end2;
1676 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 }
1678
1679 template <class _Tp>
1680 _LIBCPP_INLINE_VISIBILITY
1681 static
1682 typename enable_if
1683 <
1684 (is_same<allocator_type, allocator<_Tp> >::value
1685 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1686 is_trivially_move_constructible<_Tp>::value,
1687 void
1688 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001689 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001690 {
1691 ptrdiff_t _Np = __end1 - __begin1;
1692 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001693 if (_Np > 0)
1694 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001695 }
1696
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697private:
1698
Howard Hinnant756c69b2010-09-22 16:48:34 +00001699 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001700 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 const_void_pointer __hint, true_type)
1702 {return __a.allocate(__n, __hint);}
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 Hinnant28b24882011-12-01 20:21:04 +00001705 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 {return __a.allocate(__n);}
1707
1708#ifndef _LIBCPP_HAS_NO_VARIADICS
1709 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001712 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713 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(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1716 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001717 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001719#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720
1721 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1724 {__a.destroy(__p);}
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(false_type, allocator_type&, _Tp* __p)
1728 {
1729 __p->~_Tp();
1730 }
1731
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(true_type, const allocator_type& __a)
1734 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001737 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738
Howard Hinnant756c69b2010-09-22 16:48:34 +00001739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001741 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 {return __a.select_on_container_copy_construction();}
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(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 {return __a;}
1747};
1748
Marshall Clow940e01c2015-04-07 05:21:38 +00001749template <class _Traits, class _Tp>
1750struct __rebind_alloc_helper
1751{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001752#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001753 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001754#else
1755 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1756#endif
1757};
1758
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759// allocator
1760
1761template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001762class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763{
1764public:
1765 typedef size_t size_type;
1766 typedef ptrdiff_t difference_type;
1767 typedef _Tp* pointer;
1768 typedef const _Tp* const_pointer;
1769 typedef _Tp& reference;
1770 typedef const _Tp& const_reference;
1771 typedef _Tp value_type;
1772
Howard Hinnant4931e092011-06-02 21:38:57 +00001773 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001774 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001775
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1777
Howard Hinnant719bda32011-05-28 14:41:13 +00001778 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1779 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1780 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001781 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001782 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001783 {return _VSTD::addressof(__x);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001784 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1785 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 {
1787 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001788 __throw_length_error("allocator<T>::allocate(size_t n)"
1789 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001790 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1791 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001792 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001793 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001794 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1795 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001796#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 template <class _Up, class... _Args>
1798 _LIBCPP_INLINE_VISIBILITY
1799 void
1800 construct(_Up* __p, _Args&&... __args)
1801 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001802 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001804#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 _LIBCPP_INLINE_VISIBILITY
1806 void
1807 construct(pointer __p)
1808 {
1809 ::new((void*)__p) _Tp();
1810 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001811# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001812
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 template <class _A0>
1814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001815 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 construct(pointer __p, _A0& __a0)
1817 {
1818 ::new((void*)__p) _Tp(__a0);
1819 }
1820 template <class _A0>
1821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001822 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 construct(pointer __p, const _A0& __a0)
1824 {
1825 ::new((void*)__p) _Tp(__a0);
1826 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001827# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 template <class _A0, class _A1>
1829 _LIBCPP_INLINE_VISIBILITY
1830 void
1831 construct(pointer __p, _A0& __a0, _A1& __a1)
1832 {
1833 ::new((void*)__p) _Tp(__a0, __a1);
1834 }
1835 template <class _A0, class _A1>
1836 _LIBCPP_INLINE_VISIBILITY
1837 void
1838 construct(pointer __p, const _A0& __a0, _A1& __a1)
1839 {
1840 ::new((void*)__p) _Tp(__a0, __a1);
1841 }
1842 template <class _A0, class _A1>
1843 _LIBCPP_INLINE_VISIBILITY
1844 void
1845 construct(pointer __p, _A0& __a0, const _A1& __a1)
1846 {
1847 ::new((void*)__p) _Tp(__a0, __a1);
1848 }
1849 template <class _A0, class _A1>
1850 _LIBCPP_INLINE_VISIBILITY
1851 void
1852 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1853 {
1854 ::new((void*)__p) _Tp(__a0, __a1);
1855 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001856#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1858};
1859
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001860template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001861class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001862{
1863public:
1864 typedef size_t size_type;
1865 typedef ptrdiff_t difference_type;
1866 typedef const _Tp* pointer;
1867 typedef const _Tp* const_pointer;
1868 typedef const _Tp& reference;
1869 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001870 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871
1872 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001873 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001874
1875 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1876
1877 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1878 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1879 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1880 {return _VSTD::addressof(__x);}
1881 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 {
1883 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001884 __throw_length_error("allocator<const T>::allocate(size_t n)"
1885 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001886 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001887 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001888 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00001889 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001890 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1891 {return size_type(~0) / sizeof(_Tp);}
1892#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1893 template <class _Up, class... _Args>
1894 _LIBCPP_INLINE_VISIBILITY
1895 void
1896 construct(_Up* __p, _Args&&... __args)
1897 {
1898 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1899 }
1900#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1901 _LIBCPP_INLINE_VISIBILITY
1902 void
1903 construct(pointer __p)
1904 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001905 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001906 }
1907# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001908
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001909 template <class _A0>
1910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001911 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001912 construct(pointer __p, _A0& __a0)
1913 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001914 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 }
1916 template <class _A0>
1917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001918 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919 construct(pointer __p, const _A0& __a0)
1920 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001921 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001922 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001923# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1924 template <class _A0, class _A1>
1925 _LIBCPP_INLINE_VISIBILITY
1926 void
1927 construct(pointer __p, _A0& __a0, _A1& __a1)
1928 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001929 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001930 }
1931 template <class _A0, class _A1>
1932 _LIBCPP_INLINE_VISIBILITY
1933 void
1934 construct(pointer __p, const _A0& __a0, _A1& __a1)
1935 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001936 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001937 }
1938 template <class _A0, class _A1>
1939 _LIBCPP_INLINE_VISIBILITY
1940 void
1941 construct(pointer __p, _A0& __a0, const _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, const _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#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1953 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1954};
1955
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _Tp, class _Up>
1961inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001962bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963
1964template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001965class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966 : public iterator<output_iterator_tag,
1967 _Tp, // purposefully not C++03
1968 ptrdiff_t, // purposefully not C++03
1969 _Tp*, // purposefully not C++03
1970 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1971{
1972private:
1973 _OutputIterator __x_;
1974public:
1975 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1977 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1978 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001979#if _LIBCPP_STD_VER >= 14
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1981 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1982#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1984 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1985 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001986#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001987 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001988#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989};
1990
1991template <class _Tp>
1992pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001993get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994{
1995 pair<_Tp*, ptrdiff_t> __r(0, 0);
1996 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1997 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1998 / sizeof(_Tp);
1999 if (__n > __m)
2000 __n = __m;
2001 while (__n > 0)
2002 {
2003 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2004 if (__r.first)
2005 {
2006 __r.second = __n;
2007 break;
2008 }
2009 __n /= 2;
2010 }
2011 return __r;
2012}
2013
2014template <class _Tp>
2015inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002016void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017
Marshall Clowb22274f2017-01-24 22:22:33 +00002018#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019template <class _Tp>
2020struct auto_ptr_ref
2021{
2022 _Tp* __ptr_;
2023};
2024
2025template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002026class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027{
2028private:
2029 _Tp* __ptr_;
2030public:
2031 typedef _Tp element_type;
2032
2033 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2034 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2036 : __ptr_(__p.release()) {}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2038 {reset(__p.release()); return *this;}
2039 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2040 {reset(__p.release()); return *this;}
2041 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2042 {reset(__p.__ptr_); return *this;}
2043 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2044
2045 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2046 {return *__ptr_;}
2047 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2048 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2049 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2050 {
2051 _Tp* __t = __ptr_;
2052 __ptr_ = 0;
2053 return __t;
2054 }
2055 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2056 {
2057 if (__ptr_ != __p)
2058 delete __ptr_;
2059 __ptr_ = __p;
2060 }
2061
2062 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2063 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2064 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2065 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2066 {return auto_ptr<_Up>(release());}
2067};
2068
2069template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002070class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071{
2072public:
2073 typedef void element_type;
2074};
Marshall Clowb22274f2017-01-24 22:22:33 +00002075#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076
Eric Fiselier9d355982017-04-12 23:45:53 +00002077template <class _Tp, int _Idx,
2078 bool _CanBeEmptyBase =
2079 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2080struct __compressed_pair_elem {
2081 typedef _Tp _ParamT;
2082 typedef _Tp& reference;
2083 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084
Eric Fiselier9d355982017-04-12 23:45:53 +00002085#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002086 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087
Eric Fiselier9d355982017-04-12 23:45:53 +00002088 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002089 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2090 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002091 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002092 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002093 __compressed_pair_elem(_Up&& __u)
2094 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095
Eric Fiselier9d355982017-04-12 23:45:53 +00002096 template <class... _Args, size_t... _Indexes>
2097 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2098 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2099 __tuple_indices<_Indexes...>)
2100 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2101#else
Alex Lorenz76132112017-11-09 17:54:49 +00002102 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2103 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002104 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2105#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
Alex Lorenz76132112017-11-09 17:54:49 +00002107 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2108 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002109 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002112 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113};
2114
Eric Fiselier9d355982017-04-12 23:45:53 +00002115template <class _Tp, int _Idx>
2116struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2117 typedef _Tp _ParamT;
2118 typedef _Tp& reference;
2119 typedef const _Tp& const_reference;
2120 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121
Eric Fiselier9d355982017-04-12 23:45:53 +00002122#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002123 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124
Eric Fiselier9d355982017-04-12 23:45:53 +00002125 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002126 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2127 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002128 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002129 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002130 __compressed_pair_elem(_Up&& __u)
2131 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132
Eric Fiselier9d355982017-04-12 23:45:53 +00002133 template <class... _Args, size_t... _Indexes>
2134 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2135 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2136 __tuple_indices<_Indexes...>)
2137 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2138#else
Alex Lorenz76132112017-11-09 17:54:49 +00002139 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2140 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002141 __compressed_pair_elem(_ParamT __p)
2142 : __value_type(std::forward<_ParamT>(__p)) {}
2143#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144
Alex Lorenz76132112017-11-09 17:54:49 +00002145 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2146 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002147 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148};
2149
Eric Fiselier9d355982017-04-12 23:45:53 +00002150// Tag used to construct the second element of the compressed pair.
2151struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152
2153template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002154class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2155 private __compressed_pair_elem<_T2, 1> {
2156 typedef __compressed_pair_elem<_T1, 0> _Base1;
2157 typedef __compressed_pair_elem<_T2, 1> _Base2;
2158
2159 // NOTE: This static assert should never fire because __compressed_pair
2160 // is *almost never* used in a scenario where it's possible for T1 == T2.
2161 // (The exception is std::function where it is possible that the function
2162 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002163 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002164 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2165 "The current implementation is NOT ABI-compatible with the previous "
2166 "implementation for this configuration");
2167
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002169#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002170 template <bool _Dummy = true,
2171 class = typename enable_if<
2172 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2173 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2174 >::type
2175 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002176 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002177 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178
Eric Fiselier9d355982017-04-12 23:45:53 +00002179 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2180 __compressed_pair>::value,
2181 bool>::type = true>
2182 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2183 __compressed_pair(_Tp&& __t)
2184 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185
Eric Fiselier9d355982017-04-12 23:45:53 +00002186 template <class _Tp>
2187 _LIBCPP_INLINE_VISIBILITY constexpr
2188 __compressed_pair(__second_tag, _Tp&& __t)
2189 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190
Eric Fiselier9d355982017-04-12 23:45:53 +00002191 template <class _U1, class _U2>
2192 _LIBCPP_INLINE_VISIBILITY constexpr
2193 __compressed_pair(_U1&& __t1, _U2&& __t2)
2194 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195
Eric Fiselier9d355982017-04-12 23:45:53 +00002196 template <class... _Args1, class... _Args2>
2197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2198 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2199 tuple<_Args2...> __second_args)
2200 : _Base1(__pc, _VSTD::move(__first_args),
2201 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2202 _Base2(__pc, _VSTD::move(__second_args),
2203 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002204
Eric Fiselier9d355982017-04-12 23:45:53 +00002205#else
2206 _LIBCPP_INLINE_VISIBILITY
2207 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002208
Eric Fiselier9d355982017-04-12 23:45:53 +00002209 _LIBCPP_INLINE_VISIBILITY explicit
2210 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002211
Eric Fiselier9d355982017-04-12 23:45:53 +00002212 _LIBCPP_INLINE_VISIBILITY
2213 __compressed_pair(__second_tag, _T2 __t2)
2214 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215
Eric Fiselier9d355982017-04-12 23:45:53 +00002216 _LIBCPP_INLINE_VISIBILITY
2217 __compressed_pair(_T1 __t1, _T2 __t2)
2218 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2219#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
Eric Fiselier9d355982017-04-12 23:45:53 +00002221 _LIBCPP_INLINE_VISIBILITY
2222 typename _Base1::reference first() _NOEXCEPT {
2223 return static_cast<_Base1&>(*this).__get();
2224 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225
Eric Fiselier9d355982017-04-12 23:45:53 +00002226 _LIBCPP_INLINE_VISIBILITY
2227 typename _Base1::const_reference first() const _NOEXCEPT {
2228 return static_cast<_Base1 const&>(*this).__get();
2229 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230
Eric Fiselier9d355982017-04-12 23:45:53 +00002231 _LIBCPP_INLINE_VISIBILITY
2232 typename _Base2::reference second() _NOEXCEPT {
2233 return static_cast<_Base2&>(*this).__get();
2234 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235
Eric Fiselier9d355982017-04-12 23:45:53 +00002236 _LIBCPP_INLINE_VISIBILITY
2237 typename _Base2::const_reference second() const _NOEXCEPT {
2238 return static_cast<_Base2 const&>(*this).__get();
2239 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240
Eric Fiselier9d355982017-04-12 23:45:53 +00002241 _LIBCPP_INLINE_VISIBILITY
2242 void swap(__compressed_pair& __x)
2243 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2244 __is_nothrow_swappable<_T2>::value)
2245 {
2246 using std::swap;
2247 swap(first(), __x.first());
2248 swap(second(), __x.second());
2249 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250};
2251
2252template <class _T1, class _T2>
2253inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002254void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2255 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2256 __is_nothrow_swappable<_T2>::value) {
2257 __x.swap(__y);
2258}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002260// default_delete
2261
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002263struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002264 static_assert(!is_function<_Tp>::value,
2265 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002266#ifndef _LIBCPP_CXX03_LANG
2267 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002268#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002269 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002270#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002271 template <class _Up>
2272 _LIBCPP_INLINE_VISIBILITY
2273 default_delete(const default_delete<_Up>&,
2274 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2275 0) _NOEXCEPT {}
2276
2277 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2278 static_assert(sizeof(_Tp) > 0,
2279 "default_delete can not delete incomplete type");
2280 static_assert(!is_void<_Tp>::value,
2281 "default_delete can not delete incomplete type");
2282 delete __ptr;
2283 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284};
2285
2286template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002287struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2288private:
2289 template <class _Up>
2290 struct _EnableIfConvertible
2291 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2292
Howard Hinnant4500ca52011-12-18 21:19:44 +00002293public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002294#ifndef _LIBCPP_CXX03_LANG
2295 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002296#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002297 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002298#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002299
2300 template <class _Up>
2301 _LIBCPP_INLINE_VISIBILITY
2302 default_delete(const default_delete<_Up[]>&,
2303 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2304
2305 template <class _Up>
2306 _LIBCPP_INLINE_VISIBILITY
2307 typename _EnableIfConvertible<_Up>::type
2308 operator()(_Up* __ptr) const _NOEXCEPT {
2309 static_assert(sizeof(_Tp) > 0,
2310 "default_delete can not delete incomplete type");
2311 static_assert(!is_void<_Tp>::value,
2312 "default_delete can not delete void type");
2313 delete[] __ptr;
2314 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315};
2316
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317
Eric Fiselier6779b062017-04-16 02:06:25 +00002318
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002319#ifndef _LIBCPP_CXX03_LANG
2320template <class _Deleter>
2321struct __unique_ptr_deleter_sfinae {
2322 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2323 typedef const _Deleter& __lval_ref_type;
2324 typedef _Deleter&& __good_rval_ref_type;
2325 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326};
2327
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002328template <class _Deleter>
2329struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2330 typedef const _Deleter& __lval_ref_type;
2331 typedef const _Deleter&& __bad_rval_ref_type;
2332 typedef false_type __enable_rval_overload;
2333};
2334
2335template <class _Deleter>
2336struct __unique_ptr_deleter_sfinae<_Deleter&> {
2337 typedef _Deleter& __lval_ref_type;
2338 typedef _Deleter&& __bad_rval_ref_type;
2339 typedef false_type __enable_rval_overload;
2340};
2341#endif // !defined(_LIBCPP_CXX03_LANG)
2342
2343template <class _Tp, class _Dp = default_delete<_Tp> >
2344class _LIBCPP_TEMPLATE_VIS unique_ptr {
2345public:
2346 typedef _Tp element_type;
2347 typedef _Dp deleter_type;
2348 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2349
2350 static_assert(!is_rvalue_reference<deleter_type>::value,
2351 "the specified deleter type cannot be an rvalue reference");
2352
2353private:
2354 __compressed_pair<pointer, deleter_type> __ptr_;
2355
2356 struct __nat { int __for_bool_; };
2357
2358#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002359 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002360
2361 template <bool _Dummy>
2362 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002363 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002364
2365 template <bool _Dummy>
2366 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002367 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002368
2369 template <bool _Dummy>
2370 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002371 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002372
2373 template <bool _Dummy, class _Deleter = typename __dependent_type<
2374 __identity<deleter_type>, _Dummy>::type>
2375 using _EnableIfDeleterDefaultConstructible =
2376 typename enable_if<is_default_constructible<_Deleter>::value &&
2377 !is_pointer<_Deleter>::value>::type;
2378
2379 template <class _ArgType>
2380 using _EnableIfDeleterConstructible =
2381 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2382
2383 template <class _UPtr, class _Up>
2384 using _EnableIfMoveConvertible = typename enable_if<
2385 is_convertible<typename _UPtr::pointer, pointer>::value &&
2386 !is_array<_Up>::value
2387 >::type;
2388
2389 template <class _UDel>
2390 using _EnableIfDeleterConvertible = typename enable_if<
2391 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2392 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2393 >::type;
2394
2395 template <class _UDel>
2396 using _EnableIfDeleterAssignable = typename enable_if<
2397 is_assignable<_Dp&, _UDel&&>::value
2398 >::type;
2399
2400public:
2401 template <bool _Dummy = true,
2402 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2403 _LIBCPP_INLINE_VISIBILITY
2404 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2405
2406 template <bool _Dummy = true,
2407 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2408 _LIBCPP_INLINE_VISIBILITY
2409 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2410
2411 template <bool _Dummy = true,
2412 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2413 _LIBCPP_INLINE_VISIBILITY
2414 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2415
2416 template <bool _Dummy = true,
2417 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2418 _LIBCPP_INLINE_VISIBILITY
2419 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2420 : __ptr_(__p, __d) {}
2421
2422 template <bool _Dummy = true,
2423 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2424 _LIBCPP_INLINE_VISIBILITY
2425 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2426 : __ptr_(__p, _VSTD::move(__d)) {
2427 static_assert(!is_reference<deleter_type>::value,
2428 "rvalue deleter bound to reference");
2429 }
2430
2431 template <bool _Dummy = true,
2432 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2433 _LIBCPP_INLINE_VISIBILITY
2434 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2435
2436 _LIBCPP_INLINE_VISIBILITY
2437 unique_ptr(unique_ptr&& __u) noexcept
2438 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2439 }
2440
2441 template <class _Up, class _Ep,
2442 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2443 class = _EnableIfDeleterConvertible<_Ep>
2444 >
2445 _LIBCPP_INLINE_VISIBILITY
2446 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2447 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2448
2449#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2450 template <class _Up>
2451 _LIBCPP_INLINE_VISIBILITY
2452 unique_ptr(auto_ptr<_Up>&& __p,
2453 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2454 is_same<_Dp, default_delete<_Tp>>::value,
2455 __nat>::type = __nat()) _NOEXCEPT
2456 : __ptr_(__p.release()) {}
2457#endif
2458
2459 _LIBCPP_INLINE_VISIBILITY
2460 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2461 reset(__u.release());
2462 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2463 return *this;
2464 }
2465
2466 template <class _Up, class _Ep,
2467 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2468 class = _EnableIfDeleterAssignable<_Ep>
2469 >
2470 _LIBCPP_INLINE_VISIBILITY
2471 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2472 reset(__u.release());
2473 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2474 return *this;
2475 }
2476
2477#else // _LIBCPP_CXX03_LANG
2478private:
2479 unique_ptr(unique_ptr&);
2480 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2481
2482 unique_ptr& operator=(unique_ptr&);
2483 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2484
2485public:
2486 _LIBCPP_INLINE_VISIBILITY
2487 unique_ptr() : __ptr_(pointer())
2488 {
2489 static_assert(!is_pointer<deleter_type>::value,
2490 "unique_ptr constructed with null function pointer deleter");
2491 static_assert(is_default_constructible<deleter_type>::value,
2492 "unique_ptr::deleter_type is not default constructible");
2493 }
2494 _LIBCPP_INLINE_VISIBILITY
2495 unique_ptr(nullptr_t) : __ptr_(pointer())
2496 {
2497 static_assert(!is_pointer<deleter_type>::value,
2498 "unique_ptr constructed with null function pointer deleter");
2499 }
2500 _LIBCPP_INLINE_VISIBILITY
2501 explicit unique_ptr(pointer __p)
2502 : __ptr_(_VSTD::move(__p)) {
2503 static_assert(!is_pointer<deleter_type>::value,
2504 "unique_ptr constructed with null function pointer deleter");
2505 }
2506
2507 _LIBCPP_INLINE_VISIBILITY
2508 operator __rv<unique_ptr>() {
2509 return __rv<unique_ptr>(*this);
2510 }
2511
2512 _LIBCPP_INLINE_VISIBILITY
2513 unique_ptr(__rv<unique_ptr> __u)
2514 : __ptr_(__u->release(),
2515 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2516
2517 template <class _Up, class _Ep>
2518 _LIBCPP_INLINE_VISIBILITY
2519 typename enable_if<
2520 !is_array<_Up>::value &&
2521 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2522 pointer>::value &&
2523 is_assignable<deleter_type&, _Ep&>::value,
2524 unique_ptr&>::type
2525 operator=(unique_ptr<_Up, _Ep> __u) {
2526 reset(__u.release());
2527 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2528 return *this;
2529 }
2530
2531 _LIBCPP_INLINE_VISIBILITY
2532 unique_ptr(pointer __p, deleter_type __d)
2533 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2534#endif // _LIBCPP_CXX03_LANG
2535
2536#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2537 template <class _Up>
2538 _LIBCPP_INLINE_VISIBILITY
2539 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2540 is_same<_Dp, default_delete<_Tp> >::value,
2541 unique_ptr&>::type
2542 operator=(auto_ptr<_Up> __p) {
2543 reset(__p.release());
2544 return *this;
2545 }
2546#endif
2547
2548 _LIBCPP_INLINE_VISIBILITY
2549 ~unique_ptr() { reset(); }
2550
2551 _LIBCPP_INLINE_VISIBILITY
2552 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2553 reset();
2554 return *this;
2555 }
2556
2557 _LIBCPP_INLINE_VISIBILITY
2558 typename add_lvalue_reference<_Tp>::type
2559 operator*() const {
2560 return *__ptr_.first();
2561 }
2562 _LIBCPP_INLINE_VISIBILITY
2563 pointer operator->() const _NOEXCEPT {
2564 return __ptr_.first();
2565 }
2566 _LIBCPP_INLINE_VISIBILITY
2567 pointer get() const _NOEXCEPT {
2568 return __ptr_.first();
2569 }
2570 _LIBCPP_INLINE_VISIBILITY
2571 deleter_type& get_deleter() _NOEXCEPT {
2572 return __ptr_.second();
2573 }
2574 _LIBCPP_INLINE_VISIBILITY
2575 const deleter_type& get_deleter() const _NOEXCEPT {
2576 return __ptr_.second();
2577 }
2578 _LIBCPP_INLINE_VISIBILITY
2579 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2580 return __ptr_.first() != nullptr;
2581 }
2582
2583 _LIBCPP_INLINE_VISIBILITY
2584 pointer release() _NOEXCEPT {
2585 pointer __t = __ptr_.first();
2586 __ptr_.first() = pointer();
2587 return __t;
2588 }
2589
2590 _LIBCPP_INLINE_VISIBILITY
2591 void reset(pointer __p = pointer()) _NOEXCEPT {
2592 pointer __tmp = __ptr_.first();
2593 __ptr_.first() = __p;
2594 if (__tmp)
2595 __ptr_.second()(__tmp);
2596 }
2597
2598 _LIBCPP_INLINE_VISIBILITY
2599 void swap(unique_ptr& __u) _NOEXCEPT {
2600 __ptr_.swap(__u.__ptr_);
2601 }
2602};
2603
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002604
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002606class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002608 typedef _Tp element_type;
2609 typedef _Dp deleter_type;
2610 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2611
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002613 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614
Eric Fiselier31127cd2017-04-16 02:14:31 +00002615 template <class _From>
2616 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617
Eric Fiselier31127cd2017-04-16 02:14:31 +00002618 template <class _FromElem>
2619 struct _CheckArrayPointerConversion<_FromElem*>
2620 : integral_constant<bool,
2621 is_same<_FromElem*, pointer>::value ||
2622 (is_same<pointer, element_type*>::value &&
2623 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2624 >
2625 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002627#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002628 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629
2630 template <bool _Dummy>
2631 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002632 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002633
2634 template <bool _Dummy>
2635 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002636 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002637
2638 template <bool _Dummy>
2639 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002640 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002641
2642 template <bool _Dummy, class _Deleter = typename __dependent_type<
2643 __identity<deleter_type>, _Dummy>::type>
2644 using _EnableIfDeleterDefaultConstructible =
2645 typename enable_if<is_default_constructible<_Deleter>::value &&
2646 !is_pointer<_Deleter>::value>::type;
2647
2648 template <class _ArgType>
2649 using _EnableIfDeleterConstructible =
2650 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2651
2652 template <class _Pp>
2653 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002654 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002655 >::type;
2656
2657 template <class _UPtr, class _Up,
2658 class _ElemT = typename _UPtr::element_type>
2659 using _EnableIfMoveConvertible = typename enable_if<
2660 is_array<_Up>::value &&
2661 is_same<pointer, element_type*>::value &&
2662 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2663 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2664 >::type;
2665
2666 template <class _UDel>
2667 using _EnableIfDeleterConvertible = typename enable_if<
2668 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2669 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2670 >::type;
2671
2672 template <class _UDel>
2673 using _EnableIfDeleterAssignable = typename enable_if<
2674 is_assignable<_Dp&, _UDel&&>::value
2675 >::type;
2676
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002678 template <bool _Dummy = true,
2679 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2680 _LIBCPP_INLINE_VISIBILITY
2681 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002683 template <bool _Dummy = true,
2684 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2685 _LIBCPP_INLINE_VISIBILITY
2686 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688 template <class _Pp, bool _Dummy = true,
2689 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2690 class = _EnableIfPointerConvertible<_Pp>>
2691 _LIBCPP_INLINE_VISIBILITY
2692 explicit unique_ptr(_Pp __p) noexcept
2693 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002695 template <class _Pp, bool _Dummy = true,
2696 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2697 class = _EnableIfPointerConvertible<_Pp>>
2698 _LIBCPP_INLINE_VISIBILITY
2699 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2700 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002702 template <bool _Dummy = true,
2703 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2704 _LIBCPP_INLINE_VISIBILITY
2705 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2706 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002708 template <class _Pp, bool _Dummy = true,
2709 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2710 class = _EnableIfPointerConvertible<_Pp>>
2711 _LIBCPP_INLINE_VISIBILITY
2712 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2713 : __ptr_(__p, _VSTD::move(__d)) {
2714 static_assert(!is_reference<deleter_type>::value,
2715 "rvalue deleter bound to reference");
2716 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002718 template <bool _Dummy = true,
2719 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2720 _LIBCPP_INLINE_VISIBILITY
2721 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2722 : __ptr_(nullptr, _VSTD::move(__d)) {
2723 static_assert(!is_reference<deleter_type>::value,
2724 "rvalue deleter bound to reference");
2725 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002726
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002727 template <class _Pp, bool _Dummy = true,
2728 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2729 class = _EnableIfPointerConvertible<_Pp>>
2730 _LIBCPP_INLINE_VISIBILITY
2731 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002732
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002733 _LIBCPP_INLINE_VISIBILITY
2734 unique_ptr(unique_ptr&& __u) noexcept
2735 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2736 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002737
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002738 _LIBCPP_INLINE_VISIBILITY
2739 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2740 reset(__u.release());
2741 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2742 return *this;
2743 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002745 template <class _Up, class _Ep,
2746 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2747 class = _EnableIfDeleterConvertible<_Ep>
2748 >
2749 _LIBCPP_INLINE_VISIBILITY
2750 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002751 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002752 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 template <class _Up, class _Ep,
2755 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2756 class = _EnableIfDeleterAssignable<_Ep>
2757 >
2758 _LIBCPP_INLINE_VISIBILITY
2759 unique_ptr&
2760 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2761 reset(__u.release());
2762 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2763 return *this;
2764 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002766#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002768 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002769
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002770 unique_ptr(unique_ptr&);
2771 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2772
2773 unique_ptr& operator=(unique_ptr&);
2774 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2775
2776 template <class _Up>
2777 unique_ptr(_Up __u,
2778 typename conditional<
2779 is_reference<deleter_type>::value, deleter_type,
2780 typename add_lvalue_reference<const deleter_type>::type>::type,
2781 typename enable_if<is_convertible<_Up, pointer>::value,
2782 __nat>::type = __nat());
2783public:
2784 _LIBCPP_INLINE_VISIBILITY
2785 unique_ptr() : __ptr_(pointer()) {
2786 static_assert(!is_pointer<deleter_type>::value,
2787 "unique_ptr constructed with null function pointer deleter");
2788 }
2789 _LIBCPP_INLINE_VISIBILITY
2790 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2791 static_assert(!is_pointer<deleter_type>::value,
2792 "unique_ptr constructed with null function pointer deleter");
2793 }
2794
2795 _LIBCPP_INLINE_VISIBILITY
2796 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2797 static_assert(!is_pointer<deleter_type>::value,
2798 "unique_ptr constructed with null function pointer deleter");
2799 }
2800
2801 _LIBCPP_INLINE_VISIBILITY
2802 unique_ptr(pointer __p, deleter_type __d)
2803 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2804
2805 _LIBCPP_INLINE_VISIBILITY
2806 unique_ptr(nullptr_t, deleter_type __d)
2807 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2808
2809 _LIBCPP_INLINE_VISIBILITY
2810 operator __rv<unique_ptr>() {
2811 return __rv<unique_ptr>(*this);
2812 }
2813
2814 _LIBCPP_INLINE_VISIBILITY
2815 unique_ptr(__rv<unique_ptr> __u)
2816 : __ptr_(__u->release(),
2817 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2818
2819 _LIBCPP_INLINE_VISIBILITY
2820 unique_ptr& operator=(__rv<unique_ptr> __u) {
2821 reset(__u->release());
2822 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2823 return *this;
2824 }
2825
2826#endif // _LIBCPP_CXX03_LANG
2827
2828public:
2829 _LIBCPP_INLINE_VISIBILITY
2830 ~unique_ptr() { reset(); }
2831
2832 _LIBCPP_INLINE_VISIBILITY
2833 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2834 reset();
2835 return *this;
2836 }
2837
2838 _LIBCPP_INLINE_VISIBILITY
2839 typename add_lvalue_reference<_Tp>::type
2840 operator[](size_t __i) const {
2841 return __ptr_.first()[__i];
2842 }
2843 _LIBCPP_INLINE_VISIBILITY
2844 pointer get() const _NOEXCEPT {
2845 return __ptr_.first();
2846 }
2847
2848 _LIBCPP_INLINE_VISIBILITY
2849 deleter_type& get_deleter() _NOEXCEPT {
2850 return __ptr_.second();
2851 }
2852
2853 _LIBCPP_INLINE_VISIBILITY
2854 const deleter_type& get_deleter() const _NOEXCEPT {
2855 return __ptr_.second();
2856 }
2857 _LIBCPP_INLINE_VISIBILITY
2858 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2859 return __ptr_.first() != nullptr;
2860 }
2861
2862 _LIBCPP_INLINE_VISIBILITY
2863 pointer release() _NOEXCEPT {
2864 pointer __t = __ptr_.first();
2865 __ptr_.first() = pointer();
2866 return __t;
2867 }
2868
2869 template <class _Pp>
2870 _LIBCPP_INLINE_VISIBILITY
2871 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002872 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002873 >::type
2874 reset(_Pp __p) _NOEXCEPT {
2875 pointer __tmp = __ptr_.first();
2876 __ptr_.first() = __p;
2877 if (__tmp)
2878 __ptr_.second()(__tmp);
2879 }
2880
2881 _LIBCPP_INLINE_VISIBILITY
2882 void reset(nullptr_t = nullptr) _NOEXCEPT {
2883 pointer __tmp = __ptr_.first();
2884 __ptr_.first() = nullptr;
2885 if (__tmp)
2886 __ptr_.second()(__tmp);
2887 }
2888
2889 _LIBCPP_INLINE_VISIBILITY
2890 void swap(unique_ptr& __u) _NOEXCEPT {
2891 __ptr_.swap(__u.__ptr_);
2892 }
2893
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894};
2895
2896template <class _Tp, class _Dp>
2897inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002898typename enable_if<
2899 __is_swappable<_Dp>::value,
2900 void
2901>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002902swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903
2904template <class _T1, class _D1, class _T2, class _D2>
2905inline _LIBCPP_INLINE_VISIBILITY
2906bool
2907operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2908
2909template <class _T1, class _D1, class _T2, class _D2>
2910inline _LIBCPP_INLINE_VISIBILITY
2911bool
2912operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2913
2914template <class _T1, class _D1, class _T2, class _D2>
2915inline _LIBCPP_INLINE_VISIBILITY
2916bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002917operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2918{
2919 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2920 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002921 typedef typename common_type<_P1, _P2>::type _Vp;
2922 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002923}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924
2925template <class _T1, class _D1, class _T2, class _D2>
2926inline _LIBCPP_INLINE_VISIBILITY
2927bool
2928operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2929
2930template <class _T1, class _D1, class _T2, class _D2>
2931inline _LIBCPP_INLINE_VISIBILITY
2932bool
2933operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2934
2935template <class _T1, class _D1, class _T2, class _D2>
2936inline _LIBCPP_INLINE_VISIBILITY
2937bool
2938operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2939
Howard Hinnantb17caf92012-02-21 21:02:58 +00002940template <class _T1, class _D1>
2941inline _LIBCPP_INLINE_VISIBILITY
2942bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002943operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002944{
2945 return !__x;
2946}
2947
2948template <class _T1, class _D1>
2949inline _LIBCPP_INLINE_VISIBILITY
2950bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002951operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002952{
2953 return !__x;
2954}
2955
2956template <class _T1, class _D1>
2957inline _LIBCPP_INLINE_VISIBILITY
2958bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002959operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002960{
2961 return static_cast<bool>(__x);
2962}
2963
2964template <class _T1, class _D1>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002967operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002968{
2969 return static_cast<bool>(__x);
2970}
2971
2972template <class _T1, class _D1>
2973inline _LIBCPP_INLINE_VISIBILITY
2974bool
2975operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2976{
2977 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2978 return less<_P1>()(__x.get(), nullptr);
2979}
2980
2981template <class _T1, class _D1>
2982inline _LIBCPP_INLINE_VISIBILITY
2983bool
2984operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2985{
2986 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2987 return less<_P1>()(nullptr, __x.get());
2988}
2989
2990template <class _T1, class _D1>
2991inline _LIBCPP_INLINE_VISIBILITY
2992bool
2993operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2994{
2995 return nullptr < __x;
2996}
2997
2998template <class _T1, class _D1>
2999inline _LIBCPP_INLINE_VISIBILITY
3000bool
3001operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3002{
3003 return __x < nullptr;
3004}
3005
3006template <class _T1, class _D1>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
3009operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3010{
3011 return !(nullptr < __x);
3012}
3013
3014template <class _T1, class _D1>
3015inline _LIBCPP_INLINE_VISIBILITY
3016bool
3017operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3018{
3019 return !(__x < nullptr);
3020}
3021
3022template <class _T1, class _D1>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
3025operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3026{
3027 return !(__x < nullptr);
3028}
3029
3030template <class _T1, class _D1>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
3033operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3034{
3035 return !(nullptr < __x);
3036}
3037
Howard Hinnant9e028f12012-05-01 15:37:54 +00003038#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3039
3040template <class _Tp, class _Dp>
3041inline _LIBCPP_INLINE_VISIBILITY
3042unique_ptr<_Tp, _Dp>
3043move(unique_ptr<_Tp, _Dp>& __t)
3044{
3045 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3046}
3047
3048#endif
3049
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003050#if _LIBCPP_STD_VER > 11
3051
3052template<class _Tp>
3053struct __unique_if
3054{
3055 typedef unique_ptr<_Tp> __unique_single;
3056};
3057
3058template<class _Tp>
3059struct __unique_if<_Tp[]>
3060{
3061 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3062};
3063
3064template<class _Tp, size_t _Np>
3065struct __unique_if<_Tp[_Np]>
3066{
3067 typedef void __unique_array_known_bound;
3068};
3069
3070template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003071inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003072typename __unique_if<_Tp>::__unique_single
3073make_unique(_Args&&... __args)
3074{
3075 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3076}
3077
3078template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003079inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003080typename __unique_if<_Tp>::__unique_array_unknown_bound
3081make_unique(size_t __n)
3082{
3083 typedef typename remove_extent<_Tp>::type _Up;
3084 return unique_ptr<_Tp>(new _Up[__n]());
3085}
3086
3087template<class _Tp, class... _Args>
3088 typename __unique_if<_Tp>::__unique_array_known_bound
3089 make_unique(_Args&&...) = delete;
3090
3091#endif // _LIBCPP_STD_VER > 11
3092
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003093template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003094#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003095struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003096#else
3097struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3098 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3099#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003100{
3101 typedef unique_ptr<_Tp, _Dp> argument_type;
3102 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003103 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003104 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003105 {
3106 typedef typename argument_type::pointer pointer;
3107 return hash<pointer>()(__ptr.get());
3108 }
3109};
3110
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111struct __destruct_n
3112{
3113private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003114 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115
3116 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003117 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003118 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119
3120 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003121 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122 {}
3123
Howard Hinnant719bda32011-05-28 14:41:13 +00003124 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003125 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003126 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127 {}
3128
Howard Hinnant719bda32011-05-28 14:41:13 +00003129 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003130 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003131 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132 {}
3133public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003135 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136
3137 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003138 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003139 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140
3141 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003142 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003143 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144
3145 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003146 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003147 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148};
3149
3150template <class _Alloc>
3151class __allocator_destructor
3152{
3153 typedef allocator_traits<_Alloc> __alloc_traits;
3154public:
3155 typedef typename __alloc_traits::pointer pointer;
3156 typedef typename __alloc_traits::size_type size_type;
3157private:
3158 _Alloc& __alloc_;
3159 size_type __s_;
3160public:
3161 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003162 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003165 void operator()(pointer __p) _NOEXCEPT
3166 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167};
3168
3169template <class _InputIterator, class _ForwardIterator>
3170_ForwardIterator
3171uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3172{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003174#ifndef _LIBCPP_NO_EXCEPTIONS
3175 _ForwardIterator __s = __r;
3176 try
3177 {
3178#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003179 for (; __f != __l; ++__f, (void) ++__r)
3180 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003181#ifndef _LIBCPP_NO_EXCEPTIONS
3182 }
3183 catch (...)
3184 {
3185 for (; __s != __r; ++__s)
3186 __s->~value_type();
3187 throw;
3188 }
3189#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190 return __r;
3191}
3192
3193template <class _InputIterator, class _Size, class _ForwardIterator>
3194_ForwardIterator
3195uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3196{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003198#ifndef _LIBCPP_NO_EXCEPTIONS
3199 _ForwardIterator __s = __r;
3200 try
3201 {
3202#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003203 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3204 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003205#ifndef _LIBCPP_NO_EXCEPTIONS
3206 }
3207 catch (...)
3208 {
3209 for (; __s != __r; ++__s)
3210 __s->~value_type();
3211 throw;
3212 }
3213#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214 return __r;
3215}
3216
3217template <class _ForwardIterator, class _Tp>
3218void
3219uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3220{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003222#ifndef _LIBCPP_NO_EXCEPTIONS
3223 _ForwardIterator __s = __f;
3224 try
3225 {
3226#endif
3227 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003228 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003229#ifndef _LIBCPP_NO_EXCEPTIONS
3230 }
3231 catch (...)
3232 {
3233 for (; __s != __f; ++__s)
3234 __s->~value_type();
3235 throw;
3236 }
3237#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238}
3239
3240template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003241_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003242uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3243{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003244 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003245#ifndef _LIBCPP_NO_EXCEPTIONS
3246 _ForwardIterator __s = __f;
3247 try
3248 {
3249#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003250 for (; __n > 0; ++__f, (void) --__n)
3251 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003252#ifndef _LIBCPP_NO_EXCEPTIONS
3253 }
3254 catch (...)
3255 {
3256 for (; __s != __f; ++__s)
3257 __s->~value_type();
3258 throw;
3259 }
3260#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003261 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262}
3263
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003264#if _LIBCPP_STD_VER > 14
3265
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003266template <class _Tp>
3267inline _LIBCPP_INLINE_VISIBILITY
3268void destroy_at(_Tp* __loc) {
3269 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3270 __loc->~_Tp();
3271}
3272
3273template <class _ForwardIterator>
3274inline _LIBCPP_INLINE_VISIBILITY
3275void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3276 for (; __first != __last; ++__first)
3277 _VSTD::destroy_at(_VSTD::addressof(*__first));
3278}
3279
3280template <class _ForwardIterator, class _Size>
3281inline _LIBCPP_INLINE_VISIBILITY
3282_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3283 for (; __n > 0; (void)++__first, --__n)
3284 _VSTD::destroy_at(_VSTD::addressof(*__first));
3285 return __first;
3286}
3287
Eric Fiselier290c07c2016-10-11 21:13:44 +00003288template <class _ForwardIterator>
3289inline _LIBCPP_INLINE_VISIBILITY
3290void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3291 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3292 auto __idx = __first;
3293#ifndef _LIBCPP_NO_EXCEPTIONS
3294 try {
3295#endif
3296 for (; __idx != __last; ++__idx)
3297 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3298#ifndef _LIBCPP_NO_EXCEPTIONS
3299 } catch (...) {
3300 _VSTD::destroy(__first, __idx);
3301 throw;
3302 }
3303#endif
3304}
3305
3306template <class _ForwardIterator, class _Size>
3307inline _LIBCPP_INLINE_VISIBILITY
3308_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3309 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3310 auto __idx = __first;
3311#ifndef _LIBCPP_NO_EXCEPTIONS
3312 try {
3313#endif
3314 for (; __n > 0; (void)++__idx, --__n)
3315 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3316 return __idx;
3317#ifndef _LIBCPP_NO_EXCEPTIONS
3318 } catch (...) {
3319 _VSTD::destroy(__first, __idx);
3320 throw;
3321 }
3322#endif
3323}
3324
3325
3326template <class _ForwardIterator>
3327inline _LIBCPP_INLINE_VISIBILITY
3328void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3329 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3330 auto __idx = __first;
3331#ifndef _LIBCPP_NO_EXCEPTIONS
3332 try {
3333#endif
3334 for (; __idx != __last; ++__idx)
3335 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3336#ifndef _LIBCPP_NO_EXCEPTIONS
3337 } catch (...) {
3338 _VSTD::destroy(__first, __idx);
3339 throw;
3340 }
3341#endif
3342}
3343
3344template <class _ForwardIterator, class _Size>
3345inline _LIBCPP_INLINE_VISIBILITY
3346_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3347 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3348 auto __idx = __first;
3349#ifndef _LIBCPP_NO_EXCEPTIONS
3350 try {
3351#endif
3352 for (; __n > 0; (void)++__idx, --__n)
3353 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3354 return __idx;
3355#ifndef _LIBCPP_NO_EXCEPTIONS
3356 } catch (...) {
3357 _VSTD::destroy(__first, __idx);
3358 throw;
3359 }
3360#endif
3361}
3362
3363
3364template <class _InputIt, class _ForwardIt>
3365inline _LIBCPP_INLINE_VISIBILITY
3366_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3367 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3368 auto __idx = __first_res;
3369#ifndef _LIBCPP_NO_EXCEPTIONS
3370 try {
3371#endif
3372 for (; __first != __last; (void)++__idx, ++__first)
3373 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3374 return __idx;
3375#ifndef _LIBCPP_NO_EXCEPTIONS
3376 } catch (...) {
3377 _VSTD::destroy(__first_res, __idx);
3378 throw;
3379 }
3380#endif
3381}
3382
3383template <class _InputIt, class _Size, class _ForwardIt>
3384inline _LIBCPP_INLINE_VISIBILITY
3385pair<_InputIt, _ForwardIt>
3386uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3387 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3388 auto __idx = __first_res;
3389#ifndef _LIBCPP_NO_EXCEPTIONS
3390 try {
3391#endif
3392 for (; __n > 0; ++__idx, (void)++__first, --__n)
3393 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3394 return {__first, __idx};
3395#ifndef _LIBCPP_NO_EXCEPTIONS
3396 } catch (...) {
3397 _VSTD::destroy(__first_res, __idx);
3398 throw;
3399 }
3400#endif
3401}
3402
3403
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003404#endif // _LIBCPP_STD_VER > 14
3405
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003406// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3407// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003408// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003409#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3410 && defined(__ATOMIC_RELAXED) \
3411 && defined(__ATOMIC_ACQ_REL)
3412# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3413#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3414# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3415#endif
3416
3417template <class _Tp>
3418inline _LIBCPP_INLINE_VISIBILITY _Tp
3419__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3420{
3421#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3422 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3423#else
3424 return __t += 1;
3425#endif
3426}
3427
3428template <class _Tp>
3429inline _LIBCPP_INLINE_VISIBILITY _Tp
3430__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3431{
3432#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3433 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3434#else
3435 return __t -= 1;
3436#endif
3437}
3438
Howard Hinnant756c69b2010-09-22 16:48:34 +00003439class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 : public std::exception
3441{
3442public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003443 virtual ~bad_weak_ptr() _NOEXCEPT;
3444 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445};
3446
Marshall Clow8fea1612016-08-25 15:09:01 +00003447_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3448void __throw_bad_weak_ptr()
3449{
3450#ifndef _LIBCPP_NO_EXCEPTIONS
3451 throw bad_weak_ptr();
3452#else
3453 _VSTD::abort();
3454#endif
3455}
3456
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003457template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003459class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460{
3461 __shared_count(const __shared_count&);
3462 __shared_count& operator=(const __shared_count&);
3463
3464protected:
3465 long __shared_owners_;
3466 virtual ~__shared_count();
3467private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003468 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469
3470public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003472 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473 : __shared_owners_(__refs) {}
3474
Eric Fiselier98848572017-01-17 03:16:26 +00003475#if defined(_LIBCPP_BUILDING_MEMORY) && \
3476 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003477 void __add_shared() _NOEXCEPT;
3478 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003479#else
3480 _LIBCPP_INLINE_VISIBILITY
3481 void __add_shared() _NOEXCEPT {
3482 __libcpp_atomic_refcount_increment(__shared_owners_);
3483 }
3484 _LIBCPP_INLINE_VISIBILITY
3485 bool __release_shared() _NOEXCEPT {
3486 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3487 __on_zero_shared();
3488 return true;
3489 }
3490 return false;
3491 }
3492#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003493 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003494 long use_count() const _NOEXCEPT {
3495 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3496 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497};
3498
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003499class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500 : private __shared_count
3501{
3502 long __shared_weak_owners_;
3503
3504public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003506 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507 : __shared_count(__refs),
3508 __shared_weak_owners_(__refs) {}
3509protected:
3510 virtual ~__shared_weak_count();
3511
3512public:
Eric Fiselier98848572017-01-17 03:16:26 +00003513#if defined(_LIBCPP_BUILDING_MEMORY) && \
3514 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003515 void __add_shared() _NOEXCEPT;
3516 void __add_weak() _NOEXCEPT;
3517 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003518#else
3519 _LIBCPP_INLINE_VISIBILITY
3520 void __add_shared() _NOEXCEPT {
3521 __shared_count::__add_shared();
3522 }
3523 _LIBCPP_INLINE_VISIBILITY
3524 void __add_weak() _NOEXCEPT {
3525 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3526 }
3527 _LIBCPP_INLINE_VISIBILITY
3528 void __release_shared() _NOEXCEPT {
3529 if (__shared_count::__release_shared())
3530 __release_weak();
3531 }
3532#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003533 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003535 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3536 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537
Howard Hinnant807d6332013-02-25 15:50:36 +00003538 // Define the function out only if we build static libc++ without RTTI.
3539 // Otherwise we may break clients who need to compile their projects with
3540 // -fno-rtti and yet link against a libc++.dylib compiled
3541 // without -fno-rtti.
3542#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003543 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003544#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003546 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547};
3548
3549template <class _Tp, class _Dp, class _Alloc>
3550class __shared_ptr_pointer
3551 : public __shared_weak_count
3552{
3553 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3554public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003557 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558
Howard Hinnant72f73582010-08-11 17:04:31 +00003559#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003560 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003561#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562
3563private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003564 virtual void __on_zero_shared() _NOEXCEPT;
3565 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566};
3567
Howard Hinnant72f73582010-08-11 17:04:31 +00003568#ifndef _LIBCPP_NO_RTTI
3569
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570template <class _Tp, class _Dp, class _Alloc>
3571const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003572__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003574 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575}
3576
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003577#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003578
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579template <class _Tp, class _Dp, class _Alloc>
3580void
Howard Hinnant719bda32011-05-28 14:41:13 +00003581__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582{
3583 __data_.first().second()(__data_.first().first());
3584 __data_.first().second().~_Dp();
3585}
3586
3587template <class _Tp, class _Dp, class _Alloc>
3588void
Howard Hinnant719bda32011-05-28 14:41:13 +00003589__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003591 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3592 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003593 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3594
Eric Fiselierf8898c82015-02-05 23:01:40 +00003595 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003596 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003597 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598}
3599
3600template <class _Tp, class _Alloc>
3601class __shared_ptr_emplace
3602 : public __shared_weak_count
3603{
3604 __compressed_pair<_Alloc, _Tp> __data_;
3605public:
3606#ifndef _LIBCPP_HAS_NO_VARIADICS
3607
Howard Hinnant756c69b2010-09-22 16:48:34 +00003608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003610 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611
3612 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003615 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3616 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617
3618#else // _LIBCPP_HAS_NO_VARIADICS
3619
Howard Hinnant756c69b2010-09-22 16:48:34 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621 __shared_ptr_emplace(_Alloc __a)
3622 : __data_(__a) {}
3623
3624 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3627 : __data_(__a, _Tp(__a0)) {}
3628
3629 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3632 : __data_(__a, _Tp(__a0, __a1)) {}
3633
3634 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3637 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3638
3639#endif // _LIBCPP_HAS_NO_VARIADICS
3640
3641private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003642 virtual void __on_zero_shared() _NOEXCEPT;
3643 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003646 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647};
3648
3649template <class _Tp, class _Alloc>
3650void
Howard Hinnant719bda32011-05-28 14:41:13 +00003651__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652{
3653 __data_.second().~_Tp();
3654}
3655
3656template <class _Tp, class _Alloc>
3657void
Howard Hinnant719bda32011-05-28 14:41:13 +00003658__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003660 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3661 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003662 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003663 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003665 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666}
3667
Erik Pilkington2a398762017-05-25 15:43:31 +00003668struct __shared_ptr_dummy_rebind_allocator_type;
3669template <>
3670class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3671{
3672public:
3673 template <class _Other>
3674 struct rebind
3675 {
3676 typedef allocator<_Other> other;
3677 };
3678};
3679
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003680template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681
3682template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003683class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003685public:
3686 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003687
Eric Fiselierae5b6672016-06-27 01:02:43 +00003688#if _LIBCPP_STD_VER > 14
3689 typedef weak_ptr<_Tp> weak_type;
3690#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691private:
3692 element_type* __ptr_;
3693 __shared_weak_count* __cntrl_;
3694
3695 struct __nat {int __for_bool_;};
3696public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003698 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003700 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003701 template<class _Yp>
3702 explicit shared_ptr(_Yp* __p,
3703 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3704 template<class _Yp, class _Dp>
3705 shared_ptr(_Yp* __p, _Dp __d,
3706 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3707 template<class _Yp, class _Dp, class _Alloc>
3708 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3709 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003710 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3711 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003712 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003714 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003718 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003719 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003720#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003722 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003723 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003724 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003725 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003726#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003728 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003729#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003730#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003731 template<class _Yp>
3732 shared_ptr(auto_ptr<_Yp>&& __r,
3733 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003734#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003735 template<class _Yp>
3736 shared_ptr(auto_ptr<_Yp> __r,
3737 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003739#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003740#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003741 template <class _Yp, class _Dp>
3742 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3743 typename enable_if
3744 <
3745 !is_lvalue_reference<_Dp>::value &&
3746 !is_array<_Yp>::value &&
3747 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3748 __nat
3749 >::type = __nat());
3750 template <class _Yp, class _Dp>
3751 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3752 typename enable_if
3753 <
3754 is_lvalue_reference<_Dp>::value &&
3755 !is_array<_Yp>::value &&
3756 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3757 __nat
3758 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003759#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003760 template <class _Yp, class _Dp>
3761 shared_ptr(unique_ptr<_Yp, _Dp>,
3762 typename enable_if
3763 <
3764 !is_lvalue_reference<_Dp>::value &&
3765 !is_array<_Yp>::value &&
3766 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3767 __nat
3768 >::type = __nat());
3769 template <class _Yp, class _Dp>
3770 shared_ptr(unique_ptr<_Yp, _Dp>,
3771 typename enable_if
3772 <
3773 is_lvalue_reference<_Dp>::value &&
3774 !is_array<_Yp>::value &&
3775 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3776 __nat
3777 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003778#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779
3780 ~shared_ptr();
3781
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003783 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003784 template<class _Yp>
3785 typename enable_if
3786 <
3787 is_convertible<_Yp*, element_type*>::value,
3788 shared_ptr&
3789 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003791 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003792#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003794 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003795 template<class _Yp>
3796 typename enable_if
3797 <
3798 is_convertible<_Yp*, element_type*>::value,
3799 shared_ptr<_Tp>&
3800 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003802 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003803#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003804 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003806 typename enable_if
3807 <
3808 !is_array<_Yp>::value &&
3809 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003810 shared_ptr
3811 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003812 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003813#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003814#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003815#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003816 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003818 typename enable_if
3819 <
3820 !is_array<_Yp>::value &&
3821 is_convertible<_Yp*, element_type*>::value,
3822 shared_ptr&
3823 >::type
3824 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003826#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003827 template <class _Yp, class _Dp>
3828 typename enable_if
3829 <
3830 !is_array<_Yp>::value &&
3831 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3832 shared_ptr&
3833 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003834#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003836 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003837#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003839 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840#endif
3841
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003843 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003845 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003846 template<class _Yp>
3847 typename enable_if
3848 <
3849 is_convertible<_Yp*, element_type*>::value,
3850 void
3851 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003853 reset(_Yp* __p);
3854 template<class _Yp, class _Dp>
3855 typename enable_if
3856 <
3857 is_convertible<_Yp*, element_type*>::value,
3858 void
3859 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003861 reset(_Yp* __p, _Dp __d);
3862 template<class _Yp, class _Dp, class _Alloc>
3863 typename enable_if
3864 <
3865 is_convertible<_Yp*, element_type*>::value,
3866 void
3867 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003869 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870
Howard Hinnant756c69b2010-09-22 16:48:34 +00003871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003872 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003874 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3875 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003877 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003879 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003881 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003883 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003884 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003886 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003888 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003889 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003890 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003891 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003892 _LIBCPP_INLINE_VISIBILITY
3893 bool
3894 __owner_equivalent(const shared_ptr& __p) const
3895 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896
Howard Hinnant72f73582010-08-11 17:04:31 +00003897#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003900 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003901 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003902 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003903 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003904#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905
3906#ifndef _LIBCPP_HAS_NO_VARIADICS
3907
3908 template<class ..._Args>
3909 static
3910 shared_ptr<_Tp>
3911 make_shared(_Args&& ...__args);
3912
3913 template<class _Alloc, class ..._Args>
3914 static
3915 shared_ptr<_Tp>
3916 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3917
3918#else // _LIBCPP_HAS_NO_VARIADICS
3919
3920 static shared_ptr<_Tp> make_shared();
3921
3922 template<class _A0>
3923 static shared_ptr<_Tp> make_shared(_A0&);
3924
3925 template<class _A0, class _A1>
3926 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3927
3928 template<class _A0, class _A1, class _A2>
3929 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3930
3931 template<class _Alloc>
3932 static shared_ptr<_Tp>
3933 allocate_shared(const _Alloc& __a);
3934
3935 template<class _Alloc, class _A0>
3936 static shared_ptr<_Tp>
3937 allocate_shared(const _Alloc& __a, _A0& __a0);
3938
3939 template<class _Alloc, class _A0, class _A1>
3940 static shared_ptr<_Tp>
3941 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3942
3943 template<class _Alloc, class _A0, class _A1, class _A2>
3944 static shared_ptr<_Tp>
3945 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3946
3947#endif // _LIBCPP_HAS_NO_VARIADICS
3948
3949private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003950 template <class _Yp, bool = is_function<_Yp>::value>
3951 struct __shared_ptr_default_allocator
3952 {
3953 typedef allocator<_Yp> type;
3954 };
3955
3956 template <class _Yp>
3957 struct __shared_ptr_default_allocator<_Yp, true>
3958 {
3959 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3960 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003962 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003963 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003964 typename enable_if<is_convertible<_OrigPtr*,
3965 const enable_shared_from_this<_Yp>*
3966 >::value,
3967 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003968 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3969 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003970 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003971 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003972 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003973 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003974 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3975 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003976 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977 }
3978
Erik Pilkington2a398762017-05-25 15:43:31 +00003979 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003981 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3982 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983};
3984
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003985
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003987inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003988_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003989shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990 : __ptr_(0),
3991 __cntrl_(0)
3992{
3993}
3994
3995template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003996inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003997_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003998shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999 : __ptr_(0),
4000 __cntrl_(0)
4001{
4002}
4003
4004template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004005template<class _Yp>
4006shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4007 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008 : __ptr_(__p)
4009{
4010 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004011 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4012 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4013 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004015 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016}
4017
4018template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004019template<class _Yp, class _Dp>
4020shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4021 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022 : __ptr_(__p)
4023{
4024#ifndef _LIBCPP_NO_EXCEPTIONS
4025 try
4026 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004027#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004028 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4029 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4030 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004031 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032#ifndef _LIBCPP_NO_EXCEPTIONS
4033 }
4034 catch (...)
4035 {
4036 __d(__p);
4037 throw;
4038 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004039#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040}
4041
4042template<class _Tp>
4043template<class _Dp>
4044shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4045 : __ptr_(0)
4046{
4047#ifndef _LIBCPP_NO_EXCEPTIONS
4048 try
4049 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004050#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004051 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4052 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4053 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054#ifndef _LIBCPP_NO_EXCEPTIONS
4055 }
4056 catch (...)
4057 {
4058 __d(__p);
4059 throw;
4060 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004061#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062}
4063
4064template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004065template<class _Yp, class _Dp, class _Alloc>
4066shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4067 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068 : __ptr_(__p)
4069{
4070#ifndef _LIBCPP_NO_EXCEPTIONS
4071 try
4072 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004073#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004075 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076 typedef __allocator_destructor<_A2> _D2;
4077 _A2 __a2(__a);
4078 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004079 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4080 _CntrlBlk(__p, __d, __a);
4081 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004082 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083#ifndef _LIBCPP_NO_EXCEPTIONS
4084 }
4085 catch (...)
4086 {
4087 __d(__p);
4088 throw;
4089 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004090#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091}
4092
4093template<class _Tp>
4094template<class _Dp, class _Alloc>
4095shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4096 : __ptr_(0)
4097{
4098#ifndef _LIBCPP_NO_EXCEPTIONS
4099 try
4100 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004101#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004103 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104 typedef __allocator_destructor<_A2> _D2;
4105 _A2 __a2(__a);
4106 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004107 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4108 _CntrlBlk(__p, __d, __a);
4109 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110#ifndef _LIBCPP_NO_EXCEPTIONS
4111 }
4112 catch (...)
4113 {
4114 __d(__p);
4115 throw;
4116 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004117#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118}
4119
4120template<class _Tp>
4121template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004122inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004123shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124 : __ptr_(__p),
4125 __cntrl_(__r.__cntrl_)
4126{
4127 if (__cntrl_)
4128 __cntrl_->__add_shared();
4129}
4130
4131template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004132inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004133shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134 : __ptr_(__r.__ptr_),
4135 __cntrl_(__r.__cntrl_)
4136{
4137 if (__cntrl_)
4138 __cntrl_->__add_shared();
4139}
4140
4141template<class _Tp>
4142template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004143inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004145 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004146 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147 : __ptr_(__r.__ptr_),
4148 __cntrl_(__r.__cntrl_)
4149{
4150 if (__cntrl_)
4151 __cntrl_->__add_shared();
4152}
4153
Howard Hinnant74279a52010-09-04 23:28:19 +00004154#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155
4156template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004157inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004158shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159 : __ptr_(__r.__ptr_),
4160 __cntrl_(__r.__cntrl_)
4161{
4162 __r.__ptr_ = 0;
4163 __r.__cntrl_ = 0;
4164}
4165
4166template<class _Tp>
4167template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004168inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004170 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004171 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172 : __ptr_(__r.__ptr_),
4173 __cntrl_(__r.__cntrl_)
4174{
4175 __r.__ptr_ = 0;
4176 __r.__cntrl_ = 0;
4177}
4178
Howard Hinnant74279a52010-09-04 23:28:19 +00004179#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180
Marshall Clowb22274f2017-01-24 22:22:33 +00004181#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004183template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004184#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004185shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004187shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004189 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190 : __ptr_(__r.get())
4191{
4192 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4193 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004194 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195 __r.release();
4196}
Marshall Clowb22274f2017-01-24 22:22:33 +00004197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198
4199template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004200template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004201#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4203#else
4204shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4205#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004206 typename enable_if
4207 <
4208 !is_lvalue_reference<_Dp>::value &&
4209 !is_array<_Yp>::value &&
4210 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4211 __nat
4212 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213 : __ptr_(__r.get())
4214{
Marshall Clow35cde742015-05-10 13:59:45 +00004215#if _LIBCPP_STD_VER > 11
4216 if (__ptr_ == nullptr)
4217 __cntrl_ = nullptr;
4218 else
4219#endif
4220 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004221 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4222 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4223 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004224 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004225 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226 __r.release();
4227}
4228
4229template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004230template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004231#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4233#else
4234shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4235#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004236 typename enable_if
4237 <
4238 is_lvalue_reference<_Dp>::value &&
4239 !is_array<_Yp>::value &&
4240 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4241 __nat
4242 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243 : __ptr_(__r.get())
4244{
Marshall Clow35cde742015-05-10 13:59:45 +00004245#if _LIBCPP_STD_VER > 11
4246 if (__ptr_ == nullptr)
4247 __cntrl_ = nullptr;
4248 else
4249#endif
4250 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004251 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004252 typedef __shared_ptr_pointer<_Yp*,
4253 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004254 _AllocT > _CntrlBlk;
4255 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004256 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004257 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258 __r.release();
4259}
4260
4261#ifndef _LIBCPP_HAS_NO_VARIADICS
4262
4263template<class _Tp>
4264template<class ..._Args>
4265shared_ptr<_Tp>
4266shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4267{
4268 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4269 typedef allocator<_CntrlBlk> _A2;
4270 typedef __allocator_destructor<_A2> _D2;
4271 _A2 __a2;
4272 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004273 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274 shared_ptr<_Tp> __r;
4275 __r.__ptr_ = __hold2.get()->get();
4276 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004277 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 return __r;
4279}
4280
4281template<class _Tp>
4282template<class _Alloc, class ..._Args>
4283shared_ptr<_Tp>
4284shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4285{
4286 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004287 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288 typedef __allocator_destructor<_A2> _D2;
4289 _A2 __a2(__a);
4290 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004291 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4292 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293 shared_ptr<_Tp> __r;
4294 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004295 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004296 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297 return __r;
4298}
4299
4300#else // _LIBCPP_HAS_NO_VARIADICS
4301
4302template<class _Tp>
4303shared_ptr<_Tp>
4304shared_ptr<_Tp>::make_shared()
4305{
4306 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4307 typedef allocator<_CntrlBlk> _Alloc2;
4308 typedef __allocator_destructor<_Alloc2> _D2;
4309 _Alloc2 __alloc2;
4310 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4311 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4312 shared_ptr<_Tp> __r;
4313 __r.__ptr_ = __hold2.get()->get();
4314 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004315 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316 return __r;
4317}
4318
4319template<class _Tp>
4320template<class _A0>
4321shared_ptr<_Tp>
4322shared_ptr<_Tp>::make_shared(_A0& __a0)
4323{
4324 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4325 typedef allocator<_CntrlBlk> _Alloc2;
4326 typedef __allocator_destructor<_Alloc2> _D2;
4327 _Alloc2 __alloc2;
4328 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4329 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4330 shared_ptr<_Tp> __r;
4331 __r.__ptr_ = __hold2.get()->get();
4332 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004333 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334 return __r;
4335}
4336
4337template<class _Tp>
4338template<class _A0, class _A1>
4339shared_ptr<_Tp>
4340shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4341{
4342 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4343 typedef allocator<_CntrlBlk> _Alloc2;
4344 typedef __allocator_destructor<_Alloc2> _D2;
4345 _Alloc2 __alloc2;
4346 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4347 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4348 shared_ptr<_Tp> __r;
4349 __r.__ptr_ = __hold2.get()->get();
4350 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004351 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352 return __r;
4353}
4354
4355template<class _Tp>
4356template<class _A0, class _A1, class _A2>
4357shared_ptr<_Tp>
4358shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4359{
4360 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4361 typedef allocator<_CntrlBlk> _Alloc2;
4362 typedef __allocator_destructor<_Alloc2> _D2;
4363 _Alloc2 __alloc2;
4364 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4365 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4366 shared_ptr<_Tp> __r;
4367 __r.__ptr_ = __hold2.get()->get();
4368 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004369 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370 return __r;
4371}
4372
4373template<class _Tp>
4374template<class _Alloc>
4375shared_ptr<_Tp>
4376shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4377{
4378 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004379 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380 typedef __allocator_destructor<_Alloc2> _D2;
4381 _Alloc2 __alloc2(__a);
4382 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004383 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4384 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385 shared_ptr<_Tp> __r;
4386 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004387 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004388 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389 return __r;
4390}
4391
4392template<class _Tp>
4393template<class _Alloc, class _A0>
4394shared_ptr<_Tp>
4395shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4396{
4397 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004398 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004399 typedef __allocator_destructor<_Alloc2> _D2;
4400 _Alloc2 __alloc2(__a);
4401 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004402 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4403 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004404 shared_ptr<_Tp> __r;
4405 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004406 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004407 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408 return __r;
4409}
4410
4411template<class _Tp>
4412template<class _Alloc, class _A0, class _A1>
4413shared_ptr<_Tp>
4414shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4415{
4416 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004417 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004418 typedef __allocator_destructor<_Alloc2> _D2;
4419 _Alloc2 __alloc2(__a);
4420 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004421 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4422 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423 shared_ptr<_Tp> __r;
4424 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004425 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004426 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427 return __r;
4428}
4429
4430template<class _Tp>
4431template<class _Alloc, class _A0, class _A1, class _A2>
4432shared_ptr<_Tp>
4433shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4434{
4435 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004436 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437 typedef __allocator_destructor<_Alloc2> _D2;
4438 _Alloc2 __alloc2(__a);
4439 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004440 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4441 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442 shared_ptr<_Tp> __r;
4443 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004444 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004445 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446 return __r;
4447}
4448
4449#endif // _LIBCPP_HAS_NO_VARIADICS
4450
4451template<class _Tp>
4452shared_ptr<_Tp>::~shared_ptr()
4453{
4454 if (__cntrl_)
4455 __cntrl_->__release_shared();
4456}
4457
4458template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004459inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004460shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004461shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462{
4463 shared_ptr(__r).swap(*this);
4464 return *this;
4465}
4466
4467template<class _Tp>
4468template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004469inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004470typename enable_if
4471<
Marshall Clow7e384b72017-01-10 16:59:33 +00004472 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004473 shared_ptr<_Tp>&
4474>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004475shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476{
4477 shared_ptr(__r).swap(*this);
4478 return *this;
4479}
4480
Howard Hinnant74279a52010-09-04 23:28:19 +00004481#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482
4483template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004484inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004486shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004488 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489 return *this;
4490}
4491
4492template<class _Tp>
4493template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004494inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004495typename enable_if
4496<
Marshall Clow7e384b72017-01-10 16:59:33 +00004497 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004498 shared_ptr<_Tp>&
4499>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4501{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004502 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503 return *this;
4504}
4505
Marshall Clowb22274f2017-01-24 22:22:33 +00004506#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507template<class _Tp>
4508template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004509inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004510typename enable_if
4511<
4512 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004513 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004514 shared_ptr<_Tp>
4515>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004516shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4517{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004518 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519 return *this;
4520}
Marshall Clowb22274f2017-01-24 22:22:33 +00004521#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004522
4523template<class _Tp>
4524template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004525inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004526typename enable_if
4527<
4528 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004529 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004530 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004531 shared_ptr<_Tp>&
4532>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4534{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004535 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536 return *this;
4537}
4538
Howard Hinnant74279a52010-09-04 23:28:19 +00004539#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540
Marshall Clowb22274f2017-01-24 22:22:33 +00004541#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004542template<class _Tp>
4543template<class _Yp>
4544inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004545typename enable_if
4546<
4547 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004548 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004549 shared_ptr<_Tp>&
4550>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004551shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004552{
4553 shared_ptr(__r).swap(*this);
4554 return *this;
4555}
Marshall Clowb22274f2017-01-24 22:22:33 +00004556#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557
4558template<class _Tp>
4559template <class _Yp, class _Dp>
4560inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004561typename enable_if
4562<
4563 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004564 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004565 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004566 shared_ptr<_Tp>&
4567>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4569{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004570 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571 return *this;
4572}
4573
Howard Hinnant74279a52010-09-04 23:28:19 +00004574#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004575
4576template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004577inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578void
Howard Hinnant719bda32011-05-28 14:41:13 +00004579shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004580{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004581 _VSTD::swap(__ptr_, __r.__ptr_);
4582 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004583}
4584
4585template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004586inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004587void
Howard Hinnant719bda32011-05-28 14:41:13 +00004588shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004589{
4590 shared_ptr().swap(*this);
4591}
4592
4593template<class _Tp>
4594template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004595inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004596typename enable_if
4597<
Marshall Clow7e384b72017-01-10 16:59:33 +00004598 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004599 void
4600>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004601shared_ptr<_Tp>::reset(_Yp* __p)
4602{
4603 shared_ptr(__p).swap(*this);
4604}
4605
4606template<class _Tp>
4607template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004608inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004609typename enable_if
4610<
Marshall Clow7e384b72017-01-10 16:59:33 +00004611 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004612 void
4613>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004614shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4615{
4616 shared_ptr(__p, __d).swap(*this);
4617}
4618
4619template<class _Tp>
4620template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004621inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004622typename enable_if
4623<
Marshall Clow7e384b72017-01-10 16:59:33 +00004624 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004625 void
4626>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004627shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4628{
4629 shared_ptr(__p, __d, __a).swap(*this);
4630}
4631
4632#ifndef _LIBCPP_HAS_NO_VARIADICS
4633
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004634template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004636typename enable_if
4637<
4638 !is_array<_Tp>::value,
4639 shared_ptr<_Tp>
4640>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641make_shared(_Args&& ...__args)
4642{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004643 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644}
4645
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004646template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004647inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004648typename enable_if
4649<
4650 !is_array<_Tp>::value,
4651 shared_ptr<_Tp>
4652>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653allocate_shared(const _Alloc& __a, _Args&& ...__args)
4654{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004655 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656}
4657
4658#else // _LIBCPP_HAS_NO_VARIADICS
4659
4660template<class _Tp>
4661inline _LIBCPP_INLINE_VISIBILITY
4662shared_ptr<_Tp>
4663make_shared()
4664{
4665 return shared_ptr<_Tp>::make_shared();
4666}
4667
4668template<class _Tp, class _A0>
4669inline _LIBCPP_INLINE_VISIBILITY
4670shared_ptr<_Tp>
4671make_shared(_A0& __a0)
4672{
4673 return shared_ptr<_Tp>::make_shared(__a0);
4674}
4675
4676template<class _Tp, class _A0, class _A1>
4677inline _LIBCPP_INLINE_VISIBILITY
4678shared_ptr<_Tp>
4679make_shared(_A0& __a0, _A1& __a1)
4680{
4681 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4682}
4683
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004684template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004685inline _LIBCPP_INLINE_VISIBILITY
4686shared_ptr<_Tp>
4687make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4688{
4689 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4690}
4691
4692template<class _Tp, class _Alloc>
4693inline _LIBCPP_INLINE_VISIBILITY
4694shared_ptr<_Tp>
4695allocate_shared(const _Alloc& __a)
4696{
4697 return shared_ptr<_Tp>::allocate_shared(__a);
4698}
4699
4700template<class _Tp, class _Alloc, class _A0>
4701inline _LIBCPP_INLINE_VISIBILITY
4702shared_ptr<_Tp>
4703allocate_shared(const _Alloc& __a, _A0& __a0)
4704{
4705 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4706}
4707
4708template<class _Tp, class _Alloc, class _A0, class _A1>
4709inline _LIBCPP_INLINE_VISIBILITY
4710shared_ptr<_Tp>
4711allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4712{
4713 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4714}
4715
4716template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4717inline _LIBCPP_INLINE_VISIBILITY
4718shared_ptr<_Tp>
4719allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4720{
4721 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4722}
4723
4724#endif // _LIBCPP_HAS_NO_VARIADICS
4725
4726template<class _Tp, class _Up>
4727inline _LIBCPP_INLINE_VISIBILITY
4728bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004729operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004730{
4731 return __x.get() == __y.get();
4732}
4733
4734template<class _Tp, class _Up>
4735inline _LIBCPP_INLINE_VISIBILITY
4736bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004737operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004738{
4739 return !(__x == __y);
4740}
4741
4742template<class _Tp, class _Up>
4743inline _LIBCPP_INLINE_VISIBILITY
4744bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004745operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004746{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004747 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4748 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004749}
4750
4751template<class _Tp, class _Up>
4752inline _LIBCPP_INLINE_VISIBILITY
4753bool
4754operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4755{
4756 return __y < __x;
4757}
4758
4759template<class _Tp, class _Up>
4760inline _LIBCPP_INLINE_VISIBILITY
4761bool
4762operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4763{
4764 return !(__y < __x);
4765}
4766
4767template<class _Tp, class _Up>
4768inline _LIBCPP_INLINE_VISIBILITY
4769bool
4770operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4771{
4772 return !(__x < __y);
4773}
4774
4775template<class _Tp>
4776inline _LIBCPP_INLINE_VISIBILITY
4777bool
4778operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4779{
4780 return !__x;
4781}
4782
4783template<class _Tp>
4784inline _LIBCPP_INLINE_VISIBILITY
4785bool
4786operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4787{
4788 return !__x;
4789}
4790
4791template<class _Tp>
4792inline _LIBCPP_INLINE_VISIBILITY
4793bool
4794operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4795{
4796 return static_cast<bool>(__x);
4797}
4798
4799template<class _Tp>
4800inline _LIBCPP_INLINE_VISIBILITY
4801bool
4802operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4803{
4804 return static_cast<bool>(__x);
4805}
4806
4807template<class _Tp>
4808inline _LIBCPP_INLINE_VISIBILITY
4809bool
4810operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4811{
4812 return less<_Tp*>()(__x.get(), nullptr);
4813}
4814
4815template<class _Tp>
4816inline _LIBCPP_INLINE_VISIBILITY
4817bool
4818operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4819{
4820 return less<_Tp*>()(nullptr, __x.get());
4821}
4822
4823template<class _Tp>
4824inline _LIBCPP_INLINE_VISIBILITY
4825bool
4826operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4827{
4828 return nullptr < __x;
4829}
4830
4831template<class _Tp>
4832inline _LIBCPP_INLINE_VISIBILITY
4833bool
4834operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4835{
4836 return __x < nullptr;
4837}
4838
4839template<class _Tp>
4840inline _LIBCPP_INLINE_VISIBILITY
4841bool
4842operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4843{
4844 return !(nullptr < __x);
4845}
4846
4847template<class _Tp>
4848inline _LIBCPP_INLINE_VISIBILITY
4849bool
4850operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4851{
4852 return !(__x < nullptr);
4853}
4854
4855template<class _Tp>
4856inline _LIBCPP_INLINE_VISIBILITY
4857bool
4858operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4859{
4860 return !(__x < nullptr);
4861}
4862
4863template<class _Tp>
4864inline _LIBCPP_INLINE_VISIBILITY
4865bool
4866operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4867{
4868 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869}
4870
4871template<class _Tp>
4872inline _LIBCPP_INLINE_VISIBILITY
4873void
Howard Hinnant719bda32011-05-28 14:41:13 +00004874swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004875{
4876 __x.swap(__y);
4877}
4878
4879template<class _Tp, class _Up>
4880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004881typename enable_if
4882<
4883 !is_array<_Tp>::value && !is_array<_Up>::value,
4884 shared_ptr<_Tp>
4885>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004886static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004887{
4888 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4889}
4890
4891template<class _Tp, class _Up>
4892inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004893typename enable_if
4894<
4895 !is_array<_Tp>::value && !is_array<_Up>::value,
4896 shared_ptr<_Tp>
4897>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004898dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004899{
4900 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4901 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4902}
4903
4904template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004905typename enable_if
4906<
4907 is_array<_Tp>::value == is_array<_Up>::value,
4908 shared_ptr<_Tp>
4909>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004910const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004912 typedef typename remove_extent<_Tp>::type _RTp;
4913 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004914}
4915
Howard Hinnant72f73582010-08-11 17:04:31 +00004916#ifndef _LIBCPP_NO_RTTI
4917
Howard Hinnantc51e1022010-05-11 19:42:16 +00004918template<class _Dp, class _Tp>
4919inline _LIBCPP_INLINE_VISIBILITY
4920_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004921get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922{
4923 return __p.template __get_deleter<_Dp>();
4924}
4925
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004926#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004927
Howard Hinnantc51e1022010-05-11 19:42:16 +00004928template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004929class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004930{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004931public:
4932 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004933private:
4934 element_type* __ptr_;
4935 __shared_weak_count* __cntrl_;
4936
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004937public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004939 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004940 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004941 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4942 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004944 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004945 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004946 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4947 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004948
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004949#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004951 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004952 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004953 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4954 _NOEXCEPT;
4955#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004956 ~weak_ptr();
4957
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004959 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004960 template<class _Yp>
4961 typename enable_if
4962 <
4963 is_convertible<_Yp*, element_type*>::value,
4964 weak_ptr&
4965 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004967 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4968
4969#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4970
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004972 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4973 template<class _Yp>
4974 typename enable_if
4975 <
4976 is_convertible<_Yp*, element_type*>::value,
4977 weak_ptr&
4978 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004980 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4981
4982#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4983
4984 template<class _Yp>
4985 typename enable_if
4986 <
4987 is_convertible<_Yp*, element_type*>::value,
4988 weak_ptr&
4989 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004991 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004992
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004994 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004996 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004997
Howard Hinnant756c69b2010-09-22 16:48:34 +00004998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004999 long use_count() const _NOEXCEPT
5000 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005002 bool expired() const _NOEXCEPT
5003 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5004 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005005 template<class _Up>
5006 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005007 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005008 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005009 template<class _Up>
5010 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005011 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 {return __cntrl_ < __r.__cntrl_;}
5013
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005014 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5015 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005016};
5017
5018template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005019inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005020_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005021weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005022 : __ptr_(0),
5023 __cntrl_(0)
5024{
5025}
5026
5027template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005028inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005029weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005030 : __ptr_(__r.__ptr_),
5031 __cntrl_(__r.__cntrl_)
5032{
5033 if (__cntrl_)
5034 __cntrl_->__add_weak();
5035}
5036
5037template<class _Tp>
5038template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005039inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005040weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005041 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005042 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005043 : __ptr_(__r.__ptr_),
5044 __cntrl_(__r.__cntrl_)
5045{
5046 if (__cntrl_)
5047 __cntrl_->__add_weak();
5048}
5049
5050template<class _Tp>
5051template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005052inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005053weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005054 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005055 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005056 : __ptr_(__r.__ptr_),
5057 __cntrl_(__r.__cntrl_)
5058{
5059 if (__cntrl_)
5060 __cntrl_->__add_weak();
5061}
5062
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005063#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5064
5065template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005066inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005067weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5068 : __ptr_(__r.__ptr_),
5069 __cntrl_(__r.__cntrl_)
5070{
5071 __r.__ptr_ = 0;
5072 __r.__cntrl_ = 0;
5073}
5074
5075template<class _Tp>
5076template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005077inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005078weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5079 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5080 _NOEXCEPT
5081 : __ptr_(__r.__ptr_),
5082 __cntrl_(__r.__cntrl_)
5083{
5084 __r.__ptr_ = 0;
5085 __r.__cntrl_ = 0;
5086}
5087
5088#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5089
Howard Hinnantc51e1022010-05-11 19:42:16 +00005090template<class _Tp>
5091weak_ptr<_Tp>::~weak_ptr()
5092{
5093 if (__cntrl_)
5094 __cntrl_->__release_weak();
5095}
5096
5097template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005098inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005099weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005100weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005101{
5102 weak_ptr(__r).swap(*this);
5103 return *this;
5104}
5105
5106template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005107template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005108inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005109typename enable_if
5110<
5111 is_convertible<_Yp*, _Tp*>::value,
5112 weak_ptr<_Tp>&
5113>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005114weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005115{
5116 weak_ptr(__r).swap(*this);
5117 return *this;
5118}
5119
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5121
5122template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005123inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005124weak_ptr<_Tp>&
5125weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5126{
5127 weak_ptr(_VSTD::move(__r)).swap(*this);
5128 return *this;
5129}
5130
Howard Hinnantc51e1022010-05-11 19:42:16 +00005131template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005132template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005133inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005134typename enable_if
5135<
5136 is_convertible<_Yp*, _Tp*>::value,
5137 weak_ptr<_Tp>&
5138>::type
5139weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5140{
5141 weak_ptr(_VSTD::move(__r)).swap(*this);
5142 return *this;
5143}
5144
5145#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5146
5147template<class _Tp>
5148template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005149inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005150typename enable_if
5151<
5152 is_convertible<_Yp*, _Tp*>::value,
5153 weak_ptr<_Tp>&
5154>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005155weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005156{
5157 weak_ptr(__r).swap(*this);
5158 return *this;
5159}
5160
5161template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005162inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005163void
Howard Hinnant719bda32011-05-28 14:41:13 +00005164weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005165{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005166 _VSTD::swap(__ptr_, __r.__ptr_);
5167 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005168}
5169
5170template<class _Tp>
5171inline _LIBCPP_INLINE_VISIBILITY
5172void
Howard Hinnant719bda32011-05-28 14:41:13 +00005173swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005174{
5175 __x.swap(__y);
5176}
5177
5178template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005179inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005180void
Howard Hinnant719bda32011-05-28 14:41:13 +00005181weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005182{
5183 weak_ptr().swap(*this);
5184}
5185
5186template<class _Tp>
5187template<class _Yp>
5188shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005189 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005190 : __ptr_(__r.__ptr_),
5191 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5192{
5193 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005194 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005195}
5196
5197template<class _Tp>
5198shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005199weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005200{
5201 shared_ptr<_Tp> __r;
5202 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5203 if (__r.__cntrl_)
5204 __r.__ptr_ = __ptr_;
5205 return __r;
5206}
5207
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005208#if _LIBCPP_STD_VER > 14
5209template <class _Tp = void> struct owner_less;
5210#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005211template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005212#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005213
5214template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005215struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005216 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005217{
5218 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005219 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005220 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005221 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005222 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005223 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005224 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005225 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005226 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005228};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005229
5230template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005231struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005232 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5233{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005234 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005235 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005236 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005237 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005238 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005239 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005240 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005241 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005242 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005243 {return __x.owner_before(__y);}
5244};
5245
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005246#if _LIBCPP_STD_VER > 14
5247template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005248struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005249{
5250 template <class _Tp, class _Up>
5251 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005252 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005253 {return __x.owner_before(__y);}
5254 template <class _Tp, class _Up>
5255 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005256 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005257 {return __x.owner_before(__y);}
5258 template <class _Tp, class _Up>
5259 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005260 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005261 {return __x.owner_before(__y);}
5262 template <class _Tp, class _Up>
5263 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005264 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005265 {return __x.owner_before(__y);}
5266 typedef void is_transparent;
5267};
5268#endif
5269
Howard Hinnantc51e1022010-05-11 19:42:16 +00005270template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005271class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005272{
5273 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005274protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005276 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005278 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005280 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5281 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005283 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005284public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005286 shared_ptr<_Tp> shared_from_this()
5287 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005289 shared_ptr<_Tp const> shared_from_this() const
5290 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005291
Eric Fiselier84006862016-06-02 00:15:35 +00005292#if _LIBCPP_STD_VER > 14
5293 _LIBCPP_INLINE_VISIBILITY
5294 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5295 { return __weak_this_; }
5296
5297 _LIBCPP_INLINE_VISIBILITY
5298 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5299 { return __weak_this_; }
5300#endif // _LIBCPP_STD_VER > 14
5301
Howard Hinnantc51e1022010-05-11 19:42:16 +00005302 template <class _Up> friend class shared_ptr;
5303};
5304
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005305template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005306struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005307{
5308 typedef shared_ptr<_Tp> argument_type;
5309 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005310
Howard Hinnant756c69b2010-09-22 16:48:34 +00005311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005312 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005313 {
5314 return hash<_Tp*>()(__ptr.get());
5315 }
5316};
5317
Howard Hinnantc834c512011-11-29 18:15:50 +00005318template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005319inline _LIBCPP_INLINE_VISIBILITY
5320basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005321operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005322
Eric Fiselier9b492672016-06-18 02:12:53 +00005323
5324#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005325
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005326class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005327{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005328 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005329public:
5330 void lock() _NOEXCEPT;
5331 void unlock() _NOEXCEPT;
5332
5333private:
5334 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5335 __sp_mut(const __sp_mut&);
5336 __sp_mut& operator=(const __sp_mut&);
5337
Howard Hinnant8331b762013-03-06 23:30:19 +00005338 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005339};
5340
Mehdi Amini228053d2017-05-04 17:08:54 +00005341_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5342__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005343
5344template <class _Tp>
5345inline _LIBCPP_INLINE_VISIBILITY
5346bool
5347atomic_is_lock_free(const shared_ptr<_Tp>*)
5348{
5349 return false;
5350}
5351
5352template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005353_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005354shared_ptr<_Tp>
5355atomic_load(const shared_ptr<_Tp>* __p)
5356{
5357 __sp_mut& __m = __get_sp_mut(__p);
5358 __m.lock();
5359 shared_ptr<_Tp> __q = *__p;
5360 __m.unlock();
5361 return __q;
5362}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005363
Howard Hinnant9fa30202012-07-30 01:40:57 +00005364template <class _Tp>
5365inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005366_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005367shared_ptr<_Tp>
5368atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5369{
5370 return atomic_load(__p);
5371}
5372
5373template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005374_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005375void
5376atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5377{
5378 __sp_mut& __m = __get_sp_mut(__p);
5379 __m.lock();
5380 __p->swap(__r);
5381 __m.unlock();
5382}
5383
5384template <class _Tp>
5385inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005386_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005387void
5388atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5389{
5390 atomic_store(__p, __r);
5391}
5392
5393template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005394_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005395shared_ptr<_Tp>
5396atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5397{
5398 __sp_mut& __m = __get_sp_mut(__p);
5399 __m.lock();
5400 __p->swap(__r);
5401 __m.unlock();
5402 return __r;
5403}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005404
Howard Hinnant9fa30202012-07-30 01:40:57 +00005405template <class _Tp>
5406inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005407_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005408shared_ptr<_Tp>
5409atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5410{
5411 return atomic_exchange(__p, __r);
5412}
5413
5414template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005415_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005416bool
5417atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5418{
Marshall Clow4201ee82016-05-18 17:50:13 +00005419 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005420 __sp_mut& __m = __get_sp_mut(__p);
5421 __m.lock();
5422 if (__p->__owner_equivalent(*__v))
5423 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005424 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005425 *__p = __w;
5426 __m.unlock();
5427 return true;
5428 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005429 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005430 *__v = *__p;
5431 __m.unlock();
5432 return false;
5433}
5434
5435template <class _Tp>
5436inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005437_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005438bool
5439atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5440{
5441 return atomic_compare_exchange_strong(__p, __v, __w);
5442}
5443
5444template <class _Tp>
5445inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005446_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005447bool
5448atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5449 shared_ptr<_Tp> __w, memory_order, memory_order)
5450{
5451 return atomic_compare_exchange_strong(__p, __v, __w);
5452}
5453
5454template <class _Tp>
5455inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005456_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005457bool
5458atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5459 shared_ptr<_Tp> __w, memory_order, memory_order)
5460{
5461 return atomic_compare_exchange_weak(__p, __v, __w);
5462}
5463
Eric Fiselier9b492672016-06-18 02:12:53 +00005464#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005465
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005466//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005467#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5468# ifndef _LIBCPP_CXX03_LANG
5469enum class pointer_safety : unsigned char {
5470 relaxed,
5471 preferred,
5472 strict
5473};
5474# endif
5475#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005476struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005477{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005478 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005479 {
5480 relaxed,
5481 preferred,
5482 strict
5483 };
5484
Howard Hinnant49e145e2012-10-30 19:06:59 +00005485 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005486
Howard Hinnant756c69b2010-09-22 16:48:34 +00005487 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005488 pointer_safety() : __v_() {}
5489
5490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005491 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005493 operator int() const {return __v_;}
5494};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005495#endif
5496
5497#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5498 defined(_LIBCPP_BUILDING_MEMORY)
5499_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5500#else
5501// This function is only offered in C++03 under ABI v1.
5502# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5503inline _LIBCPP_INLINE_VISIBILITY
5504pointer_safety get_pointer_safety() _NOEXCEPT {
5505 return pointer_safety::relaxed;
5506}
5507# endif
5508#endif
5509
Howard Hinnantc51e1022010-05-11 19:42:16 +00005510
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005511_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5512_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5513_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005514_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005515
5516template <class _Tp>
5517inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005518_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005519undeclare_reachable(_Tp* __p)
5520{
5521 return static_cast<_Tp*>(__undeclare_reachable(__p));
5522}
5523
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005524_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005525
Marshall Clow8982dcd2015-07-13 20:04:56 +00005526// --- Helper for container swap --
5527template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005528inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005529void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5530#if _LIBCPP_STD_VER >= 14
5531 _NOEXCEPT
5532#else
5533 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5534#endif
5535{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005536 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005537 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5538}
5539
5540template <typename _Alloc>
5541_LIBCPP_INLINE_VISIBILITY
5542void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5543#if _LIBCPP_STD_VER >= 14
5544 _NOEXCEPT
5545#else
5546 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5547#endif
5548{
5549 using _VSTD::swap;
5550 swap(__a1, __a2);
5551}
5552
5553template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005554inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005555void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5556
Marshall Clowff91de82015-08-18 19:51:37 +00005557template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005558struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005559 _Traits::propagate_on_container_move_assignment::value
5560#if _LIBCPP_STD_VER > 14
5561 || _Traits::is_always_equal::value
5562#else
5563 && is_nothrow_move_assignable<_Alloc>::value
5564#endif
5565 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005566
Marshall Clowa591b9a2016-07-11 21:38:08 +00005567
5568#ifndef _LIBCPP_HAS_NO_VARIADICS
5569template <class _Tp, class _Alloc>
5570struct __temp_value {
5571 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005572
Marshall Clowa591b9a2016-07-11 21:38:08 +00005573 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5574 _Alloc &__a;
5575
5576 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5577 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005578
Marshall Clowa591b9a2016-07-11 21:38:08 +00005579 template<class... _Args>
5580 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5581 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005582
Marshall Clowa591b9a2016-07-11 21:38:08 +00005583 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5584 };
5585#endif
5586
Howard Hinnantc51e1022010-05-11 19:42:16 +00005587_LIBCPP_END_NAMESPACE_STD
5588
Eric Fiselierf4433a32017-05-31 22:07:49 +00005589_LIBCPP_POP_MACROS
5590
Howard Hinnantc51e1022010-05-11 19:42:16 +00005591#endif // _LIBCPP_MEMORY