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