blob: 30cb79eb58042039120c459a88f8872fa7d6cfb4 [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
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000390template<class T>
391class shared_ptr
392{
393public:
394 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000395 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396
397 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000398 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000399 template<class Y> explicit shared_ptr(Y* p);
400 template<class Y, class D> shared_ptr(Y* p, D d);
401 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
402 template <class D> shared_ptr(nullptr_t p, D d);
403 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000404 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
405 shared_ptr(const shared_ptr& r) noexcept;
406 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
407 shared_ptr(shared_ptr&& r) noexcept;
408 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000409 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000410 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000411 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
412 shared_ptr(nullptr_t) : shared_ptr() { }
413
414 // destructor:
415 ~shared_ptr();
416
417 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000418 shared_ptr& operator=(const shared_ptr& r) noexcept;
419 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
420 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000421 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000422 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000423 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
424
425 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000426 void swap(shared_ptr& r) noexcept;
427 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000428 template<class Y> void reset(Y* p);
429 template<class Y, class D> void reset(Y* p, D d);
430 template<class Y, class D, class A> void reset(Y* p, D d, A a);
431
Howard Hinnant719bda32011-05-28 14:41:13 +0000432 // observers:
433 T* get() const noexcept;
434 T& operator*() const noexcept;
435 T* operator->() const noexcept;
436 long use_count() const noexcept;
437 bool unique() const noexcept;
438 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000439 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
440 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000441};
442
443// shared_ptr comparisons:
444template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000445 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000446template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000447 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000448template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000449 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000451 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000453 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000454template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000455 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
456
457template <class T>
458 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
459template <class T>
460 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
461template <class T>
462 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
463template <class T>
464 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
465template <class T>
466 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
467template <class T>
468bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
469template <class T>
470 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
471template <class T>
472 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
473template <class T>
474 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000483template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484
485// shared_ptr casts:
486template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000487 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000488template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000489 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000490template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000491 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000492
493// shared_ptr I/O:
494template<class E, class T, class Y>
495 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
496
497// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000498template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000499
500template<class T, class... Args>
501 shared_ptr<T> make_shared(Args&&... args);
502template<class T, class A, class... Args>
503 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
504
505template<class T>
506class weak_ptr
507{
508public:
509 typedef T element_type;
510
511 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000512 constexpr weak_ptr() noexcept;
513 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
514 weak_ptr(weak_ptr const& r) noexcept;
515 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000516 weak_ptr(weak_ptr&& r) noexcept; // C++14
517 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000518
519 // destructor
520 ~weak_ptr();
521
522 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000523 weak_ptr& operator=(weak_ptr const& r) noexcept;
524 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
525 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000526 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
527 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000530 void swap(weak_ptr& r) noexcept;
531 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000532
533 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000534 long use_count() const noexcept;
535 bool expired() const noexcept;
536 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000537 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
538 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000539};
540
541// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000542template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000543
544// class owner_less:
545template<class T> struct owner_less;
546
547template<class T>
548struct owner_less<shared_ptr<T>>
549 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
550{
551 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000552 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
553 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
554 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000555};
556
557template<class T>
558struct owner_less<weak_ptr<T>>
559 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
560{
561 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000562 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
563 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
564 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
565};
566
567template <> // Added in C++14
568struct owner_less<void>
569{
570 template <class _Tp, class _Up>
571 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
572 template <class _Tp, class _Up>
573 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
574 template <class _Tp, class _Up>
575 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
576 template <class _Tp, class _Up>
577 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
578
579 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000580};
581
582template<class T>
583class enable_shared_from_this
584{
585protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000586 constexpr enable_shared_from_this() noexcept;
587 enable_shared_from_this(enable_shared_from_this const&) noexcept;
588 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000589 ~enable_shared_from_this();
590public:
591 shared_ptr<T> shared_from_this();
592 shared_ptr<T const> shared_from_this() const;
593};
594
595template<class T>
596 bool atomic_is_lock_free(const shared_ptr<T>* p);
597template<class T>
598 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
599template<class T>
600 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
601template<class T>
602 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
603template<class T>
604 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
605template<class T>
606 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
607template<class T>
608 shared_ptr<T>
609 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
610template<class T>
611 bool
612 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<class T>
614 bool
615 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
616template<class T>
617 bool
618 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
619 shared_ptr<T> w, memory_order success,
620 memory_order failure);
621template<class T>
622 bool
623 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
624 shared_ptr<T> w, memory_order success,
625 memory_order failure);
626// Hash support
627template <class T> struct hash;
628template <class T, class D> struct hash<unique_ptr<T, D> >;
629template <class T> struct hash<shared_ptr<T> >;
630
631// Pointer safety
632enum class pointer_safety { relaxed, preferred, strict };
633void declare_reachable(void *p);
634template <class T> T *undeclare_reachable(T *p);
635void declare_no_pointers(char *p, size_t n);
636void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000637pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000638
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
640
641} // std
642
643*/
644
645#include <__config>
646#include <type_traits>
647#include <typeinfo>
648#include <cstddef>
649#include <cstdint>
650#include <new>
651#include <utility>
652#include <limits>
653#include <iterator>
654#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000655#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000656#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000657#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000658#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000659#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000660#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000661# include <atomic>
662#endif
663
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000664#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000666#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667
Eric Fiselierf4433a32017-05-31 22:07:49 +0000668_LIBCPP_PUSH_MACROS
669#include <__undef_macros>
670
671
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672_LIBCPP_BEGIN_NAMESPACE_STD
673
Eric Fiselier89659d12015-07-07 00:27:16 +0000674template <class _ValueType>
675inline _LIBCPP_ALWAYS_INLINE
676_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
677#if !defined(_LIBCPP_HAS_NO_THREADS) && \
678 defined(__ATOMIC_RELAXED) && \
679 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
680 return __atomic_load_n(__value, __ATOMIC_RELAXED);
681#else
682 return *__value;
683#endif
684}
685
Kuba Breckade9d6792016-09-04 09:55:12 +0000686template <class _ValueType>
687inline _LIBCPP_ALWAYS_INLINE
688_ValueType __libcpp_acquire_load(_ValueType const* __value) {
689#if !defined(_LIBCPP_HAS_NO_THREADS) && \
690 defined(__ATOMIC_ACQUIRE) && \
691 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
692 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
693#else
694 return *__value;
695#endif
696}
697
Marshall Clow78dbe462016-11-14 18:22:19 +0000698// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700template <class _Tp> class allocator;
701
702template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000703class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704{
705public:
706 typedef void* pointer;
707 typedef const void* const_pointer;
708 typedef void value_type;
709
710 template <class _Up> struct rebind {typedef allocator<_Up> other;};
711};
712
Howard Hinnant9f771052012-01-19 23:15:22 +0000713template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000714class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000715{
716public:
717 typedef const void* pointer;
718 typedef const void* const_pointer;
719 typedef const void value_type;
720
721 template <class _Up> struct rebind {typedef allocator<_Up> other;};
722};
723
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724// pointer_traits
725
Marshall Clow0be70c32017-06-14 21:23:57 +0000726template <class _Tp, class = void>
727struct __has_element_type : false_type {};
728
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000730struct __has_element_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000731 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732
733template <class _Ptr, bool = __has_element_type<_Ptr>::value>
734struct __pointer_traits_element_type;
735
736template <class _Ptr>
737struct __pointer_traits_element_type<_Ptr, true>
738{
739 typedef typename _Ptr::element_type type;
740};
741
742#ifndef _LIBCPP_HAS_NO_VARIADICS
743
744template <template <class, class...> class _Sp, class _Tp, class ..._Args>
745struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
746{
747 typedef typename _Sp<_Tp, _Args...>::element_type type;
748};
749
750template <template <class, class...> class _Sp, class _Tp, class ..._Args>
751struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
752{
753 typedef _Tp type;
754};
755
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000756#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757
758template <template <class> class _Sp, class _Tp>
759struct __pointer_traits_element_type<_Sp<_Tp>, true>
760{
761 typedef typename _Sp<_Tp>::element_type type;
762};
763
764template <template <class> class _Sp, class _Tp>
765struct __pointer_traits_element_type<_Sp<_Tp>, false>
766{
767 typedef _Tp type;
768};
769
770template <template <class, class> class _Sp, class _Tp, class _A0>
771struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
772{
773 typedef typename _Sp<_Tp, _A0>::element_type type;
774};
775
776template <template <class, class> class _Sp, class _Tp, class _A0>
777struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
778{
779 typedef _Tp type;
780};
781
782template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
783struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
784{
785 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
786};
787
788template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
789struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
790{
791 typedef _Tp type;
792};
793
794template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
795 class _A1, class _A2>
796struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
797{
798 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
799};
800
801template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
802 class _A1, class _A2>
803struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
804{
805 typedef _Tp type;
806};
807
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000808#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809
Marshall Clow0be70c32017-06-14 21:23:57 +0000810template <class _Tp, class = void>
811struct __has_difference_type : false_type {};
812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +0000814struct __has_difference_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +0000815 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
817template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
818struct __pointer_traits_difference_type
819{
820 typedef ptrdiff_t type;
821};
822
823template <class _Ptr>
824struct __pointer_traits_difference_type<_Ptr, true>
825{
826 typedef typename _Ptr::difference_type type;
827};
828
829template <class _Tp, class _Up>
830struct __has_rebind
831{
832private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000833 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 template <class _Xp> static __two __test(...);
835 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
836public:
837 static const bool value = sizeof(__test<_Tp>(0)) == 1;
838};
839
840template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
841struct __pointer_traits_rebind
842{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000843#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 typedef typename _Tp::template rebind<_Up> type;
845#else
846 typedef typename _Tp::template rebind<_Up>::other type;
847#endif
848};
849
850#ifndef _LIBCPP_HAS_NO_VARIADICS
851
852template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
853struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
854{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000855#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
857#else
858 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
859#endif
860};
861
862template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
863struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
864{
865 typedef _Sp<_Up, _Args...> type;
866};
867
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000868#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869
870template <template <class> class _Sp, class _Tp, class _Up>
871struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
872{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000873#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 typedef typename _Sp<_Tp>::template rebind<_Up> type;
875#else
876 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
877#endif
878};
879
880template <template <class> class _Sp, class _Tp, class _Up>
881struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
882{
883 typedef _Sp<_Up> type;
884};
885
886template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
887struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
888{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000889#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
891#else
892 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
893#endif
894};
895
896template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
897struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
898{
899 typedef _Sp<_Up, _A0> type;
900};
901
902template <template <class, class, class> class _Sp, class _Tp, class _A0,
903 class _A1, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
905{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000906#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
908#else
909 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
910#endif
911};
912
913template <template <class, class, class> class _Sp, class _Tp, class _A0,
914 class _A1, class _Up>
915struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
916{
917 typedef _Sp<_Up, _A0, _A1> type;
918};
919
920template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
921 class _A1, class _A2, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
923{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000924#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
926#else
927 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
928#endif
929};
930
931template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
932 class _A1, class _A2, class _Up>
933struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
934{
935 typedef _Sp<_Up, _A0, _A1, _A2> type;
936};
937
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000938#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939
940template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000941struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942{
943 typedef _Ptr pointer;
944 typedef typename __pointer_traits_element_type<pointer>::type element_type;
945 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
946
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000947#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000948 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949#else
950 template <class _Up> struct rebind
951 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000952#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
954private:
955 struct __nat {};
956public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 static pointer pointer_to(typename conditional<is_void<element_type>::value,
959 __nat, element_type>::type& __r)
960 {return pointer::pointer_to(__r);}
961};
962
963template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000964struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965{
966 typedef _Tp* pointer;
967 typedef _Tp element_type;
968 typedef ptrdiff_t difference_type;
969
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000970#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 template <class _Up> using rebind = _Up*;
972#else
973 template <class _Up> struct rebind {typedef _Up* other;};
974#endif
975
976private:
977 struct __nat {};
978public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000981 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000982 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983};
984
Eric Fiselierae3ab842015-08-23 02:56:05 +0000985template <class _From, class _To>
986struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000987#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988 typedef typename pointer_traits<_From>::template rebind<_To> type;
989#else
990 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
991#endif
992};
993
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994// allocator_traits
995
Marshall Clow0be70c32017-06-14 21:23:57 +0000996template <class _Tp, class = void>
997struct __has_pointer_type : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998
999template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001000struct __has_pointer_type<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001001 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002
1003namespace __pointer_type_imp
1004{
1005
1006template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1007struct __pointer_type
1008{
1009 typedef typename _Dp::pointer type;
1010};
1011
1012template <class _Tp, class _Dp>
1013struct __pointer_type<_Tp, _Dp, false>
1014{
1015 typedef _Tp* type;
1016};
1017
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001018} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
1020template <class _Tp, class _Dp>
1021struct __pointer_type
1022{
1023 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1024};
1025
Marshall Clow0be70c32017-06-14 21:23:57 +00001026template <class _Tp, class = void>
1027struct __has_const_pointer : false_type {};
1028
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001030struct __has_const_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001031 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032
1033template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1034struct __const_pointer
1035{
1036 typedef typename _Alloc::const_pointer type;
1037};
1038
1039template <class _Tp, class _Ptr, class _Alloc>
1040struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1041{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001042#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1044#else
1045 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1046#endif
1047};
1048
Marshall Clow0be70c32017-06-14 21:23:57 +00001049template <class _Tp, class = void>
1050struct __has_void_pointer : false_type {};
1051
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001053struct __has_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001054 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055
1056template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1057struct __void_pointer
1058{
1059 typedef typename _Alloc::void_pointer type;
1060};
1061
1062template <class _Ptr, class _Alloc>
1063struct __void_pointer<_Ptr, _Alloc, false>
1064{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001065#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1067#else
1068 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1069#endif
1070};
1071
Marshall Clow0be70c32017-06-14 21:23:57 +00001072template <class _Tp, class = void>
1073struct __has_const_void_pointer : false_type {};
1074
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001076struct __has_const_void_pointer<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001077 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078
1079template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1080struct __const_void_pointer
1081{
1082 typedef typename _Alloc::const_void_pointer type;
1083};
1084
1085template <class _Ptr, class _Alloc>
1086struct __const_void_pointer<_Ptr, _Alloc, false>
1087{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001088#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1090#else
1091 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1092#endif
1093};
1094
Howard Hinnantc834c512011-11-29 18:15:50 +00001095template <class _Tp>
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001096inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc834c512011-11-29 18:15:50 +00001097_Tp*
1098__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099{
1100 return __p;
1101}
1102
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001103#if _LIBCPP_STD_VER <= 17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104template <class _Pointer>
1105inline _LIBCPP_INLINE_VISIBILITY
1106typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001107__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001109 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110}
Eric Fiselier45c9aac2017-11-22 19:49:21 +00001111#else
1112template <class _Pointer>
1113inline _LIBCPP_INLINE_VISIBILITY
1114auto
1115__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1116-> decltype(pointer_traits<_Pointer>::to_address(__p))
1117{
1118 return pointer_traits<_Pointer>::to_address(__p);
1119}
1120
1121template <class _Pointer, class... _None>
1122inline _LIBCPP_INLINE_VISIBILITY
1123auto
1124__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1125{
1126 return _VSTD::__to_raw_pointer(__p.operator->());
1127}
1128
1129template <class _Tp>
1130inline _LIBCPP_INLINE_VISIBILITY constexpr
1131_Tp*
1132to_address(_Tp* __p) _NOEXCEPT
1133{
1134 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1135 return __p;
1136}
1137
1138template <class _Pointer>
1139inline _LIBCPP_INLINE_VISIBILITY
1140auto
1141to_address(const _Pointer& __p) _NOEXCEPT
1142{
1143 return _VSTD::__to_raw_pointer(__p);
1144}
1145#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
Marshall Clow0be70c32017-06-14 21:23:57 +00001147template <class _Tp, class = void>
1148struct __has_size_type : false_type {};
1149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001151struct __has_size_type<_Tp,
1152 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001154template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155struct __size_type
1156{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001157 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158};
1159
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001160template <class _Alloc, class _DiffType>
1161struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162{
1163 typedef typename _Alloc::size_type type;
1164};
1165
Marshall Clow0be70c32017-06-14 21:23:57 +00001166template <class _Tp, class = void>
1167struct __has_propagate_on_container_copy_assignment : false_type {};
1168
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169template <class _Tp>
Marshall Clow0be70c32017-06-14 21:23:57 +00001170struct __has_propagate_on_container_copy_assignment<_Tp,
1171 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1172 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
1174template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1175struct __propagate_on_container_copy_assignment
1176{
1177 typedef false_type type;
1178};
1179
1180template <class _Alloc>
1181struct __propagate_on_container_copy_assignment<_Alloc, true>
1182{
1183 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1184};
1185
Marshall Clow0be70c32017-06-14 21:23:57 +00001186template <class _Tp, class = void>
1187struct __has_propagate_on_container_move_assignment : false_type {};
1188
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001190struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001191 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1192 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193
1194template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1195struct __propagate_on_container_move_assignment
1196{
1197 typedef false_type type;
1198};
1199
1200template <class _Alloc>
1201struct __propagate_on_container_move_assignment<_Alloc, true>
1202{
1203 typedef typename _Alloc::propagate_on_container_move_assignment type;
1204};
1205
Marshall Clow0be70c32017-06-14 21:23:57 +00001206template <class _Tp, class = void>
1207struct __has_propagate_on_container_swap : false_type {};
1208
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001210struct __has_propagate_on_container_swap<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001211 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1212 : true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213
1214template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1215struct __propagate_on_container_swap
1216{
1217 typedef false_type type;
1218};
1219
1220template <class _Alloc>
1221struct __propagate_on_container_swap<_Alloc, true>
1222{
1223 typedef typename _Alloc::propagate_on_container_swap type;
1224};
1225
Marshall Clow0be70c32017-06-14 21:23:57 +00001226template <class _Tp, class = void>
1227struct __has_is_always_equal : false_type {};
1228
Marshall Clow0b587562015-06-02 16:34:03 +00001229template <class _Tp>
Aditya Kumar7c5db692017-08-20 10:38:55 +00001230struct __has_is_always_equal<_Tp,
Marshall Clow0be70c32017-06-14 21:23:57 +00001231 typename __void_t<typename _Tp::is_always_equal>::type>
1232 : true_type {};
Marshall Clow0b587562015-06-02 16:34:03 +00001233
1234template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1235struct __is_always_equal
1236{
1237 typedef typename _VSTD::is_empty<_Alloc>::type type;
1238};
1239
1240template <class _Alloc>
1241struct __is_always_equal<_Alloc, true>
1242{
1243 typedef typename _Alloc::is_always_equal type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1247struct __has_rebind_other
1248{
1249private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001250 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 template <class _Xp> static __two __test(...);
1252 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1253public:
1254 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1255};
1256
1257template <class _Tp, class _Up>
1258struct __has_rebind_other<_Tp, _Up, false>
1259{
1260 static const bool value = false;
1261};
1262
1263template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1264struct __allocator_traits_rebind
1265{
1266 typedef typename _Tp::template rebind<_Up>::other type;
1267};
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1279{
1280 typedef _Alloc<_Up, _Args...> type;
1281};
1282
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
1285template <template <class> class _Alloc, class _Tp, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1293{
1294 typedef _Alloc<_Up> type;
1295};
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1299{
1300 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1301};
1302
1303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1> type;
1321};
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _A2, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1326{
1327 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1328};
1329
1330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1333{
1334 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1335};
1336
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001337#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001339#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1342auto
1343__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001344 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
1346template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1347auto
1348__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1349 -> false_type;
1350
1351template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1352struct __has_allocate_hint
1353 : integral_constant<bool,
1354 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001355 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356 declval<_SizeType>(),
1357 declval<_ConstVoidPtr>())),
1358 true_type>::value>
1359{
1360};
1361
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001362#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1365struct __has_allocate_hint
1366 : true_type
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001375decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1376 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 true_type())
1378__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1379
1380template <class _Alloc, class _Pointer, class ..._Args>
1381false_type
1382__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1383
1384template <class _Alloc, class _Pointer, class ..._Args>
1385struct __has_construct
1386 : integral_constant<bool,
1387 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001388 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 declval<_Pointer>(),
1390 declval<_Args>()...)),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc, class _Pointer>
1396auto
1397__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1398 -> decltype(__a.destroy(__p), true_type());
1399
1400template <class _Alloc, class _Pointer>
1401auto
1402__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1403 -> false_type;
1404
1405template <class _Alloc, class _Pointer>
1406struct __has_destroy
1407 : integral_constant<bool,
1408 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001409 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 declval<_Pointer>())),
1411 true_type>::value>
1412{
1413};
1414
1415template <class _Alloc>
1416auto
1417__has_max_size_test(_Alloc&& __a)
1418 -> decltype(__a.max_size(), true_type());
1419
1420template <class _Alloc>
1421auto
1422__has_max_size_test(const volatile _Alloc& __a)
1423 -> false_type;
1424
1425template <class _Alloc>
1426struct __has_max_size
1427 : integral_constant<bool,
1428 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001429 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430 true_type>::value>
1431{
1432};
1433
1434template <class _Alloc>
1435auto
1436__has_select_on_container_copy_construction_test(_Alloc&& __a)
1437 -> decltype(__a.select_on_container_copy_construction(), true_type());
1438
1439template <class _Alloc>
1440auto
1441__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1442 -> false_type;
1443
1444template <class _Alloc>
1445struct __has_select_on_container_copy_construction
1446 : integral_constant<bool,
1447 is_same<
Eric Fiselierbe4cb612017-09-15 00:31:38 +00001448 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 true_type>::value>
1450{
1451};
1452
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001453#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455#ifndef _LIBCPP_HAS_NO_VARIADICS
1456
1457template <class _Alloc, class _Pointer, class ..._Args>
1458struct __has_construct
1459 : false_type
1460{
1461};
1462
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463#else // _LIBCPP_HAS_NO_VARIADICS
1464
1465template <class _Alloc, class _Pointer, class _Args>
1466struct __has_construct
1467 : false_type
1468{
1469};
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
1474struct __has_destroy
1475 : false_type
1476{
1477};
1478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Marshall Clow0e58cae2017-11-26 02:55:38 +00001539 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001542 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001544 {return __allocate(__a, __n, __hint,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp(__a0);
1569 }
1570 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001572 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 const _A1& __a1)
1574 {
1575 ::new ((void*)__p) _Tp(__a0, __a1);
1576 }
1577 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1, const _A2& __a2)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1583 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void destroy(allocator_type& __a, _Tp* __p)
1589 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001592 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001598 {return __select_on_container_copy_construction(
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 __has_select_on_container_copy_construction<const allocator_type>(),
1600 __a);}
1601
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001602 template <class _Ptr>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 void
1606 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1607 {
1608 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1609 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1610 }
1611
1612 template <class _Tp>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 typename enable_if
1616 <
1617 (is_same<allocator_type, allocator<_Tp> >::value
1618 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1619 is_trivially_move_constructible<_Tp>::value,
1620 void
1621 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001622 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 {
1624 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001625 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001626 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 __begin2 += _Np;
1629 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 }
1631
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001632 template <class _Iter, class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
1636 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1637 {
1638 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1639 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1640 }
1641
1642 template <class _Tp>
1643 _LIBCPP_INLINE_VISIBILITY
1644 static
1645 typename enable_if
1646 <
1647 (is_same<allocator_type, allocator<_Tp> >::value
1648 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1649 is_trivially_move_constructible<_Tp>::value,
1650 void
1651 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001652 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001653 {
1654 typedef typename remove_const<_Tp>::type _Vp;
1655 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001656 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001657 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 __begin2 += _Np;
1660 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001661 }
1662
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 template <class _Ptr>
1664 _LIBCPP_INLINE_VISIBILITY
1665 static
1666 void
1667 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1668 {
1669 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 {
1671 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1672 --__end2;
1673 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 }
1675
1676 template <class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 static
1679 typename enable_if
1680 <
1681 (is_same<allocator_type, allocator<_Tp> >::value
1682 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1683 is_trivially_move_constructible<_Tp>::value,
1684 void
1685 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001686 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 {
1688 ptrdiff_t _Np = __end1 - __begin1;
1689 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001690 if (_Np > 0)
1691 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001692 }
1693
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694private:
1695
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001697 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001701 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001702 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {return __a.allocate(__n);}
1704
1705#ifndef _LIBCPP_HAS_NO_VARIADICS
1706 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001714 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001716#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
1718 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1721 {__a.destroy(__p);}
1722 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 static void __destroy(false_type, allocator_type&, _Tp* __p)
1725 {
1726 __p->~_Tp();
1727 }
1728
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static size_type __max_size(true_type, const allocator_type& __a)
1731 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001734 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001738 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static allocator_type
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001742 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 {return __a;}
1744};
1745
Marshall Clow940e01c2015-04-07 05:21:38 +00001746template <class _Traits, class _Tp>
1747struct __rebind_alloc_helper
1748{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001749#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001750 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001751#else
1752 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1753#endif
1754};
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756// allocator
1757
1758template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001759class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761public:
1762 typedef size_t size_type;
1763 typedef ptrdiff_t difference_type;
1764 typedef _Tp* pointer;
1765 typedef const _Tp* const_pointer;
1766 typedef _Tp& reference;
1767 typedef const _Tp& const_reference;
1768 typedef _Tp value_type;
1769
Howard Hinnant4931e092011-06-02 21:38:57 +00001770 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001771 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1774
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1776 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1777 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001779 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Marshall Clow0e58cae2017-11-26 02:55:38 +00001781 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1782 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001783 {
1784 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001785 __throw_length_error("allocator<T>::allocate(size_t n)"
1786 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001787 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1788 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001789 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001790 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001791 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1792 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001793#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 template <class _Up, class... _Args>
1795 _LIBCPP_INLINE_VISIBILITY
1796 void
1797 construct(_Up* __p, _Args&&... __args)
1798 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001799 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001801#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 _LIBCPP_INLINE_VISIBILITY
1803 void
1804 construct(pointer __p)
1805 {
1806 ::new((void*)__p) _Tp();
1807 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001808# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001809
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810 template <class _A0>
1811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001812 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 construct(pointer __p, _A0& __a0)
1814 {
1815 ::new((void*)__p) _Tp(__a0);
1816 }
1817 template <class _A0>
1818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001819 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 construct(pointer __p, const _A0& __a0)
1821 {
1822 ::new((void*)__p) _Tp(__a0);
1823 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001824# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 template <class _A0, class _A1>
1826 _LIBCPP_INLINE_VISIBILITY
1827 void
1828 construct(pointer __p, _A0& __a0, _A1& __a1)
1829 {
1830 ::new((void*)__p) _Tp(__a0, __a1);
1831 }
1832 template <class _A0, class _A1>
1833 _LIBCPP_INLINE_VISIBILITY
1834 void
1835 construct(pointer __p, const _A0& __a0, _A1& __a1)
1836 {
1837 ::new((void*)__p) _Tp(__a0, __a1);
1838 }
1839 template <class _A0, class _A1>
1840 _LIBCPP_INLINE_VISIBILITY
1841 void
1842 construct(pointer __p, _A0& __a0, const _A1& __a1)
1843 {
1844 ::new((void*)__p) _Tp(__a0, __a1);
1845 }
1846 template <class _A0, class _A1>
1847 _LIBCPP_INLINE_VISIBILITY
1848 void
1849 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1850 {
1851 ::new((void*)__p) _Tp(__a0, __a1);
1852 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001853#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1855};
1856
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001857template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001858class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001859{
1860public:
1861 typedef size_t size_type;
1862 typedef ptrdiff_t difference_type;
1863 typedef const _Tp* pointer;
1864 typedef const _Tp* const_pointer;
1865 typedef const _Tp& reference;
1866 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001867 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001868
1869 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001870 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871
1872 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1873
1874 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1875 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1876 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1877 {return _VSTD::addressof(__x);}
1878 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001879 {
1880 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001881 __throw_length_error("allocator<const T>::allocate(size_t n)"
1882 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001883 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001884 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001885 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00001886 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p));}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001887 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1888 {return size_type(~0) / sizeof(_Tp);}
1889#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1890 template <class _Up, class... _Args>
1891 _LIBCPP_INLINE_VISIBILITY
1892 void
1893 construct(_Up* __p, _Args&&... __args)
1894 {
1895 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1896 }
1897#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1898 _LIBCPP_INLINE_VISIBILITY
1899 void
1900 construct(pointer __p)
1901 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001902 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001903 }
1904# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001905
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001906 template <class _A0>
1907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001908 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001909 construct(pointer __p, _A0& __a0)
1910 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001911 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001912 }
1913 template <class _A0>
1914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001915 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001916 construct(pointer __p, const _A0& __a0)
1917 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001918 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001920# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1921 template <class _A0, class _A1>
1922 _LIBCPP_INLINE_VISIBILITY
1923 void
1924 construct(pointer __p, _A0& __a0, _A1& __a1)
1925 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001926 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001927 }
1928 template <class _A0, class _A1>
1929 _LIBCPP_INLINE_VISIBILITY
1930 void
1931 construct(pointer __p, const _A0& __a0, _A1& __a1)
1932 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001933 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001934 }
1935 template <class _A0, class _A1>
1936 _LIBCPP_INLINE_VISIBILITY
1937 void
1938 construct(pointer __p, _A0& __a0, const _A1& __a1)
1939 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001940 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001941 }
1942 template <class _A0, class _A1>
1943 _LIBCPP_INLINE_VISIBILITY
1944 void
1945 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1946 {
Marshall Clow31350ab2017-06-14 16:54:43 +00001947 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001948 }
1949#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1950 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1951};
1952
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953template <class _Tp, class _Up>
1954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001955bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956
1957template <class _Tp, class _Up>
1958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001959bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960
1961template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001962class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 : public iterator<output_iterator_tag,
1964 _Tp, // purposefully not C++03
1965 ptrdiff_t, // purposefully not C++03
1966 _Tp*, // purposefully not C++03
1967 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1968{
1969private:
1970 _OutputIterator __x_;
1971public:
1972 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1974 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1975 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001976#if _LIBCPP_STD_VER >= 14
1977 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1978 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1979#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1981 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1982 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001983#if _LIBCPP_STD_VER >= 14
Aditya Kumar7c5db692017-08-20 10:38:55 +00001984 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowb949f1b2015-05-10 13:14:08 +00001985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986};
1987
1988template <class _Tp>
1989pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001990get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991{
1992 pair<_Tp*, ptrdiff_t> __r(0, 0);
1993 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1994 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1995 / sizeof(_Tp);
1996 if (__n > __m)
1997 __n = __m;
1998 while (__n > 0)
1999 {
2000 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2001 if (__r.first)
2002 {
2003 __r.second = __n;
2004 break;
2005 }
2006 __n /= 2;
2007 }
2008 return __r;
2009}
2010
2011template <class _Tp>
2012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002013void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014
Marshall Clowb22274f2017-01-24 22:22:33 +00002015#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016template <class _Tp>
2017struct auto_ptr_ref
2018{
2019 _Tp* __ptr_;
2020};
2021
2022template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002023class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024{
2025private:
2026 _Tp* __ptr_;
2027public:
2028 typedef _Tp element_type;
2029
2030 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2031 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2032 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2033 : __ptr_(__p.release()) {}
2034 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2035 {reset(__p.release()); return *this;}
2036 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2037 {reset(__p.release()); return *this;}
2038 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2039 {reset(__p.__ptr_); return *this;}
2040 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2041
2042 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2043 {return *__ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2046 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2047 {
2048 _Tp* __t = __ptr_;
2049 __ptr_ = 0;
2050 return __t;
2051 }
2052 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2053 {
2054 if (__ptr_ != __p)
2055 delete __ptr_;
2056 __ptr_ = __p;
2057 }
2058
2059 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2060 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2061 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2062 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2063 {return auto_ptr<_Up>(release());}
2064};
2065
2066template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002067class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068{
2069public:
2070 typedef void element_type;
2071};
Marshall Clowb22274f2017-01-24 22:22:33 +00002072#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073
Eric Fiselier9d355982017-04-12 23:45:53 +00002074template <class _Tp, int _Idx,
2075 bool _CanBeEmptyBase =
2076 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2077struct __compressed_pair_elem {
2078 typedef _Tp _ParamT;
2079 typedef _Tp& reference;
2080 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081
Eric Fiselier9d355982017-04-12 23:45:53 +00002082#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002083 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084
Eric Fiselier9d355982017-04-12 23:45:53 +00002085 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002086 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2087 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002088 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002089 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002090 __compressed_pair_elem(_Up&& __u)
2091 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092
Eric Fiselier9d355982017-04-12 23:45:53 +00002093 template <class... _Args, size_t... _Indexes>
2094 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2095 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2096 __tuple_indices<_Indexes...>)
2097 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2098#else
Alex Lorenz76132112017-11-09 17:54:49 +00002099 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2100 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002101 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2102#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
Alex Lorenz76132112017-11-09 17:54:49 +00002104 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2105 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002106 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002109 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110};
2111
Eric Fiselier9d355982017-04-12 23:45:53 +00002112template <class _Tp, int _Idx>
2113struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2114 typedef _Tp _ParamT;
2115 typedef _Tp& reference;
2116 typedef const _Tp& const_reference;
2117 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118
Eric Fiselier9d355982017-04-12 23:45:53 +00002119#ifndef _LIBCPP_CXX03_LANG
Alex Lorenz76132112017-11-09 17:54:49 +00002120 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121
Eric Fiselier9d355982017-04-12 23:45:53 +00002122 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002123 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2124 >::type>
Alex Lorenz76132112017-11-09 17:54:49 +00002125 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002126 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002127 __compressed_pair_elem(_Up&& __u)
2128 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129
Eric Fiselier9d355982017-04-12 23:45:53 +00002130 template <class... _Args, size_t... _Indexes>
2131 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2132 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2133 __tuple_indices<_Indexes...>)
2134 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2135#else
Alex Lorenz76132112017-11-09 17:54:49 +00002136 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2137 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002138 __compressed_pair_elem(_ParamT __p)
2139 : __value_type(std::forward<_ParamT>(__p)) {}
2140#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141
Alex Lorenz76132112017-11-09 17:54:49 +00002142 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2143 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002144 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145};
2146
Eric Fiselier9d355982017-04-12 23:45:53 +00002147// Tag used to construct the second element of the compressed pair.
2148struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149
2150template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002151class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2152 private __compressed_pair_elem<_T2, 1> {
2153 typedef __compressed_pair_elem<_T1, 0> _Base1;
2154 typedef __compressed_pair_elem<_T2, 1> _Base2;
2155
2156 // NOTE: This static assert should never fire because __compressed_pair
2157 // is *almost never* used in a scenario where it's possible for T1 == T2.
2158 // (The exception is std::function where it is possible that the function
2159 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002160 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002161 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2162 "The current implementation is NOT ABI-compatible with the previous "
2163 "implementation for this configuration");
2164
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002166#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002167 template <bool _Dummy = true,
2168 class = typename enable_if<
2169 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2170 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2171 >::type
2172 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002173 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002174 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175
Eric Fiselier9d355982017-04-12 23:45:53 +00002176 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2177 __compressed_pair>::value,
2178 bool>::type = true>
2179 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2180 __compressed_pair(_Tp&& __t)
2181 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182
Eric Fiselier9d355982017-04-12 23:45:53 +00002183 template <class _Tp>
2184 _LIBCPP_INLINE_VISIBILITY constexpr
2185 __compressed_pair(__second_tag, _Tp&& __t)
2186 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187
Eric Fiselier9d355982017-04-12 23:45:53 +00002188 template <class _U1, class _U2>
2189 _LIBCPP_INLINE_VISIBILITY constexpr
2190 __compressed_pair(_U1&& __t1, _U2&& __t2)
2191 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192
Eric Fiselier9d355982017-04-12 23:45:53 +00002193 template <class... _Args1, class... _Args2>
2194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2195 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2196 tuple<_Args2...> __second_args)
2197 : _Base1(__pc, _VSTD::move(__first_args),
2198 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2199 _Base2(__pc, _VSTD::move(__second_args),
2200 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002201
Eric Fiselier9d355982017-04-12 23:45:53 +00002202#else
2203 _LIBCPP_INLINE_VISIBILITY
2204 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002205
Eric Fiselier9d355982017-04-12 23:45:53 +00002206 _LIBCPP_INLINE_VISIBILITY explicit
2207 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002208
Eric Fiselier9d355982017-04-12 23:45:53 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 __compressed_pair(__second_tag, _T2 __t2)
2211 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Eric Fiselier9d355982017-04-12 23:45:53 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 __compressed_pair(_T1 __t1, _T2 __t2)
2215 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218 _LIBCPP_INLINE_VISIBILITY
2219 typename _Base1::reference first() _NOEXCEPT {
2220 return static_cast<_Base1&>(*this).__get();
2221 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222
Eric Fiselier9d355982017-04-12 23:45:53 +00002223 _LIBCPP_INLINE_VISIBILITY
2224 typename _Base1::const_reference first() const _NOEXCEPT {
2225 return static_cast<_Base1 const&>(*this).__get();
2226 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227
Eric Fiselier9d355982017-04-12 23:45:53 +00002228 _LIBCPP_INLINE_VISIBILITY
2229 typename _Base2::reference second() _NOEXCEPT {
2230 return static_cast<_Base2&>(*this).__get();
2231 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232
Eric Fiselier9d355982017-04-12 23:45:53 +00002233 _LIBCPP_INLINE_VISIBILITY
2234 typename _Base2::const_reference second() const _NOEXCEPT {
2235 return static_cast<_Base2 const&>(*this).__get();
2236 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237
Eric Fiselier9d355982017-04-12 23:45:53 +00002238 _LIBCPP_INLINE_VISIBILITY
2239 void swap(__compressed_pair& __x)
2240 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2241 __is_nothrow_swappable<_T2>::value)
2242 {
2243 using std::swap;
2244 swap(first(), __x.first());
2245 swap(second(), __x.second());
2246 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247};
2248
2249template <class _T1, class _T2>
2250inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002251void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2252 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2253 __is_nothrow_swappable<_T2>::value) {
2254 __x.swap(__y);
2255}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002257// default_delete
2258
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002260struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkington2a398762017-05-25 15:43:31 +00002261 static_assert(!is_function<_Tp>::value,
2262 "default_delete cannot be instantiated for function types");
Eric Fiselier6779b062017-04-16 02:06:25 +00002263#ifndef _LIBCPP_CXX03_LANG
2264 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002265#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002266 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002267#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002268 template <class _Up>
2269 _LIBCPP_INLINE_VISIBILITY
2270 default_delete(const default_delete<_Up>&,
2271 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2272 0) _NOEXCEPT {}
2273
2274 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2275 static_assert(sizeof(_Tp) > 0,
2276 "default_delete can not delete incomplete type");
2277 static_assert(!is_void<_Tp>::value,
2278 "default_delete can not delete incomplete type");
2279 delete __ptr;
2280 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281};
2282
2283template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002284struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2285private:
2286 template <class _Up>
2287 struct _EnableIfConvertible
2288 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2289
Howard Hinnant4500ca52011-12-18 21:19:44 +00002290public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002291#ifndef _LIBCPP_CXX03_LANG
2292 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002293#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002294 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002295#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002296
2297 template <class _Up>
2298 _LIBCPP_INLINE_VISIBILITY
2299 default_delete(const default_delete<_Up[]>&,
2300 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2301
2302 template <class _Up>
2303 _LIBCPP_INLINE_VISIBILITY
2304 typename _EnableIfConvertible<_Up>::type
2305 operator()(_Up* __ptr) const _NOEXCEPT {
2306 static_assert(sizeof(_Tp) > 0,
2307 "default_delete can not delete incomplete type");
2308 static_assert(!is_void<_Tp>::value,
2309 "default_delete can not delete void type");
2310 delete[] __ptr;
2311 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312};
2313
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314
Eric Fiselier6779b062017-04-16 02:06:25 +00002315
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002316#ifndef _LIBCPP_CXX03_LANG
2317template <class _Deleter>
2318struct __unique_ptr_deleter_sfinae {
2319 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2320 typedef const _Deleter& __lval_ref_type;
2321 typedef _Deleter&& __good_rval_ref_type;
2322 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323};
2324
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002325template <class _Deleter>
2326struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2327 typedef const _Deleter& __lval_ref_type;
2328 typedef const _Deleter&& __bad_rval_ref_type;
2329 typedef false_type __enable_rval_overload;
2330};
2331
2332template <class _Deleter>
2333struct __unique_ptr_deleter_sfinae<_Deleter&> {
2334 typedef _Deleter& __lval_ref_type;
2335 typedef _Deleter&& __bad_rval_ref_type;
2336 typedef false_type __enable_rval_overload;
2337};
2338#endif // !defined(_LIBCPP_CXX03_LANG)
2339
2340template <class _Tp, class _Dp = default_delete<_Tp> >
2341class _LIBCPP_TEMPLATE_VIS unique_ptr {
2342public:
2343 typedef _Tp element_type;
2344 typedef _Dp deleter_type;
2345 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2346
2347 static_assert(!is_rvalue_reference<deleter_type>::value,
2348 "the specified deleter type cannot be an rvalue reference");
2349
2350private:
2351 __compressed_pair<pointer, deleter_type> __ptr_;
2352
2353 struct __nat { int __for_bool_; };
2354
2355#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002356 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002357
2358 template <bool _Dummy>
2359 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002360 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002361
2362 template <bool _Dummy>
2363 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002364 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002365
2366 template <bool _Dummy>
2367 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002368 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002369
2370 template <bool _Dummy, class _Deleter = typename __dependent_type<
2371 __identity<deleter_type>, _Dummy>::type>
2372 using _EnableIfDeleterDefaultConstructible =
2373 typename enable_if<is_default_constructible<_Deleter>::value &&
2374 !is_pointer<_Deleter>::value>::type;
2375
2376 template <class _ArgType>
2377 using _EnableIfDeleterConstructible =
2378 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2379
2380 template <class _UPtr, class _Up>
2381 using _EnableIfMoveConvertible = typename enable_if<
2382 is_convertible<typename _UPtr::pointer, pointer>::value &&
2383 !is_array<_Up>::value
2384 >::type;
2385
2386 template <class _UDel>
2387 using _EnableIfDeleterConvertible = typename enable_if<
2388 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2389 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2390 >::type;
2391
2392 template <class _UDel>
2393 using _EnableIfDeleterAssignable = typename enable_if<
2394 is_assignable<_Dp&, _UDel&&>::value
2395 >::type;
2396
2397public:
2398 template <bool _Dummy = true,
2399 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2400 _LIBCPP_INLINE_VISIBILITY
2401 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2402
2403 template <bool _Dummy = true,
2404 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2405 _LIBCPP_INLINE_VISIBILITY
2406 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2407
2408 template <bool _Dummy = true,
2409 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2410 _LIBCPP_INLINE_VISIBILITY
2411 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2412
2413 template <bool _Dummy = true,
2414 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2415 _LIBCPP_INLINE_VISIBILITY
2416 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2417 : __ptr_(__p, __d) {}
2418
2419 template <bool _Dummy = true,
2420 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2421 _LIBCPP_INLINE_VISIBILITY
2422 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2423 : __ptr_(__p, _VSTD::move(__d)) {
2424 static_assert(!is_reference<deleter_type>::value,
2425 "rvalue deleter bound to reference");
2426 }
2427
2428 template <bool _Dummy = true,
2429 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2430 _LIBCPP_INLINE_VISIBILITY
2431 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2432
2433 _LIBCPP_INLINE_VISIBILITY
2434 unique_ptr(unique_ptr&& __u) noexcept
2435 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2436 }
2437
2438 template <class _Up, class _Ep,
2439 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2440 class = _EnableIfDeleterConvertible<_Ep>
2441 >
2442 _LIBCPP_INLINE_VISIBILITY
2443 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2444 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2445
2446#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2447 template <class _Up>
2448 _LIBCPP_INLINE_VISIBILITY
2449 unique_ptr(auto_ptr<_Up>&& __p,
2450 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2451 is_same<_Dp, default_delete<_Tp>>::value,
2452 __nat>::type = __nat()) _NOEXCEPT
2453 : __ptr_(__p.release()) {}
2454#endif
2455
2456 _LIBCPP_INLINE_VISIBILITY
2457 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2458 reset(__u.release());
2459 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2460 return *this;
2461 }
2462
2463 template <class _Up, class _Ep,
2464 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2465 class = _EnableIfDeleterAssignable<_Ep>
2466 >
2467 _LIBCPP_INLINE_VISIBILITY
2468 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2469 reset(__u.release());
2470 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2471 return *this;
2472 }
2473
2474#else // _LIBCPP_CXX03_LANG
2475private:
2476 unique_ptr(unique_ptr&);
2477 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2478
2479 unique_ptr& operator=(unique_ptr&);
2480 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2481
2482public:
2483 _LIBCPP_INLINE_VISIBILITY
2484 unique_ptr() : __ptr_(pointer())
2485 {
2486 static_assert(!is_pointer<deleter_type>::value,
2487 "unique_ptr constructed with null function pointer deleter");
2488 static_assert(is_default_constructible<deleter_type>::value,
2489 "unique_ptr::deleter_type is not default constructible");
2490 }
2491 _LIBCPP_INLINE_VISIBILITY
2492 unique_ptr(nullptr_t) : __ptr_(pointer())
2493 {
2494 static_assert(!is_pointer<deleter_type>::value,
2495 "unique_ptr constructed with null function pointer deleter");
2496 }
2497 _LIBCPP_INLINE_VISIBILITY
2498 explicit unique_ptr(pointer __p)
2499 : __ptr_(_VSTD::move(__p)) {
2500 static_assert(!is_pointer<deleter_type>::value,
2501 "unique_ptr constructed with null function pointer deleter");
2502 }
2503
2504 _LIBCPP_INLINE_VISIBILITY
2505 operator __rv<unique_ptr>() {
2506 return __rv<unique_ptr>(*this);
2507 }
2508
2509 _LIBCPP_INLINE_VISIBILITY
2510 unique_ptr(__rv<unique_ptr> __u)
2511 : __ptr_(__u->release(),
2512 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2513
2514 template <class _Up, class _Ep>
2515 _LIBCPP_INLINE_VISIBILITY
2516 typename enable_if<
2517 !is_array<_Up>::value &&
2518 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2519 pointer>::value &&
2520 is_assignable<deleter_type&, _Ep&>::value,
2521 unique_ptr&>::type
2522 operator=(unique_ptr<_Up, _Ep> __u) {
2523 reset(__u.release());
2524 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2525 return *this;
2526 }
2527
2528 _LIBCPP_INLINE_VISIBILITY
2529 unique_ptr(pointer __p, deleter_type __d)
2530 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2531#endif // _LIBCPP_CXX03_LANG
2532
2533#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2534 template <class _Up>
2535 _LIBCPP_INLINE_VISIBILITY
2536 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2537 is_same<_Dp, default_delete<_Tp> >::value,
2538 unique_ptr&>::type
2539 operator=(auto_ptr<_Up> __p) {
2540 reset(__p.release());
2541 return *this;
2542 }
2543#endif
2544
2545 _LIBCPP_INLINE_VISIBILITY
2546 ~unique_ptr() { reset(); }
2547
2548 _LIBCPP_INLINE_VISIBILITY
2549 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2550 reset();
2551 return *this;
2552 }
2553
2554 _LIBCPP_INLINE_VISIBILITY
2555 typename add_lvalue_reference<_Tp>::type
2556 operator*() const {
2557 return *__ptr_.first();
2558 }
2559 _LIBCPP_INLINE_VISIBILITY
2560 pointer operator->() const _NOEXCEPT {
2561 return __ptr_.first();
2562 }
2563 _LIBCPP_INLINE_VISIBILITY
2564 pointer get() const _NOEXCEPT {
2565 return __ptr_.first();
2566 }
2567 _LIBCPP_INLINE_VISIBILITY
2568 deleter_type& get_deleter() _NOEXCEPT {
2569 return __ptr_.second();
2570 }
2571 _LIBCPP_INLINE_VISIBILITY
2572 const deleter_type& get_deleter() const _NOEXCEPT {
2573 return __ptr_.second();
2574 }
2575 _LIBCPP_INLINE_VISIBILITY
2576 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2577 return __ptr_.first() != nullptr;
2578 }
2579
2580 _LIBCPP_INLINE_VISIBILITY
2581 pointer release() _NOEXCEPT {
2582 pointer __t = __ptr_.first();
2583 __ptr_.first() = pointer();
2584 return __t;
2585 }
2586
2587 _LIBCPP_INLINE_VISIBILITY
2588 void reset(pointer __p = pointer()) _NOEXCEPT {
2589 pointer __tmp = __ptr_.first();
2590 __ptr_.first() = __p;
2591 if (__tmp)
2592 __ptr_.second()(__tmp);
2593 }
2594
2595 _LIBCPP_INLINE_VISIBILITY
2596 void swap(unique_ptr& __u) _NOEXCEPT {
2597 __ptr_.swap(__u.__ptr_);
2598 }
2599};
2600
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002601
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002603class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002605 typedef _Tp element_type;
2606 typedef _Dp deleter_type;
2607 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2608
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002610 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611
Eric Fiselier31127cd2017-04-16 02:14:31 +00002612 template <class _From>
2613 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614
Eric Fiselier31127cd2017-04-16 02:14:31 +00002615 template <class _FromElem>
2616 struct _CheckArrayPointerConversion<_FromElem*>
2617 : integral_constant<bool,
2618 is_same<_FromElem*, pointer>::value ||
2619 (is_same<pointer, element_type*>::value &&
2620 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2621 >
2622 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002624#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002625 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002626
2627 template <bool _Dummy>
2628 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002629 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002630
2631 template <bool _Dummy>
2632 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002633 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002634
2635 template <bool _Dummy>
2636 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002637 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002638
2639 template <bool _Dummy, class _Deleter = typename __dependent_type<
2640 __identity<deleter_type>, _Dummy>::type>
2641 using _EnableIfDeleterDefaultConstructible =
2642 typename enable_if<is_default_constructible<_Deleter>::value &&
2643 !is_pointer<_Deleter>::value>::type;
2644
2645 template <class _ArgType>
2646 using _EnableIfDeleterConstructible =
2647 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2648
2649 template <class _Pp>
2650 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002651 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002652 >::type;
2653
2654 template <class _UPtr, class _Up,
2655 class _ElemT = typename _UPtr::element_type>
2656 using _EnableIfMoveConvertible = typename enable_if<
2657 is_array<_Up>::value &&
2658 is_same<pointer, element_type*>::value &&
2659 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2660 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2661 >::type;
2662
2663 template <class _UDel>
2664 using _EnableIfDeleterConvertible = typename enable_if<
2665 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2666 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2667 >::type;
2668
2669 template <class _UDel>
2670 using _EnableIfDeleterAssignable = typename enable_if<
2671 is_assignable<_Dp&, _UDel&&>::value
2672 >::type;
2673
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002675 template <bool _Dummy = true,
2676 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2677 _LIBCPP_INLINE_VISIBILITY
2678 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002680 template <bool _Dummy = true,
2681 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2682 _LIBCPP_INLINE_VISIBILITY
2683 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002685 template <class _Pp, bool _Dummy = true,
2686 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2687 class = _EnableIfPointerConvertible<_Pp>>
2688 _LIBCPP_INLINE_VISIBILITY
2689 explicit unique_ptr(_Pp __p) noexcept
2690 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002692 template <class _Pp, bool _Dummy = true,
2693 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2694 class = _EnableIfPointerConvertible<_Pp>>
2695 _LIBCPP_INLINE_VISIBILITY
2696 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2697 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002699 template <bool _Dummy = true,
2700 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2701 _LIBCPP_INLINE_VISIBILITY
2702 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2703 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002705 template <class _Pp, bool _Dummy = true,
2706 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2707 class = _EnableIfPointerConvertible<_Pp>>
2708 _LIBCPP_INLINE_VISIBILITY
2709 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2710 : __ptr_(__p, _VSTD::move(__d)) {
2711 static_assert(!is_reference<deleter_type>::value,
2712 "rvalue deleter bound to reference");
2713 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 template <bool _Dummy = true,
2716 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2717 _LIBCPP_INLINE_VISIBILITY
2718 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2719 : __ptr_(nullptr, _VSTD::move(__d)) {
2720 static_assert(!is_reference<deleter_type>::value,
2721 "rvalue deleter bound to reference");
2722 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002723
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002724 template <class _Pp, bool _Dummy = true,
2725 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2726 class = _EnableIfPointerConvertible<_Pp>>
2727 _LIBCPP_INLINE_VISIBILITY
2728 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002729
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002730 _LIBCPP_INLINE_VISIBILITY
2731 unique_ptr(unique_ptr&& __u) noexcept
2732 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2733 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002734
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002735 _LIBCPP_INLINE_VISIBILITY
2736 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2737 reset(__u.release());
2738 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2739 return *this;
2740 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002742 template <class _Up, class _Ep,
2743 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2744 class = _EnableIfDeleterConvertible<_Ep>
2745 >
2746 _LIBCPP_INLINE_VISIBILITY
2747 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002748 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002749 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002751 template <class _Up, class _Ep,
2752 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2753 class = _EnableIfDeleterAssignable<_Ep>
2754 >
2755 _LIBCPP_INLINE_VISIBILITY
2756 unique_ptr&
2757 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2758 reset(__u.release());
2759 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2760 return *this;
2761 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002763#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002765 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002766
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002767 unique_ptr(unique_ptr&);
2768 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2769
2770 unique_ptr& operator=(unique_ptr&);
2771 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2772
2773 template <class _Up>
2774 unique_ptr(_Up __u,
2775 typename conditional<
2776 is_reference<deleter_type>::value, deleter_type,
2777 typename add_lvalue_reference<const deleter_type>::type>::type,
2778 typename enable_if<is_convertible<_Up, pointer>::value,
2779 __nat>::type = __nat());
2780public:
2781 _LIBCPP_INLINE_VISIBILITY
2782 unique_ptr() : __ptr_(pointer()) {
2783 static_assert(!is_pointer<deleter_type>::value,
2784 "unique_ptr constructed with null function pointer deleter");
2785 }
2786 _LIBCPP_INLINE_VISIBILITY
2787 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2788 static_assert(!is_pointer<deleter_type>::value,
2789 "unique_ptr constructed with null function pointer deleter");
2790 }
2791
2792 _LIBCPP_INLINE_VISIBILITY
2793 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2794 static_assert(!is_pointer<deleter_type>::value,
2795 "unique_ptr constructed with null function pointer deleter");
2796 }
2797
2798 _LIBCPP_INLINE_VISIBILITY
2799 unique_ptr(pointer __p, deleter_type __d)
2800 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2801
2802 _LIBCPP_INLINE_VISIBILITY
2803 unique_ptr(nullptr_t, deleter_type __d)
2804 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2805
2806 _LIBCPP_INLINE_VISIBILITY
2807 operator __rv<unique_ptr>() {
2808 return __rv<unique_ptr>(*this);
2809 }
2810
2811 _LIBCPP_INLINE_VISIBILITY
2812 unique_ptr(__rv<unique_ptr> __u)
2813 : __ptr_(__u->release(),
2814 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2815
2816 _LIBCPP_INLINE_VISIBILITY
2817 unique_ptr& operator=(__rv<unique_ptr> __u) {
2818 reset(__u->release());
2819 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2820 return *this;
2821 }
2822
2823#endif // _LIBCPP_CXX03_LANG
2824
2825public:
2826 _LIBCPP_INLINE_VISIBILITY
2827 ~unique_ptr() { reset(); }
2828
2829 _LIBCPP_INLINE_VISIBILITY
2830 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2831 reset();
2832 return *this;
2833 }
2834
2835 _LIBCPP_INLINE_VISIBILITY
2836 typename add_lvalue_reference<_Tp>::type
2837 operator[](size_t __i) const {
2838 return __ptr_.first()[__i];
2839 }
2840 _LIBCPP_INLINE_VISIBILITY
2841 pointer get() const _NOEXCEPT {
2842 return __ptr_.first();
2843 }
2844
2845 _LIBCPP_INLINE_VISIBILITY
2846 deleter_type& get_deleter() _NOEXCEPT {
2847 return __ptr_.second();
2848 }
2849
2850 _LIBCPP_INLINE_VISIBILITY
2851 const deleter_type& get_deleter() const _NOEXCEPT {
2852 return __ptr_.second();
2853 }
2854 _LIBCPP_INLINE_VISIBILITY
2855 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2856 return __ptr_.first() != nullptr;
2857 }
2858
2859 _LIBCPP_INLINE_VISIBILITY
2860 pointer release() _NOEXCEPT {
2861 pointer __t = __ptr_.first();
2862 __ptr_.first() = pointer();
2863 return __t;
2864 }
2865
2866 template <class _Pp>
2867 _LIBCPP_INLINE_VISIBILITY
2868 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002869 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002870 >::type
2871 reset(_Pp __p) _NOEXCEPT {
2872 pointer __tmp = __ptr_.first();
2873 __ptr_.first() = __p;
2874 if (__tmp)
2875 __ptr_.second()(__tmp);
2876 }
2877
2878 _LIBCPP_INLINE_VISIBILITY
2879 void reset(nullptr_t = nullptr) _NOEXCEPT {
2880 pointer __tmp = __ptr_.first();
2881 __ptr_.first() = nullptr;
2882 if (__tmp)
2883 __ptr_.second()(__tmp);
2884 }
2885
2886 _LIBCPP_INLINE_VISIBILITY
2887 void swap(unique_ptr& __u) _NOEXCEPT {
2888 __ptr_.swap(__u.__ptr_);
2889 }
2890
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891};
2892
2893template <class _Tp, class _Dp>
2894inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002895typename enable_if<
2896 __is_swappable<_Dp>::value,
2897 void
2898>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002899swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900
2901template <class _T1, class _D1, class _T2, class _D2>
2902inline _LIBCPP_INLINE_VISIBILITY
2903bool
2904operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2905
2906template <class _T1, class _D1, class _T2, class _D2>
2907inline _LIBCPP_INLINE_VISIBILITY
2908bool
2909operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2910
2911template <class _T1, class _D1, class _T2, class _D2>
2912inline _LIBCPP_INLINE_VISIBILITY
2913bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002914operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2915{
2916 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2917 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002918 typedef typename common_type<_P1, _P2>::type _Vp;
2919 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002920}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921
2922template <class _T1, class _D1, class _T2, class _D2>
2923inline _LIBCPP_INLINE_VISIBILITY
2924bool
2925operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2926
2927template <class _T1, class _D1, class _T2, class _D2>
2928inline _LIBCPP_INLINE_VISIBILITY
2929bool
2930operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2931
2932template <class _T1, class _D1, class _T2, class _D2>
2933inline _LIBCPP_INLINE_VISIBILITY
2934bool
2935operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2936
Howard Hinnantb17caf92012-02-21 21:02:58 +00002937template <class _T1, class _D1>
2938inline _LIBCPP_INLINE_VISIBILITY
2939bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002940operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002941{
2942 return !__x;
2943}
2944
2945template <class _T1, class _D1>
2946inline _LIBCPP_INLINE_VISIBILITY
2947bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002948operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002949{
2950 return !__x;
2951}
2952
2953template <class _T1, class _D1>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002956operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002957{
2958 return static_cast<bool>(__x);
2959}
2960
2961template <class _T1, class _D1>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002964operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002965{
2966 return static_cast<bool>(__x);
2967}
2968
2969template <class _T1, class _D1>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
2972operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2973{
2974 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2975 return less<_P1>()(__x.get(), nullptr);
2976}
2977
2978template <class _T1, class _D1>
2979inline _LIBCPP_INLINE_VISIBILITY
2980bool
2981operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2982{
2983 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2984 return less<_P1>()(nullptr, __x.get());
2985}
2986
2987template <class _T1, class _D1>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
2990operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2991{
2992 return nullptr < __x;
2993}
2994
2995template <class _T1, class _D1>
2996inline _LIBCPP_INLINE_VISIBILITY
2997bool
2998operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2999{
3000 return __x < nullptr;
3001}
3002
3003template <class _T1, class _D1>
3004inline _LIBCPP_INLINE_VISIBILITY
3005bool
3006operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3007{
3008 return !(nullptr < __x);
3009}
3010
3011template <class _T1, class _D1>
3012inline _LIBCPP_INLINE_VISIBILITY
3013bool
3014operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3015{
3016 return !(__x < nullptr);
3017}
3018
3019template <class _T1, class _D1>
3020inline _LIBCPP_INLINE_VISIBILITY
3021bool
3022operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3023{
3024 return !(__x < nullptr);
3025}
3026
3027template <class _T1, class _D1>
3028inline _LIBCPP_INLINE_VISIBILITY
3029bool
3030operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3031{
3032 return !(nullptr < __x);
3033}
3034
Howard Hinnant9e028f12012-05-01 15:37:54 +00003035#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3036
3037template <class _Tp, class _Dp>
3038inline _LIBCPP_INLINE_VISIBILITY
3039unique_ptr<_Tp, _Dp>
3040move(unique_ptr<_Tp, _Dp>& __t)
3041{
3042 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3043}
3044
3045#endif
3046
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003047#if _LIBCPP_STD_VER > 11
3048
3049template<class _Tp>
3050struct __unique_if
3051{
3052 typedef unique_ptr<_Tp> __unique_single;
3053};
3054
3055template<class _Tp>
3056struct __unique_if<_Tp[]>
3057{
3058 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3059};
3060
3061template<class _Tp, size_t _Np>
3062struct __unique_if<_Tp[_Np]>
3063{
3064 typedef void __unique_array_known_bound;
3065};
3066
3067template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003068inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003069typename __unique_if<_Tp>::__unique_single
3070make_unique(_Args&&... __args)
3071{
3072 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3073}
3074
3075template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003076inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003077typename __unique_if<_Tp>::__unique_array_unknown_bound
3078make_unique(size_t __n)
3079{
3080 typedef typename remove_extent<_Tp>::type _Up;
3081 return unique_ptr<_Tp>(new _Up[__n]());
3082}
3083
3084template<class _Tp, class... _Args>
3085 typename __unique_if<_Tp>::__unique_array_known_bound
3086 make_unique(_Args&&...) = delete;
3087
3088#endif // _LIBCPP_STD_VER > 11
3089
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003090template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003091#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003092struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003093#else
3094struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3095 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3096#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003097{
3098 typedef unique_ptr<_Tp, _Dp> argument_type;
3099 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003100 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003101 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003102 {
3103 typedef typename argument_type::pointer pointer;
3104 return hash<pointer>()(__ptr.get());
3105 }
3106};
3107
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108struct __destruct_n
3109{
3110private:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003111 size_t __size_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112
3113 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003114 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003115 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116
3117 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003118 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119 {}
3120
Howard Hinnant719bda32011-05-28 14:41:13 +00003121 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003122 {++__size_;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003123 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124 {}
3125
Howard Hinnant719bda32011-05-28 14:41:13 +00003126 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003127 {__size_ = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003128 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129 {}
3130public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003131 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00003132 : __size_(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133
3134 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003135 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003136 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137
3138 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003139 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003140 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141
3142 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003143 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003144 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145};
3146
3147template <class _Alloc>
3148class __allocator_destructor
3149{
3150 typedef allocator_traits<_Alloc> __alloc_traits;
3151public:
3152 typedef typename __alloc_traits::pointer pointer;
3153 typedef typename __alloc_traits::size_type size_type;
3154private:
3155 _Alloc& __alloc_;
3156 size_type __s_;
3157public:
3158 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003159 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003162 void operator()(pointer __p) _NOEXCEPT
3163 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164};
3165
3166template <class _InputIterator, class _ForwardIterator>
3167_ForwardIterator
3168uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3169{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003171#ifndef _LIBCPP_NO_EXCEPTIONS
3172 _ForwardIterator __s = __r;
3173 try
3174 {
3175#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003176 for (; __f != __l; ++__f, (void) ++__r)
3177 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003178#ifndef _LIBCPP_NO_EXCEPTIONS
3179 }
3180 catch (...)
3181 {
3182 for (; __s != __r; ++__s)
3183 __s->~value_type();
3184 throw;
3185 }
3186#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187 return __r;
3188}
3189
3190template <class _InputIterator, class _Size, class _ForwardIterator>
3191_ForwardIterator
3192uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3193{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003195#ifndef _LIBCPP_NO_EXCEPTIONS
3196 _ForwardIterator __s = __r;
3197 try
3198 {
3199#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003200 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3201 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003202#ifndef _LIBCPP_NO_EXCEPTIONS
3203 }
3204 catch (...)
3205 {
3206 for (; __s != __r; ++__s)
3207 __s->~value_type();
3208 throw;
3209 }
3210#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211 return __r;
3212}
3213
3214template <class _ForwardIterator, class _Tp>
3215void
3216uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3217{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003219#ifndef _LIBCPP_NO_EXCEPTIONS
3220 _ForwardIterator __s = __f;
3221 try
3222 {
3223#endif
3224 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003225 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003226#ifndef _LIBCPP_NO_EXCEPTIONS
3227 }
3228 catch (...)
3229 {
3230 for (; __s != __f; ++__s)
3231 __s->~value_type();
3232 throw;
3233 }
3234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235}
3236
3237template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003238_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003239uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3240{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003242#ifndef _LIBCPP_NO_EXCEPTIONS
3243 _ForwardIterator __s = __f;
3244 try
3245 {
3246#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003247 for (; __n > 0; ++__f, (void) --__n)
3248 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003249#ifndef _LIBCPP_NO_EXCEPTIONS
3250 }
3251 catch (...)
3252 {
3253 for (; __s != __f; ++__s)
3254 __s->~value_type();
3255 throw;
3256 }
3257#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003258 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259}
3260
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003261#if _LIBCPP_STD_VER > 14
3262
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003263template <class _Tp>
3264inline _LIBCPP_INLINE_VISIBILITY
3265void destroy_at(_Tp* __loc) {
3266 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3267 __loc->~_Tp();
3268}
3269
3270template <class _ForwardIterator>
3271inline _LIBCPP_INLINE_VISIBILITY
3272void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3273 for (; __first != __last; ++__first)
3274 _VSTD::destroy_at(_VSTD::addressof(*__first));
3275}
3276
3277template <class _ForwardIterator, class _Size>
3278inline _LIBCPP_INLINE_VISIBILITY
3279_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3280 for (; __n > 0; (void)++__first, --__n)
3281 _VSTD::destroy_at(_VSTD::addressof(*__first));
3282 return __first;
3283}
3284
Eric Fiselier290c07c2016-10-11 21:13:44 +00003285template <class _ForwardIterator>
3286inline _LIBCPP_INLINE_VISIBILITY
3287void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3288 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3289 auto __idx = __first;
3290#ifndef _LIBCPP_NO_EXCEPTIONS
3291 try {
3292#endif
3293 for (; __idx != __last; ++__idx)
3294 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3295#ifndef _LIBCPP_NO_EXCEPTIONS
3296 } catch (...) {
3297 _VSTD::destroy(__first, __idx);
3298 throw;
3299 }
3300#endif
3301}
3302
3303template <class _ForwardIterator, class _Size>
3304inline _LIBCPP_INLINE_VISIBILITY
3305_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3306 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3307 auto __idx = __first;
3308#ifndef _LIBCPP_NO_EXCEPTIONS
3309 try {
3310#endif
3311 for (; __n > 0; (void)++__idx, --__n)
3312 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3313 return __idx;
3314#ifndef _LIBCPP_NO_EXCEPTIONS
3315 } catch (...) {
3316 _VSTD::destroy(__first, __idx);
3317 throw;
3318 }
3319#endif
3320}
3321
3322
3323template <class _ForwardIterator>
3324inline _LIBCPP_INLINE_VISIBILITY
3325void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3326 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3327 auto __idx = __first;
3328#ifndef _LIBCPP_NO_EXCEPTIONS
3329 try {
3330#endif
3331 for (; __idx != __last; ++__idx)
3332 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3333#ifndef _LIBCPP_NO_EXCEPTIONS
3334 } catch (...) {
3335 _VSTD::destroy(__first, __idx);
3336 throw;
3337 }
3338#endif
3339}
3340
3341template <class _ForwardIterator, class _Size>
3342inline _LIBCPP_INLINE_VISIBILITY
3343_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3344 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3345 auto __idx = __first;
3346#ifndef _LIBCPP_NO_EXCEPTIONS
3347 try {
3348#endif
3349 for (; __n > 0; (void)++__idx, --__n)
3350 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3351 return __idx;
3352#ifndef _LIBCPP_NO_EXCEPTIONS
3353 } catch (...) {
3354 _VSTD::destroy(__first, __idx);
3355 throw;
3356 }
3357#endif
3358}
3359
3360
3361template <class _InputIt, class _ForwardIt>
3362inline _LIBCPP_INLINE_VISIBILITY
3363_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3364 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3365 auto __idx = __first_res;
3366#ifndef _LIBCPP_NO_EXCEPTIONS
3367 try {
3368#endif
3369 for (; __first != __last; (void)++__idx, ++__first)
3370 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3371 return __idx;
3372#ifndef _LIBCPP_NO_EXCEPTIONS
3373 } catch (...) {
3374 _VSTD::destroy(__first_res, __idx);
3375 throw;
3376 }
3377#endif
3378}
3379
3380template <class _InputIt, class _Size, class _ForwardIt>
3381inline _LIBCPP_INLINE_VISIBILITY
3382pair<_InputIt, _ForwardIt>
3383uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3384 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3385 auto __idx = __first_res;
3386#ifndef _LIBCPP_NO_EXCEPTIONS
3387 try {
3388#endif
3389 for (; __n > 0; ++__idx, (void)++__first, --__n)
3390 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3391 return {__first, __idx};
3392#ifndef _LIBCPP_NO_EXCEPTIONS
3393 } catch (...) {
3394 _VSTD::destroy(__first_res, __idx);
3395 throw;
3396 }
3397#endif
3398}
3399
3400
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003401#endif // _LIBCPP_STD_VER > 14
3402
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003403// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3404// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003405// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003406#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3407 && defined(__ATOMIC_RELAXED) \
3408 && defined(__ATOMIC_ACQ_REL)
3409# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3410#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3411# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3412#endif
3413
3414template <class _Tp>
3415inline _LIBCPP_INLINE_VISIBILITY _Tp
3416__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3417{
3418#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3419 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3420#else
3421 return __t += 1;
3422#endif
3423}
3424
3425template <class _Tp>
3426inline _LIBCPP_INLINE_VISIBILITY _Tp
3427__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3428{
3429#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3430 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3431#else
3432 return __t -= 1;
3433#endif
3434}
3435
Howard Hinnant756c69b2010-09-22 16:48:34 +00003436class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437 : public std::exception
3438{
3439public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003440 virtual ~bad_weak_ptr() _NOEXCEPT;
3441 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442};
3443
Marshall Clow8fea1612016-08-25 15:09:01 +00003444_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3445void __throw_bad_weak_ptr()
3446{
3447#ifndef _LIBCPP_NO_EXCEPTIONS
3448 throw bad_weak_ptr();
3449#else
3450 _VSTD::abort();
3451#endif
3452}
3453
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003454template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003456class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457{
3458 __shared_count(const __shared_count&);
3459 __shared_count& operator=(const __shared_count&);
3460
3461protected:
3462 long __shared_owners_;
3463 virtual ~__shared_count();
3464private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003465 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466
3467public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003469 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470 : __shared_owners_(__refs) {}
3471
Eric Fiselier98848572017-01-17 03:16:26 +00003472#if defined(_LIBCPP_BUILDING_MEMORY) && \
3473 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003474 void __add_shared() _NOEXCEPT;
3475 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003476#else
3477 _LIBCPP_INLINE_VISIBILITY
3478 void __add_shared() _NOEXCEPT {
3479 __libcpp_atomic_refcount_increment(__shared_owners_);
3480 }
3481 _LIBCPP_INLINE_VISIBILITY
3482 bool __release_shared() _NOEXCEPT {
3483 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3484 __on_zero_shared();
3485 return true;
3486 }
3487 return false;
3488 }
3489#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003490 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003491 long use_count() const _NOEXCEPT {
3492 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3493 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494};
3495
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003496class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497 : private __shared_count
3498{
3499 long __shared_weak_owners_;
3500
3501public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003503 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504 : __shared_count(__refs),
3505 __shared_weak_owners_(__refs) {}
3506protected:
3507 virtual ~__shared_weak_count();
3508
3509public:
Eric Fiselier98848572017-01-17 03:16:26 +00003510#if defined(_LIBCPP_BUILDING_MEMORY) && \
3511 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003512 void __add_shared() _NOEXCEPT;
3513 void __add_weak() _NOEXCEPT;
3514 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003515#else
3516 _LIBCPP_INLINE_VISIBILITY
3517 void __add_shared() _NOEXCEPT {
3518 __shared_count::__add_shared();
3519 }
3520 _LIBCPP_INLINE_VISIBILITY
3521 void __add_weak() _NOEXCEPT {
3522 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3523 }
3524 _LIBCPP_INLINE_VISIBILITY
3525 void __release_shared() _NOEXCEPT {
3526 if (__shared_count::__release_shared())
3527 __release_weak();
3528 }
3529#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003530 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003532 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3533 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534
Howard Hinnant807d6332013-02-25 15:50:36 +00003535 // Define the function out only if we build static libc++ without RTTI.
3536 // Otherwise we may break clients who need to compile their projects with
3537 // -fno-rtti and yet link against a libc++.dylib compiled
3538 // without -fno-rtti.
3539#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003540 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003541#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003543 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544};
3545
3546template <class _Tp, class _Dp, class _Alloc>
3547class __shared_ptr_pointer
3548 : public __shared_weak_count
3549{
3550 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3551public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003554 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555
Howard Hinnant72f73582010-08-11 17:04:31 +00003556#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003557 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003558#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559
3560private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003561 virtual void __on_zero_shared() _NOEXCEPT;
3562 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563};
3564
Howard Hinnant72f73582010-08-11 17:04:31 +00003565#ifndef _LIBCPP_NO_RTTI
3566
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567template <class _Tp, class _Dp, class _Alloc>
3568const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003569__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
Eric Fiselierc7490d02017-05-04 01:06:56 +00003571 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572}
3573
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003574#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003575
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576template <class _Tp, class _Dp, class _Alloc>
3577void
Howard Hinnant719bda32011-05-28 14:41:13 +00003578__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579{
3580 __data_.first().second()(__data_.first().first());
3581 __data_.first().second().~_Dp();
3582}
3583
3584template <class _Tp, class _Dp, class _Alloc>
3585void
Howard Hinnant719bda32011-05-28 14:41:13 +00003586__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003588 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3589 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003590 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3591
Eric Fiselierf8898c82015-02-05 23:01:40 +00003592 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003594 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595}
3596
3597template <class _Tp, class _Alloc>
3598class __shared_ptr_emplace
3599 : public __shared_weak_count
3600{
3601 __compressed_pair<_Alloc, _Tp> __data_;
3602public:
3603#ifndef _LIBCPP_HAS_NO_VARIADICS
3604
Howard Hinnant756c69b2010-09-22 16:48:34 +00003605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003607 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608
3609 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003612 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3613 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614
3615#else // _LIBCPP_HAS_NO_VARIADICS
3616
Howard Hinnant756c69b2010-09-22 16:48:34 +00003617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003618 __shared_ptr_emplace(_Alloc __a)
3619 : __data_(__a) {}
3620
3621 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3624 : __data_(__a, _Tp(__a0)) {}
3625
3626 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3629 : __data_(__a, _Tp(__a0, __a1)) {}
3630
3631 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3634 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3635
3636#endif // _LIBCPP_HAS_NO_VARIADICS
3637
3638private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003639 virtual void __on_zero_shared() _NOEXCEPT;
3640 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003643 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644};
3645
3646template <class _Tp, class _Alloc>
3647void
Howard Hinnant719bda32011-05-28 14:41:13 +00003648__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
3650 __data_.second().~_Tp();
3651}
3652
3653template <class _Tp, class _Alloc>
3654void
Howard Hinnant719bda32011-05-28 14:41:13 +00003655__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003657 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3658 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003659 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003660 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003662 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663}
3664
Erik Pilkington2a398762017-05-25 15:43:31 +00003665struct __shared_ptr_dummy_rebind_allocator_type;
3666template <>
3667class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3668{
3669public:
3670 template <class _Other>
3671 struct rebind
3672 {
3673 typedef allocator<_Other> other;
3674 };
3675};
3676
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003677template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678
3679template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003680class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003682public:
3683 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003684
Eric Fiselierae5b6672016-06-27 01:02:43 +00003685#if _LIBCPP_STD_VER > 14
3686 typedef weak_ptr<_Tp> weak_type;
3687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688private:
3689 element_type* __ptr_;
3690 __shared_weak_count* __cntrl_;
3691
3692 struct __nat {int __for_bool_;};
3693public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003695 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003697 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003698 template<class _Yp>
3699 explicit shared_ptr(_Yp* __p,
3700 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3701 template<class _Yp, class _Dp>
3702 shared_ptr(_Yp* __p, _Dp __d,
3703 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3704 template<class _Yp, class _Dp, class _Alloc>
3705 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3706 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3708 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003709 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003711 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003715 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003716 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003717#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003719 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003720 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003721 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003722 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003723#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003725 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003726#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003727#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003728 template<class _Yp>
3729 shared_ptr(auto_ptr<_Yp>&& __r,
3730 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003731#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003732 template<class _Yp>
3733 shared_ptr(auto_ptr<_Yp> __r,
3734 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003736#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003737#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003738 template <class _Yp, class _Dp>
3739 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3740 typename enable_if
3741 <
3742 !is_lvalue_reference<_Dp>::value &&
3743 !is_array<_Yp>::value &&
3744 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3745 __nat
3746 >::type = __nat());
3747 template <class _Yp, class _Dp>
3748 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3749 typename enable_if
3750 <
3751 is_lvalue_reference<_Dp>::value &&
3752 !is_array<_Yp>::value &&
3753 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3754 __nat
3755 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003756#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003757 template <class _Yp, class _Dp>
3758 shared_ptr(unique_ptr<_Yp, _Dp>,
3759 typename enable_if
3760 <
3761 !is_lvalue_reference<_Dp>::value &&
3762 !is_array<_Yp>::value &&
3763 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3764 __nat
3765 >::type = __nat());
3766 template <class _Yp, class _Dp>
3767 shared_ptr(unique_ptr<_Yp, _Dp>,
3768 typename enable_if
3769 <
3770 is_lvalue_reference<_Dp>::value &&
3771 !is_array<_Yp>::value &&
3772 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3773 __nat
3774 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003775#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776
3777 ~shared_ptr();
3778
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003780 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003781 template<class _Yp>
3782 typename enable_if
3783 <
3784 is_convertible<_Yp*, element_type*>::value,
3785 shared_ptr&
3786 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003788 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003791 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003792 template<class _Yp>
3793 typename enable_if
3794 <
3795 is_convertible<_Yp*, element_type*>::value,
3796 shared_ptr<_Tp>&
3797 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003799 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003800#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003801 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003803 typename enable_if
3804 <
3805 !is_array<_Yp>::value &&
3806 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003807 shared_ptr
3808 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003809 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003810#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003811#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003812#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003813 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003815 typename enable_if
3816 <
3817 !is_array<_Yp>::value &&
3818 is_convertible<_Yp*, element_type*>::value,
3819 shared_ptr&
3820 >::type
3821 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003823#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003824 template <class _Yp, class _Dp>
3825 typename enable_if
3826 <
3827 !is_array<_Yp>::value &&
3828 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3829 shared_ptr&
3830 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003831#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003833 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003834#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003836 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837#endif
3838
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003840 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003842 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003843 template<class _Yp>
3844 typename enable_if
3845 <
3846 is_convertible<_Yp*, element_type*>::value,
3847 void
3848 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003850 reset(_Yp* __p);
3851 template<class _Yp, class _Dp>
3852 typename enable_if
3853 <
3854 is_convertible<_Yp*, element_type*>::value,
3855 void
3856 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003858 reset(_Yp* __p, _Dp __d);
3859 template<class _Yp, class _Dp, class _Alloc>
3860 typename enable_if
3861 <
3862 is_convertible<_Yp*, element_type*>::value,
3863 void
3864 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003866 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867
Howard Hinnant756c69b2010-09-22 16:48:34 +00003868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003869 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003871 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3872 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003874 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003876 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003878 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003880 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003881 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003882 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003883 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003885 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003886 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003887 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003888 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003889 _LIBCPP_INLINE_VISIBILITY
3890 bool
3891 __owner_equivalent(const shared_ptr& __p) const
3892 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893
Howard Hinnant72f73582010-08-11 17:04:31 +00003894#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003897 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow31350ab2017-06-14 16:54:43 +00003898 {return static_cast<_Dp*>(__cntrl_
Aditya Kumar7c5db692017-08-20 10:38:55 +00003899 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow31350ab2017-06-14 16:54:43 +00003900 : nullptr);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003901#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003902
3903#ifndef _LIBCPP_HAS_NO_VARIADICS
3904
3905 template<class ..._Args>
3906 static
3907 shared_ptr<_Tp>
3908 make_shared(_Args&& ...__args);
3909
3910 template<class _Alloc, class ..._Args>
3911 static
3912 shared_ptr<_Tp>
3913 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3914
3915#else // _LIBCPP_HAS_NO_VARIADICS
3916
3917 static shared_ptr<_Tp> make_shared();
3918
3919 template<class _A0>
3920 static shared_ptr<_Tp> make_shared(_A0&);
3921
3922 template<class _A0, class _A1>
3923 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3924
3925 template<class _A0, class _A1, class _A2>
3926 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3927
3928 template<class _Alloc>
3929 static shared_ptr<_Tp>
3930 allocate_shared(const _Alloc& __a);
3931
3932 template<class _Alloc, class _A0>
3933 static shared_ptr<_Tp>
3934 allocate_shared(const _Alloc& __a, _A0& __a0);
3935
3936 template<class _Alloc, class _A0, class _A1>
3937 static shared_ptr<_Tp>
3938 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3939
3940 template<class _Alloc, class _A0, class _A1, class _A2>
3941 static shared_ptr<_Tp>
3942 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3943
3944#endif // _LIBCPP_HAS_NO_VARIADICS
3945
3946private:
Erik Pilkington2a398762017-05-25 15:43:31 +00003947 template <class _Yp, bool = is_function<_Yp>::value>
3948 struct __shared_ptr_default_allocator
3949 {
3950 typedef allocator<_Yp> type;
3951 };
3952
3953 template <class _Yp>
3954 struct __shared_ptr_default_allocator<_Yp, true>
3955 {
3956 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
3957 };
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003959 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003960 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003961 typename enable_if<is_convertible<_OrigPtr*,
3962 const enable_shared_from_this<_Yp>*
3963 >::value,
3964 void>::type
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003965 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3966 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003968 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003969 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003970 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003971 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3972 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003973 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 }
3975
Erik Pilkington2a398762017-05-25 15:43:31 +00003976 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003978 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3979 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980};
3981
Eric Fiselier30d5ac62017-05-10 19:35:49 +00003982
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003984inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003985_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003986shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 : __ptr_(0),
3988 __cntrl_(0)
3989{
3990}
3991
3992template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003993inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003994_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003995shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996 : __ptr_(0),
3997 __cntrl_(0)
3998{
3999}
4000
4001template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004002template<class _Yp>
4003shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4004 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005 : __ptr_(__p)
4006{
4007 unique_ptr<_Yp> __hold(__p);
Erik Pilkington2a398762017-05-25 15:43:31 +00004008 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4009 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4010 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004012 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013}
4014
4015template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004016template<class _Yp, class _Dp>
4017shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4018 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019 : __ptr_(__p)
4020{
4021#ifndef _LIBCPP_NO_EXCEPTIONS
4022 try
4023 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004024#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004025 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4026 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4027 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004028 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029#ifndef _LIBCPP_NO_EXCEPTIONS
4030 }
4031 catch (...)
4032 {
4033 __d(__p);
4034 throw;
4035 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004036#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037}
4038
4039template<class _Tp>
4040template<class _Dp>
4041shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4042 : __ptr_(0)
4043{
4044#ifndef _LIBCPP_NO_EXCEPTIONS
4045 try
4046 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004047#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkington2a398762017-05-25 15:43:31 +00004048 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4049 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4050 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051#ifndef _LIBCPP_NO_EXCEPTIONS
4052 }
4053 catch (...)
4054 {
4055 __d(__p);
4056 throw;
4057 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004058#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059}
4060
4061template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004062template<class _Yp, class _Dp, class _Alloc>
4063shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4064 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065 : __ptr_(__p)
4066{
4067#ifndef _LIBCPP_NO_EXCEPTIONS
4068 try
4069 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004070#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004072 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073 typedef __allocator_destructor<_A2> _D2;
4074 _A2 __a2(__a);
4075 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004076 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4077 _CntrlBlk(__p, __d, __a);
4078 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004079 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080#ifndef _LIBCPP_NO_EXCEPTIONS
4081 }
4082 catch (...)
4083 {
4084 __d(__p);
4085 throw;
4086 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004087#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088}
4089
4090template<class _Tp>
4091template<class _Dp, class _Alloc>
4092shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4093 : __ptr_(0)
4094{
4095#ifndef _LIBCPP_NO_EXCEPTIONS
4096 try
4097 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004098#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004100 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101 typedef __allocator_destructor<_A2> _D2;
4102 _A2 __a2(__a);
4103 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004104 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4105 _CntrlBlk(__p, __d, __a);
4106 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107#ifndef _LIBCPP_NO_EXCEPTIONS
4108 }
4109 catch (...)
4110 {
4111 __d(__p);
4112 throw;
4113 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004114#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115}
4116
4117template<class _Tp>
4118template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004119inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004120shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121 : __ptr_(__p),
4122 __cntrl_(__r.__cntrl_)
4123{
4124 if (__cntrl_)
4125 __cntrl_->__add_shared();
4126}
4127
4128template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004129inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004130shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131 : __ptr_(__r.__ptr_),
4132 __cntrl_(__r.__cntrl_)
4133{
4134 if (__cntrl_)
4135 __cntrl_->__add_shared();
4136}
4137
4138template<class _Tp>
4139template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004142 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004143 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144 : __ptr_(__r.__ptr_),
4145 __cntrl_(__r.__cntrl_)
4146{
4147 if (__cntrl_)
4148 __cntrl_->__add_shared();
4149}
4150
Howard Hinnant74279a52010-09-04 23:28:19 +00004151#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152
4153template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004154inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004155shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156 : __ptr_(__r.__ptr_),
4157 __cntrl_(__r.__cntrl_)
4158{
4159 __r.__ptr_ = 0;
4160 __r.__cntrl_ = 0;
4161}
4162
4163template<class _Tp>
4164template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004167 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004168 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 : __ptr_(__r.__ptr_),
4170 __cntrl_(__r.__cntrl_)
4171{
4172 __r.__ptr_ = 0;
4173 __r.__cntrl_ = 0;
4174}
4175
Howard Hinnant74279a52010-09-04 23:28:19 +00004176#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177
Marshall Clowb22274f2017-01-24 22:22:33 +00004178#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004180template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004181#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004182shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004184shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004186 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187 : __ptr_(__r.get())
4188{
4189 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4190 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004191 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192 __r.release();
4193}
Marshall Clowb22274f2017-01-24 22:22:33 +00004194#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195
4196template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004197template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004198#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4200#else
4201shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4202#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004203 typename enable_if
4204 <
4205 !is_lvalue_reference<_Dp>::value &&
4206 !is_array<_Yp>::value &&
4207 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4208 __nat
4209 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210 : __ptr_(__r.get())
4211{
Marshall Clow35cde742015-05-10 13:59:45 +00004212#if _LIBCPP_STD_VER > 11
4213 if (__ptr_ == nullptr)
4214 __cntrl_ = nullptr;
4215 else
4216#endif
4217 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004218 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4219 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4220 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004221 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004222 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223 __r.release();
4224}
4225
4226template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004227template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004228#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4230#else
4231shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4232#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004233 typename enable_if
4234 <
4235 is_lvalue_reference<_Dp>::value &&
4236 !is_array<_Yp>::value &&
4237 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4238 __nat
4239 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004240 : __ptr_(__r.get())
4241{
Marshall Clow35cde742015-05-10 13:59:45 +00004242#if _LIBCPP_STD_VER > 11
4243 if (__ptr_ == nullptr)
4244 __cntrl_ = nullptr;
4245 else
4246#endif
4247 {
Erik Pilkington2a398762017-05-25 15:43:31 +00004248 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow35cde742015-05-10 13:59:45 +00004249 typedef __shared_ptr_pointer<_Yp*,
4250 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkington2a398762017-05-25 15:43:31 +00004251 _AllocT > _CntrlBlk;
4252 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004253 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004254 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255 __r.release();
4256}
4257
4258#ifndef _LIBCPP_HAS_NO_VARIADICS
4259
4260template<class _Tp>
4261template<class ..._Args>
4262shared_ptr<_Tp>
4263shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4264{
4265 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4266 typedef allocator<_CntrlBlk> _A2;
4267 typedef __allocator_destructor<_A2> _D2;
4268 _A2 __a2;
4269 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004270 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271 shared_ptr<_Tp> __r;
4272 __r.__ptr_ = __hold2.get()->get();
4273 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004274 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275 return __r;
4276}
4277
4278template<class _Tp>
4279template<class _Alloc, class ..._Args>
4280shared_ptr<_Tp>
4281shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4282{
4283 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004284 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285 typedef __allocator_destructor<_A2> _D2;
4286 _A2 __a2(__a);
4287 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004288 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4289 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290 shared_ptr<_Tp> __r;
4291 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004292 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004293 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294 return __r;
4295}
4296
4297#else // _LIBCPP_HAS_NO_VARIADICS
4298
4299template<class _Tp>
4300shared_ptr<_Tp>
4301shared_ptr<_Tp>::make_shared()
4302{
4303 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4304 typedef allocator<_CntrlBlk> _Alloc2;
4305 typedef __allocator_destructor<_Alloc2> _D2;
4306 _Alloc2 __alloc2;
4307 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4308 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4309 shared_ptr<_Tp> __r;
4310 __r.__ptr_ = __hold2.get()->get();
4311 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004312 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313 return __r;
4314}
4315
4316template<class _Tp>
4317template<class _A0>
4318shared_ptr<_Tp>
4319shared_ptr<_Tp>::make_shared(_A0& __a0)
4320{
4321 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4322 typedef allocator<_CntrlBlk> _Alloc2;
4323 typedef __allocator_destructor<_Alloc2> _D2;
4324 _Alloc2 __alloc2;
4325 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4326 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4327 shared_ptr<_Tp> __r;
4328 __r.__ptr_ = __hold2.get()->get();
4329 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004330 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331 return __r;
4332}
4333
4334template<class _Tp>
4335template<class _A0, class _A1>
4336shared_ptr<_Tp>
4337shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4338{
4339 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4340 typedef allocator<_CntrlBlk> _Alloc2;
4341 typedef __allocator_destructor<_Alloc2> _D2;
4342 _Alloc2 __alloc2;
4343 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4344 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4345 shared_ptr<_Tp> __r;
4346 __r.__ptr_ = __hold2.get()->get();
4347 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004348 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349 return __r;
4350}
4351
4352template<class _Tp>
4353template<class _A0, class _A1, class _A2>
4354shared_ptr<_Tp>
4355shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4356{
4357 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4358 typedef allocator<_CntrlBlk> _Alloc2;
4359 typedef __allocator_destructor<_Alloc2> _D2;
4360 _Alloc2 __alloc2;
4361 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4362 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4363 shared_ptr<_Tp> __r;
4364 __r.__ptr_ = __hold2.get()->get();
4365 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004366 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367 return __r;
4368}
4369
4370template<class _Tp>
4371template<class _Alloc>
4372shared_ptr<_Tp>
4373shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4374{
4375 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004376 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 typedef __allocator_destructor<_Alloc2> _D2;
4378 _Alloc2 __alloc2(__a);
4379 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004380 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4381 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382 shared_ptr<_Tp> __r;
4383 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004384 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004385 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386 return __r;
4387}
4388
4389template<class _Tp>
4390template<class _Alloc, class _A0>
4391shared_ptr<_Tp>
4392shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4393{
4394 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004395 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396 typedef __allocator_destructor<_Alloc2> _D2;
4397 _Alloc2 __alloc2(__a);
4398 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004399 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4400 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004401 shared_ptr<_Tp> __r;
4402 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004403 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004404 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405 return __r;
4406}
4407
4408template<class _Tp>
4409template<class _Alloc, class _A0, class _A1>
4410shared_ptr<_Tp>
4411shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4412{
4413 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004414 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 typedef __allocator_destructor<_Alloc2> _D2;
4416 _Alloc2 __alloc2(__a);
4417 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004418 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4419 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004420 shared_ptr<_Tp> __r;
4421 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004422 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004423 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424 return __r;
4425}
4426
4427template<class _Tp>
4428template<class _Alloc, class _A0, class _A1, class _A2>
4429shared_ptr<_Tp>
4430shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4431{
4432 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004433 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 typedef __allocator_destructor<_Alloc2> _D2;
4435 _Alloc2 __alloc2(__a);
4436 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004437 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4438 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439 shared_ptr<_Tp> __r;
4440 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004441 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004442 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443 return __r;
4444}
4445
4446#endif // _LIBCPP_HAS_NO_VARIADICS
4447
4448template<class _Tp>
4449shared_ptr<_Tp>::~shared_ptr()
4450{
4451 if (__cntrl_)
4452 __cntrl_->__release_shared();
4453}
4454
4455template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004456inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004458shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459{
4460 shared_ptr(__r).swap(*this);
4461 return *this;
4462}
4463
4464template<class _Tp>
4465template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004466inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004467typename enable_if
4468<
Marshall Clow7e384b72017-01-10 16:59:33 +00004469 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004470 shared_ptr<_Tp>&
4471>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004472shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473{
4474 shared_ptr(__r).swap(*this);
4475 return *this;
4476}
4477
Howard Hinnant74279a52010-09-04 23:28:19 +00004478#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479
4480template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004481inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004483shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004485 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486 return *this;
4487}
4488
4489template<class _Tp>
4490template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004491inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004492typename enable_if
4493<
Marshall Clow7e384b72017-01-10 16:59:33 +00004494 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004495 shared_ptr<_Tp>&
4496>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4498{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004499 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004500 return *this;
4501}
4502
Marshall Clowb22274f2017-01-24 22:22:33 +00004503#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504template<class _Tp>
4505template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004506inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004507typename enable_if
4508<
4509 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004510 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004511 shared_ptr<_Tp>
4512>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004513shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4514{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004515 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004516 return *this;
4517}
Marshall Clowb22274f2017-01-24 22:22:33 +00004518#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519
4520template<class _Tp>
4521template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004522inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004523typename enable_if
4524<
4525 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004526 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clow7e384b72017-01-10 16:59:33 +00004527 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004528 shared_ptr<_Tp>&
4529>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4531{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004532 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533 return *this;
4534}
4535
Howard Hinnant74279a52010-09-04 23:28:19 +00004536#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004537
Marshall Clowb22274f2017-01-24 22:22:33 +00004538#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539template<class _Tp>
4540template<class _Yp>
4541inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004542typename enable_if
4543<
4544 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004545 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004546 shared_ptr<_Tp>&
4547>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004548shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549{
4550 shared_ptr(__r).swap(*this);
4551 return *this;
4552}
Marshall Clowb22274f2017-01-24 22:22:33 +00004553#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554
4555template<class _Tp>
4556template <class _Yp, class _Dp>
4557inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004558typename enable_if
4559<
4560 !is_array<_Yp>::value &&
Aditya Kumar7c5db692017-08-20 10:38:55 +00004561 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowb89a86e2017-01-10 18:40:01 +00004562 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004563 shared_ptr<_Tp>&
4564>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4566{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004567 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568 return *this;
4569}
4570
Howard Hinnant74279a52010-09-04 23:28:19 +00004571#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004572
4573template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004574inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004575void
Howard Hinnant719bda32011-05-28 14:41:13 +00004576shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004578 _VSTD::swap(__ptr_, __r.__ptr_);
4579 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004580}
4581
4582template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004583inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584void
Howard Hinnant719bda32011-05-28 14:41:13 +00004585shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004586{
4587 shared_ptr().swap(*this);
4588}
4589
4590template<class _Tp>
4591template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004592inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004593typename enable_if
4594<
Marshall Clow7e384b72017-01-10 16:59:33 +00004595 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004596 void
4597>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004598shared_ptr<_Tp>::reset(_Yp* __p)
4599{
4600 shared_ptr(__p).swap(*this);
4601}
4602
4603template<class _Tp>
4604template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004605inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004606typename enable_if
4607<
Marshall Clow7e384b72017-01-10 16:59:33 +00004608 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004609 void
4610>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4612{
4613 shared_ptr(__p, __d).swap(*this);
4614}
4615
4616template<class _Tp>
4617template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004618inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004619typename enable_if
4620<
Marshall Clow7e384b72017-01-10 16:59:33 +00004621 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004622 void
4623>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004624shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4625{
4626 shared_ptr(__p, __d, __a).swap(*this);
4627}
4628
4629#ifndef _LIBCPP_HAS_NO_VARIADICS
4630
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004631template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004633typename enable_if
4634<
4635 !is_array<_Tp>::value,
4636 shared_ptr<_Tp>
4637>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004638make_shared(_Args&& ...__args)
4639{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004640 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641}
4642
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004643template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004645typename enable_if
4646<
4647 !is_array<_Tp>::value,
4648 shared_ptr<_Tp>
4649>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004650allocate_shared(const _Alloc& __a, _Args&& ...__args)
4651{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004652 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004653}
4654
4655#else // _LIBCPP_HAS_NO_VARIADICS
4656
4657template<class _Tp>
4658inline _LIBCPP_INLINE_VISIBILITY
4659shared_ptr<_Tp>
4660make_shared()
4661{
4662 return shared_ptr<_Tp>::make_shared();
4663}
4664
4665template<class _Tp, class _A0>
4666inline _LIBCPP_INLINE_VISIBILITY
4667shared_ptr<_Tp>
4668make_shared(_A0& __a0)
4669{
4670 return shared_ptr<_Tp>::make_shared(__a0);
4671}
4672
4673template<class _Tp, class _A0, class _A1>
4674inline _LIBCPP_INLINE_VISIBILITY
4675shared_ptr<_Tp>
4676make_shared(_A0& __a0, _A1& __a1)
4677{
4678 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4679}
4680
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004681template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004682inline _LIBCPP_INLINE_VISIBILITY
4683shared_ptr<_Tp>
4684make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4685{
4686 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4687}
4688
4689template<class _Tp, class _Alloc>
4690inline _LIBCPP_INLINE_VISIBILITY
4691shared_ptr<_Tp>
4692allocate_shared(const _Alloc& __a)
4693{
4694 return shared_ptr<_Tp>::allocate_shared(__a);
4695}
4696
4697template<class _Tp, class _Alloc, class _A0>
4698inline _LIBCPP_INLINE_VISIBILITY
4699shared_ptr<_Tp>
4700allocate_shared(const _Alloc& __a, _A0& __a0)
4701{
4702 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4703}
4704
4705template<class _Tp, class _Alloc, class _A0, class _A1>
4706inline _LIBCPP_INLINE_VISIBILITY
4707shared_ptr<_Tp>
4708allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4709{
4710 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4711}
4712
4713template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4714inline _LIBCPP_INLINE_VISIBILITY
4715shared_ptr<_Tp>
4716allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4717{
4718 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4719}
4720
4721#endif // _LIBCPP_HAS_NO_VARIADICS
4722
4723template<class _Tp, class _Up>
4724inline _LIBCPP_INLINE_VISIBILITY
4725bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004726operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004727{
4728 return __x.get() == __y.get();
4729}
4730
4731template<class _Tp, class _Up>
4732inline _LIBCPP_INLINE_VISIBILITY
4733bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004734operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004735{
4736 return !(__x == __y);
4737}
4738
4739template<class _Tp, class _Up>
4740inline _LIBCPP_INLINE_VISIBILITY
4741bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004742operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004743{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004744 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4745 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004746}
4747
4748template<class _Tp, class _Up>
4749inline _LIBCPP_INLINE_VISIBILITY
4750bool
4751operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4752{
4753 return __y < __x;
4754}
4755
4756template<class _Tp, class _Up>
4757inline _LIBCPP_INLINE_VISIBILITY
4758bool
4759operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4760{
4761 return !(__y < __x);
4762}
4763
4764template<class _Tp, class _Up>
4765inline _LIBCPP_INLINE_VISIBILITY
4766bool
4767operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4768{
4769 return !(__x < __y);
4770}
4771
4772template<class _Tp>
4773inline _LIBCPP_INLINE_VISIBILITY
4774bool
4775operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4776{
4777 return !__x;
4778}
4779
4780template<class _Tp>
4781inline _LIBCPP_INLINE_VISIBILITY
4782bool
4783operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4784{
4785 return !__x;
4786}
4787
4788template<class _Tp>
4789inline _LIBCPP_INLINE_VISIBILITY
4790bool
4791operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4792{
4793 return static_cast<bool>(__x);
4794}
4795
4796template<class _Tp>
4797inline _LIBCPP_INLINE_VISIBILITY
4798bool
4799operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4800{
4801 return static_cast<bool>(__x);
4802}
4803
4804template<class _Tp>
4805inline _LIBCPP_INLINE_VISIBILITY
4806bool
4807operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4808{
4809 return less<_Tp*>()(__x.get(), nullptr);
4810}
4811
4812template<class _Tp>
4813inline _LIBCPP_INLINE_VISIBILITY
4814bool
4815operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4816{
4817 return less<_Tp*>()(nullptr, __x.get());
4818}
4819
4820template<class _Tp>
4821inline _LIBCPP_INLINE_VISIBILITY
4822bool
4823operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4824{
4825 return nullptr < __x;
4826}
4827
4828template<class _Tp>
4829inline _LIBCPP_INLINE_VISIBILITY
4830bool
4831operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4832{
4833 return __x < nullptr;
4834}
4835
4836template<class _Tp>
4837inline _LIBCPP_INLINE_VISIBILITY
4838bool
4839operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4840{
4841 return !(nullptr < __x);
4842}
4843
4844template<class _Tp>
4845inline _LIBCPP_INLINE_VISIBILITY
4846bool
4847operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4848{
4849 return !(__x < nullptr);
4850}
4851
4852template<class _Tp>
4853inline _LIBCPP_INLINE_VISIBILITY
4854bool
4855operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4856{
4857 return !(__x < nullptr);
4858}
4859
4860template<class _Tp>
4861inline _LIBCPP_INLINE_VISIBILITY
4862bool
4863operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4864{
4865 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004866}
4867
4868template<class _Tp>
4869inline _LIBCPP_INLINE_VISIBILITY
4870void
Howard Hinnant719bda32011-05-28 14:41:13 +00004871swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004872{
4873 __x.swap(__y);
4874}
4875
4876template<class _Tp, class _Up>
4877inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004878typename enable_if
4879<
4880 !is_array<_Tp>::value && !is_array<_Up>::value,
4881 shared_ptr<_Tp>
4882>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004883static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884{
4885 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4886}
4887
4888template<class _Tp, class _Up>
4889inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004890typename enable_if
4891<
4892 !is_array<_Tp>::value && !is_array<_Up>::value,
4893 shared_ptr<_Tp>
4894>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004895dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004896{
4897 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4898 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4899}
4900
4901template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004902typename enable_if
4903<
4904 is_array<_Tp>::value == is_array<_Up>::value,
4905 shared_ptr<_Tp>
4906>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004907const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004909 typedef typename remove_extent<_Tp>::type _RTp;
4910 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911}
4912
Howard Hinnant72f73582010-08-11 17:04:31 +00004913#ifndef _LIBCPP_NO_RTTI
4914
Howard Hinnantc51e1022010-05-11 19:42:16 +00004915template<class _Dp, class _Tp>
4916inline _LIBCPP_INLINE_VISIBILITY
4917_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004918get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004919{
4920 return __p.template __get_deleter<_Dp>();
4921}
4922
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004923#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004924
Howard Hinnantc51e1022010-05-11 19:42:16 +00004925template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004926class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004927{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004928public:
4929 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004930private:
4931 element_type* __ptr_;
4932 __shared_weak_count* __cntrl_;
4933
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004934public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004936 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004937 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004938 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4939 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004941 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004942 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004943 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4944 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004945
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004946#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004948 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004949 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004950 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4951 _NOEXCEPT;
4952#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004953 ~weak_ptr();
4954
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004956 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004957 template<class _Yp>
4958 typename enable_if
4959 <
4960 is_convertible<_Yp*, element_type*>::value,
4961 weak_ptr&
4962 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004964 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4965
4966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4967
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004969 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4970 template<class _Yp>
4971 typename enable_if
4972 <
4973 is_convertible<_Yp*, element_type*>::value,
4974 weak_ptr&
4975 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004977 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4978
4979#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4980
4981 template<class _Yp>
4982 typename enable_if
4983 <
4984 is_convertible<_Yp*, element_type*>::value,
4985 weak_ptr&
4986 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004988 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004989
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004991 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004993 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004994
Howard Hinnant756c69b2010-09-22 16:48:34 +00004995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004996 long use_count() const _NOEXCEPT
4997 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004999 bool expired() const _NOEXCEPT
5000 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5001 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005002 template<class _Up>
5003 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005004 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005005 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005006 template<class _Up>
5007 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005008 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005009 {return __cntrl_ < __r.__cntrl_;}
5010
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005011 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5012 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005013};
5014
5015template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005016inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005017_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005018weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005019 : __ptr_(0),
5020 __cntrl_(0)
5021{
5022}
5023
5024template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005025inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005026weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005027 : __ptr_(__r.__ptr_),
5028 __cntrl_(__r.__cntrl_)
5029{
5030 if (__cntrl_)
5031 __cntrl_->__add_weak();
5032}
5033
5034template<class _Tp>
5035template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005036inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005037weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005038 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005039 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005040 : __ptr_(__r.__ptr_),
5041 __cntrl_(__r.__cntrl_)
5042{
5043 if (__cntrl_)
5044 __cntrl_->__add_weak();
5045}
5046
5047template<class _Tp>
5048template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005049inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005051 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005052 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005053 : __ptr_(__r.__ptr_),
5054 __cntrl_(__r.__cntrl_)
5055{
5056 if (__cntrl_)
5057 __cntrl_->__add_weak();
5058}
5059
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005060#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5061
5062template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005063inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005064weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5065 : __ptr_(__r.__ptr_),
5066 __cntrl_(__r.__cntrl_)
5067{
5068 __r.__ptr_ = 0;
5069 __r.__cntrl_ = 0;
5070}
5071
5072template<class _Tp>
5073template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005074inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005075weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5076 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5077 _NOEXCEPT
5078 : __ptr_(__r.__ptr_),
5079 __cntrl_(__r.__cntrl_)
5080{
5081 __r.__ptr_ = 0;
5082 __r.__cntrl_ = 0;
5083}
5084
5085#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5086
Howard Hinnantc51e1022010-05-11 19:42:16 +00005087template<class _Tp>
5088weak_ptr<_Tp>::~weak_ptr()
5089{
5090 if (__cntrl_)
5091 __cntrl_->__release_weak();
5092}
5093
5094template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005095inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005096weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005097weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005098{
5099 weak_ptr(__r).swap(*this);
5100 return *this;
5101}
5102
5103template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005104template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005105inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005106typename enable_if
5107<
5108 is_convertible<_Yp*, _Tp*>::value,
5109 weak_ptr<_Tp>&
5110>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005111weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005112{
5113 weak_ptr(__r).swap(*this);
5114 return *this;
5115}
5116
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005117#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5118
5119template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005120inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005121weak_ptr<_Tp>&
5122weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5123{
5124 weak_ptr(_VSTD::move(__r)).swap(*this);
5125 return *this;
5126}
5127
Howard Hinnantc51e1022010-05-11 19:42:16 +00005128template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005129template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005130inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005131typename enable_if
5132<
5133 is_convertible<_Yp*, _Tp*>::value,
5134 weak_ptr<_Tp>&
5135>::type
5136weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5137{
5138 weak_ptr(_VSTD::move(__r)).swap(*this);
5139 return *this;
5140}
5141
5142#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5143
5144template<class _Tp>
5145template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005146inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005147typename enable_if
5148<
5149 is_convertible<_Yp*, _Tp*>::value,
5150 weak_ptr<_Tp>&
5151>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005152weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005153{
5154 weak_ptr(__r).swap(*this);
5155 return *this;
5156}
5157
5158template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005159inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005160void
Howard Hinnant719bda32011-05-28 14:41:13 +00005161weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005162{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005163 _VSTD::swap(__ptr_, __r.__ptr_);
5164 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005165}
5166
5167template<class _Tp>
5168inline _LIBCPP_INLINE_VISIBILITY
5169void
Howard Hinnant719bda32011-05-28 14:41:13 +00005170swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005171{
5172 __x.swap(__y);
5173}
5174
5175template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005176inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005177void
Howard Hinnant719bda32011-05-28 14:41:13 +00005178weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005179{
5180 weak_ptr().swap(*this);
5181}
5182
5183template<class _Tp>
5184template<class _Yp>
5185shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005186 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005187 : __ptr_(__r.__ptr_),
5188 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5189{
5190 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005191 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005192}
5193
5194template<class _Tp>
5195shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005196weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005197{
5198 shared_ptr<_Tp> __r;
5199 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5200 if (__r.__cntrl_)
5201 __r.__ptr_ = __ptr_;
5202 return __r;
5203}
5204
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005205#if _LIBCPP_STD_VER > 14
5206template <class _Tp = void> struct owner_less;
5207#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005208template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005210
5211template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005212struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005213 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005214{
5215 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005216 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005217 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005218 {return __x.owner_before(__y);}
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, weak_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()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005224 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005225};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005226
5227template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005228struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005229 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5230{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005231 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005232 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005233 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005234 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005235 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005236 bool operator()(shared_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()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005240 {return __x.owner_before(__y);}
5241};
5242
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005243#if _LIBCPP_STD_VER > 14
5244template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005245struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005246{
5247 template <class _Tp, class _Up>
5248 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005249 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005250 {return __x.owner_before(__y);}
5251 template <class _Tp, class _Up>
5252 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005253 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005254 {return __x.owner_before(__y);}
5255 template <class _Tp, class _Up>
5256 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005257 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005258 {return __x.owner_before(__y);}
5259 template <class _Tp, class _Up>
5260 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005261 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005262 {return __x.owner_before(__y);}
5263 typedef void is_transparent;
5264};
5265#endif
5266
Howard Hinnantc51e1022010-05-11 19:42:16 +00005267template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005268class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005269{
5270 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005271protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005272 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005273 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005275 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005277 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5278 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005280 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005281public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005283 shared_ptr<_Tp> shared_from_this()
5284 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005286 shared_ptr<_Tp const> shared_from_this() const
5287 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005288
Eric Fiselier84006862016-06-02 00:15:35 +00005289#if _LIBCPP_STD_VER > 14
5290 _LIBCPP_INLINE_VISIBILITY
5291 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5292 { return __weak_this_; }
5293
5294 _LIBCPP_INLINE_VISIBILITY
5295 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5296 { return __weak_this_; }
5297#endif // _LIBCPP_STD_VER > 14
5298
Howard Hinnantc51e1022010-05-11 19:42:16 +00005299 template <class _Up> friend class shared_ptr;
5300};
5301
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005302template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005303struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005304{
5305 typedef shared_ptr<_Tp> argument_type;
5306 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005307
Howard Hinnant756c69b2010-09-22 16:48:34 +00005308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005309 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005310 {
5311 return hash<_Tp*>()(__ptr.get());
5312 }
5313};
5314
Howard Hinnantc834c512011-11-29 18:15:50 +00005315template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005316inline _LIBCPP_INLINE_VISIBILITY
5317basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005318operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005319
Eric Fiselier9b492672016-06-18 02:12:53 +00005320
5321#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005322
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005323class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005324{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005325 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005326public:
5327 void lock() _NOEXCEPT;
5328 void unlock() _NOEXCEPT;
5329
5330private:
5331 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5332 __sp_mut(const __sp_mut&);
5333 __sp_mut& operator=(const __sp_mut&);
5334
Howard Hinnant8331b762013-03-06 23:30:19 +00005335 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005336};
5337
Mehdi Amini228053d2017-05-04 17:08:54 +00005338_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5339__sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005340
5341template <class _Tp>
5342inline _LIBCPP_INLINE_VISIBILITY
5343bool
5344atomic_is_lock_free(const shared_ptr<_Tp>*)
5345{
5346 return false;
5347}
5348
5349template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005350_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005351shared_ptr<_Tp>
5352atomic_load(const shared_ptr<_Tp>* __p)
5353{
5354 __sp_mut& __m = __get_sp_mut(__p);
5355 __m.lock();
5356 shared_ptr<_Tp> __q = *__p;
5357 __m.unlock();
5358 return __q;
5359}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005360
Howard Hinnant9fa30202012-07-30 01:40:57 +00005361template <class _Tp>
5362inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005363_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005364shared_ptr<_Tp>
5365atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5366{
5367 return atomic_load(__p);
5368}
5369
5370template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005371_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005372void
5373atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5374{
5375 __sp_mut& __m = __get_sp_mut(__p);
5376 __m.lock();
5377 __p->swap(__r);
5378 __m.unlock();
5379}
5380
5381template <class _Tp>
5382inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005383_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005384void
5385atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5386{
5387 atomic_store(__p, __r);
5388}
5389
5390template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005391_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005392shared_ptr<_Tp>
5393atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5394{
5395 __sp_mut& __m = __get_sp_mut(__p);
5396 __m.lock();
5397 __p->swap(__r);
5398 __m.unlock();
5399 return __r;
5400}
Aditya Kumar7c5db692017-08-20 10:38:55 +00005401
Howard Hinnant9fa30202012-07-30 01:40:57 +00005402template <class _Tp>
5403inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005404_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005405shared_ptr<_Tp>
5406atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5407{
5408 return atomic_exchange(__p, __r);
5409}
5410
5411template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +00005412_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005413bool
5414atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5415{
Marshall Clow4201ee82016-05-18 17:50:13 +00005416 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005417 __sp_mut& __m = __get_sp_mut(__p);
5418 __m.lock();
5419 if (__p->__owner_equivalent(*__v))
5420 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005421 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005422 *__p = __w;
5423 __m.unlock();
5424 return true;
5425 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005426 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005427 *__v = *__p;
5428 __m.unlock();
5429 return false;
5430}
5431
5432template <class _Tp>
5433inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005434_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005435bool
5436atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5437{
5438 return atomic_compare_exchange_strong(__p, __v, __w);
5439}
5440
5441template <class _Tp>
5442inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005443_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005444bool
5445atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5446 shared_ptr<_Tp> __w, memory_order, memory_order)
5447{
5448 return atomic_compare_exchange_strong(__p, __v, __w);
5449}
5450
5451template <class _Tp>
5452inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini228053d2017-05-04 17:08:54 +00005453_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant9fa30202012-07-30 01:40:57 +00005454bool
5455atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5456 shared_ptr<_Tp> __w, memory_order, memory_order)
5457{
5458 return atomic_compare_exchange_weak(__p, __v, __w);
5459}
5460
Eric Fiselier9b492672016-06-18 02:12:53 +00005461#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005462
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005463//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005464#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5465# ifndef _LIBCPP_CXX03_LANG
5466enum class pointer_safety : unsigned char {
5467 relaxed,
5468 preferred,
5469 strict
5470};
5471# endif
5472#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005473struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005474{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005475 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005476 {
5477 relaxed,
5478 preferred,
5479 strict
5480 };
5481
Howard Hinnant49e145e2012-10-30 19:06:59 +00005482 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005483
Howard Hinnant756c69b2010-09-22 16:48:34 +00005484 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005485 pointer_safety() : __v_() {}
5486
5487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005488 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005490 operator int() const {return __v_;}
5491};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005492#endif
5493
5494#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5495 defined(_LIBCPP_BUILDING_MEMORY)
5496_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5497#else
5498// This function is only offered in C++03 under ABI v1.
5499# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5500inline _LIBCPP_INLINE_VISIBILITY
5501pointer_safety get_pointer_safety() _NOEXCEPT {
5502 return pointer_safety::relaxed;
5503}
5504# endif
5505#endif
5506
Howard Hinnantc51e1022010-05-11 19:42:16 +00005507
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005508_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5509_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5510_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005511_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005512
5513template <class _Tp>
5514inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005515_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005516undeclare_reachable(_Tp* __p)
5517{
5518 return static_cast<_Tp*>(__undeclare_reachable(__p));
5519}
5520
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005521_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005522
Marshall Clow8982dcd2015-07-13 20:04:56 +00005523// --- Helper for container swap --
5524template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005525inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005526void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5527#if _LIBCPP_STD_VER >= 14
5528 _NOEXCEPT
5529#else
5530 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5531#endif
5532{
Aditya Kumar7c5db692017-08-20 10:38:55 +00005533 __swap_allocator(__a1, __a2,
Marshall Clow8982dcd2015-07-13 20:04:56 +00005534 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5535}
5536
5537template <typename _Alloc>
5538_LIBCPP_INLINE_VISIBILITY
5539void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5540#if _LIBCPP_STD_VER >= 14
5541 _NOEXCEPT
5542#else
5543 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5544#endif
5545{
5546 using _VSTD::swap;
5547 swap(__a1, __a2);
5548}
5549
5550template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005551inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005552void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5553
Marshall Clowff91de82015-08-18 19:51:37 +00005554template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumar7c5db692017-08-20 10:38:55 +00005555struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005556 _Traits::propagate_on_container_move_assignment::value
5557#if _LIBCPP_STD_VER > 14
5558 || _Traits::is_always_equal::value
5559#else
5560 && is_nothrow_move_assignable<_Alloc>::value
5561#endif
5562 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005563
Marshall Clowa591b9a2016-07-11 21:38:08 +00005564
5565#ifndef _LIBCPP_HAS_NO_VARIADICS
5566template <class _Tp, class _Alloc>
5567struct __temp_value {
5568 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumar7c5db692017-08-20 10:38:55 +00005569
Marshall Clowa591b9a2016-07-11 21:38:08 +00005570 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5571 _Alloc &__a;
5572
5573 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5574 _Tp & get() { return *__addr(); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005575
Marshall Clowa591b9a2016-07-11 21:38:08 +00005576 template<class... _Args>
5577 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5578 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
Aditya Kumar7c5db692017-08-20 10:38:55 +00005579
Marshall Clowa591b9a2016-07-11 21:38:08 +00005580 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5581 };
5582#endif
5583
Howard Hinnantc51e1022010-05-11 19:42:16 +00005584_LIBCPP_END_NAMESPACE_STD
5585
Eric Fiselierf4433a32017-05-31 22:07:49 +00005586_LIBCPP_POP_MACROS
5587
Howard Hinnantc51e1022010-05-11 19:42:16 +00005588#endif // _LIBCPP_MEMORY