blob: 068876542a65f3acdbae20686f9b8551102582bd [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
Howard Hinnantc51e1022010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
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 OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Marshall Clowb22274f2017-01-24 22:22:33 +0000211template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000214class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
216public:
217 typedef X element_type;
218
219 explicit auto_ptr(X* p =0) throw();
220 auto_ptr(auto_ptr&) throw();
221 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
222 auto_ptr& operator=(auto_ptr&) throw();
223 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
224 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
225 ~auto_ptr() throw();
226
227 typename add_lvalue_reference<X>::type operator*() const throw();
228 X* operator->() const throw();
229 X* get() const throw();
230 X* release() throw();
231 void reset(X* p =0) throw();
232
233 auto_ptr(auto_ptr_ref<X>) throw();
234 template<class Y> operator auto_ptr_ref<Y>() throw();
235 template<class Y> operator auto_ptr<Y>() throw();
236};
237
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr unique_ptr() noexcept;
265 explicit unique_ptr(pointer p) noexcept;
266 unique_ptr(pointer p, see below d1) noexcept;
267 unique_ptr(pointer p, see below d2) noexcept;
268 unique_ptr(unique_ptr&& u) noexcept;
269 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 unique_ptr& operator=(unique_ptr&& u) noexcept;
280 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
281 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 pointer operator->() const noexcept;
286 pointer get() const noexcept;
287 deleter_type& get_deleter() noexcept;
288 const deleter_type& get_deleter() const noexcept;
289 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 constexpr unique_ptr() noexcept;
307 explicit unique_ptr(pointer p) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(pointer p, see below d) noexcept;
310 unique_ptr(unique_ptr&& u) noexcept;
311 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 template<class Y> explicit shared_ptr(Y* p);
397 template<class Y, class D> shared_ptr(Y* p, D d);
398 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
399 template <class D> shared_ptr(nullptr_t p, D d);
400 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
402 shared_ptr(const shared_ptr& r) noexcept;
403 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
404 shared_ptr(shared_ptr&& r) noexcept;
405 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000407 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
409 shared_ptr(nullptr_t) : shared_ptr() { }
410
411 // destructor:
412 ~shared_ptr();
413
414 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 shared_ptr& operator=(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
417 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> void reset(Y* p);
426 template<class Y, class D> void reset(Y* p, D d);
427 template<class Y, class D, class A> void reset(Y* p, D d, A a);
428
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // observers:
430 T* get() const noexcept;
431 T& operator*() const noexcept;
432 T* operator->() const noexcept;
433 long use_count() const noexcept;
434 bool unique() const noexcept;
435 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<class E, class T, class Y>
492 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
493
494// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 constexpr weak_ptr() noexcept;
510 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
511 weak_ptr(weak_ptr const& r) noexcept;
512 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 weak_ptr& operator=(weak_ptr const& r) noexcept;
521 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
522 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
562};
563
564template <> // Added in C++14
565struct owner_less<void>
566{
567 template <class _Tp, class _Up>
568 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
569 template <class _Tp, class _Up>
570 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
571 template <class _Tp, class _Up>
572 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
573 template <class _Tp, class _Up>
574 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
575
576 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577};
578
579template<class T>
580class enable_shared_from_this
581{
582protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000583 constexpr enable_shared_from_this() noexcept;
584 enable_shared_from_this(enable_shared_from_this const&) noexcept;
585 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000586 ~enable_shared_from_this();
587public:
588 shared_ptr<T> shared_from_this();
589 shared_ptr<T const> shared_from_this() const;
590};
591
592template<class T>
593 bool atomic_is_lock_free(const shared_ptr<T>* p);
594template<class T>
595 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
596template<class T>
597 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
598template<class T>
599 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
600template<class T>
601 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
602template<class T>
603 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
604template<class T>
605 shared_ptr<T>
606 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
607template<class T>
608 bool
609 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
610template<class T>
611 bool
612 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<class T>
614 bool
615 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
616 shared_ptr<T> w, memory_order success,
617 memory_order failure);
618template<class T>
619 bool
620 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
621 shared_ptr<T> w, memory_order success,
622 memory_order failure);
623// Hash support
624template <class T> struct hash;
625template <class T, class D> struct hash<unique_ptr<T, D> >;
626template <class T> struct hash<shared_ptr<T> >;
627
628// Pointer safety
629enum class pointer_safety { relaxed, preferred, strict };
630void declare_reachable(void *p);
631template <class T> T *undeclare_reachable(T *p);
632void declare_no_pointers(char *p, size_t n);
633void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000634pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000635
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
637
638} // std
639
640*/
641
642#include <__config>
643#include <type_traits>
644#include <typeinfo>
645#include <cstddef>
646#include <cstdint>
647#include <new>
648#include <utility>
649#include <limits>
650#include <iterator>
651#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000652#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000653#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000654#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000655#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000656#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000658# include <atomic>
659#endif
660
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000661#include <__undef_min_max>
662
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000663#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000665#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667_LIBCPP_BEGIN_NAMESPACE_STD
668
Eric Fiselier89659d12015-07-07 00:27:16 +0000669template <class _ValueType>
670inline _LIBCPP_ALWAYS_INLINE
671_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
672#if !defined(_LIBCPP_HAS_NO_THREADS) && \
673 defined(__ATOMIC_RELAXED) && \
674 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
675 return __atomic_load_n(__value, __ATOMIC_RELAXED);
676#else
677 return *__value;
678#endif
679}
680
Kuba Breckade9d6792016-09-04 09:55:12 +0000681template <class _ValueType>
682inline _LIBCPP_ALWAYS_INLINE
683_ValueType __libcpp_acquire_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_ACQUIRE) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
688#else
689 return *__value;
690#endif
691}
692
Marshall Clow78dbe462016-11-14 18:22:19 +0000693// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695template <class _Tp> class allocator;
696
697template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000698class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699{
700public:
701 typedef void* pointer;
702 typedef const void* const_pointer;
703 typedef void value_type;
704
705 template <class _Up> struct rebind {typedef allocator<_Up> other;};
706};
707
Howard Hinnant9f771052012-01-19 23:15:22 +0000708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000710{
711public:
712 typedef const void* pointer;
713 typedef const void* const_pointer;
714 typedef const void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719// pointer_traits
720
721template <class _Tp>
722struct __has_element_type
723{
724private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000725 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 template <class _Up> static __two __test(...);
727 template <class _Up> static char __test(typename _Up::element_type* = 0);
728public:
729 static const bool value = sizeof(__test<_Tp>(0)) == 1;
730};
731
732template <class _Ptr, bool = __has_element_type<_Ptr>::value>
733struct __pointer_traits_element_type;
734
735template <class _Ptr>
736struct __pointer_traits_element_type<_Ptr, true>
737{
738 typedef typename _Ptr::element_type type;
739};
740
741#ifndef _LIBCPP_HAS_NO_VARIADICS
742
743template <template <class, class...> class _Sp, class _Tp, class ..._Args>
744struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
745{
746 typedef typename _Sp<_Tp, _Args...>::element_type type;
747};
748
749template <template <class, class...> class _Sp, class _Tp, class ..._Args>
750struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
751{
752 typedef _Tp type;
753};
754
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000755#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
757template <template <class> class _Sp, class _Tp>
758struct __pointer_traits_element_type<_Sp<_Tp>, true>
759{
760 typedef typename _Sp<_Tp>::element_type type;
761};
762
763template <template <class> class _Sp, class _Tp>
764struct __pointer_traits_element_type<_Sp<_Tp>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class> class _Sp, class _Tp, class _A0>
770struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
771{
772 typedef typename _Sp<_Tp, _A0>::element_type type;
773};
774
775template <template <class, class> class _Sp, class _Tp, class _A0>
776struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
777{
778 typedef _Tp type;
779};
780
781template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
782struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
783{
784 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
785};
786
787template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
788struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
794 class _A1, class _A2>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
796{
797 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
803{
804 typedef _Tp type;
805};
806
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
809template <class _Tp>
810struct __has_difference_type
811{
812private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000813 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 template <class _Up> static __two __test(...);
815 template <class _Up> static char __test(typename _Up::difference_type* = 0);
816public:
817 static const bool value = sizeof(__test<_Tp>(0)) == 1;
818};
819
820template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
821struct __pointer_traits_difference_type
822{
823 typedef ptrdiff_t type;
824};
825
826template <class _Ptr>
827struct __pointer_traits_difference_type<_Ptr, true>
828{
829 typedef typename _Ptr::difference_type type;
830};
831
832template <class _Tp, class _Up>
833struct __has_rebind
834{
835private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000836 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 template <class _Xp> static __two __test(...);
838 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
839public:
840 static const bool value = sizeof(__test<_Tp>(0)) == 1;
841};
842
843template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
844struct __pointer_traits_rebind
845{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 typedef typename _Tp::template rebind<_Up> type;
848#else
849 typedef typename _Tp::template rebind<_Up>::other type;
850#endif
851};
852
853#ifndef _LIBCPP_HAS_NO_VARIADICS
854
855template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
867{
868 typedef _Sp<_Up, _Args...> type;
869};
870
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000871#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
873template <template <class> class _Sp, class _Tp, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
875{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 typedef typename _Sp<_Tp>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class> class _Sp, class _Tp, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
885{
886 typedef _Sp<_Up> type;
887};
888
889template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
891{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000892#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
894#else
895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
896#endif
897};
898
899template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
900struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
901{
902 typedef _Sp<_Up, _A0> type;
903};
904
905template <template <class, class, class> class _Sp, class _Tp, class _A0,
906 class _A1, class _Up>
907struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
908{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000909#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
911#else
912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
913#endif
914};
915
916template <template <class, class, class> class _Sp, class _Tp, class _A0,
917 class _A1, class _Up>
918struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
919{
920 typedef _Sp<_Up, _A0, _A1> type;
921};
922
923template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
924 class _A1, class _A2, class _Up>
925struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
926{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000927#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
929#else
930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
931#endif
932};
933
934template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
935 class _A1, class _A2, class _Up>
936struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
937{
938 typedef _Sp<_Up, _A0, _A1, _A2> type;
939};
940
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000941#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000944struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
946 typedef _Ptr pointer;
947 typedef typename __pointer_traits_element_type<pointer>::type element_type;
948 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
949
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000951 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952#else
953 template <class _Up> struct rebind
954 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957private:
958 struct __nat {};
959public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 static pointer pointer_to(typename conditional<is_void<element_type>::value,
962 __nat, element_type>::type& __r)
963 {return pointer::pointer_to(__r);}
964};
965
966template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
969 typedef _Tp* pointer;
970 typedef _Tp element_type;
971 typedef ptrdiff_t difference_type;
972
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000973#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 template <class _Up> using rebind = _Up*;
975#else
976 template <class _Up> struct rebind {typedef _Up* other;};
977#endif
978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000984 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000985 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988template <class _From, class _To>
989struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991 typedef typename pointer_traits<_From>::template rebind<_To> type;
992#else
993 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
994#endif
995};
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// allocator_traits
998
999namespace __has_pointer_type_imp
1000{
Howard Hinnant6e260172013-09-21 01:45:05 +00001001 template <class _Up> static __two __test(...);
1002 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003}
1004
1005template <class _Tp>
1006struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +00001007 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
1009};
1010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
1034template <class _Tp>
1035struct __has_const_pointer
1036{
1037private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001038 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template <class _Up> static __two __test(...);
1040 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1041public:
1042 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1043};
1044
1045template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1046struct __const_pointer
1047{
1048 typedef typename _Alloc::const_pointer type;
1049};
1050
1051template <class _Tp, class _Ptr, class _Alloc>
1052struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1053{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001054#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1056#else
1057 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1058#endif
1059};
1060
1061template <class _Tp>
1062struct __has_void_pointer
1063{
1064private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001065 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1073struct __void_pointer
1074{
1075 typedef typename _Alloc::void_pointer type;
1076};
1077
1078template <class _Ptr, class _Alloc>
1079struct __void_pointer<_Ptr, _Alloc, false>
1080{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1083#else
1084 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1085#endif
1086};
1087
1088template <class _Tp>
1089struct __has_const_void_pointer
1090{
1091private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001092 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 template <class _Up> static __two __test(...);
1094 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1095public:
1096 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1097};
1098
1099template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1100struct __const_void_pointer
1101{
1102 typedef typename _Alloc::const_void_pointer type;
1103};
1104
1105template <class _Ptr, class _Alloc>
1106struct __const_void_pointer<_Ptr, _Alloc, false>
1107{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001108#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1110#else
1111 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1112#endif
1113};
1114
Howard Hinnantc834c512011-11-29 18:15:50 +00001115template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001117_Tp*
1118__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 return __p;
1121}
1122
1123template <class _Pointer>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001126__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001128 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129}
1130
1131template <class _Tp>
1132struct __has_size_type
1133{
1134private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001135 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 template <class _Up> static __two __test(...);
1137 template <class _Up> static char __test(typename _Up::size_type* = 0);
1138public:
1139 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1140};
1141
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001142template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143struct __size_type
1144{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001145 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146};
1147
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001148template <class _Alloc, class _DiffType>
1149struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 typedef typename _Alloc::size_type type;
1152};
1153
1154template <class _Tp>
1155struct __has_propagate_on_container_copy_assignment
1156{
1157private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001158 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 template <class _Up> static __two __test(...);
1160 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1161public:
1162 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1163};
1164
1165template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1166struct __propagate_on_container_copy_assignment
1167{
1168 typedef false_type type;
1169};
1170
1171template <class _Alloc>
1172struct __propagate_on_container_copy_assignment<_Alloc, true>
1173{
1174 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1175};
1176
1177template <class _Tp>
1178struct __has_propagate_on_container_move_assignment
1179{
1180private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001181 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 template <class _Up> static __two __test(...);
1183 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1184public:
1185 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1186};
1187
1188template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1189struct __propagate_on_container_move_assignment
1190{
1191 typedef false_type type;
1192};
1193
1194template <class _Alloc>
1195struct __propagate_on_container_move_assignment<_Alloc, true>
1196{
1197 typedef typename _Alloc::propagate_on_container_move_assignment type;
1198};
1199
1200template <class _Tp>
1201struct __has_propagate_on_container_swap
1202{
1203private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001204 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 template <class _Up> static __two __test(...);
1206 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1207public:
1208 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1209};
1210
1211template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1212struct __propagate_on_container_swap
1213{
1214 typedef false_type type;
1215};
1216
1217template <class _Alloc>
1218struct __propagate_on_container_swap<_Alloc, true>
1219{
1220 typedef typename _Alloc::propagate_on_container_swap type;
1221};
1222
Marshall Clow0b587562015-06-02 16:34:03 +00001223template <class _Tp>
1224struct __has_is_always_equal
1225{
1226private:
1227 struct __two {char __lx; char __lxx;};
1228 template <class _Up> static __two __test(...);
1229 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1230public:
1231 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1232};
1233
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)
1344 -> decltype(__a.allocate(__sz, __p), true_type());
1345
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<
1355 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1356 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<
1388 decltype(__has_construct_test(declval<_Alloc>(),
1389 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<
1409 decltype(__has_destroy_test(declval<_Alloc>(),
1410 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<
1429 decltype(__has_max_size_test(declval<_Alloc&>())),
1430 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<
1448 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1449 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
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _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);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1544 {return allocate(__a, __n, __hint,
1545 __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)
1598 {return select_on_container_copy_construction(
1599 __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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static pointer allocate(allocator_type& __a, size_type __n,
1698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +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
1738 select_on_container_copy_construction(true_type, const allocator_type& __a)
1739 {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
1742 select_on_container_copy_construction(false_type, const allocator_type& __a)
1743 {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);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001782 {
1783 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1787 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001788 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001789 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1791 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001792#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 template <class _Up, class... _Args>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(_Up* __p, _Args&&... __args)
1797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001798 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p)
1804 {
1805 ::new((void*)__p) _Tp();
1806 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001807# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0>
1810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001811 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 construct(pointer __p, _A0& __a0)
1813 {
1814 ::new((void*)__p) _Tp(__a0);
1815 }
1816 template <class _A0>
1817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001818 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 construct(pointer __p, const _A0& __a0)
1820 {
1821 ::new((void*)__p) _Tp(__a0);
1822 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001823# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, _A1& __a1)
1828 {
1829 ::new((void*)__p) _Tp(__a0, __a1);
1830 }
1831 template <class _A0, class _A1>
1832 _LIBCPP_INLINE_VISIBILITY
1833 void
1834 construct(pointer __p, const _A0& __a0, _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
1838 template <class _A0, class _A1>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(pointer __p, _A0& __a0, const _A1& __a1)
1842 {
1843 ::new((void*)__p) _Tp(__a0, __a1);
1844 }
1845 template <class _A0, class _A1>
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1849 {
1850 ::new((void*)__p) _Tp(__a0, __a1);
1851 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1854};
1855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858{
1859public:
1860 typedef size_t size_type;
1861 typedef ptrdiff_t difference_type;
1862 typedef const _Tp* pointer;
1863 typedef const _Tp* const_pointer;
1864 typedef const _Tp& reference;
1865 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001866 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867
1868 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001869 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870
1871 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1872
1873 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1874 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1875 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1876 {return _VSTD::addressof(__x);}
1877 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001878 {
1879 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001880 __throw_length_error("allocator<const T>::allocate(size_t n)"
1881 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001883 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001885 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1887 {return size_type(~0) / sizeof(_Tp);}
1888#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1889 template <class _Up, class... _Args>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(_Up* __p, _Args&&... __args)
1893 {
1894 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1895 }
1896#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p)
1900 {
1901 ::new((void*)__p) _Tp();
1902 }
1903# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001904
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905 template <class _A0>
1906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001907 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 construct(pointer __p, _A0& __a0)
1909 {
1910 ::new((void*)__p) _Tp(__a0);
1911 }
1912 template <class _A0>
1913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001914 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 construct(pointer __p, const _A0& __a0)
1916 {
1917 ::new((void*)__p) _Tp(__a0);
1918 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, _A1& __a1)
1924 {
1925 ::new((void*)__p) _Tp(__a0, __a1);
1926 }
1927 template <class _A0, class _A1>
1928 _LIBCPP_INLINE_VISIBILITY
1929 void
1930 construct(pointer __p, const _A0& __a0, _A1& __a1)
1931 {
1932 ::new((void*)__p) _Tp(__a0, __a1);
1933 }
1934 template <class _A0, class _A1>
1935 _LIBCPP_INLINE_VISIBILITY
1936 void
1937 construct(pointer __p, _A0& __a0, const _A1& __a1)
1938 {
1939 ::new((void*)__p) _Tp(__a0, __a1);
1940 }
1941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1945 {
1946 ::new((void*)__p) _Tp(__a0, __a1);
1947 }
1948#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1949 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1950};
1951
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952template <class _Tp, class _Up>
1953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001954bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001961class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 : public iterator<output_iterator_tag,
1963 _Tp, // purposefully not C++03
1964 ptrdiff_t, // purposefully not C++03
1965 _Tp*, // purposefully not C++03
1966 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1967{
1968private:
1969 _OutputIterator __x_;
1970public:
1971 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1972 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1974 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001975#if _LIBCPP_STD_VER >= 14
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1977 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1978#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1981 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001982#if _LIBCPP_STD_VER >= 14
1983 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985};
1986
1987template <class _Tp>
1988pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001989get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
1991 pair<_Tp*, ptrdiff_t> __r(0, 0);
1992 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1993 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1994 / sizeof(_Tp);
1995 if (__n > __m)
1996 __n = __m;
1997 while (__n > 0)
1998 {
1999 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2000 if (__r.first)
2001 {
2002 __r.second = __n;
2003 break;
2004 }
2005 __n /= 2;
2006 }
2007 return __r;
2008}
2009
2010template <class _Tp>
2011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002012void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Marshall Clowb22274f2017-01-24 22:22:33 +00002014#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _Tp>
2016struct auto_ptr_ref
2017{
2018 _Tp* __ptr_;
2019};
2020
2021template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002022class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024private:
2025 _Tp* __ptr_;
2026public:
2027 typedef _Tp element_type;
2028
2029 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2030 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2031 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2032 : __ptr_(__p.release()) {}
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2034 {reset(__p.release()); return *this;}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2038 {reset(__p.__ptr_); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2040
2041 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2042 {return *__ptr_;}
2043 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2046 {
2047 _Tp* __t = __ptr_;
2048 __ptr_ = 0;
2049 return __t;
2050 }
2051 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2052 {
2053 if (__ptr_ != __p)
2054 delete __ptr_;
2055 __ptr_ = __p;
2056 }
2057
2058 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2059 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2060 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2062 {return auto_ptr<_Up>(release());}
2063};
2064
2065template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002066class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068public:
2069 typedef void element_type;
2070};
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Eric Fiselier9d355982017-04-12 23:45:53 +00002073template <class _Tp, int _Idx,
2074 bool _CanBeEmptyBase =
2075 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2076struct __compressed_pair_elem {
2077 typedef _Tp _ParamT;
2078 typedef _Tp& reference;
2079 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081#ifndef _LIBCPP_CXX03_LANG
2082 __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083
Eric Fiselier9d355982017-04-12 23:45:53 +00002084 template <class _Up, class = typename enable_if<
2085 !is_same<__compressed_pair_elem, _Up>::value>::type>
2086 _LIBCPP_CONSTEXPR explicit
2087 __compressed_pair_elem(_Up&& __u)
2088 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089
Eric Fiselier9d355982017-04-12 23:45:53 +00002090 template <class... _Args, size_t... _Indexes>
2091 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2092 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2093 __tuple_indices<_Indexes...>)
2094 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2095#else
2096 __compressed_pair_elem() : __value_() {}
2097 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2098#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099
Eric Fiselier9d355982017-04-12 23:45:53 +00002100 reference __get() _NOEXCEPT { return __value_; }
2101 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002104 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105};
2106
Eric Fiselier9d355982017-04-12 23:45:53 +00002107template <class _Tp, int _Idx>
2108struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2109 typedef _Tp _ParamT;
2110 typedef _Tp& reference;
2111 typedef const _Tp& const_reference;
2112 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113
Eric Fiselier9d355982017-04-12 23:45:53 +00002114#ifndef _LIBCPP_CXX03_LANG
2115 __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116
Eric Fiselier9d355982017-04-12 23:45:53 +00002117 template <class _Up, class = typename enable_if<
2118 !is_same<__compressed_pair_elem, _Up>::value>::type>
2119 _LIBCPP_CONSTEXPR explicit
2120 __compressed_pair_elem(_Up&& __u)
2121 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
Eric Fiselier9d355982017-04-12 23:45:53 +00002123 template <class... _Args, size_t... _Indexes>
2124 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2125 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2126 __tuple_indices<_Indexes...>)
2127 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2128#else
2129 __compressed_pair_elem() : __value_type() {}
2130 __compressed_pair_elem(_ParamT __p)
2131 : __value_type(std::forward<_ParamT>(__p)) {}
2132#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133
Eric Fiselier9d355982017-04-12 23:45:53 +00002134 reference __get() _NOEXCEPT { return *this; }
2135 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136};
2137
Eric Fiselier9d355982017-04-12 23:45:53 +00002138// Tag used to construct the second element of the compressed pair.
2139struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140
2141template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002142class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2143 private __compressed_pair_elem<_T2, 1> {
2144 typedef __compressed_pair_elem<_T1, 0> _Base1;
2145 typedef __compressed_pair_elem<_T2, 1> _Base2;
2146
2147 // NOTE: This static assert should never fire because __compressed_pair
2148 // is *almost never* used in a scenario where it's possible for T1 == T2.
2149 // (The exception is std::function where it is possible that the function
2150 // object and the allocator have the same type).
2151 static_assert(!is_same<_T1, _T2>::value,
2152 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2153 "The current implementation is NOT ABI-compatible with the previous "
2154 "implementation for this configuration");
2155
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002157#ifndef _LIBCPP_CXX03_LANG
2158 _LIBCPP_INLINE_VISIBILITY
2159 __compressed_pair() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160
Eric Fiselier9d355982017-04-12 23:45:53 +00002161 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2162 __compressed_pair>::value,
2163 bool>::type = true>
2164 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2165 __compressed_pair(_Tp&& __t)
2166 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167
Eric Fiselier9d355982017-04-12 23:45:53 +00002168 template <class _Tp>
2169 _LIBCPP_INLINE_VISIBILITY constexpr
2170 __compressed_pair(__second_tag, _Tp&& __t)
2171 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172
Eric Fiselier9d355982017-04-12 23:45:53 +00002173 template <class _U1, class _U2>
2174 _LIBCPP_INLINE_VISIBILITY constexpr
2175 __compressed_pair(_U1&& __t1, _U2&& __t2)
2176 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177
Eric Fiselier9d355982017-04-12 23:45:53 +00002178 template <class... _Args1, class... _Args2>
2179 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2180 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2181 tuple<_Args2...> __second_args)
2182 : _Base1(__pc, _VSTD::move(__first_args),
2183 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2184 _Base2(__pc, _VSTD::move(__second_args),
2185 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002186
Eric Fiselier9d355982017-04-12 23:45:53 +00002187#else
2188 _LIBCPP_INLINE_VISIBILITY
2189 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002190
Eric Fiselier9d355982017-04-12 23:45:53 +00002191 _LIBCPP_INLINE_VISIBILITY explicit
2192 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002193
Eric Fiselier9d355982017-04-12 23:45:53 +00002194 _LIBCPP_INLINE_VISIBILITY
2195 __compressed_pair(__second_tag, _T2 __t2)
2196 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197
Eric Fiselier9d355982017-04-12 23:45:53 +00002198 _LIBCPP_INLINE_VISIBILITY
2199 __compressed_pair(_T1 __t1, _T2 __t2)
2200 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2201#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202
Eric Fiselier9d355982017-04-12 23:45:53 +00002203 _LIBCPP_INLINE_VISIBILITY
2204 typename _Base1::reference first() _NOEXCEPT {
2205 return static_cast<_Base1&>(*this).__get();
2206 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207
Eric Fiselier9d355982017-04-12 23:45:53 +00002208 _LIBCPP_INLINE_VISIBILITY
2209 typename _Base1::const_reference first() const _NOEXCEPT {
2210 return static_cast<_Base1 const&>(*this).__get();
2211 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212
Eric Fiselier9d355982017-04-12 23:45:53 +00002213 _LIBCPP_INLINE_VISIBILITY
2214 typename _Base2::reference second() _NOEXCEPT {
2215 return static_cast<_Base2&>(*this).__get();
2216 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Eric Fiselier9d355982017-04-12 23:45:53 +00002218 _LIBCPP_INLINE_VISIBILITY
2219 typename _Base2::const_reference second() const _NOEXCEPT {
2220 return static_cast<_Base2 const&>(*this).__get();
2221 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222
Eric Fiselier9d355982017-04-12 23:45:53 +00002223 _LIBCPP_INLINE_VISIBILITY
2224 void swap(__compressed_pair& __x)
2225 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2226 __is_nothrow_swappable<_T2>::value)
2227 {
2228 using std::swap;
2229 swap(first(), __x.first());
2230 swap(second(), __x.second());
2231 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232};
2233
2234template <class _T1, class _T2>
2235inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002236void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2237 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2238 __is_nothrow_swappable<_T2>::value) {
2239 __x.swap(__y);
2240}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002242// __same_or_less_cv_qualified
2243
2244template <class _Ptr1, class _Ptr2,
2245 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2246 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2247 >::value
2248 >
2249struct __same_or_less_cv_qualified_imp
2250 : is_convertible<_Ptr1, _Ptr2> {};
2251
2252template <class _Ptr1, class _Ptr2>
2253struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2254 : false_type {};
2255
Marshall Clowdf296162014-04-26 05:19:48 +00002256template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2257 is_same<_Ptr1, _Ptr2>::value ||
2258 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002259struct __same_or_less_cv_qualified
2260 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2261
2262template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002263struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002264 : false_type {};
2265
2266// default_delete
2267
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002269struct _LIBCPP_TEMPLATE_VIS default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270{
Eric Fiselier9d355982017-04-12 23:45:53 +00002271#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002272 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2273#else
2274 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 template <class _Up>
2277 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002278 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2279 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 {
2281 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002282 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 delete __ptr;
2284 }
2285};
2286
2287template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002288struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002290public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002291#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002292 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2293#else
2294 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2295#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002296 template <class _Up>
2297 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002298 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002299 template <class _Up>
2300 _LIBCPP_INLINE_VISIBILITY
2301 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002302 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303 {
2304 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002305 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306 delete [] __ptr;
2307 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308};
2309
2310template <class _Tp, class _Dp = default_delete<_Tp> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002311class _LIBCPP_TEMPLATE_VIS unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312{
2313public:
2314 typedef _Tp element_type;
2315 typedef _Dp deleter_type;
2316 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2317private:
2318 __compressed_pair<pointer, deleter_type> __ptr_;
2319
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002320#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 unique_ptr(unique_ptr&);
2322 template <class _Up, class _Ep>
2323 unique_ptr(unique_ptr<_Up, _Ep>&);
2324 unique_ptr& operator=(unique_ptr&);
2325 template <class _Up, class _Ep>
2326 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002327#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328
2329 struct __nat {int __for_bool_;};
2330
Eric Fiselier593652b2017-04-12 22:43:49 +00002331 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2332 typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
2333 _Dp_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 : __ptr_(pointer())
2337 {
2338 static_assert(!is_pointer<deleter_type>::value,
2339 "unique_ptr constructed with null function pointer deleter");
2340 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342 : __ptr_(pointer())
2343 {
2344 static_assert(!is_pointer<deleter_type>::value,
2345 "unique_ptr constructed with null function pointer deleter");
2346 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002347 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002348 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349 {
2350 static_assert(!is_pointer<deleter_type>::value,
2351 "unique_ptr constructed with null function pointer deleter");
2352 }
2353
Howard Hinnant74279a52010-09-04 23:28:19 +00002354#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2356 is_reference<deleter_type>::value,
2357 deleter_type,
2358 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002359 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360 : __ptr_(__p, __d) {}
2361
2362 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002363 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002364 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 {
2366 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2367 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002368 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002369 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370 template <class _Up, class _Ep>
2371 _LIBCPP_INLINE_VISIBILITY
2372 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2373 typename enable_if
2374 <
2375 !is_array<_Up>::value &&
2376 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2377 is_convertible<_Ep, deleter_type>::value &&
2378 (
2379 !is_reference<deleter_type>::value ||
2380 is_same<deleter_type, _Ep>::value
2381 ),
2382 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002383 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002384 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Marshall Clowb22274f2017-01-24 22:22:33 +00002386#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 template <class _Up>
2388 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2389 typename enable_if<
2390 is_convertible<_Up*, _Tp*>::value &&
2391 is_same<_Dp, default_delete<_Tp> >::value,
2392 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002393 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394 : __ptr_(__p.release())
2395 {
2396 }
Marshall Clowb22274f2017-01-24 22:22:33 +00002397#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398
Howard Hinnant719bda32011-05-28 14:41:13 +00002399 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 {
2401 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002402 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 return *this;
2404 }
2405
2406 template <class _Up, class _Ep>
2407 _LIBCPP_INLINE_VISIBILITY
2408 typename enable_if
2409 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002410 !is_array<_Up>::value &&
2411 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2412 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413 unique_ptr&
2414 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002415 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 {
2417 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002418 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419 return *this;
2420 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002421#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422
2423 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2424 {
2425 return __rv<unique_ptr>(*this);
2426 }
2427
2428 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002429 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430
2431 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002432 _LIBCPP_INLINE_VISIBILITY
2433 typename enable_if<
2434 !is_array<_Up>::value &&
2435 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2436 is_assignable<deleter_type&, _Ep&>::value,
2437 unique_ptr&
2438 >::type
2439 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440 {
2441 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002442 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 return *this;
2444 }
2445
2446 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002447 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448
Marshall Clowb22274f2017-01-24 22:22:33 +00002449#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 template <class _Up>
2451 _LIBCPP_INLINE_VISIBILITY
2452 typename enable_if<
2453 is_convertible<_Up*, _Tp*>::value &&
2454 is_same<_Dp, default_delete<_Tp> >::value,
2455 unique_ptr&
2456 >::type
2457 operator=(auto_ptr<_Up> __p)
2458 {reset(__p.release()); return *this;}
Marshall Clowb22274f2017-01-24 22:22:33 +00002459#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00002460#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2462
Howard Hinnant719bda32011-05-28 14:41:13 +00002463 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 {
2465 reset();
2466 return *this;
2467 }
2468
2469 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2470 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002471 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2472 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2473 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2474 {return __ptr_.second();}
2475 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2476 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002477 _LIBCPP_INLINE_VISIBILITY
2478 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2479 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480
Howard Hinnant719bda32011-05-28 14:41:13 +00002481 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 {
2483 pointer __t = __ptr_.first();
2484 __ptr_.first() = pointer();
2485 return __t;
2486 }
2487
Howard Hinnant719bda32011-05-28 14:41:13 +00002488 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 {
2490 pointer __tmp = __ptr_.first();
2491 __ptr_.first() = __p;
2492 if (__tmp)
2493 __ptr_.second()(__tmp);
2494 }
2495
Howard Hinnant719bda32011-05-28 14:41:13 +00002496 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2497 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498};
2499
2500template <class _Tp, class _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002501class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502{
2503public:
2504 typedef _Tp element_type;
2505 typedef _Dp deleter_type;
2506 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2507private:
2508 __compressed_pair<pointer, deleter_type> __ptr_;
2509
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002510#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 unique_ptr(unique_ptr&);
2512 template <class _Up>
2513 unique_ptr(unique_ptr<_Up>&);
2514 unique_ptr& operator=(unique_ptr&);
2515 template <class _Up>
2516 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002517#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518
2519 struct __nat {int __for_bool_;};
2520
Eric Fiselier593652b2017-04-12 22:43:49 +00002521 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2522 typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
2523 _Dp_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002525 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 : __ptr_(pointer())
2527 {
2528 static_assert(!is_pointer<deleter_type>::value,
2529 "unique_ptr constructed with null function pointer deleter");
2530 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002531 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 : __ptr_(pointer())
2533 {
2534 static_assert(!is_pointer<deleter_type>::value,
2535 "unique_ptr constructed with null function pointer deleter");
2536 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002537#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002538 template <class _Pp>
2539 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2540 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541 : __ptr_(__p)
2542 {
2543 static_assert(!is_pointer<deleter_type>::value,
2544 "unique_ptr constructed with null function pointer deleter");
2545 }
2546
Logan Chiend435f8b2014-01-31 09:30:46 +00002547 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002548 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549 is_reference<deleter_type>::value,
2550 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002551 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2552 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002553 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554 : __ptr_(__p, __d) {}
2555
2556 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2557 is_reference<deleter_type>::value,
2558 deleter_type,
2559 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002560 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 : __ptr_(pointer(), __d) {}
2562
Logan Chiend435f8b2014-01-31 09:30:46 +00002563 template <class _Pp>
2564 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2565 typename remove_reference<deleter_type>::type&& __d,
2566 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002567 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002568 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 {
2570 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2571 }
2572
2573 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002574 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002575 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576 {
2577 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2578 }
2579
Howard Hinnant719bda32011-05-28 14:41:13 +00002580 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002581 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582
Howard Hinnant719bda32011-05-28 14:41:13 +00002583 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584 {
2585 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002586 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587 return *this;
2588 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002589
2590 template <class _Up, class _Ep>
2591 _LIBCPP_INLINE_VISIBILITY
2592 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2593 typename enable_if
2594 <
2595 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002596 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002597 && is_convertible<_Ep, deleter_type>::value &&
2598 (
2599 !is_reference<deleter_type>::value ||
2600 is_same<deleter_type, _Ep>::value
2601 ),
2602 __nat
2603 >::type = __nat()
2604 ) _NOEXCEPT
2605 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2606
2607
2608 template <class _Up, class _Ep>
2609 _LIBCPP_INLINE_VISIBILITY
2610 typename enable_if
2611 <
2612 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002613 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2614 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002615 unique_ptr&
2616 >::type
2617 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2618 {
2619 reset(__u.release());
2620 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2621 return *this;
2622 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002623#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624
2625 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2626 : __ptr_(__p)
2627 {
2628 static_assert(!is_pointer<deleter_type>::value,
2629 "unique_ptr constructed with null function pointer deleter");
2630 }
2631
2632 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002633 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634
2635 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002636 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637
2638 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2639 {
2640 return __rv<unique_ptr>(*this);
2641 }
2642
2643 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002644 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645
2646 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2647 {
2648 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002649 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 return *this;
2651 }
2652
Howard Hinnant74279a52010-09-04 23:28:19 +00002653#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2655
Howard Hinnant719bda32011-05-28 14:41:13 +00002656 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657 {
2658 reset();
2659 return *this;
2660 }
2661
2662 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2663 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002664 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2665 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2666 {return __ptr_.second();}
2667 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2668 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002669 _LIBCPP_INLINE_VISIBILITY
2670 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2671 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672
Howard Hinnant719bda32011-05-28 14:41:13 +00002673 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 {
2675 pointer __t = __ptr_.first();
2676 __ptr_.first() = pointer();
2677 return __t;
2678 }
2679
Logan Chiend435f8b2014-01-31 09:30:46 +00002680 template <class _Pp>
2681 _LIBCPP_INLINE_VISIBILITY
2682 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2683 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 {
2685 pointer __tmp = __ptr_.first();
2686 __ptr_.first() = __p;
2687 if (__tmp)
2688 __ptr_.second()(__tmp);
2689 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002690 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 {
2692 pointer __tmp = __ptr_.first();
2693 __ptr_.first() = nullptr;
2694 if (__tmp)
2695 __ptr_.second()(__tmp);
2696 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697
2698 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2699private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002700
Howard Hinnant74279a52010-09-04 23:28:19 +00002701#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 template <class _Up>
2703 explicit unique_ptr(_Up);
2704 template <class _Up>
2705 unique_ptr(_Up __u,
2706 typename conditional<
2707 is_reference<deleter_type>::value,
2708 deleter_type,
2709 typename add_lvalue_reference<const deleter_type>::type>::type,
2710 typename enable_if
2711 <
2712 is_convertible<_Up, pointer>::value,
2713 __nat
2714 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002715#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716};
2717
2718template <class _Tp, class _Dp>
2719inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002720typename enable_if<
2721 __is_swappable<_Dp>::value,
2722 void
2723>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002724swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725
2726template <class _T1, class _D1, class _T2, class _D2>
2727inline _LIBCPP_INLINE_VISIBILITY
2728bool
2729operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2730
2731template <class _T1, class _D1, class _T2, class _D2>
2732inline _LIBCPP_INLINE_VISIBILITY
2733bool
2734operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2735
2736template <class _T1, class _D1, class _T2, class _D2>
2737inline _LIBCPP_INLINE_VISIBILITY
2738bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002739operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2740{
2741 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2742 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002743 typedef typename common_type<_P1, _P2>::type _Vp;
2744 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002745}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746
2747template <class _T1, class _D1, class _T2, class _D2>
2748inline _LIBCPP_INLINE_VISIBILITY
2749bool
2750operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2751
2752template <class _T1, class _D1, class _T2, class _D2>
2753inline _LIBCPP_INLINE_VISIBILITY
2754bool
2755operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2756
2757template <class _T1, class _D1, class _T2, class _D2>
2758inline _LIBCPP_INLINE_VISIBILITY
2759bool
2760operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2761
Howard Hinnantb17caf92012-02-21 21:02:58 +00002762template <class _T1, class _D1>
2763inline _LIBCPP_INLINE_VISIBILITY
2764bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002765operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002766{
2767 return !__x;
2768}
2769
2770template <class _T1, class _D1>
2771inline _LIBCPP_INLINE_VISIBILITY
2772bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002773operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002774{
2775 return !__x;
2776}
2777
2778template <class _T1, class _D1>
2779inline _LIBCPP_INLINE_VISIBILITY
2780bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002781operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002782{
2783 return static_cast<bool>(__x);
2784}
2785
2786template <class _T1, class _D1>
2787inline _LIBCPP_INLINE_VISIBILITY
2788bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002789operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002790{
2791 return static_cast<bool>(__x);
2792}
2793
2794template <class _T1, class _D1>
2795inline _LIBCPP_INLINE_VISIBILITY
2796bool
2797operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2798{
2799 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2800 return less<_P1>()(__x.get(), nullptr);
2801}
2802
2803template <class _T1, class _D1>
2804inline _LIBCPP_INLINE_VISIBILITY
2805bool
2806operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2807{
2808 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2809 return less<_P1>()(nullptr, __x.get());
2810}
2811
2812template <class _T1, class _D1>
2813inline _LIBCPP_INLINE_VISIBILITY
2814bool
2815operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2816{
2817 return nullptr < __x;
2818}
2819
2820template <class _T1, class _D1>
2821inline _LIBCPP_INLINE_VISIBILITY
2822bool
2823operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2824{
2825 return __x < nullptr;
2826}
2827
2828template <class _T1, class _D1>
2829inline _LIBCPP_INLINE_VISIBILITY
2830bool
2831operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2832{
2833 return !(nullptr < __x);
2834}
2835
2836template <class _T1, class _D1>
2837inline _LIBCPP_INLINE_VISIBILITY
2838bool
2839operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2840{
2841 return !(__x < nullptr);
2842}
2843
2844template <class _T1, class _D1>
2845inline _LIBCPP_INLINE_VISIBILITY
2846bool
2847operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2848{
2849 return !(__x < nullptr);
2850}
2851
2852template <class _T1, class _D1>
2853inline _LIBCPP_INLINE_VISIBILITY
2854bool
2855operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2856{
2857 return !(nullptr < __x);
2858}
2859
Howard Hinnant9e028f12012-05-01 15:37:54 +00002860#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2861
2862template <class _Tp, class _Dp>
2863inline _LIBCPP_INLINE_VISIBILITY
2864unique_ptr<_Tp, _Dp>
2865move(unique_ptr<_Tp, _Dp>& __t)
2866{
2867 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
2868}
2869
2870#endif
2871
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002872#if _LIBCPP_STD_VER > 11
2873
2874template<class _Tp>
2875struct __unique_if
2876{
2877 typedef unique_ptr<_Tp> __unique_single;
2878};
2879
2880template<class _Tp>
2881struct __unique_if<_Tp[]>
2882{
2883 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2884};
2885
2886template<class _Tp, size_t _Np>
2887struct __unique_if<_Tp[_Np]>
2888{
2889 typedef void __unique_array_known_bound;
2890};
2891
2892template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00002893inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002894typename __unique_if<_Tp>::__unique_single
2895make_unique(_Args&&... __args)
2896{
2897 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
2898}
2899
2900template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00002901inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002902typename __unique_if<_Tp>::__unique_array_unknown_bound
2903make_unique(size_t __n)
2904{
2905 typedef typename remove_extent<_Tp>::type _Up;
2906 return unique_ptr<_Tp>(new _Up[__n]());
2907}
2908
2909template<class _Tp, class... _Args>
2910 typename __unique_if<_Tp>::__unique_array_known_bound
2911 make_unique(_Args&&...) = delete;
2912
2913#endif // _LIBCPP_STD_VER > 11
2914
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002915template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00002916#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002917struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00002918#else
2919struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
2920 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
2921#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002922{
2923 typedef unique_ptr<_Tp, _Dp> argument_type;
2924 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00002925 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00002926 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002927 {
2928 typedef typename argument_type::pointer pointer;
2929 return hash<pointer>()(__ptr.get());
2930 }
2931};
2932
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933struct __destruct_n
2934{
2935private:
2936 size_t size;
2937
2938 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002939 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2941
2942 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002943 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 {}
2945
Howard Hinnant719bda32011-05-28 14:41:13 +00002946 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002948 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 {}
2950
Howard Hinnant719bda32011-05-28 14:41:13 +00002951 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00002953 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954 {}
2955public:
Howard Hinnant719bda32011-05-28 14:41:13 +00002956 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2957 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958
2959 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002960 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002961 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962
2963 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002964 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002965 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966
2967 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00002968 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00002969 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970};
2971
2972template <class _Alloc>
2973class __allocator_destructor
2974{
2975 typedef allocator_traits<_Alloc> __alloc_traits;
2976public:
2977 typedef typename __alloc_traits::pointer pointer;
2978 typedef typename __alloc_traits::size_type size_type;
2979private:
2980 _Alloc& __alloc_;
2981 size_type __s_;
2982public:
2983 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00002984 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00002986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002987 void operator()(pointer __p) _NOEXCEPT
2988 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989};
2990
2991template <class _InputIterator, class _ForwardIterator>
2992_ForwardIterator
2993uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2994{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00002996#ifndef _LIBCPP_NO_EXCEPTIONS
2997 _ForwardIterator __s = __r;
2998 try
2999 {
3000#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003001 for (; __f != __l; ++__f, (void) ++__r)
3002 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003003#ifndef _LIBCPP_NO_EXCEPTIONS
3004 }
3005 catch (...)
3006 {
3007 for (; __s != __r; ++__s)
3008 __s->~value_type();
3009 throw;
3010 }
3011#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012 return __r;
3013}
3014
3015template <class _InputIterator, class _Size, class _ForwardIterator>
3016_ForwardIterator
3017uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3018{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003020#ifndef _LIBCPP_NO_EXCEPTIONS
3021 _ForwardIterator __s = __r;
3022 try
3023 {
3024#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003025 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3026 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003027#ifndef _LIBCPP_NO_EXCEPTIONS
3028 }
3029 catch (...)
3030 {
3031 for (; __s != __r; ++__s)
3032 __s->~value_type();
3033 throw;
3034 }
3035#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036 return __r;
3037}
3038
3039template <class _ForwardIterator, class _Tp>
3040void
3041uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3042{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003044#ifndef _LIBCPP_NO_EXCEPTIONS
3045 _ForwardIterator __s = __f;
3046 try
3047 {
3048#endif
3049 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003050 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003051#ifndef _LIBCPP_NO_EXCEPTIONS
3052 }
3053 catch (...)
3054 {
3055 for (; __s != __f; ++__s)
3056 __s->~value_type();
3057 throw;
3058 }
3059#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060}
3061
3062template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003063_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3065{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003067#ifndef _LIBCPP_NO_EXCEPTIONS
3068 _ForwardIterator __s = __f;
3069 try
3070 {
3071#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003072 for (; __n > 0; ++__f, (void) --__n)
3073 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003074#ifndef _LIBCPP_NO_EXCEPTIONS
3075 }
3076 catch (...)
3077 {
3078 for (; __s != __f; ++__s)
3079 __s->~value_type();
3080 throw;
3081 }
3082#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003083 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084}
3085
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003086#if _LIBCPP_STD_VER > 14
3087
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003088template <class _Tp>
3089inline _LIBCPP_INLINE_VISIBILITY
3090void destroy_at(_Tp* __loc) {
3091 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3092 __loc->~_Tp();
3093}
3094
3095template <class _ForwardIterator>
3096inline _LIBCPP_INLINE_VISIBILITY
3097void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3098 for (; __first != __last; ++__first)
3099 _VSTD::destroy_at(_VSTD::addressof(*__first));
3100}
3101
3102template <class _ForwardIterator, class _Size>
3103inline _LIBCPP_INLINE_VISIBILITY
3104_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3105 for (; __n > 0; (void)++__first, --__n)
3106 _VSTD::destroy_at(_VSTD::addressof(*__first));
3107 return __first;
3108}
3109
Eric Fiselier290c07c2016-10-11 21:13:44 +00003110template <class _ForwardIterator>
3111inline _LIBCPP_INLINE_VISIBILITY
3112void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3113 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3114 auto __idx = __first;
3115#ifndef _LIBCPP_NO_EXCEPTIONS
3116 try {
3117#endif
3118 for (; __idx != __last; ++__idx)
3119 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3120#ifndef _LIBCPP_NO_EXCEPTIONS
3121 } catch (...) {
3122 _VSTD::destroy(__first, __idx);
3123 throw;
3124 }
3125#endif
3126}
3127
3128template <class _ForwardIterator, class _Size>
3129inline _LIBCPP_INLINE_VISIBILITY
3130_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3131 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3132 auto __idx = __first;
3133#ifndef _LIBCPP_NO_EXCEPTIONS
3134 try {
3135#endif
3136 for (; __n > 0; (void)++__idx, --__n)
3137 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3138 return __idx;
3139#ifndef _LIBCPP_NO_EXCEPTIONS
3140 } catch (...) {
3141 _VSTD::destroy(__first, __idx);
3142 throw;
3143 }
3144#endif
3145}
3146
3147
3148template <class _ForwardIterator>
3149inline _LIBCPP_INLINE_VISIBILITY
3150void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3151 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3152 auto __idx = __first;
3153#ifndef _LIBCPP_NO_EXCEPTIONS
3154 try {
3155#endif
3156 for (; __idx != __last; ++__idx)
3157 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3158#ifndef _LIBCPP_NO_EXCEPTIONS
3159 } catch (...) {
3160 _VSTD::destroy(__first, __idx);
3161 throw;
3162 }
3163#endif
3164}
3165
3166template <class _ForwardIterator, class _Size>
3167inline _LIBCPP_INLINE_VISIBILITY
3168_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3169 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3170 auto __idx = __first;
3171#ifndef _LIBCPP_NO_EXCEPTIONS
3172 try {
3173#endif
3174 for (; __n > 0; (void)++__idx, --__n)
3175 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3176 return __idx;
3177#ifndef _LIBCPP_NO_EXCEPTIONS
3178 } catch (...) {
3179 _VSTD::destroy(__first, __idx);
3180 throw;
3181 }
3182#endif
3183}
3184
3185
3186template <class _InputIt, class _ForwardIt>
3187inline _LIBCPP_INLINE_VISIBILITY
3188_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3189 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3190 auto __idx = __first_res;
3191#ifndef _LIBCPP_NO_EXCEPTIONS
3192 try {
3193#endif
3194 for (; __first != __last; (void)++__idx, ++__first)
3195 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3196 return __idx;
3197#ifndef _LIBCPP_NO_EXCEPTIONS
3198 } catch (...) {
3199 _VSTD::destroy(__first_res, __idx);
3200 throw;
3201 }
3202#endif
3203}
3204
3205template <class _InputIt, class _Size, class _ForwardIt>
3206inline _LIBCPP_INLINE_VISIBILITY
3207pair<_InputIt, _ForwardIt>
3208uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3209 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3210 auto __idx = __first_res;
3211#ifndef _LIBCPP_NO_EXCEPTIONS
3212 try {
3213#endif
3214 for (; __n > 0; ++__idx, (void)++__first, --__n)
3215 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3216 return {__first, __idx};
3217#ifndef _LIBCPP_NO_EXCEPTIONS
3218 } catch (...) {
3219 _VSTD::destroy(__first_res, __idx);
3220 throw;
3221 }
3222#endif
3223}
3224
3225
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003226#endif // _LIBCPP_STD_VER > 14
3227
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003228// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3229// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003230// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003231#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3232 && defined(__ATOMIC_RELAXED) \
3233 && defined(__ATOMIC_ACQ_REL)
3234# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3235#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3236# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3237#endif
3238
3239template <class _Tp>
3240inline _LIBCPP_INLINE_VISIBILITY _Tp
3241__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3242{
3243#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3244 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3245#else
3246 return __t += 1;
3247#endif
3248}
3249
3250template <class _Tp>
3251inline _LIBCPP_INLINE_VISIBILITY _Tp
3252__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3253{
3254#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3255 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3256#else
3257 return __t -= 1;
3258#endif
3259}
3260
Howard Hinnant756c69b2010-09-22 16:48:34 +00003261class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262 : public std::exception
3263{
3264public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003265 virtual ~bad_weak_ptr() _NOEXCEPT;
3266 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267};
3268
Marshall Clow8fea1612016-08-25 15:09:01 +00003269_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3270void __throw_bad_weak_ptr()
3271{
3272#ifndef _LIBCPP_NO_EXCEPTIONS
3273 throw bad_weak_ptr();
3274#else
3275 _VSTD::abort();
3276#endif
3277}
3278
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003279template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003280
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003281class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282{
3283 __shared_count(const __shared_count&);
3284 __shared_count& operator=(const __shared_count&);
3285
3286protected:
3287 long __shared_owners_;
3288 virtual ~__shared_count();
3289private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003290 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003291
3292public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003294 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295 : __shared_owners_(__refs) {}
3296
Eric Fiselier98848572017-01-17 03:16:26 +00003297#if defined(_LIBCPP_BUILDING_MEMORY) && \
3298 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003299 void __add_shared() _NOEXCEPT;
3300 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003301#else
3302 _LIBCPP_INLINE_VISIBILITY
3303 void __add_shared() _NOEXCEPT {
3304 __libcpp_atomic_refcount_increment(__shared_owners_);
3305 }
3306 _LIBCPP_INLINE_VISIBILITY
3307 bool __release_shared() _NOEXCEPT {
3308 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3309 __on_zero_shared();
3310 return true;
3311 }
3312 return false;
3313 }
3314#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003315 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003316 long use_count() const _NOEXCEPT {
3317 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3318 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319};
3320
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003321class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322 : private __shared_count
3323{
3324 long __shared_weak_owners_;
3325
3326public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003328 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 : __shared_count(__refs),
3330 __shared_weak_owners_(__refs) {}
3331protected:
3332 virtual ~__shared_weak_count();
3333
3334public:
Eric Fiselier98848572017-01-17 03:16:26 +00003335#if defined(_LIBCPP_BUILDING_MEMORY) && \
3336 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003337 void __add_shared() _NOEXCEPT;
3338 void __add_weak() _NOEXCEPT;
3339 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003340#else
3341 _LIBCPP_INLINE_VISIBILITY
3342 void __add_shared() _NOEXCEPT {
3343 __shared_count::__add_shared();
3344 }
3345 _LIBCPP_INLINE_VISIBILITY
3346 void __add_weak() _NOEXCEPT {
3347 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3348 }
3349 _LIBCPP_INLINE_VISIBILITY
3350 void __release_shared() _NOEXCEPT {
3351 if (__shared_count::__release_shared())
3352 __release_weak();
3353 }
3354#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003355 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003357 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3358 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359
Howard Hinnant807d6332013-02-25 15:50:36 +00003360 // Define the function out only if we build static libc++ without RTTI.
3361 // Otherwise we may break clients who need to compile their projects with
3362 // -fno-rtti and yet link against a libc++.dylib compiled
3363 // without -fno-rtti.
3364#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003365 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003366#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003368 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369};
3370
3371template <class _Tp, class _Dp, class _Alloc>
3372class __shared_ptr_pointer
3373 : public __shared_weak_count
3374{
3375 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3376public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003379 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380
Howard Hinnant72f73582010-08-11 17:04:31 +00003381#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003382 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003383#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384
3385private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003386 virtual void __on_zero_shared() _NOEXCEPT;
3387 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388};
3389
Howard Hinnant72f73582010-08-11 17:04:31 +00003390#ifndef _LIBCPP_NO_RTTI
3391
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392template <class _Tp, class _Dp, class _Alloc>
3393const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003394__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003396 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397}
3398
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003399#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003400
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401template <class _Tp, class _Dp, class _Alloc>
3402void
Howard Hinnant719bda32011-05-28 14:41:13 +00003403__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404{
3405 __data_.first().second()(__data_.first().first());
3406 __data_.first().second().~_Dp();
3407}
3408
3409template <class _Tp, class _Dp, class _Alloc>
3410void
Howard Hinnant719bda32011-05-28 14:41:13 +00003411__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003413 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3414 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003415 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3416
Eric Fiselierf8898c82015-02-05 23:01:40 +00003417 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003419 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420}
3421
3422template <class _Tp, class _Alloc>
3423class __shared_ptr_emplace
3424 : public __shared_weak_count
3425{
3426 __compressed_pair<_Alloc, _Tp> __data_;
3427public:
3428#ifndef _LIBCPP_HAS_NO_VARIADICS
3429
Howard Hinnant756c69b2010-09-22 16:48:34 +00003430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003432 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433
3434 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003437 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3438 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439
3440#else // _LIBCPP_HAS_NO_VARIADICS
3441
Howard Hinnant756c69b2010-09-22 16:48:34 +00003442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443 __shared_ptr_emplace(_Alloc __a)
3444 : __data_(__a) {}
3445
3446 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3449 : __data_(__a, _Tp(__a0)) {}
3450
3451 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3454 : __data_(__a, _Tp(__a0, __a1)) {}
3455
3456 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3459 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3460
3461#endif // _LIBCPP_HAS_NO_VARIADICS
3462
3463private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003464 virtual void __on_zero_shared() _NOEXCEPT;
3465 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003468 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469};
3470
3471template <class _Tp, class _Alloc>
3472void
Howard Hinnant719bda32011-05-28 14:41:13 +00003473__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474{
3475 __data_.second().~_Tp();
3476}
3477
3478template <class _Tp, class _Alloc>
3479void
Howard Hinnant719bda32011-05-28 14:41:13 +00003480__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003482 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3483 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003484 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003485 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003487 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488}
3489
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003490template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491
3492template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003493class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003495public:
3496 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003497
Eric Fiselierae5b6672016-06-27 01:02:43 +00003498#if _LIBCPP_STD_VER > 14
3499 typedef weak_ptr<_Tp> weak_type;
3500#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501private:
3502 element_type* __ptr_;
3503 __shared_weak_count* __cntrl_;
3504
3505 struct __nat {int __for_bool_;};
3506public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003508 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003510 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003511 template<class _Yp>
3512 explicit shared_ptr(_Yp* __p,
3513 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3514 template<class _Yp, class _Dp>
3515 shared_ptr(_Yp* __p, _Dp __d,
3516 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3517 template<class _Yp, class _Dp, class _Alloc>
3518 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3519 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3521 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003522 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003524 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003528 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003529 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003530#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003532 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003533 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003534 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003535 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003536#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003538 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003539#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003540#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003541 template<class _Yp>
3542 shared_ptr(auto_ptr<_Yp>&& __r,
3543 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003544#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003545 template<class _Yp>
3546 shared_ptr(auto_ptr<_Yp> __r,
3547 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003549#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003550#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003551 template <class _Yp, class _Dp>
3552 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3553 typename enable_if
3554 <
3555 !is_lvalue_reference<_Dp>::value &&
3556 !is_array<_Yp>::value &&
3557 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3558 __nat
3559 >::type = __nat());
3560 template <class _Yp, class _Dp>
3561 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3562 typename enable_if
3563 <
3564 is_lvalue_reference<_Dp>::value &&
3565 !is_array<_Yp>::value &&
3566 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3567 __nat
3568 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003569#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003570 template <class _Yp, class _Dp>
3571 shared_ptr(unique_ptr<_Yp, _Dp>,
3572 typename enable_if
3573 <
3574 !is_lvalue_reference<_Dp>::value &&
3575 !is_array<_Yp>::value &&
3576 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3577 __nat
3578 >::type = __nat());
3579 template <class _Yp, class _Dp>
3580 shared_ptr(unique_ptr<_Yp, _Dp>,
3581 typename enable_if
3582 <
3583 is_lvalue_reference<_Dp>::value &&
3584 !is_array<_Yp>::value &&
3585 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3586 __nat
3587 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003588#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589
3590 ~shared_ptr();
3591
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003593 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003594 template<class _Yp>
3595 typename enable_if
3596 <
3597 is_convertible<_Yp*, element_type*>::value,
3598 shared_ptr&
3599 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003601 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003602#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003604 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003605 template<class _Yp>
3606 typename enable_if
3607 <
3608 is_convertible<_Yp*, element_type*>::value,
3609 shared_ptr<_Tp>&
3610 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003612 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003613#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003614 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003616 typename enable_if
3617 <
3618 !is_array<_Yp>::value &&
3619 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003620 shared_ptr
3621 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003622 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003623#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003624#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003625#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003626 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003628 typename enable_if
3629 <
3630 !is_array<_Yp>::value &&
3631 is_convertible<_Yp*, element_type*>::value,
3632 shared_ptr&
3633 >::type
3634 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003636#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003637 template <class _Yp, class _Dp>
3638 typename enable_if
3639 <
3640 !is_array<_Yp>::value &&
3641 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3642 shared_ptr&
3643 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003644#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003646 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003647#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003649 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650#endif
3651
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003653 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003655 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003656 template<class _Yp>
3657 typename enable_if
3658 <
3659 is_convertible<_Yp*, element_type*>::value,
3660 void
3661 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003663 reset(_Yp* __p);
3664 template<class _Yp, class _Dp>
3665 typename enable_if
3666 <
3667 is_convertible<_Yp*, element_type*>::value,
3668 void
3669 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003671 reset(_Yp* __p, _Dp __d);
3672 template<class _Yp, class _Dp, class _Alloc>
3673 typename enable_if
3674 <
3675 is_convertible<_Yp*, element_type*>::value,
3676 void
3677 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003679 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680
Howard Hinnant756c69b2010-09-22 16:48:34 +00003681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003682 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003684 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3685 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003687 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003689 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003691 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003693 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003694 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003695 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003696 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003698 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003699 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003700 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003702 _LIBCPP_INLINE_VISIBILITY
3703 bool
3704 __owner_equivalent(const shared_ptr& __p) const
3705 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706
Howard Hinnant72f73582010-08-11 17:04:31 +00003707#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003710 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003712#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713
3714#ifndef _LIBCPP_HAS_NO_VARIADICS
3715
3716 template<class ..._Args>
3717 static
3718 shared_ptr<_Tp>
3719 make_shared(_Args&& ...__args);
3720
3721 template<class _Alloc, class ..._Args>
3722 static
3723 shared_ptr<_Tp>
3724 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3725
3726#else // _LIBCPP_HAS_NO_VARIADICS
3727
3728 static shared_ptr<_Tp> make_shared();
3729
3730 template<class _A0>
3731 static shared_ptr<_Tp> make_shared(_A0&);
3732
3733 template<class _A0, class _A1>
3734 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3735
3736 template<class _A0, class _A1, class _A2>
3737 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3738
3739 template<class _Alloc>
3740 static shared_ptr<_Tp>
3741 allocate_shared(const _Alloc& __a);
3742
3743 template<class _Alloc, class _A0>
3744 static shared_ptr<_Tp>
3745 allocate_shared(const _Alloc& __a, _A0& __a0);
3746
3747 template<class _Alloc, class _A0, class _A1>
3748 static shared_ptr<_Tp>
3749 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3750
3751 template<class _Alloc, class _A0, class _A1, class _A2>
3752 static shared_ptr<_Tp>
3753 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3754
3755#endif // _LIBCPP_HAS_NO_VARIADICS
3756
3757private:
3758
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003759 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003762 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3763 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003765 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003766 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003767 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003768 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3769 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003770 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771 }
3772
Howard Hinnant756c69b2010-09-22 16:48:34 +00003773 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003774 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003776 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3777 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778};
3779
3780template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003781inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003782_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003783shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784 : __ptr_(0),
3785 __cntrl_(0)
3786{
3787}
3788
3789template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003790inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003791_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003792shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793 : __ptr_(0),
3794 __cntrl_(0)
3795{
3796}
3797
3798template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003799template<class _Yp>
3800shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3801 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802 : __ptr_(__p)
3803{
3804 unique_ptr<_Yp> __hold(__p);
3805 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3806 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3807 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003808 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809}
3810
3811template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003812template<class _Yp, class _Dp>
3813shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3814 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815 : __ptr_(__p)
3816{
3817#ifndef _LIBCPP_NO_EXCEPTIONS
3818 try
3819 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003820#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3822 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003823 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824#ifndef _LIBCPP_NO_EXCEPTIONS
3825 }
3826 catch (...)
3827 {
3828 __d(__p);
3829 throw;
3830 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003831#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832}
3833
3834template<class _Tp>
3835template<class _Dp>
3836shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3837 : __ptr_(0)
3838{
3839#ifndef _LIBCPP_NO_EXCEPTIONS
3840 try
3841 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003842#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3844 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3845#ifndef _LIBCPP_NO_EXCEPTIONS
3846 }
3847 catch (...)
3848 {
3849 __d(__p);
3850 throw;
3851 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003852#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853}
3854
3855template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003856template<class _Yp, class _Dp, class _Alloc>
3857shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3858 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859 : __ptr_(__p)
3860{
3861#ifndef _LIBCPP_NO_EXCEPTIONS
3862 try
3863 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003864#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003866 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 typedef __allocator_destructor<_A2> _D2;
3868 _A2 __a2(__a);
3869 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003870 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3871 _CntrlBlk(__p, __d, __a);
3872 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003873 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874#ifndef _LIBCPP_NO_EXCEPTIONS
3875 }
3876 catch (...)
3877 {
3878 __d(__p);
3879 throw;
3880 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003881#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882}
3883
3884template<class _Tp>
3885template<class _Dp, class _Alloc>
3886shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3887 : __ptr_(0)
3888{
3889#ifndef _LIBCPP_NO_EXCEPTIONS
3890 try
3891 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003892#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003893 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003894 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895 typedef __allocator_destructor<_A2> _D2;
3896 _A2 __a2(__a);
3897 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003898 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
3899 _CntrlBlk(__p, __d, __a);
3900 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901#ifndef _LIBCPP_NO_EXCEPTIONS
3902 }
3903 catch (...)
3904 {
3905 __d(__p);
3906 throw;
3907 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003908#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909}
3910
3911template<class _Tp>
3912template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003913inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003914shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 : __ptr_(__p),
3916 __cntrl_(__r.__cntrl_)
3917{
3918 if (__cntrl_)
3919 __cntrl_->__add_shared();
3920}
3921
3922template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003923inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003924shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 : __ptr_(__r.__ptr_),
3926 __cntrl_(__r.__cntrl_)
3927{
3928 if (__cntrl_)
3929 __cntrl_->__add_shared();
3930}
3931
3932template<class _Tp>
3933template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003934inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003936 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003937 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003938 : __ptr_(__r.__ptr_),
3939 __cntrl_(__r.__cntrl_)
3940{
3941 if (__cntrl_)
3942 __cntrl_->__add_shared();
3943}
3944
Howard Hinnant74279a52010-09-04 23:28:19 +00003945#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003946
3947template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003948inline
Howard Hinnant719bda32011-05-28 14:41:13 +00003949shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 : __ptr_(__r.__ptr_),
3951 __cntrl_(__r.__cntrl_)
3952{
3953 __r.__ptr_ = 0;
3954 __r.__cntrl_ = 0;
3955}
3956
3957template<class _Tp>
3958template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003959inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003961 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00003962 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963 : __ptr_(__r.__ptr_),
3964 __cntrl_(__r.__cntrl_)
3965{
3966 __r.__ptr_ = 0;
3967 __r.__cntrl_ = 0;
3968}
3969
Howard Hinnant74279a52010-09-04 23:28:19 +00003970#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971
Marshall Clowb22274f2017-01-24 22:22:33 +00003972#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003974template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00003975#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003976shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003978shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003980 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981 : __ptr_(__r.get())
3982{
3983 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3984 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003985 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986 __r.release();
3987}
Marshall Clowb22274f2017-01-24 22:22:33 +00003988#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989
3990template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003991template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00003992#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3994#else
3995shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3996#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00003997 typename enable_if
3998 <
3999 !is_lvalue_reference<_Dp>::value &&
4000 !is_array<_Yp>::value &&
4001 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4002 __nat
4003 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004 : __ptr_(__r.get())
4005{
Marshall Clow35cde742015-05-10 13:59:45 +00004006#if _LIBCPP_STD_VER > 11
4007 if (__ptr_ == nullptr)
4008 __cntrl_ = nullptr;
4009 else
4010#endif
4011 {
4012 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4013 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004014 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004015 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016 __r.release();
4017}
4018
4019template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004020template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4023#else
4024shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4025#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004026 typename enable_if
4027 <
4028 is_lvalue_reference<_Dp>::value &&
4029 !is_array<_Yp>::value &&
4030 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4031 __nat
4032 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033 : __ptr_(__r.get())
4034{
Marshall Clow35cde742015-05-10 13:59:45 +00004035#if _LIBCPP_STD_VER > 11
4036 if (__ptr_ == nullptr)
4037 __cntrl_ = nullptr;
4038 else
4039#endif
4040 {
4041 typedef __shared_ptr_pointer<_Yp*,
4042 reference_wrapper<typename remove_reference<_Dp>::type>,
4043 allocator<_Yp> > _CntrlBlk;
4044 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004045 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004046 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047 __r.release();
4048}
4049
4050#ifndef _LIBCPP_HAS_NO_VARIADICS
4051
4052template<class _Tp>
4053template<class ..._Args>
4054shared_ptr<_Tp>
4055shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4056{
4057 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4058 typedef allocator<_CntrlBlk> _A2;
4059 typedef __allocator_destructor<_A2> _D2;
4060 _A2 __a2;
4061 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004062 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063 shared_ptr<_Tp> __r;
4064 __r.__ptr_ = __hold2.get()->get();
4065 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004066 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067 return __r;
4068}
4069
4070template<class _Tp>
4071template<class _Alloc, class ..._Args>
4072shared_ptr<_Tp>
4073shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4074{
4075 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004076 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077 typedef __allocator_destructor<_A2> _D2;
4078 _A2 __a2(__a);
4079 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004080 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4081 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082 shared_ptr<_Tp> __r;
4083 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004084 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004085 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086 return __r;
4087}
4088
4089#else // _LIBCPP_HAS_NO_VARIADICS
4090
4091template<class _Tp>
4092shared_ptr<_Tp>
4093shared_ptr<_Tp>::make_shared()
4094{
4095 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4096 typedef allocator<_CntrlBlk> _Alloc2;
4097 typedef __allocator_destructor<_Alloc2> _D2;
4098 _Alloc2 __alloc2;
4099 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4100 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4101 shared_ptr<_Tp> __r;
4102 __r.__ptr_ = __hold2.get()->get();
4103 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004104 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105 return __r;
4106}
4107
4108template<class _Tp>
4109template<class _A0>
4110shared_ptr<_Tp>
4111shared_ptr<_Tp>::make_shared(_A0& __a0)
4112{
4113 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4114 typedef allocator<_CntrlBlk> _Alloc2;
4115 typedef __allocator_destructor<_Alloc2> _D2;
4116 _Alloc2 __alloc2;
4117 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4118 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4119 shared_ptr<_Tp> __r;
4120 __r.__ptr_ = __hold2.get()->get();
4121 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004122 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123 return __r;
4124}
4125
4126template<class _Tp>
4127template<class _A0, class _A1>
4128shared_ptr<_Tp>
4129shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4130{
4131 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4132 typedef allocator<_CntrlBlk> _Alloc2;
4133 typedef __allocator_destructor<_Alloc2> _D2;
4134 _Alloc2 __alloc2;
4135 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4136 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4137 shared_ptr<_Tp> __r;
4138 __r.__ptr_ = __hold2.get()->get();
4139 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004140 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141 return __r;
4142}
4143
4144template<class _Tp>
4145template<class _A0, class _A1, class _A2>
4146shared_ptr<_Tp>
4147shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4148{
4149 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4150 typedef allocator<_CntrlBlk> _Alloc2;
4151 typedef __allocator_destructor<_Alloc2> _D2;
4152 _Alloc2 __alloc2;
4153 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4154 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4155 shared_ptr<_Tp> __r;
4156 __r.__ptr_ = __hold2.get()->get();
4157 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004158 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159 return __r;
4160}
4161
4162template<class _Tp>
4163template<class _Alloc>
4164shared_ptr<_Tp>
4165shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4166{
4167 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004168 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 typedef __allocator_destructor<_Alloc2> _D2;
4170 _Alloc2 __alloc2(__a);
4171 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004172 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4173 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174 shared_ptr<_Tp> __r;
4175 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004176 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004177 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178 return __r;
4179}
4180
4181template<class _Tp>
4182template<class _Alloc, class _A0>
4183shared_ptr<_Tp>
4184shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4185{
4186 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004187 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188 typedef __allocator_destructor<_Alloc2> _D2;
4189 _Alloc2 __alloc2(__a);
4190 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004191 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4192 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193 shared_ptr<_Tp> __r;
4194 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004195 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004196 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197 return __r;
4198}
4199
4200template<class _Tp>
4201template<class _Alloc, class _A0, class _A1>
4202shared_ptr<_Tp>
4203shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4204{
4205 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004206 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207 typedef __allocator_destructor<_Alloc2> _D2;
4208 _Alloc2 __alloc2(__a);
4209 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004210 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4211 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212 shared_ptr<_Tp> __r;
4213 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004214 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004215 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216 return __r;
4217}
4218
4219template<class _Tp>
4220template<class _Alloc, class _A0, class _A1, class _A2>
4221shared_ptr<_Tp>
4222shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4223{
4224 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004225 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226 typedef __allocator_destructor<_Alloc2> _D2;
4227 _Alloc2 __alloc2(__a);
4228 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004229 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4230 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231 shared_ptr<_Tp> __r;
4232 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004233 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004234 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235 return __r;
4236}
4237
4238#endif // _LIBCPP_HAS_NO_VARIADICS
4239
4240template<class _Tp>
4241shared_ptr<_Tp>::~shared_ptr()
4242{
4243 if (__cntrl_)
4244 __cntrl_->__release_shared();
4245}
4246
4247template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004250shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251{
4252 shared_ptr(__r).swap(*this);
4253 return *this;
4254}
4255
4256template<class _Tp>
4257template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004258inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004259typename enable_if
4260<
Marshall Clow7e384b72017-01-10 16:59:33 +00004261 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004262 shared_ptr<_Tp>&
4263>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004264shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265{
4266 shared_ptr(__r).swap(*this);
4267 return *this;
4268}
4269
Howard Hinnant74279a52010-09-04 23:28:19 +00004270#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271
4272template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004273inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004275shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 return *this;
4279}
4280
4281template<class _Tp>
4282template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004283inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004284typename enable_if
4285<
Marshall Clow7e384b72017-01-10 16:59:33 +00004286 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004287 shared_ptr<_Tp>&
4288>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4290{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004291 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 return *this;
4293}
4294
Marshall Clowb22274f2017-01-24 22:22:33 +00004295#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296template<class _Tp>
4297template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004298inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004299typename enable_if
4300<
4301 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004302 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004303 shared_ptr<_Tp>
4304>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4306{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004307 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308 return *this;
4309}
Marshall Clowb22274f2017-01-24 22:22:33 +00004310#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311
4312template<class _Tp>
4313template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004314inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004315typename enable_if
4316<
4317 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004318 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4319 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004320 shared_ptr<_Tp>&
4321>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4323{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004324 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325 return *this;
4326}
4327
Howard Hinnant74279a52010-09-04 23:28:19 +00004328#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329
Marshall Clowb22274f2017-01-24 22:22:33 +00004330#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331template<class _Tp>
4332template<class _Yp>
4333inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004334typename enable_if
4335<
4336 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004337 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004338 shared_ptr<_Tp>&
4339>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004340shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341{
4342 shared_ptr(__r).swap(*this);
4343 return *this;
4344}
Marshall Clowb22274f2017-01-24 22:22:33 +00004345#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004346
4347template<class _Tp>
4348template <class _Yp, class _Dp>
4349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004350typename enable_if
4351<
4352 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004353 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4354 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004355 shared_ptr<_Tp>&
4356>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4358{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004359 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360 return *this;
4361}
4362
Howard Hinnant74279a52010-09-04 23:28:19 +00004363#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364
4365template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004366inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367void
Howard Hinnant719bda32011-05-28 14:41:13 +00004368shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004370 _VSTD::swap(__ptr_, __r.__ptr_);
4371 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372}
4373
4374template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004375inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376void
Howard Hinnant719bda32011-05-28 14:41:13 +00004377shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378{
4379 shared_ptr().swap(*this);
4380}
4381
4382template<class _Tp>
4383template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004384inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004385typename enable_if
4386<
Marshall Clow7e384b72017-01-10 16:59:33 +00004387 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004388 void
4389>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390shared_ptr<_Tp>::reset(_Yp* __p)
4391{
4392 shared_ptr(__p).swap(*this);
4393}
4394
4395template<class _Tp>
4396template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004397inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004398typename enable_if
4399<
Marshall Clow7e384b72017-01-10 16:59:33 +00004400 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004401 void
4402>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4404{
4405 shared_ptr(__p, __d).swap(*this);
4406}
4407
4408template<class _Tp>
4409template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004410inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004411typename enable_if
4412<
Marshall Clow7e384b72017-01-10 16:59:33 +00004413 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004414 void
4415>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4417{
4418 shared_ptr(__p, __d, __a).swap(*this);
4419}
4420
4421#ifndef _LIBCPP_HAS_NO_VARIADICS
4422
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004423template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004424inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004425typename enable_if
4426<
4427 !is_array<_Tp>::value,
4428 shared_ptr<_Tp>
4429>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430make_shared(_Args&& ...__args)
4431{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004432 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433}
4434
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004435template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004437typename enable_if
4438<
4439 !is_array<_Tp>::value,
4440 shared_ptr<_Tp>
4441>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442allocate_shared(const _Alloc& __a, _Args&& ...__args)
4443{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004444 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445}
4446
4447#else // _LIBCPP_HAS_NO_VARIADICS
4448
4449template<class _Tp>
4450inline _LIBCPP_INLINE_VISIBILITY
4451shared_ptr<_Tp>
4452make_shared()
4453{
4454 return shared_ptr<_Tp>::make_shared();
4455}
4456
4457template<class _Tp, class _A0>
4458inline _LIBCPP_INLINE_VISIBILITY
4459shared_ptr<_Tp>
4460make_shared(_A0& __a0)
4461{
4462 return shared_ptr<_Tp>::make_shared(__a0);
4463}
4464
4465template<class _Tp, class _A0, class _A1>
4466inline _LIBCPP_INLINE_VISIBILITY
4467shared_ptr<_Tp>
4468make_shared(_A0& __a0, _A1& __a1)
4469{
4470 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4471}
4472
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004473template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474inline _LIBCPP_INLINE_VISIBILITY
4475shared_ptr<_Tp>
4476make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4477{
4478 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4479}
4480
4481template<class _Tp, class _Alloc>
4482inline _LIBCPP_INLINE_VISIBILITY
4483shared_ptr<_Tp>
4484allocate_shared(const _Alloc& __a)
4485{
4486 return shared_ptr<_Tp>::allocate_shared(__a);
4487}
4488
4489template<class _Tp, class _Alloc, class _A0>
4490inline _LIBCPP_INLINE_VISIBILITY
4491shared_ptr<_Tp>
4492allocate_shared(const _Alloc& __a, _A0& __a0)
4493{
4494 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4495}
4496
4497template<class _Tp, class _Alloc, class _A0, class _A1>
4498inline _LIBCPP_INLINE_VISIBILITY
4499shared_ptr<_Tp>
4500allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4501{
4502 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4503}
4504
4505template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4506inline _LIBCPP_INLINE_VISIBILITY
4507shared_ptr<_Tp>
4508allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4509{
4510 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4511}
4512
4513#endif // _LIBCPP_HAS_NO_VARIADICS
4514
4515template<class _Tp, class _Up>
4516inline _LIBCPP_INLINE_VISIBILITY
4517bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004518operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519{
4520 return __x.get() == __y.get();
4521}
4522
4523template<class _Tp, class _Up>
4524inline _LIBCPP_INLINE_VISIBILITY
4525bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004526operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004527{
4528 return !(__x == __y);
4529}
4530
4531template<class _Tp, class _Up>
4532inline _LIBCPP_INLINE_VISIBILITY
4533bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004534operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004535{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004536 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4537 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004538}
4539
4540template<class _Tp, class _Up>
4541inline _LIBCPP_INLINE_VISIBILITY
4542bool
4543operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4544{
4545 return __y < __x;
4546}
4547
4548template<class _Tp, class _Up>
4549inline _LIBCPP_INLINE_VISIBILITY
4550bool
4551operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4552{
4553 return !(__y < __x);
4554}
4555
4556template<class _Tp, class _Up>
4557inline _LIBCPP_INLINE_VISIBILITY
4558bool
4559operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4560{
4561 return !(__x < __y);
4562}
4563
4564template<class _Tp>
4565inline _LIBCPP_INLINE_VISIBILITY
4566bool
4567operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4568{
4569 return !__x;
4570}
4571
4572template<class _Tp>
4573inline _LIBCPP_INLINE_VISIBILITY
4574bool
4575operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4576{
4577 return !__x;
4578}
4579
4580template<class _Tp>
4581inline _LIBCPP_INLINE_VISIBILITY
4582bool
4583operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4584{
4585 return static_cast<bool>(__x);
4586}
4587
4588template<class _Tp>
4589inline _LIBCPP_INLINE_VISIBILITY
4590bool
4591operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4592{
4593 return static_cast<bool>(__x);
4594}
4595
4596template<class _Tp>
4597inline _LIBCPP_INLINE_VISIBILITY
4598bool
4599operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4600{
4601 return less<_Tp*>()(__x.get(), nullptr);
4602}
4603
4604template<class _Tp>
4605inline _LIBCPP_INLINE_VISIBILITY
4606bool
4607operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4608{
4609 return less<_Tp*>()(nullptr, __x.get());
4610}
4611
4612template<class _Tp>
4613inline _LIBCPP_INLINE_VISIBILITY
4614bool
4615operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4616{
4617 return nullptr < __x;
4618}
4619
4620template<class _Tp>
4621inline _LIBCPP_INLINE_VISIBILITY
4622bool
4623operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4624{
4625 return __x < nullptr;
4626}
4627
4628template<class _Tp>
4629inline _LIBCPP_INLINE_VISIBILITY
4630bool
4631operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4632{
4633 return !(nullptr < __x);
4634}
4635
4636template<class _Tp>
4637inline _LIBCPP_INLINE_VISIBILITY
4638bool
4639operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4640{
4641 return !(__x < nullptr);
4642}
4643
4644template<class _Tp>
4645inline _LIBCPP_INLINE_VISIBILITY
4646bool
4647operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4648{
4649 return !(__x < nullptr);
4650}
4651
4652template<class _Tp>
4653inline _LIBCPP_INLINE_VISIBILITY
4654bool
4655operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4656{
4657 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004658}
4659
4660template<class _Tp>
4661inline _LIBCPP_INLINE_VISIBILITY
4662void
Howard Hinnant719bda32011-05-28 14:41:13 +00004663swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664{
4665 __x.swap(__y);
4666}
4667
4668template<class _Tp, class _Up>
4669inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004670typename enable_if
4671<
4672 !is_array<_Tp>::value && !is_array<_Up>::value,
4673 shared_ptr<_Tp>
4674>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004675static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004676{
4677 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4678}
4679
4680template<class _Tp, class _Up>
4681inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004682typename enable_if
4683<
4684 !is_array<_Tp>::value && !is_array<_Up>::value,
4685 shared_ptr<_Tp>
4686>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004687dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004688{
4689 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4690 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4691}
4692
4693template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004694typename enable_if
4695<
4696 is_array<_Tp>::value == is_array<_Up>::value,
4697 shared_ptr<_Tp>
4698>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004699const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004700{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004701 typedef typename remove_extent<_Tp>::type _RTp;
4702 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004703}
4704
Howard Hinnant72f73582010-08-11 17:04:31 +00004705#ifndef _LIBCPP_NO_RTTI
4706
Howard Hinnantc51e1022010-05-11 19:42:16 +00004707template<class _Dp, class _Tp>
4708inline _LIBCPP_INLINE_VISIBILITY
4709_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004710get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004711{
4712 return __p.template __get_deleter<_Dp>();
4713}
4714
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004715#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004716
Howard Hinnantc51e1022010-05-11 19:42:16 +00004717template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004718class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004719{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004720public:
4721 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004722private:
4723 element_type* __ptr_;
4724 __shared_weak_count* __cntrl_;
4725
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004726public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004728 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004729 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004730 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4731 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004733 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004734 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004735 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4736 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004737
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004738#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004740 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004741 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004742 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4743 _NOEXCEPT;
4744#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004745 ~weak_ptr();
4746
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004748 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004749 template<class _Yp>
4750 typename enable_if
4751 <
4752 is_convertible<_Yp*, element_type*>::value,
4753 weak_ptr&
4754 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004756 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4757
4758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4759
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004761 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4762 template<class _Yp>
4763 typename enable_if
4764 <
4765 is_convertible<_Yp*, element_type*>::value,
4766 weak_ptr&
4767 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004769 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4770
4771#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4772
4773 template<class _Yp>
4774 typename enable_if
4775 <
4776 is_convertible<_Yp*, element_type*>::value,
4777 weak_ptr&
4778 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004780 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004781
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004783 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004785 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004786
Howard Hinnant756c69b2010-09-22 16:48:34 +00004787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004788 long use_count() const _NOEXCEPT
4789 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004791 bool expired() const _NOEXCEPT
4792 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4793 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004794 template<class _Up>
4795 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004796 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004797 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004798 template<class _Up>
4799 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004800 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004801 {return __cntrl_ < __r.__cntrl_;}
4802
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004803 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4804 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004805};
4806
4807template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004808inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004809_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004810weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004811 : __ptr_(0),
4812 __cntrl_(0)
4813{
4814}
4815
4816template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004817inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004818weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004819 : __ptr_(__r.__ptr_),
4820 __cntrl_(__r.__cntrl_)
4821{
4822 if (__cntrl_)
4823 __cntrl_->__add_weak();
4824}
4825
4826template<class _Tp>
4827template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004828inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004829weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004830 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004831 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004832 : __ptr_(__r.__ptr_),
4833 __cntrl_(__r.__cntrl_)
4834{
4835 if (__cntrl_)
4836 __cntrl_->__add_weak();
4837}
4838
4839template<class _Tp>
4840template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004841inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004843 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004844 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004845 : __ptr_(__r.__ptr_),
4846 __cntrl_(__r.__cntrl_)
4847{
4848 if (__cntrl_)
4849 __cntrl_->__add_weak();
4850}
4851
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4853
4854template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004855inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004856weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4857 : __ptr_(__r.__ptr_),
4858 __cntrl_(__r.__cntrl_)
4859{
4860 __r.__ptr_ = 0;
4861 __r.__cntrl_ = 0;
4862}
4863
4864template<class _Tp>
4865template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004866inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004867weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4868 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4869 _NOEXCEPT
4870 : __ptr_(__r.__ptr_),
4871 __cntrl_(__r.__cntrl_)
4872{
4873 __r.__ptr_ = 0;
4874 __r.__cntrl_ = 0;
4875}
4876
4877#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4878
Howard Hinnantc51e1022010-05-11 19:42:16 +00004879template<class _Tp>
4880weak_ptr<_Tp>::~weak_ptr()
4881{
4882 if (__cntrl_)
4883 __cntrl_->__release_weak();
4884}
4885
4886template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004887inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004888weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004889weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004890{
4891 weak_ptr(__r).swap(*this);
4892 return *this;
4893}
4894
4895template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004896template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004897inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004898typename enable_if
4899<
4900 is_convertible<_Yp*, _Tp*>::value,
4901 weak_ptr<_Tp>&
4902>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004903weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004904{
4905 weak_ptr(__r).swap(*this);
4906 return *this;
4907}
4908
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4910
4911template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004912inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004913weak_ptr<_Tp>&
4914weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4915{
4916 weak_ptr(_VSTD::move(__r)).swap(*this);
4917 return *this;
4918}
4919
Howard Hinnantc51e1022010-05-11 19:42:16 +00004920template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004921template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004922inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004923typename enable_if
4924<
4925 is_convertible<_Yp*, _Tp*>::value,
4926 weak_ptr<_Tp>&
4927>::type
4928weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4929{
4930 weak_ptr(_VSTD::move(__r)).swap(*this);
4931 return *this;
4932}
4933
4934#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4935
4936template<class _Tp>
4937template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004938inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004939typename enable_if
4940<
4941 is_convertible<_Yp*, _Tp*>::value,
4942 weak_ptr<_Tp>&
4943>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004944weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004945{
4946 weak_ptr(__r).swap(*this);
4947 return *this;
4948}
4949
4950template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004951inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004952void
Howard Hinnant719bda32011-05-28 14:41:13 +00004953weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004954{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004955 _VSTD::swap(__ptr_, __r.__ptr_);
4956 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004957}
4958
4959template<class _Tp>
4960inline _LIBCPP_INLINE_VISIBILITY
4961void
Howard Hinnant719bda32011-05-28 14:41:13 +00004962swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963{
4964 __x.swap(__y);
4965}
4966
4967template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004968inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969void
Howard Hinnant719bda32011-05-28 14:41:13 +00004970weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004971{
4972 weak_ptr().swap(*this);
4973}
4974
4975template<class _Tp>
4976template<class _Yp>
4977shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004978 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004979 : __ptr_(__r.__ptr_),
4980 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4981{
4982 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00004983 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004984}
4985
4986template<class _Tp>
4987shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00004988weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004989{
4990 shared_ptr<_Tp> __r;
4991 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4992 if (__r.__cntrl_)
4993 __r.__ptr_ = __ptr_;
4994 return __r;
4995}
4996
Marshall Clow3c2d8a42015-11-12 15:56:44 +00004997#if _LIBCPP_STD_VER > 14
4998template <class _Tp = void> struct owner_less;
4999#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005000template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005001#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005002
5003template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005004struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005005 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005006{
5007 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005008 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005009 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005010 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005011 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005012 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005013 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005014 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005015 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005016 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005017};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018
5019template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005020struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005021 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5022{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005023 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005024 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005025 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005026 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005027 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005028 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005029 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005030 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005031 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005032 {return __x.owner_before(__y);}
5033};
5034
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005035#if _LIBCPP_STD_VER > 14
5036template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005037struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005038{
5039 template <class _Tp, class _Up>
5040 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005041 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005042 {return __x.owner_before(__y);}
5043 template <class _Tp, class _Up>
5044 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005045 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005046 {return __x.owner_before(__y);}
5047 template <class _Tp, class _Up>
5048 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005049 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005050 {return __x.owner_before(__y);}
5051 template <class _Tp, class _Up>
5052 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005053 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005054 {return __x.owner_before(__y);}
5055 typedef void is_transparent;
5056};
5057#endif
5058
Howard Hinnantc51e1022010-05-11 19:42:16 +00005059template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005060class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005061{
5062 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005063protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005064 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005065 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005067 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005069 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5070 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005072 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005073public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005075 shared_ptr<_Tp> shared_from_this()
5076 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005078 shared_ptr<_Tp const> shared_from_this() const
5079 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005080
Eric Fiselier84006862016-06-02 00:15:35 +00005081#if _LIBCPP_STD_VER > 14
5082 _LIBCPP_INLINE_VISIBILITY
5083 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5084 { return __weak_this_; }
5085
5086 _LIBCPP_INLINE_VISIBILITY
5087 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5088 { return __weak_this_; }
5089#endif // _LIBCPP_STD_VER > 14
5090
Howard Hinnantc51e1022010-05-11 19:42:16 +00005091 template <class _Up> friend class shared_ptr;
5092};
5093
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005094template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005095struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005096{
5097 typedef shared_ptr<_Tp> argument_type;
5098 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005099
Howard Hinnant756c69b2010-09-22 16:48:34 +00005100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005101 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005102 {
5103 return hash<_Tp*>()(__ptr.get());
5104 }
5105};
5106
Howard Hinnantc834c512011-11-29 18:15:50 +00005107template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005108inline _LIBCPP_INLINE_VISIBILITY
5109basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005110operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005111
Eric Fiselier9b492672016-06-18 02:12:53 +00005112
5113#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005114
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005115class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005116{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005117 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005118public:
5119 void lock() _NOEXCEPT;
5120 void unlock() _NOEXCEPT;
5121
5122private:
5123 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5124 __sp_mut(const __sp_mut&);
5125 __sp_mut& operator=(const __sp_mut&);
5126
Howard Hinnant8331b762013-03-06 23:30:19 +00005127 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005128};
5129
Howard Hinnant8331b762013-03-06 23:30:19 +00005130_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005131
5132template <class _Tp>
5133inline _LIBCPP_INLINE_VISIBILITY
5134bool
5135atomic_is_lock_free(const shared_ptr<_Tp>*)
5136{
5137 return false;
5138}
5139
5140template <class _Tp>
5141shared_ptr<_Tp>
5142atomic_load(const shared_ptr<_Tp>* __p)
5143{
5144 __sp_mut& __m = __get_sp_mut(__p);
5145 __m.lock();
5146 shared_ptr<_Tp> __q = *__p;
5147 __m.unlock();
5148 return __q;
5149}
5150
5151template <class _Tp>
5152inline _LIBCPP_INLINE_VISIBILITY
5153shared_ptr<_Tp>
5154atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5155{
5156 return atomic_load(__p);
5157}
5158
5159template <class _Tp>
5160void
5161atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5162{
5163 __sp_mut& __m = __get_sp_mut(__p);
5164 __m.lock();
5165 __p->swap(__r);
5166 __m.unlock();
5167}
5168
5169template <class _Tp>
5170inline _LIBCPP_INLINE_VISIBILITY
5171void
5172atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5173{
5174 atomic_store(__p, __r);
5175}
5176
5177template <class _Tp>
5178shared_ptr<_Tp>
5179atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5180{
5181 __sp_mut& __m = __get_sp_mut(__p);
5182 __m.lock();
5183 __p->swap(__r);
5184 __m.unlock();
5185 return __r;
5186}
5187
5188template <class _Tp>
5189inline _LIBCPP_INLINE_VISIBILITY
5190shared_ptr<_Tp>
5191atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5192{
5193 return atomic_exchange(__p, __r);
5194}
5195
5196template <class _Tp>
5197bool
5198atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5199{
Marshall Clow4201ee82016-05-18 17:50:13 +00005200 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005201 __sp_mut& __m = __get_sp_mut(__p);
5202 __m.lock();
5203 if (__p->__owner_equivalent(*__v))
5204 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005205 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005206 *__p = __w;
5207 __m.unlock();
5208 return true;
5209 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005210 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005211 *__v = *__p;
5212 __m.unlock();
5213 return false;
5214}
5215
5216template <class _Tp>
5217inline _LIBCPP_INLINE_VISIBILITY
5218bool
5219atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5220{
5221 return atomic_compare_exchange_strong(__p, __v, __w);
5222}
5223
5224template <class _Tp>
5225inline _LIBCPP_INLINE_VISIBILITY
5226bool
5227atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5228 shared_ptr<_Tp> __w, memory_order, memory_order)
5229{
5230 return atomic_compare_exchange_strong(__p, __v, __w);
5231}
5232
5233template <class _Tp>
5234inline _LIBCPP_INLINE_VISIBILITY
5235bool
5236atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5237 shared_ptr<_Tp> __w, memory_order, memory_order)
5238{
5239 return atomic_compare_exchange_weak(__p, __v, __w);
5240}
5241
Eric Fiselier9b492672016-06-18 02:12:53 +00005242#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005243
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005244//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005245#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5246# ifndef _LIBCPP_CXX03_LANG
5247enum class pointer_safety : unsigned char {
5248 relaxed,
5249 preferred,
5250 strict
5251};
5252# endif
5253#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005254struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005255{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005256 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005257 {
5258 relaxed,
5259 preferred,
5260 strict
5261 };
5262
Howard Hinnant49e145e2012-10-30 19:06:59 +00005263 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005264
Howard Hinnant756c69b2010-09-22 16:48:34 +00005265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005266 pointer_safety() : __v_() {}
5267
5268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005269 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005271 operator int() const {return __v_;}
5272};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005273#endif
5274
5275#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5276 defined(_LIBCPP_BUILDING_MEMORY)
5277_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5278#else
5279// This function is only offered in C++03 under ABI v1.
5280# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5281inline _LIBCPP_INLINE_VISIBILITY
5282pointer_safety get_pointer_safety() _NOEXCEPT {
5283 return pointer_safety::relaxed;
5284}
5285# endif
5286#endif
5287
Howard Hinnantc51e1022010-05-11 19:42:16 +00005288
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005289_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5290_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5291_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005292_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005293
5294template <class _Tp>
5295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005296_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005297undeclare_reachable(_Tp* __p)
5298{
5299 return static_cast<_Tp*>(__undeclare_reachable(__p));
5300}
5301
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005302_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005303
Marshall Clow8982dcd2015-07-13 20:04:56 +00005304// --- Helper for container swap --
5305template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005306inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005307void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5308#if _LIBCPP_STD_VER >= 14
5309 _NOEXCEPT
5310#else
5311 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5312#endif
5313{
5314 __swap_allocator(__a1, __a2,
5315 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5316}
5317
5318template <typename _Alloc>
5319_LIBCPP_INLINE_VISIBILITY
5320void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5321#if _LIBCPP_STD_VER >= 14
5322 _NOEXCEPT
5323#else
5324 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5325#endif
5326{
5327 using _VSTD::swap;
5328 swap(__a1, __a2);
5329}
5330
5331template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005332inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005333void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5334
Marshall Clowff91de82015-08-18 19:51:37 +00005335template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005336struct __noexcept_move_assign_container : public integral_constant<bool,
5337 _Traits::propagate_on_container_move_assignment::value
5338#if _LIBCPP_STD_VER > 14
5339 || _Traits::is_always_equal::value
5340#else
5341 && is_nothrow_move_assignable<_Alloc>::value
5342#endif
5343 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005344
Marshall Clowa591b9a2016-07-11 21:38:08 +00005345
5346#ifndef _LIBCPP_HAS_NO_VARIADICS
5347template <class _Tp, class _Alloc>
5348struct __temp_value {
5349 typedef allocator_traits<_Alloc> _Traits;
5350
5351 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5352 _Alloc &__a;
5353
5354 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5355 _Tp & get() { return *__addr(); }
5356
5357 template<class... _Args>
5358 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5359 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5360
5361 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5362 };
5363#endif
5364
Howard Hinnantc51e1022010-05-11 19:42:16 +00005365_LIBCPP_END_NAMESPACE_STD
5366
5367#endif // _LIBCPP_MEMORY