1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___MEMORY_UNIQUE_PTR_H
11#define _LIBCPP___MEMORY_UNIQUE_PTR_H
12
13#include <__assert>
14#include <__compare/compare_three_way.h>
15#include <__compare/compare_three_way_result.h>
16#include <__compare/three_way_comparable.h>
17#include <__config>
18#include <__cstddef/nullptr_t.h>
19#include <__cstddef/size_t.h>
20#include <__functional/hash.h>
21#include <__functional/operations.h>
22#include <__memory/allocator_traits.h> // __pointer
23#include <__memory/array_cookie.h>
24#include <__memory/auto_ptr.h>
25#include <__memory/compressed_pair.h>
26#include <__memory/pointer_traits.h>
27#include <__type_traits/add_reference.h>
28#include <__type_traits/common_type.h>
29#include <__type_traits/conditional.h>
30#include <__type_traits/enable_if.h>
31#include <__type_traits/integral_constant.h>
32#include <__type_traits/is_array.h>
33#include <__type_traits/is_assignable.h>
34#include <__type_traits/is_constant_evaluated.h>
35#include <__type_traits/is_constructible.h>
36#include <__type_traits/is_convertible.h>
37#include <__type_traits/is_function.h>
38#include <__type_traits/is_pointer.h>
39#include <__type_traits/is_reference.h>
40#include <__type_traits/is_same.h>
41#include <__type_traits/is_swappable.h>
42#include <__type_traits/is_trivially_relocatable.h>
43#include <__type_traits/is_void.h>
44#include <__type_traits/remove_extent.h>
45#include <__type_traits/remove_reference.h>
46#include <__type_traits/void_t.h>
47#include <__utility/declval.h>
48#include <__utility/forward.h>
49#include <__utility/move.h>
50#include <__utility/private_constructor_tag.h>
51#include <cstdint>
52
53#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
54# pragma GCC system_header
55#endif
56
57_LIBCPP_PUSH_MACROS
58#include <__undef_macros>
59
60_LIBCPP_BEGIN_NAMESPACE_STD
61
62template <class _Tp>
63struct default_delete {
64 static_assert(!is_function<_Tp>::value, "default_delete cannot be instantiated for function types");
65
66 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
67
68 template <class _Up, __enable_if_t<is_convertible<_Up*, _Tp*>::value, int> = 0>
69 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 default_delete(const default_delete<_Up>&) _NOEXCEPT {}
70
71 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator()(_Tp* __ptr) const _NOEXCEPT {
72 static_assert(sizeof(_Tp) >= 0, "cannot delete an incomplete type");
73 static_assert(!is_void<_Tp>::value, "cannot delete an incomplete type");
74 delete __ptr;
75 }
76};
77
78template <class _Tp>
79struct default_delete<_Tp[]> {
80 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
81
82 template <class _Up, __enable_if_t<is_convertible<_Up (*)[], _Tp (*)[]>::value, int> = 0>
83 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 default_delete(const default_delete<_Up[]>&) _NOEXCEPT {}
84
85 template <class _Up, __enable_if_t<is_convertible<_Up (*)[], _Tp (*)[]>::value, int> = 0>
86 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator()(_Up* __ptr) const _NOEXCEPT {
87 static_assert(sizeof(_Up) >= 0, "cannot delete an incomplete type");
88 delete[] __ptr;
89 }
90};
91
92template <class _Deleter>
93inline const bool __is_default_deleter_v = false;
94
95template <class _Tp>
96inline const bool __is_default_deleter_v<default_delete<_Tp> > = true;
97
98template <class, class = void>
99inline const bool __can_dereference = false;
100
101template <class _Tp>
102inline const bool __can_dereference<_Tp, decltype((void)*std::declval<_Tp>())> = true;
103
104template <class _Tp, class = void>
105inline const bool __can_add_pointer = false;
106
107template <class _Tp>
108inline const bool __can_add_pointer<_Tp, __void_t<_Tp*> > = true;
109
110#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
111# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))
112#else
113# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
114#endif
115
116template <class _Tp, class _Dp = default_delete<_Tp> >
117class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI unique_ptr {
118 static_assert(__can_add_pointer<_Tp>, "unique_ptr<T, D> requires T* to be a valid type");
119 static_assert(!is_rvalue_reference<_Dp>::value, "the specified deleter type cannot be an rvalue reference");
120
121public:
122 typedef _Tp element_type;
123 typedef _Dp deleter_type;
124 using pointer _LIBCPP_NODEBUG = __pointer<_Tp, deleter_type>;
125
126 // A unique_ptr contains the following members which may be trivially relocatable:
127 // - pointer : this may be trivially relocatable, so it's checked
128 // - deleter_type: this may be trivially relocatable, so it's checked
129 //
130 // This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no
131 // references to itself. This means that the entire structure is trivially relocatable if its members are.
132 using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<
133 __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,
134 unique_ptr,
135 void>;
136
137private:
138 _LIBCPP_COMPRESSED_PAIR(pointer, __ptr_, deleter_type, __deleter_);
139
140 template <class _UPtr, class _Up>
141 using _EnableIfMoveConvertible _LIBCPP_NODEBUG =
142 __enable_if_t< is_convertible<typename _UPtr::pointer, pointer>::value && !is_array<_Up>::value >;
143
144 template <class _UDel>
145 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG =
146 __enable_if_t< (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
147 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) >;
148
149 template <class _UDel>
150 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG = __enable_if_t< is_assignable<_Dp&, _UDel&&>::value >;
151
152public:
153 template <class _Deleter = deleter_type,
154 __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value, int> = 0>
155 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(), __deleter_() {}
156
157 template <class _Deleter = deleter_type,
158 __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value, int> = 0>
159 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(), __deleter_() {}
160
161 template <class _Deleter = deleter_type,
162 __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value, int> = 0>
163 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(pointer __p) _NOEXCEPT
164 : __ptr_(__p),
165 __deleter_() {}
166
167 template <class _Deleter = deleter_type, __enable_if_t<is_copy_constructible<_Deleter>::value, int> = 0>
168 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(pointer __p, const deleter_type& __d) _NOEXCEPT
169 : __ptr_(__p),
170 __deleter_(__d) {}
171
172 template <class _Deleter = deleter_type,
173 __enable_if_t<!is_reference<_Deleter>::value && is_move_constructible<_Deleter>::value, int> = 0>
174 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(pointer __p, deleter_type&& __d) _NOEXCEPT
175 : __ptr_(__p),
176 __deleter_(std::move(__d)) {}
177
178 template <class _Deleter = deleter_type, __enable_if_t<is_reference<_Deleter>::value, int> = 0>
179 _LIBCPP_HIDE_FROM_ABI unique_ptr(pointer, __libcpp_remove_reference_t<deleter_type>&&) = delete;
180
181 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT
182 : __ptr_(__u.release()),
183 __deleter_(std::forward<deleter_type>(__u.get_deleter())) {}
184
185 template <class _Up,
186 class _Ep,
187 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
188 class = _EnableIfDeleterConvertible<_Ep> >
189 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
190 : __ptr_(__u.release()),
191 __deleter_(std::forward<_Ep>(__u.get_deleter())) {}
192
193#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
194 template <class _Up,
195 __enable_if_t<is_convertible<_Up*, _Tp*>::value && is_same<_Dp, default_delete<_Tp> >::value, int> = 0>
196 _LIBCPP_HIDE_FROM_ABI unique_ptr(auto_ptr<_Up>&& __p) _NOEXCEPT : __ptr_(__p.release()), __deleter_() {}
197#endif
198
199 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
200 reset(p: __u.release()); // NOLINT(misc-uniqueptr-reset-release)
201 __deleter_ = std::forward<deleter_type>(__u.get_deleter());
202 return *this;
203 }
204
205 template <class _Up,
206 class _Ep,
207 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
208 class = _EnableIfDeleterAssignable<_Ep> >
209 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
210 reset(p: __u.release());
211 __deleter_ = std::forward<_Ep>(__u.get_deleter());
212 return *this;
213 }
214
215#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
216 template <class _Up,
217 __enable_if_t<is_convertible<_Up*, _Tp*>::value && is_same<_Dp, default_delete<_Tp> >::value, int> = 0>
218 _LIBCPP_HIDE_FROM_ABI unique_ptr& operator=(auto_ptr<_Up> __p) {
219 reset(__p.release());
220 return *this;
221 }
222#endif
223
224#ifdef _LIBCPP_CXX03_LANG
225 unique_ptr(unique_ptr const&) = delete;
226 unique_ptr& operator=(unique_ptr const&) = delete;
227#endif
228
229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }
230
231 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
232 reset();
233 return *this;
234 }
235
236 template <class _Ptr = pointer, __enable_if_t<__can_dereference<_Ptr>, int> = 0>
237 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator*() const
238 _NOEXCEPT_(_NOEXCEPT_(*std::declval<pointer>())) {
239 return *__ptr_;
240 }
241 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer operator->() const _NOEXCEPT { return __ptr_; }
242 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_; }
243 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT {
244 return __deleter_;
245 }
246 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type&
247 get_deleter() const _NOEXCEPT {
248 return __deleter_;
249 }
250 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {
251 return __ptr_ != nullptr;
252 }
253
254 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {
255 pointer __t = __ptr_;
256 __ptr_ = pointer();
257 return __t;
258 }
259
260 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(pointer __p = pointer()) _NOEXCEPT {
261 pointer __tmp = __ptr_;
262 __ptr_ = __p;
263 if (__tmp)
264 __deleter_(__tmp);
265 }
266
267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT {
268 using std::swap;
269 swap(__ptr_, __u.__ptr_);
270 swap(__deleter_, __u.__deleter_);
271 }
272};
273
274// Bounds checking in unique_ptr<T[]>
275// ==================================
276//
277// We provide some helper classes that allow bounds checking when accessing a unique_ptr<T[]>.
278// There are a few cases where bounds checking can be implemented:
279//
280// 1. When an array cookie exists at the beginning of the array allocation, we are
281// able to reuse that cookie to extract the size of the array and perform bounds checking.
282// An array cookie is a size inserted at the beginning of the allocation by the compiler.
283// That size is inserted implicitly when doing `new T[n]` in some cases (as of writing this
284// exactly when the array elements are not trivially destructible), and its main purpose is
285// to allow the runtime to destroy the `n` array elements when doing `delete[] array`.
286// When we are able to use array cookies, we reuse information already available in the
287// current runtime, so bounds checking does not require changing libc++'s ABI.
288//
289// However, note that we cannot assume the presence of an array cookie when a custom deleter
290// is used, because the unique_ptr could have been created from an allocation that wasn't
291// obtained via `new T[n]` (since it may not be deleted with `delete[] arr`).
292//
293// 2. When the "bounded unique_ptr" ABI configuration (controlled by `_LIBCPP_ABI_BOUNDED_UNIQUE_PTR`)
294// is enabled, we store the size of the allocation (when it is known) so we can check it when
295// indexing into the `unique_ptr`. That changes the layout of `std::unique_ptr<T[]>`, which is
296// an ABI break from the default configuration.
297//
298// Note that even under this ABI configuration, we can't always know the size of the unique_ptr.
299// Indeed, the size of the allocation can only be known when the unique_ptr is created via
300// make_unique or a similar API. For example, it can't be known when constructed from an arbitrary
301// pointer, in which case we are not able to check the bounds on access:
302//
303// unique_ptr<T[], MyDeleter> ptr(new T[3]);
304//
305// When we don't know the size of the allocation via the API used to create the unique_ptr, we
306// try to fall back to using an array cookie when available.
307//
308// Finally, note that when this ABI configuration is enabled, we have no choice but to always
309// make space for the size to be stored in the unique_ptr. Indeed, while we might want to avoid
310// storing the size when an array cookie is available, knowing whether an array cookie is available
311// requires the type stored in the unique_ptr to be complete, while unique_ptr can normally
312// accommodate incomplete types.
313//
314// (1) Implementation where we rely on the array cookie to know the size of the allocation, if
315// an array cookie exists.
316struct __unique_ptr_array_bounds_stateless {
317 __unique_ptr_array_bounds_stateless() = default;
318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __unique_ptr_array_bounds_stateless(size_t) {}
319
320 template <class _Deleter,
321 class _Tp,
322 __enable_if_t<__is_default_deleter_v<_Deleter> && __has_array_cookie<_Tp>::value, int> = 0>
323 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp* __ptr, size_t __index) const {
324 // In constant expressions, we can't check the array cookie so we just pretend that the index
325 // is in-bounds. The compiler catches invalid accesses anyway.
326 if (__libcpp_is_constant_evaluated())
327 return true;
328 size_t __cookie = std::__get_array_cookie(__ptr);
329 return __index < __cookie;
330 }
331
332 template <class _Deleter,
333 class _Tp,
334 __enable_if_t<!__is_default_deleter_v<_Deleter> || !__has_array_cookie<_Tp>::value, int> = 0>
335 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp*, size_t) const {
336 return true; // If we don't have an array cookie, we assume the access is in-bounds
337 }
338};
339
340// (2) Implementation where we store the size in the class whenever we have it.
341//
342// Semantically, we'd need to store the size as an optional<size_t>. However, since that
343// is really heavy weight, we instead store a size_t and use SIZE_MAX as a magic value
344// meaning that we don't know the size.
345struct __unique_ptr_array_bounds_stored {
346 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __unique_ptr_array_bounds_stored() : __size_(SIZE_MAX) {}
347 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __unique_ptr_array_bounds_stored(size_t __size) : __size_(__size) {}
348
349 // Use the array cookie if there's one
350 template <class _Deleter,
351 class _Tp,
352 __enable_if_t<__is_default_deleter_v<_Deleter> && __has_array_cookie<_Tp>::value, int> = 0>
353 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp* __ptr, size_t __index) const {
354 if (__libcpp_is_constant_evaluated())
355 return true;
356 size_t __cookie = std::__get_array_cookie(__ptr);
357 return __index < __cookie;
358 }
359
360 // Otherwise, fall back on the stored size (if any)
361 template <class _Deleter,
362 class _Tp,
363 __enable_if_t<!__is_default_deleter_v<_Deleter> || !__has_array_cookie<_Tp>::value, int> = 0>
364 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Tp*, size_t __index) const {
365 return __index < __size_;
366 }
367
368private:
369 size_t __size_;
370};
371
372template <class _Tp, class _Dp>
373class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI unique_ptr<_Tp[], _Dp> {
374public:
375 typedef _Tp element_type;
376 typedef _Dp deleter_type;
377 using pointer = __pointer<_Tp, deleter_type>;
378
379 // A unique_ptr contains the following members which may be trivially relocatable:
380 // - pointer: this may be trivially relocatable, so it's checked
381 // - deleter_type: this may be trivially relocatable, so it's checked
382 // - (optionally) size: this is trivially relocatable
383 //
384 // This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no
385 // references to itself. This means that the entire structure is trivially relocatable if its members are.
386 using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<
387 __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,
388 unique_ptr,
389 void>;
390
391private:
392 template <class _Up, class _OtherDeleter>
393 friend class unique_ptr;
394
395 _LIBCPP_COMPRESSED_PAIR(pointer, __ptr_, deleter_type, __deleter_);
396#ifdef _LIBCPP_ABI_BOUNDED_UNIQUE_PTR
397 using _BoundsChecker _LIBCPP_NODEBUG = __unique_ptr_array_bounds_stored;
398#else
399 using _BoundsChecker _LIBCPP_NODEBUG = __unique_ptr_array_bounds_stateless;
400#endif
401 _LIBCPP_NO_UNIQUE_ADDRESS _BoundsChecker __checker_;
402
403 template <class _From>
404 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
405
406 template <class _FromElem>
407 struct _CheckArrayPointerConversion<_FromElem*>
408 : integral_constant<bool,
409 is_same<_FromElem*, pointer>::value ||
410 (is_same<pointer, element_type*>::value &&
411 is_convertible<_FromElem (*)[], element_type (*)[]>::value) > {};
412
413 template <class _UPtr, class _Up, class _ElemT = typename _UPtr::element_type>
414 using _EnableIfMoveConvertible _LIBCPP_NODEBUG =
415 __enable_if_t< is_array<_Up>::value && is_same<pointer, element_type*>::value &&
416 is_same<typename _UPtr::pointer, _ElemT*>::value &&
417 is_convertible<_ElemT (*)[], element_type (*)[]>::value >;
418
419 template <class _UDel>
420 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG =
421 __enable_if_t< (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
422 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) >;
423
424 template <class _UDel>
425 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG = __enable_if_t< is_assignable<_Dp&, _UDel&&>::value >;
426
427public:
428 template <class _Deleter = deleter_type,
429 __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value, int> = 0>
430 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(), __deleter_() {}
431
432 template <class _Deleter = deleter_type,
433 __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value, int> = 0>
434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(), __deleter_() {}
435
436 template <class _Ptr,
437 class _Deleter = deleter_type,
438 __enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value, int> = 0,
439 __enable_if_t<_CheckArrayPointerConversion<_Ptr>::value, int> = 0>
440 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(_Ptr __ptr) _NOEXCEPT
441 : __ptr_(__ptr),
442 __deleter_() {}
443
444 // Private constructor used by make_unique & friends to pass the size that was allocated
445 template <class _Tag, class _Ptr, __enable_if_t<is_same<_Tag, __private_constructor_tag>::value, int> = 0>
446 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(_Tag, _Ptr __ptr, size_t __size) _NOEXCEPT
447 : __ptr_(__ptr),
448 __checker_(__size) {}
449
450 template <class _Ptr,
451 class _Deleter = deleter_type,
452 __enable_if_t<is_copy_constructible<_Deleter>::value, int> = 0,
453 __enable_if_t<_CheckArrayPointerConversion<_Ptr>::value, int> = 0>
454 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(_Ptr __ptr, const deleter_type& __deleter) _NOEXCEPT
455 : __ptr_(__ptr),
456 __deleter_(__deleter) {}
457
458 template <class _Deleter = deleter_type, __enable_if_t<is_copy_constructible<_Deleter>::value, int> = 0>
459 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(nullptr_t, const deleter_type& __deleter) _NOEXCEPT
460 : __ptr_(nullptr),
461 __deleter_(__deleter) {}
462
463 template <class _Ptr,
464 class _Deleter = deleter_type,
465 __enable_if_t<!is_reference<_Deleter>::value && is_move_constructible<_Deleter>::value, int> = 0,
466 __enable_if_t<_CheckArrayPointerConversion<_Ptr>::value, int> = 0>
467 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(_Ptr __ptr, deleter_type&& __deleter) _NOEXCEPT
468 : __ptr_(__ptr),
469 __deleter_(std::move(__deleter)) {}
470
471 template <class _Deleter = deleter_type,
472 __enable_if_t<!is_reference<_Deleter>::value && is_move_constructible<_Deleter>::value, int> = 0>
473 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(nullptr_t, deleter_type&& __deleter) _NOEXCEPT
474 : __ptr_(nullptr),
475 __deleter_(std::move(__deleter)) {}
476
477 template <class _Ptr,
478 class _Deleter = deleter_type,
479 __enable_if_t<is_reference<_Deleter>::value, int> = 0,
480 __enable_if_t<_CheckArrayPointerConversion<_Ptr>::value, int> = 0>
481 _LIBCPP_HIDE_FROM_ABI unique_ptr(_Ptr, __libcpp_remove_reference_t<deleter_type>&&) = delete;
482
483 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT
484 : __ptr_(__u.release()),
485 __deleter_(std::forward<deleter_type>(__u.get_deleter())),
486 __checker_(std::move(__u.__checker_)) {}
487
488 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
489 reset(__u.release()); // NOLINT(misc-uniqueptr-reset-release)
490 __deleter_ = std::forward<deleter_type>(__u.get_deleter());
491 __checker_ = std::move(__u.__checker_);
492 return *this;
493 }
494
495 template <class _Up,
496 class _Ep,
497 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
498 class = _EnableIfDeleterConvertible<_Ep> >
499 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
500 : __ptr_(__u.release()),
501 __deleter_(std::forward<_Ep>(__u.get_deleter())),
502 __checker_(std::move(__u.__checker_)) {}
503
504 template <class _Up,
505 class _Ep,
506 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
507 class = _EnableIfDeleterAssignable<_Ep> >
508 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
509 reset(__u.release());
510 __deleter_ = std::forward<_Ep>(__u.get_deleter());
511 __checker_ = std::move(__u.__checker_);
512 return *this;
513 }
514
515#ifdef _LIBCPP_CXX03_LANG
516 unique_ptr(unique_ptr const&) = delete;
517 unique_ptr& operator=(unique_ptr const&) = delete;
518#endif
519
520public:
521 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }
522
523 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
524 reset();
525 return *this;
526 }
527
528 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator[](size_t __i) const {
529 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__checker_.__in_bounds<deleter_type>(std::__to_address(__ptr_), __i),
530 "unique_ptr<T[]>::operator[](index): index out of range");
531 return __ptr_[__i];
532 }
533 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_; }
534
535 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT { return __deleter_; }
536
537 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type& get_deleter() const _NOEXCEPT {
538 return __deleter_;
539 }
540 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {
541 return __ptr_ != nullptr;
542 }
543
544 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {
545 pointer __t = __ptr_;
546 __ptr_ = pointer();
547 // The deleter and the optional bounds-checker are left unchanged. The bounds-checker
548 // will be reinitialized appropriately when/if the unique_ptr gets assigned-to or reset.
549 return __t;
550 }
551
552 template <class _Pp, __enable_if_t<_CheckArrayPointerConversion<_Pp>::value, int> = 0>
553 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(_Pp __ptr) _NOEXCEPT {
554 pointer __tmp = __ptr_;
555 __ptr_ = __ptr;
556 __checker_ = _BoundsChecker();
557 if (__tmp)
558 __deleter_(__tmp);
559 }
560
561 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(nullptr_t = nullptr) _NOEXCEPT {
562 pointer __tmp = __ptr_;
563 __ptr_ = nullptr;
564 __checker_ = _BoundsChecker();
565 if (__tmp)
566 __deleter_(__tmp);
567 }
568
569 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT {
570 using std::swap;
571 swap(__ptr_, __u.__ptr_);
572 swap(__deleter_, __u.__deleter_);
573 swap(__checker_, __u.__checker_);
574 }
575};
576
577template <class _Tp, class _Dp, __enable_if_t<__is_swappable_v<_Dp>, int> = 0>
578inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
579swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {
580 __x.swap(__y);
581}
582
583template <class _T1, class _D1, class _T2, class _D2>
584inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
585operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
586 return __x.get() == __y.get();
587}
588
589#if _LIBCPP_STD_VER <= 17
590template <class _T1, class _D1, class _T2, class _D2>
591inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
592 return !(__x == __y);
593}
594#endif
595
596template <class _T1, class _D1, class _T2, class _D2>
597inline _LIBCPP_HIDE_FROM_ABI bool operator<(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
598 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
599 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
600 typedef typename common_type<_P1, _P2>::type _Vp;
601 return less<_Vp>()(__x.get(), __y.get());
602}
603
604template <class _T1, class _D1, class _T2, class _D2>
605inline _LIBCPP_HIDE_FROM_ABI bool operator>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
606 return __y < __x;
607}
608
609template <class _T1, class _D1, class _T2, class _D2>
610inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
611 return !(__y < __x);
612}
613
614template <class _T1, class _D1, class _T2, class _D2>
615inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
616 return !(__x < __y);
617}
618
619#if _LIBCPP_STD_VER >= 20
620template <class _T1, class _D1, class _T2, class _D2>
621 requires three_way_comparable_with<typename unique_ptr<_T1, _D1>::pointer, typename unique_ptr<_T2, _D2>::pointer>
622_LIBCPP_HIDE_FROM_ABI
623compare_three_way_result_t<typename unique_ptr<_T1, _D1>::pointer, typename unique_ptr<_T2, _D2>::pointer>
624operator<=>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
625 return compare_three_way()(__x.get(), __y.get());
626}
627#endif
628
629template <class _T1, class _D1>
630inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
631operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {
632 return !__x;
633}
634
635#if _LIBCPP_STD_VER <= 17
636template <class _T1, class _D1>
637inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT {
638 return !__x;
639}
640
641template <class _T1, class _D1>
642inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {
643 return static_cast<bool>(__x);
644}
645
646template <class _T1, class _D1>
647inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT {
648 return static_cast<bool>(__x);
649}
650#endif // _LIBCPP_STD_VER <= 17
651
652template <class _T1, class _D1>
653inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
654 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
655 return less<_P1>()(__x.get(), nullptr);
656}
657
658template <class _T1, class _D1>
659inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
660 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
661 return less<_P1>()(nullptr, __x.get());
662}
663
664template <class _T1, class _D1>
665inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
666 return nullptr < __x;
667}
668
669template <class _T1, class _D1>
670inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
671 return __x < nullptr;
672}
673
674template <class _T1, class _D1>
675inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
676 return !(nullptr < __x);
677}
678
679template <class _T1, class _D1>
680inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
681 return !(__x < nullptr);
682}
683
684template <class _T1, class _D1>
685inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
686 return !(__x < nullptr);
687}
688
689template <class _T1, class _D1>
690inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x) {
691 return !(nullptr < __x);
692}
693
694#if _LIBCPP_STD_VER >= 20
695template <class _T1, class _D1>
696 requires three_way_comparable< typename unique_ptr<_T1, _D1>::pointer>
697_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 compare_three_way_result_t<typename unique_ptr<_T1, _D1>::pointer>
698operator<=>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
699 return compare_three_way()(__x.get(), static_cast<typename unique_ptr<_T1, _D1>::pointer>(nullptr));
700}
701#endif
702
703#if _LIBCPP_STD_VER >= 14
704
705template <class _Tp, class... _Args, enable_if_t<!is_array<_Tp>::value, int> = 0>
706[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
707_LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(_Args&&... __args) {
708 return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
709}
710
711template <class _Tp, enable_if_t<__is_unbounded_array_v<_Tp>, int> = 0>
712[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(size_t __n) {
713 typedef __remove_extent_t<_Tp> _Up;
714 return unique_ptr<_Tp>(__private_constructor_tag(), new _Up[__n](), __n);
715}
716
717template <class _Tp, class... _Args, enable_if_t<__is_bounded_array_v<_Tp>, int> = 0>
718void make_unique(_Args&&...) = delete;
719
720#endif // _LIBCPP_STD_VER >= 14
721
722#if _LIBCPP_STD_VER >= 20
723
724template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
725[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique_for_overwrite() {
726 return unique_ptr<_Tp>(new _Tp);
727}
728
729template <class _Tp, enable_if_t<is_unbounded_array_v<_Tp>, int> = 0>
730[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp>
731make_unique_for_overwrite(size_t __n) {
732 return unique_ptr<_Tp>(__private_constructor_tag(), new __remove_extent_t<_Tp>[__n], __n);
733}
734
735template <class _Tp, class... _Args, enable_if_t<is_bounded_array_v<_Tp>, int> = 0>
736void make_unique_for_overwrite(_Args&&...) = delete;
737
738#endif // _LIBCPP_STD_VER >= 20
739
740template <class _Tp>
741struct hash;
742
743template <class _Tp, class _Dp>
744#ifdef _LIBCPP_CXX03_LANG
745struct hash<unique_ptr<_Tp, _Dp> >
746#else
747struct hash<__enable_hash_helper< unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
748#endif
749{
750#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
751 _LIBCPP_DEPRECATED_IN_CXX17 typedef unique_ptr<_Tp, _Dp> argument_type;
752 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
753#endif
754
755 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const {
756 typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;
757 return hash<pointer>()(__ptr.get());
758 }
759};
760
761_LIBCPP_END_NAMESPACE_STD
762
763_LIBCPP_POP_MACROS
764
765#endif // _LIBCPP___MEMORY_UNIQUE_PTR_H
766