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