blob: 93f04c6faff4be6ad4f006c2d26d3463f6068591 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
Marshall Clowc58e4722018-01-02 17:17:01 +000021inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant1694d232011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
Louis Dionne52ddb5e2018-11-13 17:04:05 +000046 static pointer pointer_to(<details>) noexcept; // constexpr in C++20
Howard Hinnant1694d232011-05-28 14:41:13 +000047};
48
Eric Fiselier18a26852017-11-22 19:49:21 +000049template <class T> constexpr T* to_address(T* p) noexcept; // C++20
50template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
51
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052template <class Alloc>
53struct allocator_traits
54{
55 typedef Alloc allocator_type;
56 typedef typename allocator_type::value_type
57 value_type;
58
59 typedef Alloc::pointer | value_type* pointer;
60 typedef Alloc::const_pointer
61 | pointer_traits<pointer>::rebind<const value_type>
62 const_pointer;
63 typedef Alloc::void_pointer
64 | pointer_traits<pointer>::rebind<void>
65 void_pointer;
66 typedef Alloc::const_void_pointer
67 | pointer_traits<pointer>::rebind<const void>
68 const_void_pointer;
69 typedef Alloc::difference_type
Howard Hinnant47761072010-11-18 01:40:00 +000070 | pointer_traits<pointer>::difference_type
71 difference_type;
72 typedef Alloc::size_type
73 | make_unsigned<difference_type>::type
74 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075 typedef Alloc::propagate_on_container_copy_assignment
76 | false_type propagate_on_container_copy_assignment;
77 typedef Alloc::propagate_on_container_move_assignment
78 | false_type propagate_on_container_move_assignment;
79 typedef Alloc::propagate_on_container_swap
80 | false_type propagate_on_container_swap;
Marshall Clowf0324bc2015-06-02 16:34:03 +000081 typedef Alloc::is_always_equal
82 | is_empty is_always_equal;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083
84 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
85 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
86
Marshall Clowc72032b2017-11-26 02:55:38 +000087 static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
88 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089
Howard Hinnant1694d232011-05-28 14:41:13 +000090 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091
92 template <class T, class... Args>
93 static void construct(allocator_type& a, T* p, Args&&... args);
94
95 template <class T>
96 static void destroy(allocator_type& a, T* p);
97
Marshall Clow08b4f3f2013-08-27 20:22:15 +000098 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099
100 static allocator_type
101 select_on_container_copy_construction(const allocator_type& a);
102};
103
104template <>
105class allocator<void>
106{
107public:
108 typedef void* pointer;
109 typedef const void* const_pointer;
110 typedef void value_type;
111
112 template <class _Up> struct rebind {typedef allocator<_Up> other;};
113};
114
115template <class T>
116class allocator
117{
118public:
119 typedef size_t size_type;
120 typedef ptrdiff_t difference_type;
121 typedef T* pointer;
122 typedef const T* const_pointer;
123 typedef typename add_lvalue_reference<T>::type reference;
124 typedef typename add_lvalue_reference<const T>::type const_reference;
125 typedef T value_type;
126
127 template <class U> struct rebind {typedef allocator<U> other;};
128
Marshall Clowdfeb9b22018-03-20 23:02:53 +0000129 constexpr allocator() noexcept; // constexpr in C++20
130 constexpr allocator(const allocator&) noexcept; // constexpr in C++20
131 template <class U>
132 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
Howard Hinnant1694d232011-05-28 14:41:13 +0000133 ~allocator();
134 pointer address(reference x) const noexcept;
135 const_pointer address(const_reference x) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-05-28 14:41:13 +0000137 void deallocate(pointer p, size_type n) noexcept;
138 size_type max_size() const noexcept;
139 template<class U, class... Args>
140 void construct(U* p, Args&&... args);
141 template <class U>
142 void destroy(U* p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143};
144
145template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000146bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147
148template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000149bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150
151template <class OutputIterator, class T>
152class raw_storage_iterator
153 : public iterator<output_iterator_tag,
154 T, // purposefully not C++03
155 ptrdiff_t, // purposefully not C++03
156 T*, // purposefully not C++03
157 raw_storage_iterator&> // purposefully not C++03
158{
159public:
160 explicit raw_storage_iterator(OutputIterator x);
161 raw_storage_iterator& operator*();
162 raw_storage_iterator& operator=(const T& element);
163 raw_storage_iterator& operator++();
164 raw_storage_iterator operator++(int);
165};
166
Howard Hinnant1694d232011-05-28 14:41:13 +0000167template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
168template <class T> void return_temporary_buffer(T* p) noexcept;
169
170template <class T> T* addressof(T& r) noexcept;
Marshall Clowdb7fa112016-11-14 18:22:19 +0000171template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172
173template <class InputIterator, class ForwardIterator>
174ForwardIterator
175uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
176
Howard Hinnant1694d232011-05-28 14:41:13 +0000177template <class InputIterator, class Size, class ForwardIterator>
178ForwardIterator
179uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
180
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181template <class ForwardIterator, class T>
182void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
183
184template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000185ForwardIterator
186uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187
Eric Fiselierc672a742016-07-24 03:51:39 +0000188template <class T>
189void destroy_at(T* location);
190
191template <class ForwardIterator>
192 void destroy(ForwardIterator first, ForwardIterator last);
193
194template <class ForwardIterator, class Size>
195 ForwardIterator destroy_n(ForwardIterator first, Size n);
196
197template <class InputIterator, class ForwardIterator>
198 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
199
200template <class InputIterator, class Size, class ForwardIterator>
201 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
202
203template <class ForwardIterator>
204 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
205
206template <class ForwardIterator, class Size>
207 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
208
209template <class ForwardIterator>
210 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
211
212template <class ForwardIterator, class Size>
213 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
214
Louis Dionne13cf3b92018-09-23 18:35:00 +0000215template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216
217template<class X>
Louis Dionne13cf3b92018-09-23 18:35:00 +0000218class auto_ptr // deprecated in C++11, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
220public:
221 typedef X element_type;
222
223 explicit auto_ptr(X* p =0) throw();
224 auto_ptr(auto_ptr&) throw();
225 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
226 auto_ptr& operator=(auto_ptr&) throw();
227 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
228 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
229 ~auto_ptr() throw();
230
231 typename add_lvalue_reference<X>::type operator*() const throw();
232 X* operator->() const throw();
233 X* get() const throw();
234 X* release() throw();
235 void reset(X* p =0) throw();
236
237 auto_ptr(auto_ptr_ref<X>) throw();
238 template<class Y> operator auto_ptr_ref<Y>() throw();
239 template<class Y> operator auto_ptr<Y>() throw();
240};
241
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242template <class T>
243struct default_delete
244{
Howard Hinnant1694d232011-05-28 14:41:13 +0000245 constexpr default_delete() noexcept = default;
246 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000247
Howard Hinnant1694d232011-05-28 14:41:13 +0000248 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000249};
250
251template <class T>
252struct default_delete<T[]>
253{
Howard Hinnant1694d232011-05-28 14:41:13 +0000254 constexpr default_delete() noexcept = default;
255 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000256 template <class U> void operator()(U*) const = delete;
257};
258
Howard Hinnante92c3d72010-08-19 18:39:17 +0000259template <class T, class D = default_delete<T>>
260class unique_ptr
261{
262public:
263 typedef see below pointer;
264 typedef T element_type;
265 typedef D deleter_type;
266
267 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000268 constexpr unique_ptr() noexcept;
269 explicit unique_ptr(pointer p) noexcept;
270 unique_ptr(pointer p, see below d1) noexcept;
271 unique_ptr(pointer p, see below d2) noexcept;
272 unique_ptr(unique_ptr&& u) noexcept;
273 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000274 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000275 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000276 template <class U>
Marshall Clowb4d17ad2017-01-24 22:22:33 +0000277 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnante92c3d72010-08-19 18:39:17 +0000278
279 // destructor
280 ~unique_ptr();
281
282 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000283 unique_ptr& operator=(unique_ptr&& u) noexcept;
284 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
285 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000286
287 // observers
288 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000289 pointer operator->() const noexcept;
290 pointer get() const noexcept;
291 deleter_type& get_deleter() noexcept;
292 const deleter_type& get_deleter() const noexcept;
293 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000294
295 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000296 pointer release() noexcept;
297 void reset(pointer p = pointer()) noexcept;
298 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000299};
300
301template <class T, class D>
302class unique_ptr<T[], D>
303{
304public:
305 typedef implementation-defined pointer;
306 typedef T element_type;
307 typedef D deleter_type;
308
309 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000310 constexpr unique_ptr() noexcept;
311 explicit unique_ptr(pointer p) noexcept;
312 unique_ptr(pointer p, see below d) noexcept;
313 unique_ptr(pointer p, see below d) noexcept;
314 unique_ptr(unique_ptr&& u) noexcept;
315 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000316
317 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000318 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000319
320 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000321 unique_ptr& operator=(unique_ptr&& u) noexcept;
322 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000323
324 // observers
325 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000326 pointer get() const noexcept;
327 deleter_type& get_deleter() noexcept;
328 const deleter_type& get_deleter() const noexcept;
329 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000330
331 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000332 pointer release() noexcept;
333 void reset(pointer p = pointer()) noexcept;
334 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000335 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000336 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000337};
338
339template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000340 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000341
342template <class T1, class D1, class T2, class D2>
343 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350template <class T1, class D1, class T2, class D2>
351 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
352template <class T1, class D1, class T2, class D2>
353 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
354
Howard Hinnant1694d232011-05-28 14:41:13 +0000355template <class T, class D>
356 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359template <class T, class D>
360 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
361template <class T, class D>
362 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
363
364template <class T, class D>
365 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
376template <class T, class D>
377 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
378template <class T, class D>
379 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
380
Howard Hinnante92c3d72010-08-19 18:39:17 +0000381class bad_weak_ptr
382 : public std::exception
383{
Howard Hinnant1694d232011-05-28 14:41:13 +0000384 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000385};
386
Marshall Clowfd7481e2013-07-01 18:16:03 +0000387template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
388template<class T> unique_ptr<T> make_unique(size_t n); // C++14
389template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
390
Marshall Clowb2502942017-11-27 15:51:36 +0000391template<class E, class T, class Y, class D>
392 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
393
Howard Hinnante92c3d72010-08-19 18:39:17 +0000394template<class T>
395class shared_ptr
396{
397public:
398 typedef T element_type;
Eric Fiselier83d7ca92016-06-27 01:02:43 +0000399 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnante92c3d72010-08-19 18:39:17 +0000400
401 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000402 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000403 template<class Y> explicit shared_ptr(Y* p);
404 template<class Y, class D> shared_ptr(Y* p, D d);
405 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
406 template <class D> shared_ptr(nullptr_t p, D d);
407 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000408 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
409 shared_ptr(const shared_ptr& r) noexcept;
410 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
411 shared_ptr(shared_ptr&& r) noexcept;
412 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000413 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb4d17ad2017-01-24 22:22:33 +0000414 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnante92c3d72010-08-19 18:39:17 +0000415 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
416 shared_ptr(nullptr_t) : shared_ptr() { }
417
418 // destructor:
419 ~shared_ptr();
420
421 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000422 shared_ptr& operator=(const shared_ptr& r) noexcept;
423 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
424 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000425 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb4d17ad2017-01-24 22:22:33 +0000426 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnante92c3d72010-08-19 18:39:17 +0000427 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
428
429 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000430 void swap(shared_ptr& r) noexcept;
431 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000432 template<class Y> void reset(Y* p);
433 template<class Y, class D> void reset(Y* p, D d);
434 template<class Y, class D, class A> void reset(Y* p, D d, A a);
435
Howard Hinnant1694d232011-05-28 14:41:13 +0000436 // observers:
437 T* get() const noexcept;
438 T& operator*() const noexcept;
439 T* operator->() const noexcept;
440 long use_count() const noexcept;
441 bool unique() const noexcept;
442 explicit operator bool() const noexcept;
Marshall Clow01208af2017-04-11 17:08:53 +0000443 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
444 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000445};
446
447// shared_ptr comparisons:
448template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000449 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000451 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000455 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000456template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000457 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000458template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000459 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
460
461template <class T>
462 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
463template <class T>
464 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
465template <class T>
466 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
467template <class T>
468 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
469template <class T>
470 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
471template <class T>
472bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
473template <class T>
474 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
477template <class T>
478 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
479template <class T>
480 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
481template <class T>
482 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
483template <class T>
484 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000485
486// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000487template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000488
489// shared_ptr casts:
490template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000491 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000492template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000493 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000494template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000495 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000496
497// shared_ptr I/O:
498template<class E, class T, class Y>
499 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
500
501// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000502template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000503
504template<class T, class... Args>
505 shared_ptr<T> make_shared(Args&&... args);
506template<class T, class A, class... Args>
507 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
508
509template<class T>
510class weak_ptr
511{
512public:
513 typedef T element_type;
514
515 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000516 constexpr weak_ptr() noexcept;
517 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
518 weak_ptr(weak_ptr const& r) noexcept;
519 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clow23ef1512014-03-05 03:12:04 +0000520 weak_ptr(weak_ptr&& r) noexcept; // C++14
521 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnante92c3d72010-08-19 18:39:17 +0000522
523 // destructor
524 ~weak_ptr();
525
526 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000527 weak_ptr& operator=(weak_ptr const& r) noexcept;
528 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
529 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clow23ef1512014-03-05 03:12:04 +0000530 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
531 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnante92c3d72010-08-19 18:39:17 +0000532
533 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000534 void swap(weak_ptr& r) noexcept;
535 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000536
537 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000538 long use_count() const noexcept;
539 bool expired() const noexcept;
540 shared_ptr<T> lock() const noexcept;
Marshall Clow01208af2017-04-11 17:08:53 +0000541 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
542 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000543};
544
545// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000546template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000547
548// class owner_less:
549template<class T> struct owner_less;
550
551template<class T>
552struct owner_less<shared_ptr<T>>
553 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
554{
555 typedef bool result_type;
Marshall Clow01208af2017-04-11 17:08:53 +0000556 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
557 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
558 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000559};
560
561template<class T>
562struct owner_less<weak_ptr<T>>
563 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
564{
565 typedef bool result_type;
Marshall Clow01208af2017-04-11 17:08:53 +0000566 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
567 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
568 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
569};
570
571template <> // Added in C++14
572struct owner_less<void>
573{
574 template <class _Tp, class _Up>
575 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
576 template <class _Tp, class _Up>
577 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
578 template <class _Tp, class _Up>
579 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
580 template <class _Tp, class _Up>
581 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
582
583 typedef void is_transparent;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000584};
585
586template<class T>
587class enable_shared_from_this
588{
589protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000590 constexpr enable_shared_from_this() noexcept;
591 enable_shared_from_this(enable_shared_from_this const&) noexcept;
592 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000593 ~enable_shared_from_this();
594public:
595 shared_ptr<T> shared_from_this();
596 shared_ptr<T const> shared_from_this() const;
597};
598
599template<class T>
600 bool atomic_is_lock_free(const shared_ptr<T>* p);
601template<class T>
602 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
603template<class T>
604 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
605template<class T>
606 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
607template<class T>
608 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
609template<class T>
610 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
611template<class T>
612 shared_ptr<T>
613 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
614template<class T>
615 bool
616 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
617template<class T>
618 bool
619 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
620template<class T>
621 bool
622 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
623 shared_ptr<T> w, memory_order success,
624 memory_order failure);
625template<class T>
626 bool
627 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
628 shared_ptr<T> w, memory_order success,
629 memory_order failure);
630// Hash support
631template <class T> struct hash;
632template <class T, class D> struct hash<unique_ptr<T, D> >;
633template <class T> struct hash<shared_ptr<T> >;
634
Marshall Clowc58e4722018-01-02 17:17:01 +0000635template <class T, class Alloc>
636 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
637
Howard Hinnante92c3d72010-08-19 18:39:17 +0000638// Pointer safety
639enum class pointer_safety { relaxed, preferred, strict };
640void declare_reachable(void *p);
641template <class T> T *undeclare_reachable(T *p);
642void declare_no_pointers(char *p, size_t n);
643void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000644pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000645
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
647
648} // std
649
650*/
651
652#include <__config>
653#include <type_traits>
654#include <typeinfo>
655#include <cstddef>
656#include <cstdint>
657#include <new>
658#include <utility>
659#include <limits>
660#include <iterator>
661#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000662#include <iosfwd>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000663#include <tuple>
Eric Fiselier4db388b2016-05-07 03:12:24 +0000664#include <stdexcept>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000665#include <cstring>
Eric Fiselierdb14bcc2017-04-12 23:45:53 +0000666#include <cassert>
Eric Fiselier00f4a492015-08-19 17:21:46 +0000667#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000668# include <atomic>
669#endif
Marshall Clowe3973fd2018-09-12 19:41:40 +0000670#include <version>
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000671
Howard Hinnant08e17472011-10-17 20:05:10 +0000672#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000674#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675
Eric Fiselier018a3d52017-05-31 22:07:49 +0000676_LIBCPP_PUSH_MACROS
677#include <__undef_macros>
678
679
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680_LIBCPP_BEGIN_NAMESPACE_STD
681
Eric Fiselierc6e46692015-07-07 00:27:16 +0000682template <class _ValueType>
Louis Dionne54238052018-07-11 23:14:33 +0000683inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +0000684_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
685#if !defined(_LIBCPP_HAS_NO_THREADS) && \
686 defined(__ATOMIC_RELAXED) && \
687 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
688 return __atomic_load_n(__value, __ATOMIC_RELAXED);
689#else
690 return *__value;
691#endif
692}
693
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000694template <class _ValueType>
Louis Dionne54238052018-07-11 23:14:33 +0000695inline _LIBCPP_INLINE_VISIBILITY
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000696_ValueType __libcpp_acquire_load(_ValueType const* __value) {
697#if !defined(_LIBCPP_HAS_NO_THREADS) && \
698 defined(__ATOMIC_ACQUIRE) && \
699 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
700 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
701#else
702 return *__value;
703#endif
704}
705
Marshall Clowdb7fa112016-11-14 18:22:19 +0000706// addressof moved to <type_traits>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000707
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708template <class _Tp> class allocator;
709
710template <>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000711class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712{
713public:
714 typedef void* pointer;
715 typedef const void* const_pointer;
716 typedef void value_type;
717
718 template <class _Up> struct rebind {typedef allocator<_Up> other;};
719};
720
Howard Hinnanta1877872012-01-19 23:15:22 +0000721template <>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000722class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000723{
724public:
725 typedef const void* pointer;
726 typedef const void* const_pointer;
727 typedef const void value_type;
728
729 template <class _Up> struct rebind {typedef allocator<_Up> other;};
730};
731
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732// pointer_traits
733
Marshall Clow405af582017-06-14 21:23:57 +0000734template <class _Tp, class = void>
735struct __has_element_type : false_type {};
736
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +0000738struct __has_element_type<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +0000739 typename __void_t<typename _Tp::element_type>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740
741template <class _Ptr, bool = __has_element_type<_Ptr>::value>
742struct __pointer_traits_element_type;
743
744template <class _Ptr>
745struct __pointer_traits_element_type<_Ptr, true>
746{
747 typedef typename _Ptr::element_type type;
748};
749
750#ifndef _LIBCPP_HAS_NO_VARIADICS
751
752template <template <class, class...> class _Sp, class _Tp, class ..._Args>
753struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
754{
755 typedef typename _Sp<_Tp, _Args...>::element_type type;
756};
757
758template <template <class, class...> class _Sp, class _Tp, class ..._Args>
759struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
760{
761 typedef _Tp type;
762};
763
Howard Hinnant324bb032010-08-22 00:02:43 +0000764#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765
766template <template <class> class _Sp, class _Tp>
767struct __pointer_traits_element_type<_Sp<_Tp>, true>
768{
769 typedef typename _Sp<_Tp>::element_type type;
770};
771
772template <template <class> class _Sp, class _Tp>
773struct __pointer_traits_element_type<_Sp<_Tp>, false>
774{
775 typedef _Tp type;
776};
777
778template <template <class, class> class _Sp, class _Tp, class _A0>
779struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
780{
781 typedef typename _Sp<_Tp, _A0>::element_type type;
782};
783
784template <template <class, class> class _Sp, class _Tp, class _A0>
785struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
786{
787 typedef _Tp type;
788};
789
790template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
791struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
792{
793 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
794};
795
796template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
797struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
798{
799 typedef _Tp type;
800};
801
802template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
803 class _A1, class _A2>
804struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
805{
806 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
807};
808
809template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
810 class _A1, class _A2>
811struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
812{
813 typedef _Tp type;
814};
815
Howard Hinnant324bb032010-08-22 00:02:43 +0000816#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817
Marshall Clow405af582017-06-14 21:23:57 +0000818template <class _Tp, class = void>
819struct __has_difference_type : false_type {};
820
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +0000822struct __has_difference_type<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +0000823 typename __void_t<typename _Tp::difference_type>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824
825template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
826struct __pointer_traits_difference_type
827{
828 typedef ptrdiff_t type;
829};
830
831template <class _Ptr>
832struct __pointer_traits_difference_type<_Ptr, true>
833{
834 typedef typename _Ptr::difference_type type;
835};
836
837template <class _Tp, class _Up>
838struct __has_rebind
839{
840private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000841 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842 template <class _Xp> static __two __test(...);
843 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
844public:
845 static const bool value = sizeof(__test<_Tp>(0)) == 1;
846};
847
848template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
849struct __pointer_traits_rebind
850{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000851#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000852 typedef typename _Tp::template rebind<_Up> type;
853#else
854 typedef typename _Tp::template rebind<_Up>::other type;
855#endif
856};
857
858#ifndef _LIBCPP_HAS_NO_VARIADICS
859
860template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
861struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
862{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000863#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
865#else
866 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
867#endif
868};
869
870template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
871struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
872{
873 typedef _Sp<_Up, _Args...> type;
874};
875
Howard Hinnant324bb032010-08-22 00:02:43 +0000876#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877
878template <template <class> class _Sp, class _Tp, class _Up>
879struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
880{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000881#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882 typedef typename _Sp<_Tp>::template rebind<_Up> type;
883#else
884 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
885#endif
886};
887
888template <template <class> class _Sp, class _Tp, class _Up>
889struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
890{
891 typedef _Sp<_Up> type;
892};
893
894template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
895struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
896{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000897#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
899#else
900 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
901#endif
902};
903
904template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
905struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
906{
907 typedef _Sp<_Up, _A0> type;
908};
909
910template <template <class, class, class> class _Sp, class _Tp, class _A0,
911 class _A1, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
913{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000914#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
916#else
917 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
918#endif
919};
920
921template <template <class, class, class> class _Sp, class _Tp, class _A0,
922 class _A1, class _Up>
923struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
924{
925 typedef _Sp<_Up, _A0, _A1> type;
926};
927
928template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
929 class _A1, class _A2, class _Up>
930struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
931{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000932#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
934#else
935 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
936#endif
937};
938
939template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
940 class _A1, class _A2, class _Up>
941struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
942{
943 typedef _Sp<_Up, _A0, _A1, _A2> type;
944};
945
Howard Hinnant324bb032010-08-22 00:02:43 +0000946#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947
948template <class _Ptr>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000949struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950{
951 typedef _Ptr pointer;
952 typedef typename __pointer_traits_element_type<pointer>::type element_type;
953 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
954
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000955#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant6b41c602011-05-11 20:21:19 +0000956 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957#else
958 template <class _Up> struct rebind
959 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000960#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961
962private:
963 struct __nat {};
964public:
Howard Hinnant82894812010-09-22 16:48:34 +0000965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966 static pointer pointer_to(typename conditional<is_void<element_type>::value,
967 __nat, element_type>::type& __r)
968 {return pointer::pointer_to(__r);}
969};
970
971template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000972struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973{
974 typedef _Tp* pointer;
975 typedef _Tp element_type;
976 typedef ptrdiff_t difference_type;
977
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000978#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 template <class _Up> using rebind = _Up*;
980#else
981 template <class _Up> struct rebind {typedef _Up* other;};
982#endif
983
984private:
985 struct __nat {};
986public:
Louis Dionne52ddb5e2018-11-13 17:04:05 +0000987 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000989 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000990 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991};
992
Eric Fiselierbb2f28e2015-08-23 02:56:05 +0000993template <class _From, class _To>
994struct __rebind_pointer {
Eric Fiselier4e3e15a2016-09-25 03:34:28 +0000995#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierbb2f28e2015-08-23 02:56:05 +0000996 typedef typename pointer_traits<_From>::template rebind<_To> type;
997#else
998 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
999#endif
1000};
1001
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002// allocator_traits
1003
Marshall Clow405af582017-06-14 21:23:57 +00001004template <class _Tp, class = void>
1005struct __has_pointer_type : false_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006
1007template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001008struct __has_pointer_type<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001009 typename __void_t<typename _Tp::pointer>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant47761072010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
Marshall Clow405af582017-06-14 21:23:57 +00001034template <class _Tp, class = void>
1035struct __has_const_pointer : false_type {};
1036
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001038struct __has_const_pointer<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001039 typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040
1041template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1042struct __const_pointer
1043{
1044 typedef typename _Alloc::const_pointer type;
1045};
1046
1047template <class _Tp, class _Ptr, class _Alloc>
1048struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1049{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001050#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1052#else
1053 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1054#endif
1055};
1056
Marshall Clow405af582017-06-14 21:23:57 +00001057template <class _Tp, class = void>
1058struct __has_void_pointer : false_type {};
1059
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001061struct __has_void_pointer<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001062 typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063
1064template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1065struct __void_pointer
1066{
1067 typedef typename _Alloc::void_pointer type;
1068};
1069
1070template <class _Ptr, class _Alloc>
1071struct __void_pointer<_Ptr, _Alloc, false>
1072{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001073#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1075#else
1076 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1077#endif
1078};
1079
Marshall Clow405af582017-06-14 21:23:57 +00001080template <class _Tp, class = void>
1081struct __has_const_void_pointer : false_type {};
1082
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001084struct __has_const_void_pointer<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001085 typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086
1087template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1088struct __const_void_pointer
1089{
1090 typedef typename _Alloc::const_void_pointer type;
1091};
1092
1093template <class _Ptr, class _Alloc>
1094struct __const_void_pointer<_Ptr, _Alloc, false>
1095{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001096#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1098#else
1099 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1100#endif
1101};
1102
Howard Hinnant99968442011-11-29 18:15:50 +00001103template <class _Tp>
Eric Fiselier18a26852017-11-22 19:49:21 +00001104inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00001105_Tp*
1106__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107{
1108 return __p;
1109}
1110
Eric Fiselier18a26852017-11-22 19:49:21 +00001111#if _LIBCPP_STD_VER <= 17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112template <class _Pointer>
1113inline _LIBCPP_INLINE_VISIBILITY
1114typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001115__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001117 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118}
Eric Fiselier18a26852017-11-22 19:49:21 +00001119#else
1120template <class _Pointer>
1121inline _LIBCPP_INLINE_VISIBILITY
1122auto
1123__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
1124-> decltype(pointer_traits<_Pointer>::to_address(__p))
1125{
1126 return pointer_traits<_Pointer>::to_address(__p);
1127}
1128
1129template <class _Pointer, class... _None>
1130inline _LIBCPP_INLINE_VISIBILITY
1131auto
1132__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
1133{
1134 return _VSTD::__to_raw_pointer(__p.operator->());
1135}
1136
1137template <class _Tp>
1138inline _LIBCPP_INLINE_VISIBILITY constexpr
1139_Tp*
1140to_address(_Tp* __p) _NOEXCEPT
1141{
1142 static_assert(!is_function_v<_Tp>, "_Tp is a function type");
1143 return __p;
1144}
1145
1146template <class _Pointer>
1147inline _LIBCPP_INLINE_VISIBILITY
1148auto
1149to_address(const _Pointer& __p) _NOEXCEPT
1150{
1151 return _VSTD::__to_raw_pointer(__p);
1152}
1153#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154
Marshall Clow405af582017-06-14 21:23:57 +00001155template <class _Tp, class = void>
1156struct __has_size_type : false_type {};
1157
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158template <class _Tp>
Marshall Clow405af582017-06-14 21:23:57 +00001159struct __has_size_type<_Tp,
1160 typename __void_t<typename _Tp::size_type>::type> : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161
Howard Hinnant47761072010-11-18 01:40:00 +00001162template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163struct __size_type
1164{
Howard Hinnant47761072010-11-18 01:40:00 +00001165 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166};
1167
Howard Hinnant47761072010-11-18 01:40:00 +00001168template <class _Alloc, class _DiffType>
1169struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170{
1171 typedef typename _Alloc::size_type type;
1172};
1173
Marshall Clow405af582017-06-14 21:23:57 +00001174template <class _Tp, class = void>
1175struct __has_propagate_on_container_copy_assignment : false_type {};
1176
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177template <class _Tp>
Marshall Clow405af582017-06-14 21:23:57 +00001178struct __has_propagate_on_container_copy_assignment<_Tp,
1179 typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1180 : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181
1182template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1183struct __propagate_on_container_copy_assignment
1184{
1185 typedef false_type type;
1186};
1187
1188template <class _Alloc>
1189struct __propagate_on_container_copy_assignment<_Alloc, true>
1190{
1191 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1192};
1193
Marshall Clow405af582017-06-14 21:23:57 +00001194template <class _Tp, class = void>
1195struct __has_propagate_on_container_move_assignment : false_type {};
1196
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001198struct __has_propagate_on_container_move_assignment<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001199 typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1200 : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201
1202template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1203struct __propagate_on_container_move_assignment
1204{
1205 typedef false_type type;
1206};
1207
1208template <class _Alloc>
1209struct __propagate_on_container_move_assignment<_Alloc, true>
1210{
1211 typedef typename _Alloc::propagate_on_container_move_assignment type;
1212};
1213
Marshall Clow405af582017-06-14 21:23:57 +00001214template <class _Tp, class = void>
1215struct __has_propagate_on_container_swap : false_type {};
1216
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001218struct __has_propagate_on_container_swap<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001219 typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1220 : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221
1222template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1223struct __propagate_on_container_swap
1224{
1225 typedef false_type type;
1226};
1227
1228template <class _Alloc>
1229struct __propagate_on_container_swap<_Alloc, true>
1230{
1231 typedef typename _Alloc::propagate_on_container_swap type;
1232};
1233
Marshall Clow405af582017-06-14 21:23:57 +00001234template <class _Tp, class = void>
1235struct __has_is_always_equal : false_type {};
1236
Marshall Clowf0324bc2015-06-02 16:34:03 +00001237template <class _Tp>
Aditya Kumard4c89052017-08-20 10:38:55 +00001238struct __has_is_always_equal<_Tp,
Marshall Clow405af582017-06-14 21:23:57 +00001239 typename __void_t<typename _Tp::is_always_equal>::type>
1240 : true_type {};
Marshall Clowf0324bc2015-06-02 16:34:03 +00001241
1242template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1243struct __is_always_equal
1244{
1245 typedef typename _VSTD::is_empty<_Alloc>::type type;
1246};
1247
1248template <class _Alloc>
1249struct __is_always_equal<_Alloc, true>
1250{
1251 typedef typename _Alloc::is_always_equal type;
1252};
1253
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1255struct __has_rebind_other
1256{
1257private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001258 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259 template <class _Xp> static __two __test(...);
1260 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1261public:
1262 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1263};
1264
1265template <class _Tp, class _Up>
1266struct __has_rebind_other<_Tp, _Up, false>
1267{
1268 static const bool value = false;
1269};
1270
1271template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1272struct __allocator_traits_rebind
1273{
1274 typedef typename _Tp::template rebind<_Up>::other type;
1275};
1276
1277#ifndef _LIBCPP_HAS_NO_VARIADICS
1278
1279template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1280struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1281{
1282 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1283};
1284
1285template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1287{
1288 typedef _Alloc<_Up, _Args...> type;
1289};
1290
Howard Hinnant324bb032010-08-22 00:02:43 +00001291#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292
1293template <template <class> class _Alloc, class _Tp, class _Up>
1294struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1295{
1296 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1297};
1298
1299template <template <class> class _Alloc, class _Tp, class _Up>
1300struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1301{
1302 typedef _Alloc<_Up> type;
1303};
1304
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1306struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1307{
1308 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1309};
1310
1311template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1312struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1313{
1314 typedef _Alloc<_Up, _A0> type;
1315};
1316
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1318 class _A1, class _Up>
1319struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1320{
1321 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1322};
1323
1324template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1325 class _A1, class _Up>
1326struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1327{
1328 typedef _Alloc<_Up, _A0, _A1> type;
1329};
1330
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1332 class _A1, class _A2, class _Up>
1333struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1334{
1335 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1336};
1337
1338template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1339 class _A1, class _A2, class _Up>
1340struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1341{
1342 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1343};
1344
Howard Hinnant324bb032010-08-22 00:02:43 +00001345#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001347#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1350auto
1351__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
Eric Fiselier63d88112017-09-15 00:31:38 +00001352 -> decltype((void)__a.allocate(__sz, __p), true_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353
1354template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1355auto
1356__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1357 -> false_type;
1358
1359template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1360struct __has_allocate_hint
1361 : integral_constant<bool,
1362 is_same<
Eric Fiselier63d88112017-09-15 00:31:38 +00001363 decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 declval<_SizeType>(),
1365 declval<_ConstVoidPtr>())),
1366 true_type>::value>
1367{
1368};
1369
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001370#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371
1372template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1373struct __has_allocate_hint
1374 : true_type
1375{
1376};
1377
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001378#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001380#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381
1382template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001383decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1384 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 true_type())
1386__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1387
1388template <class _Alloc, class _Pointer, class ..._Args>
1389false_type
1390__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1391
1392template <class _Alloc, class _Pointer, class ..._Args>
1393struct __has_construct
1394 : integral_constant<bool,
1395 is_same<
Eric Fiselier63d88112017-09-15 00:31:38 +00001396 decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 declval<_Pointer>(),
1398 declval<_Args>()...)),
1399 true_type>::value>
1400{
1401};
1402
1403template <class _Alloc, class _Pointer>
1404auto
1405__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1406 -> decltype(__a.destroy(__p), true_type());
1407
1408template <class _Alloc, class _Pointer>
1409auto
1410__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1411 -> false_type;
1412
1413template <class _Alloc, class _Pointer>
1414struct __has_destroy
1415 : integral_constant<bool,
1416 is_same<
Eric Fiselier63d88112017-09-15 00:31:38 +00001417 decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418 declval<_Pointer>())),
1419 true_type>::value>
1420{
1421};
1422
1423template <class _Alloc>
1424auto
1425__has_max_size_test(_Alloc&& __a)
1426 -> decltype(__a.max_size(), true_type());
1427
1428template <class _Alloc>
1429auto
1430__has_max_size_test(const volatile _Alloc& __a)
1431 -> false_type;
1432
1433template <class _Alloc>
1434struct __has_max_size
1435 : integral_constant<bool,
1436 is_same<
Eric Fiselier63d88112017-09-15 00:31:38 +00001437 decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438 true_type>::value>
1439{
1440};
1441
1442template <class _Alloc>
1443auto
1444__has_select_on_container_copy_construction_test(_Alloc&& __a)
1445 -> decltype(__a.select_on_container_copy_construction(), true_type());
1446
1447template <class _Alloc>
1448auto
1449__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1450 -> false_type;
1451
1452template <class _Alloc>
1453struct __has_select_on_container_copy_construction
1454 : integral_constant<bool,
1455 is_same<
Eric Fiselier63d88112017-09-15 00:31:38 +00001456 decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 true_type>::value>
1458{
1459};
1460
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001461#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001463template <class _Alloc, class _Pointer, class _Tp, class = void>
1464struct __has_construct : std::false_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001466template <class _Alloc, class _Pointer, class _Tp>
1467struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1468 decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1469>::type> : std::true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001471template <class _Alloc, class _Pointer, class = void>
1472struct __has_destroy : false_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473
1474template <class _Alloc, class _Pointer>
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001475struct __has_destroy<_Alloc, _Pointer, typename __void_t<
1476 decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1477>::type> : std::true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492
Howard Hinnant47761072010-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
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001505template <class _Tp>
1506struct __is_default_allocator : false_type {};
1507
1508template <class _Tp>
1509struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511template <class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001512struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513{
1514 typedef _Alloc allocator_type;
1515 typedef typename allocator_type::value_type value_type;
1516
1517 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1518 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1519 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1520 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1521
Howard Hinnant47761072010-11-18 01:40:00 +00001522 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1523 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524
1525 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1526 propagate_on_container_copy_assignment;
1527 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1528 propagate_on_container_move_assignment;
1529 typedef typename __propagate_on_container_swap<allocator_type>::type
1530 propagate_on_container_swap;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001531 typedef typename __is_always_equal<allocator_type>::type
1532 is_always_equal;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001534#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001536 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001538#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 template <class _Tp> struct rebind_alloc
1540 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1541 template <class _Tp> struct rebind_traits
1542 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001543#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544
Marshall Clowc72032b2017-11-26 02:55:38 +00001545 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 static pointer allocate(allocator_type& __a, size_type __n)
1547 {return __a.allocate(__n);}
Marshall Clowc72032b2017-11-26 02:55:38 +00001548 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001550 {return __allocate(__a, __n, __hint,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1552
Howard Hinnant82894812010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001554 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 {__a.deallocate(__p, __n);}
1556
1557#ifndef _LIBCPP_HAS_NO_VARIADICS
1558 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3f5579f2014-11-11 19:22:33 +00001561 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001562 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001563#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbfcceee2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp();
1569 }
1570 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001572 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 {
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001574 __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1575 __a, __p, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 }
1577 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbfcceee2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 const _A1& __a1)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1);
1583 }
1584 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001585 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbfcceee2017-01-14 01:33:53 +00001586 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 const _A1& __a1, const _A2& __a2)
1588 {
1589 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1590 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001591#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592
1593 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 static void destroy(allocator_type& __a, _Tp* __p)
1596 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1597
Howard Hinnant82894812010-09-22 16:48:34 +00001598 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001599 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1601
Howard Hinnant82894812010-09-22 16:48:34 +00001602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 static allocator_type
1604 select_on_container_copy_construction(const allocator_type& __a)
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001605 {return __select_on_container_copy_construction(
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 __has_select_on_container_copy_construction<const allocator_type>(),
1607 __a);}
1608
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001609 template <class _Ptr>
1610 _LIBCPP_INLINE_VISIBILITY
1611 static
1612 void
1613 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1614 {
Louis Dionnea796feb2018-12-07 16:42:28 +00001615 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001616 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1617 }
1618
1619 template <class _Tp>
1620 _LIBCPP_INLINE_VISIBILITY
1621 static
1622 typename enable_if
1623 <
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001624 (__is_default_allocator<allocator_type>::value
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001625 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1626 is_trivially_move_constructible<_Tp>::value,
1627 void
1628 >::type
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001629 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001630 {
1631 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001632 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001633 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001634 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001635 __begin2 += _Np;
1636 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001637 }
1638
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001639 template <class _Iter, class _Ptr>
1640 _LIBCPP_INLINE_VISIBILITY
1641 static
1642 void
1643 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1644 {
1645 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1646 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1647 }
1648
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001649 template <class _SourceTp, class _DestTp,
1650 class _RawSourceTp = typename remove_const<_SourceTp>::type,
1651 class _RawDestTp = typename remove_const<_DestTp>::type>
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001652 _LIBCPP_INLINE_VISIBILITY
1653 static
1654 typename enable_if
1655 <
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001656 is_trivially_move_constructible<_DestTp>::value &&
1657 is_same<_RawSourceTp, _RawDestTp>::value &&
1658 (__is_default_allocator<allocator_type>::value ||
1659 !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001660 void
1661 >::type
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001662 __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001663 {
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001664 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001665 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001666 {
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001667 _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001668 __begin2 += _Np;
1669 }
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001670 }
1671
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001672 template <class _Ptr>
1673 _LIBCPP_INLINE_VISIBILITY
1674 static
1675 void
1676 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1677 {
1678 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001679 {
1680 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1681 --__end2;
1682 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001683 }
1684
1685 template <class _Tp>
1686 _LIBCPP_INLINE_VISIBILITY
1687 static
1688 typename enable_if
1689 <
Volodymyr Sapsai354589c2019-01-08 00:03:16 +00001690 (__is_default_allocator<allocator_type>::value
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001691 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1692 is_trivially_move_constructible<_Tp>::value,
1693 void
1694 >::type
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001695 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001696 {
1697 ptrdiff_t _Np = __end1 - __begin1;
1698 __end2 -= _Np;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001699 if (_Np > 0)
1700 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001701 }
1702
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703private:
1704
Howard Hinnant82894812010-09-22 16:48:34 +00001705 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001706 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 const_void_pointer __hint, true_type)
1708 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001709 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001710 static pointer __allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001711 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712 {return __a.allocate(__n);}
1713
1714#ifndef _LIBCPP_HAS_NO_VARIADICS
1715 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001718 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1722 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001723 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 }
Volodymyr Sapsaid805c872018-12-19 20:08:43 +00001725#else // _LIBCPP_HAS_NO_VARIADICS
1726 template <class _Tp, class _A0>
1727 _LIBCPP_INLINE_VISIBILITY
1728 static void __construct(true_type, allocator_type& __a, _Tp* __p,
1729 const _A0& __a0)
1730 {__a.construct(__p, __a0);}
1731 template <class _Tp, class _A0>
1732 _LIBCPP_INLINE_VISIBILITY
1733 static void __construct(false_type, allocator_type&, _Tp* __p,
1734 const _A0& __a0)
1735 {
1736 ::new ((void*)__p) _Tp(__a0);
1737 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001738#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739
1740 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1743 {__a.destroy(__p);}
1744 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746 static void __destroy(false_type, allocator_type&, _Tp* __p)
1747 {
1748 __p->~_Tp();
1749 }
1750
Howard Hinnant82894812010-09-22 16:48:34 +00001751 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab7cb212017-12-05 15:56:26 +00001752 static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001754 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab7cb212017-12-05 15:56:26 +00001755 static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
Marshall Clow88fa03a2015-10-25 19:34:04 +00001756 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757
Howard Hinnant82894812010-09-22 16:48:34 +00001758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 static allocator_type
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001760 __select_on_container_copy_construction(true_type, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763 static allocator_type
Eric Fiselier59e24fe2017-06-01 02:29:37 +00001764 __select_on_container_copy_construction(false_type, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 {return __a;}
1766};
1767
Marshall Clow66302c62015-04-07 05:21:38 +00001768template <class _Traits, class _Tp>
1769struct __rebind_alloc_helper
1770{
Eric Fiselier4e3e15a2016-09-25 03:34:28 +00001771#ifndef _LIBCPP_CXX03_LANG
Marshall Clow0ad232a2015-05-10 13:59:45 +00001772 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow66302c62015-04-07 05:21:38 +00001773#else
1774 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1775#endif
1776};
1777
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778// allocator
1779
1780template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001781class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782{
1783public:
1784 typedef size_t size_type;
1785 typedef ptrdiff_t difference_type;
1786 typedef _Tp* pointer;
1787 typedef const _Tp* const_pointer;
1788 typedef _Tp& reference;
1789 typedef const _Tp& const_reference;
1790 typedef _Tp value_type;
1791
Howard Hinnant18884f42011-06-02 21:38:57 +00001792 typedef true_type propagate_on_container_move_assignment;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001793 typedef true_type is_always_equal;
Howard Hinnant18884f42011-06-02 21:38:57 +00001794
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1796
Marshall Clowdfeb9b22018-03-20 23:02:53 +00001797 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1798 allocator() _NOEXCEPT {}
1799
Louis Dionne13cf3b92018-09-23 18:35:00 +00001800 template <class _Up>
Marshall Clowdfeb9b22018-03-20 23:02:53 +00001801 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1802 allocator(const allocator<_Up>&) _NOEXCEPT {}
1803
Howard Hinnant1694d232011-05-28 14:41:13 +00001804 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001805 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001806 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001807 {return _VSTD::addressof(__x);}
Louis Dionne13cf3b92018-09-23 18:35:00 +00001808 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc72032b2017-11-26 02:55:38 +00001809 pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow4951a482016-03-03 12:04:39 +00001810 {
1811 if (__n > max_size())
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001812 __throw_length_error("allocator<T>::allocate(size_t n)"
1813 " 'n' exceeds maximum supported size");
Eric Fiseliere3e576a2018-11-28 22:24:19 +00001814 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Marshall Clow4951a482016-03-03 12:04:39 +00001815 }
Eric Fiseliere09f85b2018-10-25 17:21:30 +00001816 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiseliere3e576a2018-11-28 22:24:19 +00001817 {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), __alignof(_Tp));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001818 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1819 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001820#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 template <class _Up, class... _Args>
1822 _LIBCPP_INLINE_VISIBILITY
1823 void
1824 construct(_Up* __p, _Args&&... __args)
1825 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001826 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001828#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 _LIBCPP_INLINE_VISIBILITY
1830 void
1831 construct(pointer __p)
1832 {
1833 ::new((void*)__p) _Tp();
1834 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001835# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001836
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 template <class _A0>
1838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001839 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 construct(pointer __p, _A0& __a0)
1841 {
1842 ::new((void*)__p) _Tp(__a0);
1843 }
1844 template <class _A0>
1845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001846 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 construct(pointer __p, const _A0& __a0)
1848 {
1849 ::new((void*)__p) _Tp(__a0);
1850 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001851# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 template <class _A0, class _A1>
1853 _LIBCPP_INLINE_VISIBILITY
1854 void
1855 construct(pointer __p, _A0& __a0, _A1& __a1)
1856 {
1857 ::new((void*)__p) _Tp(__a0, __a1);
1858 }
1859 template <class _A0, class _A1>
1860 _LIBCPP_INLINE_VISIBILITY
1861 void
1862 construct(pointer __p, const _A0& __a0, _A1& __a1)
1863 {
1864 ::new((void*)__p) _Tp(__a0, __a1);
1865 }
1866 template <class _A0, class _A1>
1867 _LIBCPP_INLINE_VISIBILITY
1868 void
1869 construct(pointer __p, _A0& __a0, const _A1& __a1)
1870 {
1871 ::new((void*)__p) _Tp(__a0, __a1);
1872 }
1873 template <class _A0, class _A1>
1874 _LIBCPP_INLINE_VISIBILITY
1875 void
1876 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1877 {
1878 ::new((void*)__p) _Tp(__a0, __a1);
1879 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001880#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1882};
1883
Howard Hinnant57199402012-01-02 17:56:02 +00001884template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001885class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001886{
1887public:
1888 typedef size_t size_type;
1889 typedef ptrdiff_t difference_type;
1890 typedef const _Tp* pointer;
1891 typedef const _Tp* const_pointer;
1892 typedef const _Tp& reference;
1893 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001894 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001895
1896 typedef true_type propagate_on_container_move_assignment;
Marshall Clowb81d6f52015-07-01 21:23:40 +00001897 typedef true_type is_always_equal;
Howard Hinnant57199402012-01-02 17:56:02 +00001898
1899 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1900
Marshall Clowdfeb9b22018-03-20 23:02:53 +00001901 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1902 allocator() _NOEXCEPT {}
1903
1904 template <class _Up>
Louis Dionne13cf3b92018-09-23 18:35:00 +00001905 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdfeb9b22018-03-20 23:02:53 +00001906 allocator(const allocator<_Up>&) _NOEXCEPT {}
1907
Howard Hinnant57199402012-01-02 17:56:02 +00001908 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1909 {return _VSTD::addressof(__x);}
1910 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow4951a482016-03-03 12:04:39 +00001911 {
1912 if (__n > max_size())
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001913 __throw_length_error("allocator<const T>::allocate(size_t n)"
1914 " 'n' exceeds maximum supported size");
Eric Fiseliere3e576a2018-11-28 22:24:19 +00001915 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Eric Fiselier4db388b2016-05-07 03:12:24 +00001916 }
Eric Fiseliere09f85b2018-10-25 17:21:30 +00001917 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
Eric Fiseliere3e576a2018-11-28 22:24:19 +00001918 {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), __alignof(_Tp));}
Howard Hinnant57199402012-01-02 17:56:02 +00001919 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1920 {return size_type(~0) / sizeof(_Tp);}
1921#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1922 template <class _Up, class... _Args>
1923 _LIBCPP_INLINE_VISIBILITY
1924 void
1925 construct(_Up* __p, _Args&&... __args)
1926 {
1927 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1928 }
1929#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1930 _LIBCPP_INLINE_VISIBILITY
1931 void
1932 construct(pointer __p)
1933 {
Marshall Clow899f1132017-06-14 16:54:43 +00001934 ::new((void*) const_cast<_Tp *>(__p)) _Tp();
Howard Hinnant57199402012-01-02 17:56:02 +00001935 }
1936# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001937
Howard Hinnant57199402012-01-02 17:56:02 +00001938 template <class _A0>
1939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001940 void
Howard Hinnant57199402012-01-02 17:56:02 +00001941 construct(pointer __p, _A0& __a0)
1942 {
Marshall Clow899f1132017-06-14 16:54:43 +00001943 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant57199402012-01-02 17:56:02 +00001944 }
1945 template <class _A0>
1946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001947 void
Howard Hinnant57199402012-01-02 17:56:02 +00001948 construct(pointer __p, const _A0& __a0)
1949 {
Marshall Clow899f1132017-06-14 16:54:43 +00001950 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
Howard Hinnant57199402012-01-02 17:56:02 +00001951 }
Howard Hinnant57199402012-01-02 17:56:02 +00001952# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1953 template <class _A0, class _A1>
1954 _LIBCPP_INLINE_VISIBILITY
1955 void
1956 construct(pointer __p, _A0& __a0, _A1& __a1)
1957 {
Marshall Clow899f1132017-06-14 16:54:43 +00001958 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant57199402012-01-02 17:56:02 +00001959 }
1960 template <class _A0, class _A1>
1961 _LIBCPP_INLINE_VISIBILITY
1962 void
1963 construct(pointer __p, const _A0& __a0, _A1& __a1)
1964 {
Marshall Clow899f1132017-06-14 16:54:43 +00001965 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant57199402012-01-02 17:56:02 +00001966 }
1967 template <class _A0, class _A1>
1968 _LIBCPP_INLINE_VISIBILITY
1969 void
1970 construct(pointer __p, _A0& __a0, const _A1& __a1)
1971 {
Marshall Clow899f1132017-06-14 16:54:43 +00001972 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant57199402012-01-02 17:56:02 +00001973 }
1974 template <class _A0, class _A1>
1975 _LIBCPP_INLINE_VISIBILITY
1976 void
1977 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1978 {
Marshall Clow899f1132017-06-14 16:54:43 +00001979 ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
Howard Hinnant57199402012-01-02 17:56:02 +00001980 }
1981#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1982 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1983};
1984
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985template <class _Tp, class _Up>
1986inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001987bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988
1989template <class _Tp, class _Up>
1990inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001991bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992
1993template <class _OutputIterator, class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001994class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995 : public iterator<output_iterator_tag,
1996 _Tp, // purposefully not C++03
1997 ptrdiff_t, // purposefully not C++03
1998 _Tp*, // purposefully not C++03
1999 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
2000{
2001private:
2002 _OutputIterator __x_;
2003public:
2004 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
2005 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
2006 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
Marshall Clowc4f0f1e2018-08-28 13:29:30 +00002007 {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
Marshall Clow332ab912015-10-25 18:58:07 +00002008#if _LIBCPP_STD_VER >= 14
2009 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
Marshall Clowc4f0f1e2018-08-28 13:29:30 +00002010 {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
Marshall Clow332ab912015-10-25 18:58:07 +00002011#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
2013 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
2014 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00002015#if _LIBCPP_STD_VER >= 14
Aditya Kumard4c89052017-08-20 10:38:55 +00002016 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
Marshall Clowdbaf7a02015-05-10 13:14:08 +00002017#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018};
2019
2020template <class _Tp>
Roman Lebedevcaf40ae2018-09-22 17:54:48 +00002021_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00002023get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024{
2025 pair<_Tp*, ptrdiff_t> __r(0, 0);
2026 const ptrdiff_t __m = (~ptrdiff_t(0) ^
2027 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
2028 / sizeof(_Tp);
2029 if (__n > __m)
2030 __n = __m;
2031 while (__n > 0)
2032 {
Eric Fiseliere6968262018-10-26 17:12:32 +00002033#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiseliere3e576a2018-11-28 22:24:19 +00002034 if (__is_overaligned_for_new(__alignof(_Tp)))
Richard Smithdfb13512018-02-01 22:24:45 +00002035 {
2036 std::align_val_t __al =
2037 std::align_val_t(std::alignment_of<_Tp>::value);
2038 __r.first = static_cast<_Tp*>(::operator new(
2039 __n * sizeof(_Tp), __al, nothrow));
2040 } else {
2041 __r.first = static_cast<_Tp*>(::operator new(
2042 __n * sizeof(_Tp), nothrow));
2043 }
2044#else
Eric Fiseliere3e576a2018-11-28 22:24:19 +00002045 if (__is_overaligned_for_new(__alignof(_Tp)))
Richard Smithdfb13512018-02-01 22:24:45 +00002046 {
2047 // Since aligned operator new is unavailable, return an empty
2048 // buffer rather than one with invalid alignment.
2049 return __r;
2050 }
2051
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
Richard Smithdfb13512018-02-01 22:24:45 +00002053#endif
2054
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 if (__r.first)
2056 {
2057 __r.second = __n;
2058 break;
2059 }
2060 __n /= 2;
2061 }
2062 return __r;
2063}
2064
2065template <class _Tp>
2066inline _LIBCPP_INLINE_VISIBILITY
Richard Smithdfb13512018-02-01 22:24:45 +00002067void return_temporary_buffer(_Tp* __p) _NOEXCEPT
2068{
Eric Fiseliere3e576a2018-11-28 22:24:19 +00002069 _VSTD::__libcpp_deallocate_unsized((void*)__p, __alignof(_Tp));
Richard Smithdfb13512018-02-01 22:24:45 +00002070}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071
Marshall Clowb4d17ad2017-01-24 22:22:33 +00002072#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073template <class _Tp>
Louis Dionne13cf3b92018-09-23 18:35:00 +00002074struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075{
2076 _Tp* __ptr_;
2077};
2078
2079template<class _Tp>
Louis Dionne13cf3b92018-09-23 18:35:00 +00002080class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081{
2082private:
2083 _Tp* __ptr_;
2084public:
2085 typedef _Tp element_type;
2086
2087 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2088 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2089 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2090 : __ptr_(__p.release()) {}
2091 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2092 {reset(__p.release()); return *this;}
2093 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2094 {reset(__p.release()); return *this;}
2095 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2096 {reset(__p.__ptr_); return *this;}
2097 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2098
2099 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2100 {return *__ptr_;}
2101 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2102 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2103 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2104 {
2105 _Tp* __t = __ptr_;
2106 __ptr_ = 0;
2107 return __t;
2108 }
2109 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2110 {
2111 if (__ptr_ != __p)
2112 delete __ptr_;
2113 __ptr_ = __p;
2114 }
2115
2116 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2117 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2118 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2119 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2120 {return auto_ptr<_Up>(release());}
2121};
2122
2123template <>
Louis Dionne13cf3b92018-09-23 18:35:00 +00002124class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125{
2126public:
2127 typedef void element_type;
2128};
Marshall Clowb4d17ad2017-01-24 22:22:33 +00002129#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002131template <class _Tp, int _Idx,
2132 bool _CanBeEmptyBase =
2133 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2134struct __compressed_pair_elem {
2135 typedef _Tp _ParamT;
2136 typedef _Tp& reference;
2137 typedef const _Tp& const_reference;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002138
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002139#ifndef _LIBCPP_CXX03_LANG
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002140 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002142 template <class _Up, class = typename enable_if<
Eric Fiselier55dc5da2017-04-17 22:32:02 +00002143 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2144 >::type>
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002145 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier55dc5da2017-04-17 22:32:02 +00002146 constexpr explicit
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002147 __compressed_pair_elem(_Up&& __u)
Eric Fiselierf7fac082018-10-01 01:59:37 +00002148 : __value_(_VSTD::forward<_Up>(__u))
2149 {
2150 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002152 template <class... _Args, size_t... _Indexes>
2153 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2154 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2155 __tuple_indices<_Indexes...>)
2156 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2157#else
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002158 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
2159 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002160 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2161#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002163 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
2164 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002165 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167private:
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002168 _Tp __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002169};
2170
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002171template <class _Tp, int _Idx>
2172struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2173 typedef _Tp _ParamT;
2174 typedef _Tp& reference;
2175 typedef const _Tp& const_reference;
2176 typedef _Tp __value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002177
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002178#ifndef _LIBCPP_CXX03_LANG
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002179 _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002181 template <class _Up, class = typename enable_if<
Eric Fiselier55dc5da2017-04-17 22:32:02 +00002182 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2183 >::type>
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002184 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier55dc5da2017-04-17 22:32:02 +00002185 constexpr explicit
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002186 __compressed_pair_elem(_Up&& __u)
Eric Fiselierf7fac082018-10-01 01:59:37 +00002187 : __value_type(_VSTD::forward<_Up>(__u))
2188 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002190 template <class... _Args, size_t... _Indexes>
2191 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2192 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2193 __tuple_indices<_Indexes...>)
2194 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2195#else
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002196 _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
2197 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002198 __compressed_pair_elem(_ParamT __p)
2199 : __value_type(std::forward<_ParamT>(__p)) {}
2200#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201
Alex Lorenzb4a34c02017-11-09 17:54:49 +00002202 _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
2203 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002204 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205};
2206
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002207// Tag used to construct the second element of the compressed pair.
2208struct __second_tag {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209
2210template <class _T1, class _T2>
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002211class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2212 private __compressed_pair_elem<_T2, 1> {
2213 typedef __compressed_pair_elem<_T1, 0> _Base1;
2214 typedef __compressed_pair_elem<_T2, 1> _Base2;
2215
2216 // NOTE: This static assert should never fire because __compressed_pair
2217 // is *almost never* used in a scenario where it's possible for T1 == T2.
2218 // (The exception is std::function where it is possible that the function
2219 // object and the allocator have the same type).
Eric Fiselierdd598262017-04-13 01:13:58 +00002220 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002221 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2222 "The current implementation is NOT ABI-compatible with the previous "
2223 "implementation for this configuration");
2224
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225public:
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002226#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier55dc5da2017-04-17 22:32:02 +00002227 template <bool _Dummy = true,
2228 class = typename enable_if<
2229 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2230 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2231 >::type
2232 >
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002233 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier55dc5da2017-04-17 22:32:02 +00002234 constexpr __compressed_pair() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002236 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2237 __compressed_pair>::value,
2238 bool>::type = true>
2239 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2240 __compressed_pair(_Tp&& __t)
2241 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002242
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002243 template <class _Tp>
2244 _LIBCPP_INLINE_VISIBILITY constexpr
2245 __compressed_pair(__second_tag, _Tp&& __t)
2246 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002248 template <class _U1, class _U2>
2249 _LIBCPP_INLINE_VISIBILITY constexpr
2250 __compressed_pair(_U1&& __t1, _U2&& __t2)
2251 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002253 template <class... _Args1, class... _Args2>
2254 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2255 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2256 tuple<_Args2...> __second_args)
2257 : _Base1(__pc, _VSTD::move(__first_args),
2258 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2259 _Base2(__pc, _VSTD::move(__second_args),
2260 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002261
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002262#else
2263 _LIBCPP_INLINE_VISIBILITY
2264 __compressed_pair() {}
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002265
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002266 _LIBCPP_INLINE_VISIBILITY explicit
2267 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002268
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002269 _LIBCPP_INLINE_VISIBILITY
2270 __compressed_pair(__second_tag, _T2 __t2)
2271 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002273 _LIBCPP_INLINE_VISIBILITY
2274 __compressed_pair(_T1 __t1, _T2 __t2)
2275 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2276#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002278 _LIBCPP_INLINE_VISIBILITY
2279 typename _Base1::reference first() _NOEXCEPT {
2280 return static_cast<_Base1&>(*this).__get();
2281 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002283 _LIBCPP_INLINE_VISIBILITY
2284 typename _Base1::const_reference first() const _NOEXCEPT {
2285 return static_cast<_Base1 const&>(*this).__get();
2286 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002288 _LIBCPP_INLINE_VISIBILITY
2289 typename _Base2::reference second() _NOEXCEPT {
2290 return static_cast<_Base2&>(*this).__get();
2291 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002293 _LIBCPP_INLINE_VISIBILITY
2294 typename _Base2::const_reference second() const _NOEXCEPT {
2295 return static_cast<_Base2 const&>(*this).__get();
2296 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002298 _LIBCPP_INLINE_VISIBILITY
2299 void swap(__compressed_pair& __x)
2300 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2301 __is_nothrow_swappable<_T2>::value)
2302 {
2303 using std::swap;
2304 swap(first(), __x.first());
2305 swap(second(), __x.second());
2306 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307};
2308
2309template <class _T1, class _T2>
2310inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00002311void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2312 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2313 __is_nothrow_swappable<_T2>::value) {
2314 __x.swap(__y);
2315}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316
Howard Hinnant57199402012-01-02 17:56:02 +00002317// default_delete
2318
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319template <class _Tp>
Eric Fiseliere3aef862017-04-16 02:06:25 +00002320struct _LIBCPP_TEMPLATE_VIS default_delete {
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00002321 static_assert(!is_function<_Tp>::value,
2322 "default_delete cannot be instantiated for function types");
Eric Fiseliere3aef862017-04-16 02:06:25 +00002323#ifndef _LIBCPP_CXX03_LANG
2324 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnant46e94932012-07-07 20:56:04 +00002325#else
Eric Fiseliere3aef862017-04-16 02:06:25 +00002326 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnant46e94932012-07-07 20:56:04 +00002327#endif
Eric Fiseliere3aef862017-04-16 02:06:25 +00002328 template <class _Up>
2329 _LIBCPP_INLINE_VISIBILITY
2330 default_delete(const default_delete<_Up>&,
2331 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2332 0) _NOEXCEPT {}
2333
2334 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2335 static_assert(sizeof(_Tp) > 0,
2336 "default_delete can not delete incomplete type");
2337 static_assert(!is_void<_Tp>::value,
2338 "default_delete can not delete incomplete type");
2339 delete __ptr;
2340 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341};
2342
2343template <class _Tp>
Eric Fiseliere3aef862017-04-16 02:06:25 +00002344struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2345private:
2346 template <class _Up>
2347 struct _EnableIfConvertible
2348 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2349
Howard Hinnant8e843502011-12-18 21:19:44 +00002350public:
Eric Fiseliere3aef862017-04-16 02:06:25 +00002351#ifndef _LIBCPP_CXX03_LANG
2352 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnant46e94932012-07-07 20:56:04 +00002353#else
Eric Fiseliere3aef862017-04-16 02:06:25 +00002354 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnant46e94932012-07-07 20:56:04 +00002355#endif
Eric Fiseliere3aef862017-04-16 02:06:25 +00002356
2357 template <class _Up>
2358 _LIBCPP_INLINE_VISIBILITY
2359 default_delete(const default_delete<_Up[]>&,
2360 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2361
2362 template <class _Up>
2363 _LIBCPP_INLINE_VISIBILITY
2364 typename _EnableIfConvertible<_Up>::type
2365 operator()(_Up* __ptr) const _NOEXCEPT {
2366 static_assert(sizeof(_Tp) > 0,
2367 "default_delete can not delete incomplete type");
2368 static_assert(!is_void<_Tp>::value,
2369 "default_delete can not delete void type");
2370 delete[] __ptr;
2371 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372};
2373
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374
Eric Fiseliere3aef862017-04-16 02:06:25 +00002375
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002376#ifndef _LIBCPP_CXX03_LANG
2377template <class _Deleter>
2378struct __unique_ptr_deleter_sfinae {
2379 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2380 typedef const _Deleter& __lval_ref_type;
2381 typedef _Deleter&& __good_rval_ref_type;
2382 typedef true_type __enable_rval_overload;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383};
2384
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002385template <class _Deleter>
2386struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2387 typedef const _Deleter& __lval_ref_type;
2388 typedef const _Deleter&& __bad_rval_ref_type;
2389 typedef false_type __enable_rval_overload;
2390};
2391
2392template <class _Deleter>
2393struct __unique_ptr_deleter_sfinae<_Deleter&> {
2394 typedef _Deleter& __lval_ref_type;
2395 typedef _Deleter&& __bad_rval_ref_type;
2396 typedef false_type __enable_rval_overload;
2397};
2398#endif // !defined(_LIBCPP_CXX03_LANG)
2399
2400template <class _Tp, class _Dp = default_delete<_Tp> >
2401class _LIBCPP_TEMPLATE_VIS unique_ptr {
2402public:
2403 typedef _Tp element_type;
2404 typedef _Dp deleter_type;
2405 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2406
2407 static_assert(!is_rvalue_reference<deleter_type>::value,
2408 "the specified deleter type cannot be an rvalue reference");
2409
2410private:
2411 __compressed_pair<pointer, deleter_type> __ptr_;
2412
2413 struct __nat { int __for_bool_; };
2414
2415#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002416 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002417
2418 template <bool _Dummy>
2419 using _LValRefType =
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002420 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002421
2422 template <bool _Dummy>
2423 using _GoodRValRefType =
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002424 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002425
2426 template <bool _Dummy>
2427 using _BadRValRefType =
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002428 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002429
2430 template <bool _Dummy, class _Deleter = typename __dependent_type<
2431 __identity<deleter_type>, _Dummy>::type>
2432 using _EnableIfDeleterDefaultConstructible =
2433 typename enable_if<is_default_constructible<_Deleter>::value &&
2434 !is_pointer<_Deleter>::value>::type;
2435
2436 template <class _ArgType>
2437 using _EnableIfDeleterConstructible =
2438 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2439
2440 template <class _UPtr, class _Up>
2441 using _EnableIfMoveConvertible = typename enable_if<
2442 is_convertible<typename _UPtr::pointer, pointer>::value &&
2443 !is_array<_Up>::value
2444 >::type;
2445
2446 template <class _UDel>
2447 using _EnableIfDeleterConvertible = typename enable_if<
2448 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2449 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2450 >::type;
2451
2452 template <class _UDel>
2453 using _EnableIfDeleterAssignable = typename enable_if<
2454 is_assignable<_Dp&, _UDel&&>::value
2455 >::type;
2456
2457public:
2458 template <bool _Dummy = true,
2459 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2460 _LIBCPP_INLINE_VISIBILITY
2461 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2462
2463 template <bool _Dummy = true,
2464 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2465 _LIBCPP_INLINE_VISIBILITY
2466 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2467
2468 template <bool _Dummy = true,
2469 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2470 _LIBCPP_INLINE_VISIBILITY
2471 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2472
2473 template <bool _Dummy = true,
2474 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2475 _LIBCPP_INLINE_VISIBILITY
2476 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2477 : __ptr_(__p, __d) {}
2478
2479 template <bool _Dummy = true,
2480 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2481 _LIBCPP_INLINE_VISIBILITY
2482 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2483 : __ptr_(__p, _VSTD::move(__d)) {
2484 static_assert(!is_reference<deleter_type>::value,
2485 "rvalue deleter bound to reference");
2486 }
2487
2488 template <bool _Dummy = true,
2489 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2490 _LIBCPP_INLINE_VISIBILITY
2491 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2492
2493 _LIBCPP_INLINE_VISIBILITY
2494 unique_ptr(unique_ptr&& __u) noexcept
2495 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2496 }
2497
2498 template <class _Up, class _Ep,
2499 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2500 class = _EnableIfDeleterConvertible<_Ep>
2501 >
2502 _LIBCPP_INLINE_VISIBILITY
2503 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2504 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2505
2506#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2507 template <class _Up>
2508 _LIBCPP_INLINE_VISIBILITY
2509 unique_ptr(auto_ptr<_Up>&& __p,
2510 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2511 is_same<_Dp, default_delete<_Tp>>::value,
2512 __nat>::type = __nat()) _NOEXCEPT
2513 : __ptr_(__p.release()) {}
2514#endif
2515
2516 _LIBCPP_INLINE_VISIBILITY
2517 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2518 reset(__u.release());
2519 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2520 return *this;
2521 }
2522
2523 template <class _Up, class _Ep,
2524 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2525 class = _EnableIfDeleterAssignable<_Ep>
2526 >
2527 _LIBCPP_INLINE_VISIBILITY
2528 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2529 reset(__u.release());
2530 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2531 return *this;
2532 }
2533
2534#else // _LIBCPP_CXX03_LANG
2535private:
2536 unique_ptr(unique_ptr&);
2537 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2538
2539 unique_ptr& operator=(unique_ptr&);
2540 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2541
2542public:
2543 _LIBCPP_INLINE_VISIBILITY
2544 unique_ptr() : __ptr_(pointer())
2545 {
2546 static_assert(!is_pointer<deleter_type>::value,
2547 "unique_ptr constructed with null function pointer deleter");
2548 static_assert(is_default_constructible<deleter_type>::value,
2549 "unique_ptr::deleter_type is not default constructible");
2550 }
2551 _LIBCPP_INLINE_VISIBILITY
2552 unique_ptr(nullptr_t) : __ptr_(pointer())
2553 {
2554 static_assert(!is_pointer<deleter_type>::value,
2555 "unique_ptr constructed with null function pointer deleter");
2556 }
2557 _LIBCPP_INLINE_VISIBILITY
2558 explicit unique_ptr(pointer __p)
2559 : __ptr_(_VSTD::move(__p)) {
2560 static_assert(!is_pointer<deleter_type>::value,
2561 "unique_ptr constructed with null function pointer deleter");
2562 }
2563
2564 _LIBCPP_INLINE_VISIBILITY
2565 operator __rv<unique_ptr>() {
2566 return __rv<unique_ptr>(*this);
2567 }
2568
2569 _LIBCPP_INLINE_VISIBILITY
2570 unique_ptr(__rv<unique_ptr> __u)
2571 : __ptr_(__u->release(),
2572 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2573
2574 template <class _Up, class _Ep>
2575 _LIBCPP_INLINE_VISIBILITY
2576 typename enable_if<
2577 !is_array<_Up>::value &&
2578 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2579 pointer>::value &&
2580 is_assignable<deleter_type&, _Ep&>::value,
2581 unique_ptr&>::type
2582 operator=(unique_ptr<_Up, _Ep> __u) {
2583 reset(__u.release());
2584 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2585 return *this;
2586 }
2587
2588 _LIBCPP_INLINE_VISIBILITY
2589 unique_ptr(pointer __p, deleter_type __d)
2590 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2591#endif // _LIBCPP_CXX03_LANG
2592
2593#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2594 template <class _Up>
2595 _LIBCPP_INLINE_VISIBILITY
2596 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2597 is_same<_Dp, default_delete<_Tp> >::value,
2598 unique_ptr&>::type
2599 operator=(auto_ptr<_Up> __p) {
2600 reset(__p.release());
2601 return *this;
2602 }
2603#endif
2604
2605 _LIBCPP_INLINE_VISIBILITY
2606 ~unique_ptr() { reset(); }
2607
2608 _LIBCPP_INLINE_VISIBILITY
2609 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2610 reset();
2611 return *this;
2612 }
2613
2614 _LIBCPP_INLINE_VISIBILITY
2615 typename add_lvalue_reference<_Tp>::type
2616 operator*() const {
2617 return *__ptr_.first();
2618 }
2619 _LIBCPP_INLINE_VISIBILITY
2620 pointer operator->() const _NOEXCEPT {
2621 return __ptr_.first();
2622 }
2623 _LIBCPP_INLINE_VISIBILITY
2624 pointer get() const _NOEXCEPT {
2625 return __ptr_.first();
2626 }
2627 _LIBCPP_INLINE_VISIBILITY
2628 deleter_type& get_deleter() _NOEXCEPT {
2629 return __ptr_.second();
2630 }
2631 _LIBCPP_INLINE_VISIBILITY
2632 const deleter_type& get_deleter() const _NOEXCEPT {
2633 return __ptr_.second();
2634 }
2635 _LIBCPP_INLINE_VISIBILITY
2636 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2637 return __ptr_.first() != nullptr;
2638 }
2639
2640 _LIBCPP_INLINE_VISIBILITY
2641 pointer release() _NOEXCEPT {
2642 pointer __t = __ptr_.first();
2643 __ptr_.first() = pointer();
2644 return __t;
2645 }
2646
2647 _LIBCPP_INLINE_VISIBILITY
2648 void reset(pointer __p = pointer()) _NOEXCEPT {
2649 pointer __tmp = __ptr_.first();
2650 __ptr_.first() = __p;
2651 if (__tmp)
2652 __ptr_.second()(__tmp);
2653 }
2654
2655 _LIBCPP_INLINE_VISIBILITY
2656 void swap(unique_ptr& __u) _NOEXCEPT {
2657 __ptr_.swap(__u.__ptr_);
2658 }
2659};
2660
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002661
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662template <class _Tp, class _Dp>
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002663class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664public:
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002665 typedef _Tp element_type;
2666 typedef _Dp deleter_type;
2667 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2668
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669private:
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002670 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002672 template <class _From>
2673 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002674
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002675 template <class _FromElem>
2676 struct _CheckArrayPointerConversion<_FromElem*>
2677 : integral_constant<bool,
2678 is_same<_FromElem*, pointer>::value ||
2679 (is_same<pointer, element_type*>::value &&
2680 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2681 >
2682 {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002684#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002685 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002686
2687 template <bool _Dummy>
2688 using _LValRefType =
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002689 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002690
2691 template <bool _Dummy>
2692 using _GoodRValRefType =
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002693 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002694
2695 template <bool _Dummy>
2696 using _BadRValRefType =
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002697 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002698
2699 template <bool _Dummy, class _Deleter = typename __dependent_type<
2700 __identity<deleter_type>, _Dummy>::type>
2701 using _EnableIfDeleterDefaultConstructible =
2702 typename enable_if<is_default_constructible<_Deleter>::value &&
2703 !is_pointer<_Deleter>::value>::type;
2704
2705 template <class _ArgType>
2706 using _EnableIfDeleterConstructible =
2707 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2708
2709 template <class _Pp>
2710 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002711 _CheckArrayPointerConversion<_Pp>::value
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002712 >::type;
2713
2714 template <class _UPtr, class _Up,
2715 class _ElemT = typename _UPtr::element_type>
2716 using _EnableIfMoveConvertible = typename enable_if<
2717 is_array<_Up>::value &&
2718 is_same<pointer, element_type*>::value &&
2719 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2720 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2721 >::type;
2722
2723 template <class _UDel>
2724 using _EnableIfDeleterConvertible = typename enable_if<
2725 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2726 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2727 >::type;
2728
2729 template <class _UDel>
2730 using _EnableIfDeleterAssignable = typename enable_if<
2731 is_assignable<_Dp&, _UDel&&>::value
2732 >::type;
2733
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734public:
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002735 template <bool _Dummy = true,
2736 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2737 _LIBCPP_INLINE_VISIBILITY
2738 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002740 template <bool _Dummy = true,
2741 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2742 _LIBCPP_INLINE_VISIBILITY
2743 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002745 template <class _Pp, bool _Dummy = true,
2746 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2747 class = _EnableIfPointerConvertible<_Pp>>
2748 _LIBCPP_INLINE_VISIBILITY
2749 explicit unique_ptr(_Pp __p) noexcept
2750 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002752 template <class _Pp, bool _Dummy = true,
2753 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2754 class = _EnableIfPointerConvertible<_Pp>>
2755 _LIBCPP_INLINE_VISIBILITY
2756 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2757 : __ptr_(__p, __d) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002759 template <bool _Dummy = true,
2760 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2761 _LIBCPP_INLINE_VISIBILITY
2762 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2763 : __ptr_(nullptr, __d) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002765 template <class _Pp, bool _Dummy = true,
2766 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2767 class = _EnableIfPointerConvertible<_Pp>>
2768 _LIBCPP_INLINE_VISIBILITY
2769 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2770 : __ptr_(__p, _VSTD::move(__d)) {
2771 static_assert(!is_reference<deleter_type>::value,
2772 "rvalue deleter bound to reference");
2773 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002775 template <bool _Dummy = true,
2776 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2777 _LIBCPP_INLINE_VISIBILITY
2778 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2779 : __ptr_(nullptr, _VSTD::move(__d)) {
2780 static_assert(!is_reference<deleter_type>::value,
2781 "rvalue deleter bound to reference");
2782 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002783
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002784 template <class _Pp, bool _Dummy = true,
2785 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2786 class = _EnableIfPointerConvertible<_Pp>>
2787 _LIBCPP_INLINE_VISIBILITY
2788 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant8e843502011-12-18 21:19:44 +00002789
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002790 _LIBCPP_INLINE_VISIBILITY
2791 unique_ptr(unique_ptr&& __u) noexcept
2792 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2793 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002794
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002795 _LIBCPP_INLINE_VISIBILITY
2796 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2797 reset(__u.release());
2798 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2799 return *this;
2800 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002802 template <class _Up, class _Ep,
2803 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2804 class = _EnableIfDeleterConvertible<_Ep>
2805 >
2806 _LIBCPP_INLINE_VISIBILITY
2807 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier7e698522017-04-17 20:20:27 +00002808 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002809 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002810
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002811 template <class _Up, class _Ep,
2812 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2813 class = _EnableIfDeleterAssignable<_Ep>
2814 >
2815 _LIBCPP_INLINE_VISIBILITY
2816 unique_ptr&
2817 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2818 reset(__u.release());
2819 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2820 return *this;
2821 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002823#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824private:
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002825 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant324bb032010-08-22 00:02:43 +00002826
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002827 unique_ptr(unique_ptr&);
2828 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2829
2830 unique_ptr& operator=(unique_ptr&);
2831 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2832
2833 template <class _Up>
2834 unique_ptr(_Up __u,
2835 typename conditional<
2836 is_reference<deleter_type>::value, deleter_type,
2837 typename add_lvalue_reference<const deleter_type>::type>::type,
2838 typename enable_if<is_convertible<_Up, pointer>::value,
2839 __nat>::type = __nat());
2840public:
2841 _LIBCPP_INLINE_VISIBILITY
2842 unique_ptr() : __ptr_(pointer()) {
2843 static_assert(!is_pointer<deleter_type>::value,
2844 "unique_ptr constructed with null function pointer deleter");
2845 }
2846 _LIBCPP_INLINE_VISIBILITY
2847 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2848 static_assert(!is_pointer<deleter_type>::value,
2849 "unique_ptr constructed with null function pointer deleter");
2850 }
2851
2852 _LIBCPP_INLINE_VISIBILITY
2853 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2854 static_assert(!is_pointer<deleter_type>::value,
2855 "unique_ptr constructed with null function pointer deleter");
2856 }
2857
2858 _LIBCPP_INLINE_VISIBILITY
2859 unique_ptr(pointer __p, deleter_type __d)
2860 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2861
2862 _LIBCPP_INLINE_VISIBILITY
2863 unique_ptr(nullptr_t, deleter_type __d)
2864 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2865
2866 _LIBCPP_INLINE_VISIBILITY
2867 operator __rv<unique_ptr>() {
2868 return __rv<unique_ptr>(*this);
2869 }
2870
2871 _LIBCPP_INLINE_VISIBILITY
2872 unique_ptr(__rv<unique_ptr> __u)
2873 : __ptr_(__u->release(),
2874 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2875
2876 _LIBCPP_INLINE_VISIBILITY
2877 unique_ptr& operator=(__rv<unique_ptr> __u) {
2878 reset(__u->release());
2879 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2880 return *this;
2881 }
2882
2883#endif // _LIBCPP_CXX03_LANG
2884
2885public:
2886 _LIBCPP_INLINE_VISIBILITY
2887 ~unique_ptr() { reset(); }
2888
2889 _LIBCPP_INLINE_VISIBILITY
2890 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2891 reset();
2892 return *this;
2893 }
2894
2895 _LIBCPP_INLINE_VISIBILITY
2896 typename add_lvalue_reference<_Tp>::type
2897 operator[](size_t __i) const {
2898 return __ptr_.first()[__i];
2899 }
2900 _LIBCPP_INLINE_VISIBILITY
2901 pointer get() const _NOEXCEPT {
2902 return __ptr_.first();
2903 }
2904
2905 _LIBCPP_INLINE_VISIBILITY
2906 deleter_type& get_deleter() _NOEXCEPT {
2907 return __ptr_.second();
2908 }
2909
2910 _LIBCPP_INLINE_VISIBILITY
2911 const deleter_type& get_deleter() const _NOEXCEPT {
2912 return __ptr_.second();
2913 }
2914 _LIBCPP_INLINE_VISIBILITY
2915 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2916 return __ptr_.first() != nullptr;
2917 }
2918
2919 _LIBCPP_INLINE_VISIBILITY
2920 pointer release() _NOEXCEPT {
2921 pointer __t = __ptr_.first();
2922 __ptr_.first() = pointer();
2923 return __t;
2924 }
2925
2926 template <class _Pp>
2927 _LIBCPP_INLINE_VISIBILITY
2928 typename enable_if<
Eric Fiselier745a5cd2017-04-16 02:14:31 +00002929 _CheckArrayPointerConversion<_Pp>::value
Eric Fiseliera4fd0c92017-04-16 01:51:04 +00002930 >::type
2931 reset(_Pp __p) _NOEXCEPT {
2932 pointer __tmp = __ptr_.first();
2933 __ptr_.first() = __p;
2934 if (__tmp)
2935 __ptr_.second()(__tmp);
2936 }
2937
2938 _LIBCPP_INLINE_VISIBILITY
2939 void reset(nullptr_t = nullptr) _NOEXCEPT {
2940 pointer __tmp = __ptr_.first();
2941 __ptr_.first() = nullptr;
2942 if (__tmp)
2943 __ptr_.second()(__tmp);
2944 }
2945
2946 _LIBCPP_INLINE_VISIBILITY
2947 void swap(unique_ptr& __u) _NOEXCEPT {
2948 __ptr_.swap(__u.__ptr_);
2949 }
2950
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002951};
2952
2953template <class _Tp, class _Dp>
2954inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8f1e73d2016-04-21 23:38:59 +00002955typename enable_if<
2956 __is_swappable<_Dp>::value,
2957 void
2958>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002959swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960
2961template <class _T1, class _D1, class _T2, class _D2>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
2964operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2965
2966template <class _T1, class _D1, class _T2, class _D2>
2967inline _LIBCPP_INLINE_VISIBILITY
2968bool
2969operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2970
2971template <class _T1, class _D1, class _T2, class _D2>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002974operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2975{
2976 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2977 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002978 typedef typename common_type<_P1, _P2>::type _Vp;
2979 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002980}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981
2982template <class _T1, class _D1, class _T2, class _D2>
2983inline _LIBCPP_INLINE_VISIBILITY
2984bool
2985operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2986
2987template <class _T1, class _D1, class _T2, class _D2>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
2990operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2991
2992template <class _T1, class _D1, class _T2, class _D2>
2993inline _LIBCPP_INLINE_VISIBILITY
2994bool
2995operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2996
Howard Hinnant3fadda32012-02-21 21:02:58 +00002997template <class _T1, class _D1>
2998inline _LIBCPP_INLINE_VISIBILITY
2999bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003000operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003001{
3002 return !__x;
3003}
3004
3005template <class _T1, class _D1>
3006inline _LIBCPP_INLINE_VISIBILITY
3007bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003008operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003009{
3010 return !__x;
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003016operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003017{
3018 return static_cast<bool>(__x);
3019}
3020
3021template <class _T1, class _D1>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003024operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003025{
3026 return static_cast<bool>(__x);
3027}
3028
3029template <class _T1, class _D1>
3030inline _LIBCPP_INLINE_VISIBILITY
3031bool
3032operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3033{
3034 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3035 return less<_P1>()(__x.get(), nullptr);
3036}
3037
3038template <class _T1, class _D1>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
3041operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3042{
3043 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3044 return less<_P1>()(nullptr, __x.get());
3045}
3046
3047template <class _T1, class _D1>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3051{
3052 return nullptr < __x;
3053}
3054
3055template <class _T1, class _D1>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3059{
3060 return __x < nullptr;
3061}
3062
3063template <class _T1, class _D1>
3064inline _LIBCPP_INLINE_VISIBILITY
3065bool
3066operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3067{
3068 return !(nullptr < __x);
3069}
3070
3071template <class _T1, class _D1>
3072inline _LIBCPP_INLINE_VISIBILITY
3073bool
3074operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3075{
3076 return !(__x < nullptr);
3077}
3078
3079template <class _T1, class _D1>
3080inline _LIBCPP_INLINE_VISIBILITY
3081bool
3082operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3083{
3084 return !(__x < nullptr);
3085}
3086
3087template <class _T1, class _D1>
3088inline _LIBCPP_INLINE_VISIBILITY
3089bool
3090operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3091{
3092 return !(nullptr < __x);
3093}
3094
Howard Hinnant87073e42012-05-01 15:37:54 +00003095#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3096
3097template <class _Tp, class _Dp>
3098inline _LIBCPP_INLINE_VISIBILITY
3099unique_ptr<_Tp, _Dp>
3100move(unique_ptr<_Tp, _Dp>& __t)
3101{
3102 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3103}
3104
3105#endif
3106
Marshall Clowfd7481e2013-07-01 18:16:03 +00003107#if _LIBCPP_STD_VER > 11
3108
3109template<class _Tp>
3110struct __unique_if
3111{
3112 typedef unique_ptr<_Tp> __unique_single;
3113};
3114
3115template<class _Tp>
3116struct __unique_if<_Tp[]>
3117{
3118 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3119};
3120
3121template<class _Tp, size_t _Np>
3122struct __unique_if<_Tp[_Np]>
3123{
3124 typedef void __unique_array_known_bound;
3125};
3126
3127template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003128inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003129typename __unique_if<_Tp>::__unique_single
3130make_unique(_Args&&... __args)
3131{
3132 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3133}
3134
3135template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003136inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003137typename __unique_if<_Tp>::__unique_array_unknown_bound
3138make_unique(size_t __n)
3139{
3140 typedef typename remove_extent<_Tp>::type _Up;
3141 return unique_ptr<_Tp>(new _Up[__n]());
3142}
3143
3144template<class _Tp, class... _Args>
3145 typename __unique_if<_Tp>::__unique_array_known_bound
3146 make_unique(_Args&&...) = delete;
3147
3148#endif // _LIBCPP_STD_VER > 11
3149
Howard Hinnant21aefc32010-06-03 16:42:57 +00003150template <class _Tp, class _Dp>
Eric Fiselier952eaec2017-01-21 00:02:12 +00003151#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierc3589a82017-01-04 23:56:00 +00003152struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier952eaec2017-01-21 00:02:12 +00003153#else
3154struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3155 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3156#endif
Howard Hinnant21aefc32010-06-03 16:42:57 +00003157{
3158 typedef unique_ptr<_Tp, _Dp> argument_type;
3159 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003160 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf552ba2017-03-23 02:40:28 +00003161 result_type operator()(const argument_type& __ptr) const
Howard Hinnant21aefc32010-06-03 16:42:57 +00003162 {
3163 typedef typename argument_type::pointer pointer;
3164 return hash<pointer>()(__ptr.get());
3165 }
3166};
3167
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003168struct __destruct_n
3169{
3170private:
Eric Fiselier59e24fe2017-06-01 02:29:37 +00003171 size_t __size_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003172
3173 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003174 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Eric Fiselier59e24fe2017-06-01 02:29:37 +00003175 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003176
3177 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003178 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179 {}
3180
Howard Hinnant1694d232011-05-28 14:41:13 +00003181 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Eric Fiselier59e24fe2017-06-01 02:29:37 +00003182 {++__size_;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003183 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184 {}
3185
Howard Hinnant1694d232011-05-28 14:41:13 +00003186 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Eric Fiselier59e24fe2017-06-01 02:29:37 +00003187 {__size_ = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003188 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189 {}
3190public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003191 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
Eric Fiselier59e24fe2017-06-01 02:29:37 +00003192 : __size_(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003193
3194 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003195 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003196 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197
3198 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003199 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003200 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003201
3202 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003203 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003204 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205};
3206
3207template <class _Alloc>
3208class __allocator_destructor
3209{
3210 typedef allocator_traits<_Alloc> __alloc_traits;
3211public:
3212 typedef typename __alloc_traits::pointer pointer;
3213 typedef typename __alloc_traits::size_type size_type;
3214private:
3215 _Alloc& __alloc_;
3216 size_type __s_;
3217public:
3218 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003219 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003220 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003222 void operator()(pointer __p) _NOEXCEPT
3223 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003224};
3225
3226template <class _InputIterator, class _ForwardIterator>
3227_ForwardIterator
3228uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3229{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003231#ifndef _LIBCPP_NO_EXCEPTIONS
3232 _ForwardIterator __s = __r;
3233 try
3234 {
3235#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003236 for (; __f != __l; ++__f, (void) ++__r)
3237 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003238#ifndef _LIBCPP_NO_EXCEPTIONS
3239 }
3240 catch (...)
3241 {
3242 for (; __s != __r; ++__s)
3243 __s->~value_type();
3244 throw;
3245 }
3246#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247 return __r;
3248}
3249
3250template <class _InputIterator, class _Size, class _ForwardIterator>
3251_ForwardIterator
3252uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3253{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003255#ifndef _LIBCPP_NO_EXCEPTIONS
3256 _ForwardIterator __s = __r;
3257 try
3258 {
3259#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003260 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3261 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003262#ifndef _LIBCPP_NO_EXCEPTIONS
3263 }
3264 catch (...)
3265 {
3266 for (; __s != __r; ++__s)
3267 __s->~value_type();
3268 throw;
3269 }
3270#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271 return __r;
3272}
3273
3274template <class _ForwardIterator, class _Tp>
3275void
3276uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3277{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003278 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003279#ifndef _LIBCPP_NO_EXCEPTIONS
3280 _ForwardIterator __s = __f;
3281 try
3282 {
3283#endif
3284 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003285 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003286#ifndef _LIBCPP_NO_EXCEPTIONS
3287 }
3288 catch (...)
3289 {
3290 for (; __s != __f; ++__s)
3291 __s->~value_type();
3292 throw;
3293 }
3294#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295}
3296
3297template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003298_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003299uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3300{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003301 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003302#ifndef _LIBCPP_NO_EXCEPTIONS
3303 _ForwardIterator __s = __f;
3304 try
3305 {
3306#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003307 for (; __n > 0; ++__f, (void) --__n)
3308 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003309#ifndef _LIBCPP_NO_EXCEPTIONS
3310 }
3311 catch (...)
3312 {
3313 for (; __s != __f; ++__s)
3314 __s->~value_type();
3315 throw;
3316 }
3317#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003318 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003319}
3320
Eric Fiselierc672a742016-07-24 03:51:39 +00003321#if _LIBCPP_STD_VER > 14
3322
Eric Fiselierc672a742016-07-24 03:51:39 +00003323template <class _Tp>
3324inline _LIBCPP_INLINE_VISIBILITY
3325void destroy_at(_Tp* __loc) {
3326 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3327 __loc->~_Tp();
3328}
3329
3330template <class _ForwardIterator>
3331inline _LIBCPP_INLINE_VISIBILITY
3332void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3333 for (; __first != __last; ++__first)
3334 _VSTD::destroy_at(_VSTD::addressof(*__first));
3335}
3336
3337template <class _ForwardIterator, class _Size>
3338inline _LIBCPP_INLINE_VISIBILITY
3339_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3340 for (; __n > 0; (void)++__first, --__n)
3341 _VSTD::destroy_at(_VSTD::addressof(*__first));
3342 return __first;
3343}
3344
Eric Fiselier05577c82016-10-11 21:13:44 +00003345template <class _ForwardIterator>
3346inline _LIBCPP_INLINE_VISIBILITY
3347void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3348 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3349 auto __idx = __first;
3350#ifndef _LIBCPP_NO_EXCEPTIONS
3351 try {
3352#endif
3353 for (; __idx != __last; ++__idx)
3354 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3355#ifndef _LIBCPP_NO_EXCEPTIONS
3356 } catch (...) {
3357 _VSTD::destroy(__first, __idx);
3358 throw;
3359 }
3360#endif
3361}
3362
3363template <class _ForwardIterator, class _Size>
3364inline _LIBCPP_INLINE_VISIBILITY
3365_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3366 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3367 auto __idx = __first;
3368#ifndef _LIBCPP_NO_EXCEPTIONS
3369 try {
3370#endif
3371 for (; __n > 0; (void)++__idx, --__n)
3372 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3373 return __idx;
3374#ifndef _LIBCPP_NO_EXCEPTIONS
3375 } catch (...) {
3376 _VSTD::destroy(__first, __idx);
3377 throw;
3378 }
3379#endif
3380}
3381
3382
3383template <class _ForwardIterator>
3384inline _LIBCPP_INLINE_VISIBILITY
3385void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3386 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3387 auto __idx = __first;
3388#ifndef _LIBCPP_NO_EXCEPTIONS
3389 try {
3390#endif
3391 for (; __idx != __last; ++__idx)
3392 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3393#ifndef _LIBCPP_NO_EXCEPTIONS
3394 } catch (...) {
3395 _VSTD::destroy(__first, __idx);
3396 throw;
3397 }
3398#endif
3399}
3400
3401template <class _ForwardIterator, class _Size>
3402inline _LIBCPP_INLINE_VISIBILITY
3403_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3404 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3405 auto __idx = __first;
3406#ifndef _LIBCPP_NO_EXCEPTIONS
3407 try {
3408#endif
3409 for (; __n > 0; (void)++__idx, --__n)
3410 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3411 return __idx;
3412#ifndef _LIBCPP_NO_EXCEPTIONS
3413 } catch (...) {
3414 _VSTD::destroy(__first, __idx);
3415 throw;
3416 }
3417#endif
3418}
3419
3420
3421template <class _InputIt, class _ForwardIt>
3422inline _LIBCPP_INLINE_VISIBILITY
3423_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3424 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3425 auto __idx = __first_res;
3426#ifndef _LIBCPP_NO_EXCEPTIONS
3427 try {
3428#endif
3429 for (; __first != __last; (void)++__idx, ++__first)
3430 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3431 return __idx;
3432#ifndef _LIBCPP_NO_EXCEPTIONS
3433 } catch (...) {
3434 _VSTD::destroy(__first_res, __idx);
3435 throw;
3436 }
3437#endif
3438}
3439
3440template <class _InputIt, class _Size, class _ForwardIt>
3441inline _LIBCPP_INLINE_VISIBILITY
3442pair<_InputIt, _ForwardIt>
3443uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3444 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3445 auto __idx = __first_res;
3446#ifndef _LIBCPP_NO_EXCEPTIONS
3447 try {
3448#endif
3449 for (; __n > 0; ++__idx, (void)++__first, --__n)
3450 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3451 return {__first, __idx};
3452#ifndef _LIBCPP_NO_EXCEPTIONS
3453 } catch (...) {
3454 _VSTD::destroy(__first_res, __idx);
3455 throw;
3456 }
3457#endif
3458}
3459
3460
Eric Fiselierc672a742016-07-24 03:51:39 +00003461#endif // _LIBCPP_STD_VER > 14
3462
Kevin Hu89937592017-01-17 02:46:33 +00003463// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3464// should be sufficient for thread safety.
Eric Fiselierb7fd0be2017-02-17 08:37:03 +00003465// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu89937592017-01-17 02:46:33 +00003466#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3467 && defined(__ATOMIC_RELAXED) \
3468 && defined(__ATOMIC_ACQ_REL)
3469# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3470#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3471# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3472#endif
3473
3474template <class _Tp>
3475inline _LIBCPP_INLINE_VISIBILITY _Tp
3476__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3477{
3478#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3479 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3480#else
3481 return __t += 1;
3482#endif
3483}
3484
3485template <class _Tp>
3486inline _LIBCPP_INLINE_VISIBILITY _Tp
3487__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3488{
3489#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3490 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3491#else
3492 return __t -= 1;
3493#endif
3494}
3495
Howard Hinnant82894812010-09-22 16:48:34 +00003496class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497 : public std::exception
3498{
3499public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003500 virtual ~bad_weak_ptr() _NOEXCEPT;
3501 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003502};
3503
Louis Dionne54238052018-07-11 23:14:33 +00003504_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow14c09a22016-08-25 15:09:01 +00003505void __throw_bad_weak_ptr()
3506{
3507#ifndef _LIBCPP_NO_EXCEPTIONS
3508 throw bad_weak_ptr();
3509#else
3510 _VSTD::abort();
3511#endif
3512}
3513
Eric Fiselierc3589a82017-01-04 23:56:00 +00003514template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003515
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003516class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517{
3518 __shared_count(const __shared_count&);
3519 __shared_count& operator=(const __shared_count&);
3520
3521protected:
3522 long __shared_owners_;
3523 virtual ~__shared_count();
3524private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003525 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526
3527public:
Howard Hinnant82894812010-09-22 16:48:34 +00003528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003529 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530 : __shared_owners_(__refs) {}
3531
Louis Dionne6952d142018-08-01 02:08:59 +00003532#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiseliera7ae3032017-01-17 03:16:26 +00003533 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiselier9133ead2017-01-17 03:05:31 +00003534 void __add_shared() _NOEXCEPT;
3535 bool __release_shared() _NOEXCEPT;
Kevin Hu89937592017-01-17 02:46:33 +00003536#else
3537 _LIBCPP_INLINE_VISIBILITY
3538 void __add_shared() _NOEXCEPT {
3539 __libcpp_atomic_refcount_increment(__shared_owners_);
3540 }
3541 _LIBCPP_INLINE_VISIBILITY
3542 bool __release_shared() _NOEXCEPT {
3543 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3544 __on_zero_shared();
3545 return true;
3546 }
3547 return false;
3548 }
3549#endif
Howard Hinnant82894812010-09-22 16:48:34 +00003550 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +00003551 long use_count() const _NOEXCEPT {
3552 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3553 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003554};
3555
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003556class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557 : private __shared_count
3558{
3559 long __shared_weak_owners_;
3560
3561public:
Howard Hinnant82894812010-09-22 16:48:34 +00003562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003563 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003564 : __shared_count(__refs),
3565 __shared_weak_owners_(__refs) {}
3566protected:
3567 virtual ~__shared_weak_count();
3568
3569public:
Louis Dionne6952d142018-08-01 02:08:59 +00003570#if defined(_LIBCPP_BUILDING_LIBRARY) && \
Eric Fiseliera7ae3032017-01-17 03:16:26 +00003571 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiselier9133ead2017-01-17 03:05:31 +00003572 void __add_shared() _NOEXCEPT;
3573 void __add_weak() _NOEXCEPT;
3574 void __release_shared() _NOEXCEPT;
Kevin Hu89937592017-01-17 02:46:33 +00003575#else
3576 _LIBCPP_INLINE_VISIBILITY
3577 void __add_shared() _NOEXCEPT {
3578 __shared_count::__add_shared();
3579 }
3580 _LIBCPP_INLINE_VISIBILITY
3581 void __add_weak() _NOEXCEPT {
3582 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3583 }
3584 _LIBCPP_INLINE_VISIBILITY
3585 void __release_shared() _NOEXCEPT {
3586 if (__shared_count::__release_shared())
3587 __release_weak();
3588 }
3589#endif
Howard Hinnant1694d232011-05-28 14:41:13 +00003590 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003592 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3593 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003595 // Define the function out only if we build static libc++ without RTTI.
3596 // Otherwise we may break clients who need to compile their projects with
3597 // -fno-rtti and yet link against a libc++.dylib compiled
3598 // without -fno-rtti.
3599#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003600 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003601#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003603 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604};
3605
3606template <class _Tp, class _Dp, class _Alloc>
3607class __shared_ptr_pointer
3608 : public __shared_weak_count
3609{
3610 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3611public:
Howard Hinnant82894812010-09-22 16:48:34 +00003612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003614 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615
Howard Hinnantd4444702010-08-11 17:04:31 +00003616#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003617 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003618#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619
3620private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003621 virtual void __on_zero_shared() _NOEXCEPT;
3622 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623};
3624
Howard Hinnantd4444702010-08-11 17:04:31 +00003625#ifndef _LIBCPP_NO_RTTI
3626
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627template <class _Tp, class _Dp, class _Alloc>
3628const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003629__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630{
Eric Fiselier83e040f2017-05-04 01:06:56 +00003631 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632}
3633
Howard Hinnant324bb032010-08-22 00:02:43 +00003634#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003635
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636template <class _Tp, class _Dp, class _Alloc>
3637void
Howard Hinnant1694d232011-05-28 14:41:13 +00003638__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003639{
3640 __data_.first().second()(__data_.first().first());
3641 __data_.first().second().~_Dp();
3642}
3643
3644template <class _Tp, class _Dp, class _Alloc>
3645void
Howard Hinnant1694d232011-05-28 14:41:13 +00003646__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003648 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3649 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003650 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3651
Eric Fiselier8492cd82015-02-05 23:01:40 +00003652 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003654 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655}
3656
3657template <class _Tp, class _Alloc>
3658class __shared_ptr_emplace
3659 : public __shared_weak_count
3660{
3661 __compressed_pair<_Alloc, _Tp> __data_;
3662public:
3663#ifndef _LIBCPP_HAS_NO_VARIADICS
3664
Howard Hinnant82894812010-09-22 16:48:34 +00003665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003667 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668
3669 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003671 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003672 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3673 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674
3675#else // _LIBCPP_HAS_NO_VARIADICS
3676
Howard Hinnant82894812010-09-22 16:48:34 +00003677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678 __shared_ptr_emplace(_Alloc __a)
3679 : __data_(__a) {}
3680
3681 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3684 : __data_(__a, _Tp(__a0)) {}
3685
3686 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3689 : __data_(__a, _Tp(__a0, __a1)) {}
3690
3691 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003693 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3694 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3695
3696#endif // _LIBCPP_HAS_NO_VARIADICS
3697
3698private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003699 virtual void __on_zero_shared() _NOEXCEPT;
3700 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701public:
Howard Hinnant82894812010-09-22 16:48:34 +00003702 _LIBCPP_INLINE_VISIBILITY
Marshall Clowc4f0f1e2018-08-28 13:29:30 +00003703 _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003704};
3705
3706template <class _Tp, class _Alloc>
3707void
Howard Hinnant1694d232011-05-28 14:41:13 +00003708__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709{
3710 __data_.second().~_Tp();
3711}
3712
3713template <class _Tp, class _Alloc>
3714void
Howard Hinnant1694d232011-05-28 14:41:13 +00003715__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003717 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3718 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003719 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003720 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003721 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003722 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723}
3724
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00003725struct __shared_ptr_dummy_rebind_allocator_type;
3726template <>
3727class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
3728{
3729public:
3730 template <class _Other>
3731 struct rebind
3732 {
3733 typedef allocator<_Other> other;
3734 };
3735};
3736
Eric Fiselierc3589a82017-01-04 23:56:00 +00003737template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738
3739template<class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003740class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741{
Howard Hinnant324bb032010-08-22 00:02:43 +00003742public:
3743 typedef _Tp element_type;
Marshall Clowf6c0b902017-01-10 16:59:33 +00003744
Eric Fiselier83d7ca92016-06-27 01:02:43 +00003745#if _LIBCPP_STD_VER > 14
3746 typedef weak_ptr<_Tp> weak_type;
3747#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748private:
3749 element_type* __ptr_;
3750 __shared_weak_count* __cntrl_;
3751
3752 struct __nat {int __for_bool_;};
3753public:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00003755 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00003757 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003758 template<class _Yp>
3759 explicit shared_ptr(_Yp* __p,
3760 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3761 template<class _Yp, class _Dp>
3762 shared_ptr(_Yp* __p, _Dp __d,
3763 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3764 template<class _Yp, class _Dp, class _Alloc>
3765 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3766 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3768 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003769 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003771 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772 template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clowf6c0b902017-01-10 16:59:33 +00003775 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00003776 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003777#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003779 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003780 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clowf6c0b902017-01-10 16:59:33 +00003781 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00003782 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003783#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003784 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clowf6c0b902017-01-10 16:59:33 +00003785 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb4d17ad2017-01-24 22:22:33 +00003786#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant73d21a42010-09-04 23:28:19 +00003787#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003788 template<class _Yp>
3789 shared_ptr(auto_ptr<_Yp>&& __r,
3790 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003791#else
Logan Chiene1678a12014-01-31 09:30:46 +00003792 template<class _Yp>
3793 shared_ptr(auto_ptr<_Yp> __r,
3794 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795#endif
Marshall Clowb4d17ad2017-01-24 22:22:33 +00003796#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003797#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003798 template <class _Yp, class _Dp>
3799 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3800 typename enable_if
3801 <
3802 !is_lvalue_reference<_Dp>::value &&
3803 !is_array<_Yp>::value &&
3804 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3805 __nat
3806 >::type = __nat());
3807 template <class _Yp, class _Dp>
3808 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3809 typename enable_if
3810 <
3811 is_lvalue_reference<_Dp>::value &&
3812 !is_array<_Yp>::value &&
3813 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3814 __nat
3815 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003816#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003817 template <class _Yp, class _Dp>
3818 shared_ptr(unique_ptr<_Yp, _Dp>,
3819 typename enable_if
3820 <
3821 !is_lvalue_reference<_Dp>::value &&
3822 !is_array<_Yp>::value &&
3823 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3824 __nat
3825 >::type = __nat());
3826 template <class _Yp, class _Dp>
3827 shared_ptr(unique_ptr<_Yp, _Dp>,
3828 typename enable_if
3829 <
3830 is_lvalue_reference<_Dp>::value &&
3831 !is_array<_Yp>::value &&
3832 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3833 __nat
3834 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003835#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003836
3837 ~shared_ptr();
3838
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003840 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003841 template<class _Yp>
3842 typename enable_if
3843 <
3844 is_convertible<_Yp*, element_type*>::value,
3845 shared_ptr&
3846 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003848 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003849#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003851 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003852 template<class _Yp>
3853 typename enable_if
3854 <
3855 is_convertible<_Yp*, element_type*>::value,
3856 shared_ptr<_Tp>&
3857 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003859 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb4d17ad2017-01-24 22:22:33 +00003860#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant57199402012-01-02 17:56:02 +00003861 template<class _Yp>
Eric Fiselier566bcb42016-04-21 22:54:21 +00003862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003863 typename enable_if
3864 <
3865 !is_array<_Yp>::value &&
3866 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003867 shared_ptr
3868 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003869 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb4d17ad2017-01-24 22:22:33 +00003870#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003871#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb4d17ad2017-01-24 22:22:33 +00003872#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant57199402012-01-02 17:56:02 +00003873 template<class _Yp>
Eric Fiselier566bcb42016-04-21 22:54:21 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003875 typename enable_if
3876 <
3877 !is_array<_Yp>::value &&
3878 is_convertible<_Yp*, element_type*>::value,
3879 shared_ptr&
3880 >::type
3881 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882#endif
Marshall Clowb4d17ad2017-01-24 22:22:33 +00003883#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003884 template <class _Yp, class _Dp>
3885 typename enable_if
3886 <
3887 !is_array<_Yp>::value &&
3888 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3889 shared_ptr&
3890 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003891#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003893 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003894#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov28c02db2015-12-09 22:32:36 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003896 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003897#endif
3898
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003900 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003902 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003903 template<class _Yp>
3904 typename enable_if
3905 <
3906 is_convertible<_Yp*, element_type*>::value,
3907 void
3908 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003910 reset(_Yp* __p);
3911 template<class _Yp, class _Dp>
3912 typename enable_if
3913 <
3914 is_convertible<_Yp*, element_type*>::value,
3915 void
3916 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003918 reset(_Yp* __p, _Dp __d);
3919 template<class _Yp, class _Dp, class _Alloc>
3920 typename enable_if
3921 <
3922 is_convertible<_Yp*, element_type*>::value,
3923 void
3924 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003926 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003927
Howard Hinnant82894812010-09-22 16:48:34 +00003928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003929 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003931 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3932 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003934 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003936 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003938 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00003940 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003941 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003942 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00003943 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003944 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003945 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003946 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00003947 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003949 _LIBCPP_INLINE_VISIBILITY
3950 bool
3951 __owner_equivalent(const shared_ptr& __p) const
3952 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003953
Howard Hinnantd4444702010-08-11 17:04:31 +00003954#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003955 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003957 _Dp* __get_deleter() const _NOEXCEPT
Marshall Clow899f1132017-06-14 16:54:43 +00003958 {return static_cast<_Dp*>(__cntrl_
Aditya Kumard4c89052017-08-20 10:38:55 +00003959 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
Marshall Clow899f1132017-06-14 16:54:43 +00003960 : nullptr);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003961#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003962
3963#ifndef _LIBCPP_HAS_NO_VARIADICS
3964
3965 template<class ..._Args>
3966 static
3967 shared_ptr<_Tp>
3968 make_shared(_Args&& ...__args);
3969
3970 template<class _Alloc, class ..._Args>
3971 static
3972 shared_ptr<_Tp>
3973 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3974
3975#else // _LIBCPP_HAS_NO_VARIADICS
3976
3977 static shared_ptr<_Tp> make_shared();
3978
3979 template<class _A0>
3980 static shared_ptr<_Tp> make_shared(_A0&);
3981
3982 template<class _A0, class _A1>
3983 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3984
3985 template<class _A0, class _A1, class _A2>
3986 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3987
3988 template<class _Alloc>
3989 static shared_ptr<_Tp>
3990 allocate_shared(const _Alloc& __a);
3991
3992 template<class _Alloc, class _A0>
3993 static shared_ptr<_Tp>
3994 allocate_shared(const _Alloc& __a, _A0& __a0);
3995
3996 template<class _Alloc, class _A0, class _A1>
3997 static shared_ptr<_Tp>
3998 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3999
4000 template<class _Alloc, class _A0, class _A1, class _A2>
4001 static shared_ptr<_Tp>
4002 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4003
4004#endif // _LIBCPP_HAS_NO_VARIADICS
4005
4006private:
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004007 template <class _Yp, bool = is_function<_Yp>::value>
4008 struct __shared_ptr_default_allocator
4009 {
4010 typedef allocator<_Yp> type;
4011 };
4012
4013 template <class _Yp>
4014 struct __shared_ptr_default_allocator<_Yp, true>
4015 {
4016 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
4017 };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004018
Eric Fiselier78387682016-06-26 23:56:32 +00004019 template <class _Yp, class _OrigPtr>
Howard Hinnant82894812010-09-22 16:48:34 +00004020 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier1bc7a4b2017-05-10 19:35:49 +00004021 typename enable_if<is_convertible<_OrigPtr*,
4022 const enable_shared_from_this<_Yp>*
4023 >::value,
4024 void>::type
Eric Fiselier78387682016-06-26 23:56:32 +00004025 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4026 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004027 {
Eric Fiselier78387682016-06-26 23:56:32 +00004028 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier18e1ea62016-06-02 00:15:35 +00004029 if (__e && __e->__weak_this_.expired())
Marshall Clowc4113372015-06-19 15:54:13 +00004030 {
Eric Fiselier78387682016-06-26 23:56:32 +00004031 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4032 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clowc4113372015-06-19 15:54:13 +00004033 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004034 }
4035
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004036 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004037
Eric Fiselierc3589a82017-01-04 23:56:00 +00004038 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4039 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004040};
4041
Eric Fiselier1bc7a4b2017-05-10 19:35:49 +00004042
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004044inline
Howard Hinnant46e94932012-07-07 20:56:04 +00004045_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004046shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004047 : __ptr_(0),
4048 __cntrl_(0)
4049{
4050}
4051
4052template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004053inline
Howard Hinnant46e94932012-07-07 20:56:04 +00004054_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004055shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004056 : __ptr_(0),
4057 __cntrl_(0)
4058{
4059}
4060
4061template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004062template<class _Yp>
4063shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4064 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004065 : __ptr_(__p)
4066{
4067 unique_ptr<_Yp> __hold(__p);
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004068 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4069 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
4070 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004071 __hold.release();
Eric Fiselier78387682016-06-26 23:56:32 +00004072 __enable_weak_this(__p, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004073}
4074
4075template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004076template<class _Yp, class _Dp>
4077shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4078 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004079 : __ptr_(__p)
4080{
4081#ifndef _LIBCPP_NO_EXCEPTIONS
4082 try
4083 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004084#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004085 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4086 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4087 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Eric Fiselier78387682016-06-26 23:56:32 +00004088 __enable_weak_this(__p, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004089#ifndef _LIBCPP_NO_EXCEPTIONS
4090 }
4091 catch (...)
4092 {
4093 __d(__p);
4094 throw;
4095 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004097}
4098
4099template<class _Tp>
4100template<class _Dp>
4101shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4102 : __ptr_(0)
4103{
4104#ifndef _LIBCPP_NO_EXCEPTIONS
4105 try
4106 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004107#endif // _LIBCPP_NO_EXCEPTIONS
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004108 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
4109 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
4110 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004111#ifndef _LIBCPP_NO_EXCEPTIONS
4112 }
4113 catch (...)
4114 {
4115 __d(__p);
4116 throw;
4117 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004118#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004119}
4120
4121template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004122template<class _Yp, class _Dp, class _Alloc>
4123shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4124 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004125 : __ptr_(__p)
4126{
4127#ifndef _LIBCPP_NO_EXCEPTIONS
4128 try
4129 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004130#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004131 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004132 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004133 typedef __allocator_destructor<_A2> _D2;
4134 _A2 __a2(__a);
4135 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004136 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4137 _CntrlBlk(__p, __d, __a);
4138 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselier78387682016-06-26 23:56:32 +00004139 __enable_weak_this(__p, __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004140#ifndef _LIBCPP_NO_EXCEPTIONS
4141 }
4142 catch (...)
4143 {
4144 __d(__p);
4145 throw;
4146 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004147#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004148}
4149
4150template<class _Tp>
4151template<class _Dp, class _Alloc>
4152shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4153 : __ptr_(0)
4154{
4155#ifndef _LIBCPP_NO_EXCEPTIONS
4156 try
4157 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004158#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004159 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004160 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004161 typedef __allocator_destructor<_A2> _D2;
4162 _A2 __a2(__a);
4163 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004164 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4165 _CntrlBlk(__p, __d, __a);
4166 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004167#ifndef _LIBCPP_NO_EXCEPTIONS
4168 }
4169 catch (...)
4170 {
4171 __d(__p);
4172 throw;
4173 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004174#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004175}
4176
4177template<class _Tp>
4178template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004179inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004180shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004181 : __ptr_(__p),
4182 __cntrl_(__r.__cntrl_)
4183{
4184 if (__cntrl_)
4185 __cntrl_->__add_shared();
4186}
4187
4188template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004189inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004190shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004191 : __ptr_(__r.__ptr_),
4192 __cntrl_(__r.__cntrl_)
4193{
4194 if (__cntrl_)
4195 __cntrl_->__add_shared();
4196}
4197
4198template<class _Tp>
4199template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004200inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clowf6c0b902017-01-10 16:59:33 +00004202 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004203 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004204 : __ptr_(__r.__ptr_),
4205 __cntrl_(__r.__cntrl_)
4206{
4207 if (__cntrl_)
4208 __cntrl_->__add_shared();
4209}
4210
Howard Hinnant73d21a42010-09-04 23:28:19 +00004211#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004212
4213template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004214inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004215shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004216 : __ptr_(__r.__ptr_),
4217 __cntrl_(__r.__cntrl_)
4218{
4219 __r.__ptr_ = 0;
4220 __r.__cntrl_ = 0;
4221}
4222
4223template<class _Tp>
4224template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004225inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004226shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clowf6c0b902017-01-10 16:59:33 +00004227 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004228 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004229 : __ptr_(__r.__ptr_),
4230 __cntrl_(__r.__cntrl_)
4231{
4232 __r.__ptr_ = 0;
4233 __r.__cntrl_ = 0;
4234}
4235
Howard Hinnant73d21a42010-09-04 23:28:19 +00004236#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004237
Marshall Clowb4d17ad2017-01-24 22:22:33 +00004238#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004239template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004240template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004241#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004242shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004243#else
Logan Chiene1678a12014-01-31 09:30:46 +00004244shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004245#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004246 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004247 : __ptr_(__r.get())
4248{
4249 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4250 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselier78387682016-06-26 23:56:32 +00004251 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004252 __r.release();
4253}
Marshall Clowb4d17ad2017-01-24 22:22:33 +00004254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255
4256template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004257template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004258#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004259shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4260#else
4261shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4262#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004263 typename enable_if
4264 <
4265 !is_lvalue_reference<_Dp>::value &&
4266 !is_array<_Yp>::value &&
4267 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4268 __nat
4269 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004270 : __ptr_(__r.get())
4271{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004272#if _LIBCPP_STD_VER > 11
4273 if (__ptr_ == nullptr)
4274 __cntrl_ = nullptr;
4275 else
4276#endif
4277 {
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004278 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
4279 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
4280 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
Eric Fiselier78387682016-06-26 23:56:32 +00004281 __enable_weak_this(__r.get(), __r.get());
Marshall Clow0ad232a2015-05-10 13:59:45 +00004282 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 __r.release();
4284}
4285
4286template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004287template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004288#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004289shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4290#else
4291shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4292#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004293 typename enable_if
4294 <
4295 is_lvalue_reference<_Dp>::value &&
4296 !is_array<_Yp>::value &&
4297 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4298 __nat
4299 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004300 : __ptr_(__r.get())
4301{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004302#if _LIBCPP_STD_VER > 11
4303 if (__ptr_ == nullptr)
4304 __cntrl_ = nullptr;
4305 else
4306#endif
4307 {
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004308 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
Marshall Clow0ad232a2015-05-10 13:59:45 +00004309 typedef __shared_ptr_pointer<_Yp*,
4310 reference_wrapper<typename remove_reference<_Dp>::type>,
Erik Pilkingtona3e0bf42017-05-25 15:43:31 +00004311 _AllocT > _CntrlBlk;
4312 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
Eric Fiselier78387682016-06-26 23:56:32 +00004313 __enable_weak_this(__r.get(), __r.get());
Marshall Clow0ad232a2015-05-10 13:59:45 +00004314 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004315 __r.release();
4316}
4317
4318#ifndef _LIBCPP_HAS_NO_VARIADICS
4319
4320template<class _Tp>
4321template<class ..._Args>
4322shared_ptr<_Tp>
4323shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4324{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004325 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4327 typedef allocator<_CntrlBlk> _A2;
4328 typedef __allocator_destructor<_A2> _D2;
4329 _A2 __a2;
4330 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004331 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004332 shared_ptr<_Tp> __r;
4333 __r.__ptr_ = __hold2.get()->get();
4334 __r.__cntrl_ = __hold2.release();
Eric Fiselier78387682016-06-26 23:56:32 +00004335 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336 return __r;
4337}
4338
4339template<class _Tp>
4340template<class _Alloc, class ..._Args>
4341shared_ptr<_Tp>
4342shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4343{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004344 static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004345 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004346 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004347 typedef __allocator_destructor<_A2> _D2;
4348 _A2 __a2(__a);
4349 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004350 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4351 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004352 shared_ptr<_Tp> __r;
4353 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004354 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselier78387682016-06-26 23:56:32 +00004355 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356 return __r;
4357}
4358
4359#else // _LIBCPP_HAS_NO_VARIADICS
4360
4361template<class _Tp>
4362shared_ptr<_Tp>
4363shared_ptr<_Tp>::make_shared()
4364{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004365 static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004366 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4367 typedef allocator<_CntrlBlk> _Alloc2;
4368 typedef __allocator_destructor<_Alloc2> _D2;
4369 _Alloc2 __alloc2;
4370 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4371 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4372 shared_ptr<_Tp> __r;
4373 __r.__ptr_ = __hold2.get()->get();
4374 __r.__cntrl_ = __hold2.release();
Eric Fiselier78387682016-06-26 23:56:32 +00004375 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004376 return __r;
4377}
4378
4379template<class _Tp>
4380template<class _A0>
4381shared_ptr<_Tp>
4382shared_ptr<_Tp>::make_shared(_A0& __a0)
4383{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004384 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004385 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4386 typedef allocator<_CntrlBlk> _Alloc2;
4387 typedef __allocator_destructor<_Alloc2> _D2;
4388 _Alloc2 __alloc2;
4389 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4390 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4391 shared_ptr<_Tp> __r;
4392 __r.__ptr_ = __hold2.get()->get();
4393 __r.__cntrl_ = __hold2.release();
Eric Fiselier78387682016-06-26 23:56:32 +00004394 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004395 return __r;
4396}
4397
4398template<class _Tp>
4399template<class _A0, class _A1>
4400shared_ptr<_Tp>
4401shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4402{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004403 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004404 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4405 typedef allocator<_CntrlBlk> _Alloc2;
4406 typedef __allocator_destructor<_Alloc2> _D2;
4407 _Alloc2 __alloc2;
4408 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4409 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4410 shared_ptr<_Tp> __r;
4411 __r.__ptr_ = __hold2.get()->get();
4412 __r.__cntrl_ = __hold2.release();
Eric Fiselier78387682016-06-26 23:56:32 +00004413 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004414 return __r;
4415}
4416
4417template<class _Tp>
4418template<class _A0, class _A1, class _A2>
4419shared_ptr<_Tp>
4420shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4421{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004422 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004423 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4424 typedef allocator<_CntrlBlk> _Alloc2;
4425 typedef __allocator_destructor<_Alloc2> _D2;
4426 _Alloc2 __alloc2;
4427 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4428 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4429 shared_ptr<_Tp> __r;
4430 __r.__ptr_ = __hold2.get()->get();
4431 __r.__cntrl_ = __hold2.release();
Eric Fiselier78387682016-06-26 23:56:32 +00004432 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004433 return __r;
4434}
4435
4436template<class _Tp>
4437template<class _Alloc>
4438shared_ptr<_Tp>
4439shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4440{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004441 static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004442 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004443 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004444 typedef __allocator_destructor<_Alloc2> _D2;
4445 _Alloc2 __alloc2(__a);
4446 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004447 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4448 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004449 shared_ptr<_Tp> __r;
4450 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004451 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselier78387682016-06-26 23:56:32 +00004452 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004453 return __r;
4454}
4455
4456template<class _Tp>
4457template<class _Alloc, class _A0>
4458shared_ptr<_Tp>
4459shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4460{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004461 static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004462 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004463 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004464 typedef __allocator_destructor<_Alloc2> _D2;
4465 _Alloc2 __alloc2(__a);
4466 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004467 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4468 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004469 shared_ptr<_Tp> __r;
4470 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004471 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselier78387682016-06-26 23:56:32 +00004472 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004473 return __r;
4474}
4475
4476template<class _Tp>
4477template<class _Alloc, class _A0, class _A1>
4478shared_ptr<_Tp>
4479shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4480{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004481 static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004482 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004483 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004484 typedef __allocator_destructor<_Alloc2> _D2;
4485 _Alloc2 __alloc2(__a);
4486 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004487 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4488 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004489 shared_ptr<_Tp> __r;
4490 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004491 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselier78387682016-06-26 23:56:32 +00004492 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004493 return __r;
4494}
4495
4496template<class _Tp>
4497template<class _Alloc, class _A0, class _A1, class _A2>
4498shared_ptr<_Tp>
4499shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4500{
Marshall Clowdf68ebc2017-12-05 04:09:49 +00004501 static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004502 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004503 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004504 typedef __allocator_destructor<_Alloc2> _D2;
4505 _Alloc2 __alloc2(__a);
4506 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004507 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4508 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004509 shared_ptr<_Tp> __r;
4510 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004511 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselier78387682016-06-26 23:56:32 +00004512 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004513 return __r;
4514}
4515
4516#endif // _LIBCPP_HAS_NO_VARIADICS
4517
4518template<class _Tp>
4519shared_ptr<_Tp>::~shared_ptr()
4520{
4521 if (__cntrl_)
4522 __cntrl_->__release_shared();
4523}
4524
4525template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004526inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004527shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004528shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004529{
4530 shared_ptr(__r).swap(*this);
4531 return *this;
4532}
4533
4534template<class _Tp>
4535template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004536inline
Howard Hinnant57199402012-01-02 17:56:02 +00004537typename enable_if
4538<
Marshall Clowf6c0b902017-01-10 16:59:33 +00004539 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004540 shared_ptr<_Tp>&
4541>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004542shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004543{
4544 shared_ptr(__r).swap(*this);
4545 return *this;
4546}
4547
Howard Hinnant73d21a42010-09-04 23:28:19 +00004548#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004549
4550template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004551inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004552shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004553shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004554{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004555 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004556 return *this;
4557}
4558
4559template<class _Tp>
4560template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004561inline
Howard Hinnant57199402012-01-02 17:56:02 +00004562typename enable_if
4563<
Marshall Clowf6c0b902017-01-10 16:59:33 +00004564 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004565 shared_ptr<_Tp>&
4566>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004567shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4568{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004569 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004570 return *this;
4571}
4572
Marshall Clowb4d17ad2017-01-24 22:22:33 +00004573#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004574template<class _Tp>
4575template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004576inline
Howard Hinnant57199402012-01-02 17:56:02 +00004577typename enable_if
4578<
4579 !is_array<_Yp>::value &&
Marshall Clowf6c0b902017-01-10 16:59:33 +00004580 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004581 shared_ptr<_Tp>
4582>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004583shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4584{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004585 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004586 return *this;
4587}
Marshall Clowb4d17ad2017-01-24 22:22:33 +00004588#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004589
4590template<class _Tp>
4591template <class _Yp, class _Dp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004592inline
Howard Hinnant57199402012-01-02 17:56:02 +00004593typename enable_if
4594<
4595 !is_array<_Yp>::value &&
Aditya Kumard4c89052017-08-20 10:38:55 +00004596 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowf6c0b902017-01-10 16:59:33 +00004597 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004598 shared_ptr<_Tp>&
4599>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004600shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4601{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004602 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004603 return *this;
4604}
4605
Howard Hinnant73d21a42010-09-04 23:28:19 +00004606#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004607
Marshall Clowb4d17ad2017-01-24 22:22:33 +00004608#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004609template<class _Tp>
4610template<class _Yp>
4611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004612typename enable_if
4613<
4614 !is_array<_Yp>::value &&
Marshall Clowbd7c8842017-01-10 18:40:01 +00004615 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004616 shared_ptr<_Tp>&
4617>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004618shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004619{
4620 shared_ptr(__r).swap(*this);
4621 return *this;
4622}
Marshall Clowb4d17ad2017-01-24 22:22:33 +00004623#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004624
4625template<class _Tp>
4626template <class _Yp, class _Dp>
4627inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004628typename enable_if
4629<
4630 !is_array<_Yp>::value &&
Aditya Kumard4c89052017-08-20 10:38:55 +00004631 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
Marshall Clowbd7c8842017-01-10 18:40:01 +00004632 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004633 shared_ptr<_Tp>&
4634>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004635shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4636{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004637 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004638 return *this;
4639}
4640
Howard Hinnant73d21a42010-09-04 23:28:19 +00004641#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004642
4643template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004644inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645void
Howard Hinnant1694d232011-05-28 14:41:13 +00004646shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004647{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004648 _VSTD::swap(__ptr_, __r.__ptr_);
4649 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004650}
4651
4652template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004653inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004654void
Howard Hinnant1694d232011-05-28 14:41:13 +00004655shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004656{
4657 shared_ptr().swap(*this);
4658}
4659
4660template<class _Tp>
4661template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004662inline
Howard Hinnant57199402012-01-02 17:56:02 +00004663typename enable_if
4664<
Marshall Clowf6c0b902017-01-10 16:59:33 +00004665 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004666 void
4667>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004668shared_ptr<_Tp>::reset(_Yp* __p)
4669{
4670 shared_ptr(__p).swap(*this);
4671}
4672
4673template<class _Tp>
4674template<class _Yp, class _Dp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004675inline
Howard Hinnant57199402012-01-02 17:56:02 +00004676typename enable_if
4677<
Marshall Clowf6c0b902017-01-10 16:59:33 +00004678 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004679 void
4680>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004681shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4682{
4683 shared_ptr(__p, __d).swap(*this);
4684}
4685
4686template<class _Tp>
4687template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004688inline
Howard Hinnant57199402012-01-02 17:56:02 +00004689typename enable_if
4690<
Marshall Clowf6c0b902017-01-10 16:59:33 +00004691 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant57199402012-01-02 17:56:02 +00004692 void
4693>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004694shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4695{
4696 shared_ptr(__p, __d, __a).swap(*this);
4697}
4698
4699#ifndef _LIBCPP_HAS_NO_VARIADICS
4700
Howard Hinnant324bb032010-08-22 00:02:43 +00004701template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004703typename enable_if
4704<
4705 !is_array<_Tp>::value,
4706 shared_ptr<_Tp>
4707>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004708make_shared(_Args&& ...__args)
4709{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004710 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004711}
4712
Howard Hinnant324bb032010-08-22 00:02:43 +00004713template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004714inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004715typename enable_if
4716<
4717 !is_array<_Tp>::value,
4718 shared_ptr<_Tp>
4719>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004720allocate_shared(const _Alloc& __a, _Args&& ...__args)
4721{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004722 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004723}
4724
4725#else // _LIBCPP_HAS_NO_VARIADICS
4726
4727template<class _Tp>
4728inline _LIBCPP_INLINE_VISIBILITY
4729shared_ptr<_Tp>
4730make_shared()
4731{
4732 return shared_ptr<_Tp>::make_shared();
4733}
4734
4735template<class _Tp, class _A0>
4736inline _LIBCPP_INLINE_VISIBILITY
4737shared_ptr<_Tp>
4738make_shared(_A0& __a0)
4739{
4740 return shared_ptr<_Tp>::make_shared(__a0);
4741}
4742
4743template<class _Tp, class _A0, class _A1>
4744inline _LIBCPP_INLINE_VISIBILITY
4745shared_ptr<_Tp>
4746make_shared(_A0& __a0, _A1& __a1)
4747{
4748 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4749}
4750
Howard Hinnant324bb032010-08-22 00:02:43 +00004751template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004752inline _LIBCPP_INLINE_VISIBILITY
4753shared_ptr<_Tp>
4754make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4755{
4756 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4757}
4758
4759template<class _Tp, class _Alloc>
4760inline _LIBCPP_INLINE_VISIBILITY
4761shared_ptr<_Tp>
4762allocate_shared(const _Alloc& __a)
4763{
4764 return shared_ptr<_Tp>::allocate_shared(__a);
4765}
4766
4767template<class _Tp, class _Alloc, class _A0>
4768inline _LIBCPP_INLINE_VISIBILITY
4769shared_ptr<_Tp>
4770allocate_shared(const _Alloc& __a, _A0& __a0)
4771{
4772 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4773}
4774
4775template<class _Tp, class _Alloc, class _A0, class _A1>
4776inline _LIBCPP_INLINE_VISIBILITY
4777shared_ptr<_Tp>
4778allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4779{
4780 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4781}
4782
4783template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4784inline _LIBCPP_INLINE_VISIBILITY
4785shared_ptr<_Tp>
4786allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4787{
4788 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4789}
4790
4791#endif // _LIBCPP_HAS_NO_VARIADICS
4792
4793template<class _Tp, class _Up>
4794inline _LIBCPP_INLINE_VISIBILITY
4795bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004796operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004797{
4798 return __x.get() == __y.get();
4799}
4800
4801template<class _Tp, class _Up>
4802inline _LIBCPP_INLINE_VISIBILITY
4803bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004804operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004805{
4806 return !(__x == __y);
4807}
4808
4809template<class _Tp, class _Up>
4810inline _LIBCPP_INLINE_VISIBILITY
4811bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004812operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004813{
Marshall Clowf72f2192018-02-12 17:26:40 +00004814#if _LIBCPP_STD_VER <= 11
Eric Fiselier8492cd82015-02-05 23:01:40 +00004815 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4816 return less<_Vp>()(__x.get(), __y.get());
Marshall Clowf72f2192018-02-12 17:26:40 +00004817#else
4818 return less<>()(__x.get(), __y.get());
4819#endif
4820
Howard Hinnant3fadda32012-02-21 21:02:58 +00004821}
4822
4823template<class _Tp, class _Up>
4824inline _LIBCPP_INLINE_VISIBILITY
4825bool
4826operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4827{
4828 return __y < __x;
4829}
4830
4831template<class _Tp, class _Up>
4832inline _LIBCPP_INLINE_VISIBILITY
4833bool
4834operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4835{
4836 return !(__y < __x);
4837}
4838
4839template<class _Tp, class _Up>
4840inline _LIBCPP_INLINE_VISIBILITY
4841bool
4842operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4843{
4844 return !(__x < __y);
4845}
4846
4847template<class _Tp>
4848inline _LIBCPP_INLINE_VISIBILITY
4849bool
4850operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4851{
4852 return !__x;
4853}
4854
4855template<class _Tp>
4856inline _LIBCPP_INLINE_VISIBILITY
4857bool
4858operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4859{
4860 return !__x;
4861}
4862
4863template<class _Tp>
4864inline _LIBCPP_INLINE_VISIBILITY
4865bool
4866operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4867{
4868 return static_cast<bool>(__x);
4869}
4870
4871template<class _Tp>
4872inline _LIBCPP_INLINE_VISIBILITY
4873bool
4874operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4875{
4876 return static_cast<bool>(__x);
4877}
4878
4879template<class _Tp>
4880inline _LIBCPP_INLINE_VISIBILITY
4881bool
4882operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4883{
4884 return less<_Tp*>()(__x.get(), nullptr);
4885}
4886
4887template<class _Tp>
4888inline _LIBCPP_INLINE_VISIBILITY
4889bool
4890operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4891{
4892 return less<_Tp*>()(nullptr, __x.get());
4893}
4894
4895template<class _Tp>
4896inline _LIBCPP_INLINE_VISIBILITY
4897bool
4898operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4899{
4900 return nullptr < __x;
4901}
4902
4903template<class _Tp>
4904inline _LIBCPP_INLINE_VISIBILITY
4905bool
4906operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4907{
4908 return __x < nullptr;
4909}
4910
4911template<class _Tp>
4912inline _LIBCPP_INLINE_VISIBILITY
4913bool
4914operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4915{
4916 return !(nullptr < __x);
4917}
4918
4919template<class _Tp>
4920inline _LIBCPP_INLINE_VISIBILITY
4921bool
4922operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4923{
4924 return !(__x < nullptr);
4925}
4926
4927template<class _Tp>
4928inline _LIBCPP_INLINE_VISIBILITY
4929bool
4930operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4931{
4932 return !(__x < nullptr);
4933}
4934
4935template<class _Tp>
4936inline _LIBCPP_INLINE_VISIBILITY
4937bool
4938operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4939{
4940 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004941}
4942
4943template<class _Tp>
4944inline _LIBCPP_INLINE_VISIBILITY
4945void
Howard Hinnant1694d232011-05-28 14:41:13 +00004946swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004947{
4948 __x.swap(__y);
4949}
4950
4951template<class _Tp, class _Up>
4952inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004953typename enable_if
4954<
4955 !is_array<_Tp>::value && !is_array<_Up>::value,
4956 shared_ptr<_Tp>
4957>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004958static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004959{
4960 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4961}
4962
4963template<class _Tp, class _Up>
4964inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004965typename enable_if
4966<
4967 !is_array<_Tp>::value && !is_array<_Up>::value,
4968 shared_ptr<_Tp>
4969>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004970dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004971{
4972 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4973 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4974}
4975
4976template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004977typename enable_if
4978<
4979 is_array<_Tp>::value == is_array<_Up>::value,
4980 shared_ptr<_Tp>
4981>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004982const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004983{
Howard Hinnant57199402012-01-02 17:56:02 +00004984 typedef typename remove_extent<_Tp>::type _RTp;
4985 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004986}
4987
Howard Hinnantd4444702010-08-11 17:04:31 +00004988#ifndef _LIBCPP_NO_RTTI
4989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004990template<class _Dp, class _Tp>
4991inline _LIBCPP_INLINE_VISIBILITY
4992_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004993get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004994{
4995 return __p.template __get_deleter<_Dp>();
4996}
4997
Howard Hinnant324bb032010-08-22 00:02:43 +00004998#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004999
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005000template<class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00005001class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005002{
Howard Hinnant324bb032010-08-22 00:02:43 +00005003public:
5004 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005private:
5006 element_type* __ptr_;
5007 __shared_weak_count* __cntrl_;
5008
Howard Hinnant324bb032010-08-22 00:02:43 +00005009public:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005011 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005012 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005013 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5014 _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005016 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005017 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005018 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5019 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005020
Howard Hinnant57199402012-01-02 17:56:02 +00005021#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005023 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005024 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant57199402012-01-02 17:56:02 +00005025 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5026 _NOEXCEPT;
5027#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005028 ~weak_ptr();
5029
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005031 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005032 template<class _Yp>
5033 typename enable_if
5034 <
5035 is_convertible<_Yp*, element_type*>::value,
5036 weak_ptr&
5037 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005039 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5040
5041#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5042
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005044 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5045 template<class _Yp>
5046 typename enable_if
5047 <
5048 is_convertible<_Yp*, element_type*>::value,
5049 weak_ptr&
5050 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005052 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5053
5054#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5055
5056 template<class _Yp>
5057 typename enable_if
5058 <
5059 is_convertible<_Yp*, element_type*>::value,
5060 weak_ptr&
5061 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005063 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005064
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005066 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005068 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005069
Howard Hinnant82894812010-09-22 16:48:34 +00005070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005071 long use_count() const _NOEXCEPT
5072 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005074 bool expired() const _NOEXCEPT
5075 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5076 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005077 template<class _Up>
5078 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005079 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005080 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005081 template<class _Up>
5082 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005083 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005084 {return __cntrl_ < __r.__cntrl_;}
5085
Eric Fiselierc3589a82017-01-04 23:56:00 +00005086 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5087 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005088};
5089
5090template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005091inline
Howard Hinnant46e94932012-07-07 20:56:04 +00005092_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005093weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005094 : __ptr_(0),
5095 __cntrl_(0)
5096{
5097}
5098
5099template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005100inline
Howard Hinnant1694d232011-05-28 14:41:13 +00005101weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005102 : __ptr_(__r.__ptr_),
5103 __cntrl_(__r.__cntrl_)
5104{
5105 if (__cntrl_)
5106 __cntrl_->__add_weak();
5107}
5108
5109template<class _Tp>
5110template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005111inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005112weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005113 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005114 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005115 : __ptr_(__r.__ptr_),
5116 __cntrl_(__r.__cntrl_)
5117{
5118 if (__cntrl_)
5119 __cntrl_->__add_weak();
5120}
5121
5122template<class _Tp>
5123template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005124inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005125weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005126 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005127 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005128 : __ptr_(__r.__ptr_),
5129 __cntrl_(__r.__cntrl_)
5130{
5131 if (__cntrl_)
5132 __cntrl_->__add_weak();
5133}
5134
Howard Hinnant57199402012-01-02 17:56:02 +00005135#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5136
5137template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005138inline
Howard Hinnant57199402012-01-02 17:56:02 +00005139weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5140 : __ptr_(__r.__ptr_),
5141 __cntrl_(__r.__cntrl_)
5142{
5143 __r.__ptr_ = 0;
5144 __r.__cntrl_ = 0;
5145}
5146
5147template<class _Tp>
5148template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005149inline
Howard Hinnant57199402012-01-02 17:56:02 +00005150weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5151 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5152 _NOEXCEPT
5153 : __ptr_(__r.__ptr_),
5154 __cntrl_(__r.__cntrl_)
5155{
5156 __r.__ptr_ = 0;
5157 __r.__cntrl_ = 0;
5158}
5159
5160#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5161
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005162template<class _Tp>
5163weak_ptr<_Tp>::~weak_ptr()
5164{
5165 if (__cntrl_)
5166 __cntrl_->__release_weak();
5167}
5168
5169template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005170inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005171weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005172weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005173{
5174 weak_ptr(__r).swap(*this);
5175 return *this;
5176}
5177
5178template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005179template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005180inline
Howard Hinnant57199402012-01-02 17:56:02 +00005181typename enable_if
5182<
5183 is_convertible<_Yp*, _Tp*>::value,
5184 weak_ptr<_Tp>&
5185>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005186weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005187{
5188 weak_ptr(__r).swap(*this);
5189 return *this;
5190}
5191
Howard Hinnant57199402012-01-02 17:56:02 +00005192#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5193
5194template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005195inline
Howard Hinnant57199402012-01-02 17:56:02 +00005196weak_ptr<_Tp>&
5197weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5198{
5199 weak_ptr(_VSTD::move(__r)).swap(*this);
5200 return *this;
5201}
5202
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005203template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005204template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005205inline
Howard Hinnant57199402012-01-02 17:56:02 +00005206typename enable_if
5207<
5208 is_convertible<_Yp*, _Tp*>::value,
5209 weak_ptr<_Tp>&
5210>::type
5211weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5212{
5213 weak_ptr(_VSTD::move(__r)).swap(*this);
5214 return *this;
5215}
5216
5217#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5218
5219template<class _Tp>
5220template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005221inline
Howard Hinnant57199402012-01-02 17:56:02 +00005222typename enable_if
5223<
5224 is_convertible<_Yp*, _Tp*>::value,
5225 weak_ptr<_Tp>&
5226>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005227weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005228{
5229 weak_ptr(__r).swap(*this);
5230 return *this;
5231}
5232
5233template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005234inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005235void
Howard Hinnant1694d232011-05-28 14:41:13 +00005236weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005237{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005238 _VSTD::swap(__ptr_, __r.__ptr_);
5239 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005240}
5241
5242template<class _Tp>
5243inline _LIBCPP_INLINE_VISIBILITY
5244void
Howard Hinnant1694d232011-05-28 14:41:13 +00005245swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005246{
5247 __x.swap(__y);
5248}
5249
5250template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005251inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005252void
Howard Hinnant1694d232011-05-28 14:41:13 +00005253weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005254{
5255 weak_ptr().swap(*this);
5256}
5257
5258template<class _Tp>
5259template<class _Yp>
5260shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clowf6c0b902017-01-10 16:59:33 +00005261 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005262 : __ptr_(__r.__ptr_),
5263 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5264{
5265 if (__cntrl_ == 0)
Marshall Clow14c09a22016-08-25 15:09:01 +00005266 __throw_bad_weak_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005267}
5268
5269template<class _Tp>
5270shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005271weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005272{
5273 shared_ptr<_Tp> __r;
5274 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5275 if (__r.__cntrl_)
5276 __r.__ptr_ = __ptr_;
5277 return __r;
5278}
5279
Marshall Clow3f159e82015-11-12 15:56:44 +00005280#if _LIBCPP_STD_VER > 14
5281template <class _Tp = void> struct owner_less;
5282#else
Howard Hinnant324bb032010-08-22 00:02:43 +00005283template <class _Tp> struct owner_less;
Marshall Clow3f159e82015-11-12 15:56:44 +00005284#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005285
5286template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00005287struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005288 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005289{
5290 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005291 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005292 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005293 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005294 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005295 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005296 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005297 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005298 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005299 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005300};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005301
5302template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00005303struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005304 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5305{
Howard Hinnant324bb032010-08-22 00:02:43 +00005306 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005307 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005308 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005309 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005310 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005311 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005312 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005313 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005314 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005315 {return __x.owner_before(__y);}
5316};
5317
Marshall Clow3f159e82015-11-12 15:56:44 +00005318#if _LIBCPP_STD_VER > 14
5319template <>
Eric Fiselierc3589a82017-01-04 23:56:00 +00005320struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3f159e82015-11-12 15:56:44 +00005321{
5322 template <class _Tp, class _Up>
5323 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005324 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3f159e82015-11-12 15:56:44 +00005325 {return __x.owner_before(__y);}
5326 template <class _Tp, class _Up>
5327 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005328 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3f159e82015-11-12 15:56:44 +00005329 {return __x.owner_before(__y);}
5330 template <class _Tp, class _Up>
5331 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005332 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3f159e82015-11-12 15:56:44 +00005333 {return __x.owner_before(__y);}
5334 template <class _Tp, class _Up>
5335 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01208af2017-04-11 17:08:53 +00005336 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3f159e82015-11-12 15:56:44 +00005337 {return __x.owner_before(__y);}
5338 typedef void is_transparent;
5339};
5340#endif
5341
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005342template<class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00005343class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005344{
5345 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005346protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005347 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005348 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005350 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005352 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5353 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005355 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005356public:
Howard Hinnant82894812010-09-22 16:48:34 +00005357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005358 shared_ptr<_Tp> shared_from_this()
5359 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005361 shared_ptr<_Tp const> shared_from_this() const
5362 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005363
Eric Fiselier18e1ea62016-06-02 00:15:35 +00005364#if _LIBCPP_STD_VER > 14
5365 _LIBCPP_INLINE_VISIBILITY
5366 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5367 { return __weak_this_; }
5368
5369 _LIBCPP_INLINE_VISIBILITY
5370 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5371 { return __weak_this_; }
5372#endif // _LIBCPP_STD_VER > 14
5373
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005374 template <class _Up> friend class shared_ptr;
5375};
5376
Howard Hinnant21aefc32010-06-03 16:42:57 +00005377template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00 +00005378struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005379{
5380 typedef shared_ptr<_Tp> argument_type;
5381 typedef size_t result_type;
Eric Fiselier952eaec2017-01-21 00:02:12 +00005382
Howard Hinnant82894812010-09-22 16:48:34 +00005383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005384 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005385 {
5386 return hash<_Tp*>()(__ptr.get());
5387 }
5388};
5389
Howard Hinnant99968442011-11-29 18:15:50 +00005390template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005391inline _LIBCPP_INLINE_VISIBILITY
5392basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005393operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005394
Eric Fiselierba9dccd2016-06-18 02:12:53 +00005395
5396#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005397
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005398class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005399{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005400 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005401public:
5402 void lock() _NOEXCEPT;
5403 void unlock() _NOEXCEPT;
5404
5405private:
5406 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5407 __sp_mut(const __sp_mut&);
5408 __sp_mut& operator=(const __sp_mut&);
5409
Howard Hinnant83eade62013-03-06 23:30:19 +00005410 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005411};
5412
Mehdi Amini907c1192017-05-04 17:08:54 +00005413_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5414__sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005415
5416template <class _Tp>
5417inline _LIBCPP_INLINE_VISIBILITY
5418bool
5419atomic_is_lock_free(const shared_ptr<_Tp>*)
5420{
5421 return false;
5422}
5423
5424template <class _Tp>
Mehdi Amini907c1192017-05-04 17:08:54 +00005425_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005426shared_ptr<_Tp>
5427atomic_load(const shared_ptr<_Tp>* __p)
5428{
5429 __sp_mut& __m = __get_sp_mut(__p);
5430 __m.lock();
5431 shared_ptr<_Tp> __q = *__p;
5432 __m.unlock();
5433 return __q;
5434}
Aditya Kumard4c89052017-08-20 10:38:55 +00005435
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005436template <class _Tp>
5437inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54 +00005438_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005439shared_ptr<_Tp>
5440atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5441{
5442 return atomic_load(__p);
5443}
5444
5445template <class _Tp>
Mehdi Amini907c1192017-05-04 17:08:54 +00005446_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005447void
5448atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5449{
5450 __sp_mut& __m = __get_sp_mut(__p);
5451 __m.lock();
5452 __p->swap(__r);
5453 __m.unlock();
5454}
5455
5456template <class _Tp>
5457inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54 +00005458_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005459void
5460atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5461{
5462 atomic_store(__p, __r);
5463}
5464
5465template <class _Tp>
Mehdi Amini907c1192017-05-04 17:08:54 +00005466_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005467shared_ptr<_Tp>
5468atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5469{
5470 __sp_mut& __m = __get_sp_mut(__p);
5471 __m.lock();
5472 __p->swap(__r);
5473 __m.unlock();
5474 return __r;
5475}
Aditya Kumard4c89052017-08-20 10:38:55 +00005476
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005477template <class _Tp>
5478inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54 +00005479_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005480shared_ptr<_Tp>
5481atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5482{
5483 return atomic_exchange(__p, __r);
5484}
5485
5486template <class _Tp>
Mehdi Amini907c1192017-05-04 17:08:54 +00005487_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005488bool
5489atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5490{
Marshall Clow2241cf02016-05-18 17:50:13 +00005491 shared_ptr<_Tp> __temp;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005492 __sp_mut& __m = __get_sp_mut(__p);
5493 __m.lock();
5494 if (__p->__owner_equivalent(*__v))
5495 {
Marshall Clow2241cf02016-05-18 17:50:13 +00005496 _VSTD::swap(__temp, *__p);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005497 *__p = __w;
5498 __m.unlock();
5499 return true;
5500 }
Marshall Clow2241cf02016-05-18 17:50:13 +00005501 _VSTD::swap(__temp, *__v);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005502 *__v = *__p;
5503 __m.unlock();
5504 return false;
5505}
5506
5507template <class _Tp>
5508inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54 +00005509_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005510bool
5511atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5512{
5513 return atomic_compare_exchange_strong(__p, __v, __w);
5514}
5515
5516template <class _Tp>
5517inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54 +00005518_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005519bool
5520atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5521 shared_ptr<_Tp> __w, memory_order, memory_order)
5522{
5523 return atomic_compare_exchange_strong(__p, __v, __w);
5524}
5525
5526template <class _Tp>
5527inline _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54 +00005528_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005529bool
5530atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5531 shared_ptr<_Tp> __w, memory_order, memory_order)
5532{
5533 return atomic_compare_exchange_weak(__p, __v, __w);
5534}
5535
Eric Fiselierba9dccd2016-06-18 02:12:53 +00005536#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005537
Howard Hinnant324bb032010-08-22 00:02:43 +00005538//enum class
Eric Fiselier46a0c2e2017-01-05 01:15:42 +00005539#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5540# ifndef _LIBCPP_CXX03_LANG
5541enum class pointer_safety : unsigned char {
5542 relaxed,
5543 preferred,
5544 strict
5545};
5546# endif
5547#else
Howard Hinnant83eade62013-03-06 23:30:19 +00005548struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005549{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005550 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005551 {
5552 relaxed,
5553 preferred,
5554 strict
5555 };
5556
Howard Hinnant9c0df142012-10-30 19:06:59 +00005557 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005558
Howard Hinnant82894812010-09-22 16:48:34 +00005559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc3dfece2017-01-05 01:28:40 +00005560 pointer_safety() : __v_() {}
5561
5562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005563 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005565 operator int() const {return __v_;}
5566};
Eric Fiselier46a0c2e2017-01-05 01:15:42 +00005567#endif
5568
5569#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
Louis Dionne6952d142018-08-01 02:08:59 +00005570 defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselier46a0c2e2017-01-05 01:15:42 +00005571_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5572#else
5573// This function is only offered in C++03 under ABI v1.
5574# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5575inline _LIBCPP_INLINE_VISIBILITY
5576pointer_safety get_pointer_safety() _NOEXCEPT {
5577 return pointer_safety::relaxed;
5578}
5579# endif
5580#endif
5581
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005582
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005583_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5584_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5585_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005586_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005587
5588template <class _Tp>
5589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005590_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005591undeclare_reachable(_Tp* __p)
5592{
5593 return static_cast<_Tp*>(__undeclare_reachable(__p));
5594}
5595
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005596_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005597
Marshall Clow7d914d12015-07-13 20:04:56 +00005598// --- Helper for container swap --
5599template <typename _Alloc>
Eric Fiselier566bcb42016-04-21 22:54:21 +00005600inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005601void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5602#if _LIBCPP_STD_VER >= 14
5603 _NOEXCEPT
5604#else
5605 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5606#endif
5607{
Aditya Kumard4c89052017-08-20 10:38:55 +00005608 __swap_allocator(__a1, __a2,
Marshall Clow7d914d12015-07-13 20:04:56 +00005609 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5610}
5611
5612template <typename _Alloc>
5613_LIBCPP_INLINE_VISIBILITY
5614void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5615#if _LIBCPP_STD_VER >= 14
5616 _NOEXCEPT
5617#else
5618 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5619#endif
5620{
5621 using _VSTD::swap;
5622 swap(__a1, __a2);
5623}
5624
5625template <typename _Alloc>
Eric Fiselier566bcb42016-04-21 22:54:21 +00005626inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005627void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5628
Marshall Clowd434e2a2015-08-18 19:51:37 +00005629template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Aditya Kumard4c89052017-08-20 10:38:55 +00005630struct __noexcept_move_assign_container : public integral_constant<bool,
Marshall Clowaf961ed2015-08-18 18:57:00 +00005631 _Traits::propagate_on_container_move_assignment::value
5632#if _LIBCPP_STD_VER > 14
5633 || _Traits::is_always_equal::value
5634#else
5635 && is_nothrow_move_assignable<_Alloc>::value
5636#endif
5637 > {};
Marshall Clow7d914d12015-07-13 20:04:56 +00005638
Marshall Clow51d7e8e2016-07-11 21:38:08 +00005639
5640#ifndef _LIBCPP_HAS_NO_VARIADICS
5641template <class _Tp, class _Alloc>
5642struct __temp_value {
5643 typedef allocator_traits<_Alloc> _Traits;
Aditya Kumard4c89052017-08-20 10:38:55 +00005644
Marshall Clow51d7e8e2016-07-11 21:38:08 +00005645 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5646 _Alloc &__a;
5647
5648 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5649 _Tp & get() { return *__addr(); }
Aditya Kumard4c89052017-08-20 10:38:55 +00005650
Marshall Clow51d7e8e2016-07-11 21:38:08 +00005651 template<class... _Args>
Peter Collingbourne62176002018-08-15 17:49:30 +00005652 _LIBCPP_NO_CFI
5653 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5654 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5655 _VSTD::forward<_Args>(__args)...);
5656 }
Aditya Kumard4c89052017-08-20 10:38:55 +00005657
Marshall Clow51d7e8e2016-07-11 21:38:08 +00005658 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5659 };
5660#endif
5661
Marshall Clow64c10d02018-07-02 18:41:15 +00005662template<typename _Alloc, typename = void, typename = void>
Marshall Clowd09b2ed2018-01-11 19:36:22 +00005663struct __is_allocator : false_type {};
5664
5665template<typename _Alloc>
5666struct __is_allocator<_Alloc,
Marshall Clow64c10d02018-07-02 18:41:15 +00005667 typename __void_t<typename _Alloc::value_type>::type,
5668 typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
5669 >
Marshall Clowd09b2ed2018-01-11 19:36:22 +00005670 : true_type {};
Marshall Clowd09b2ed2018-01-11 19:36:22 +00005671
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005672_LIBCPP_END_NAMESPACE_STD
5673
Eric Fiselier018a3d52017-05-31 22:07:49 +00005674_LIBCPP_POP_MACROS
5675
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005676#endif // _LIBCPP_MEMORY