2 // unique_ptr implementation -*- C++ -*-
4 // Copyright (C) 2008-2024 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file bits/unique_ptr.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{memory}
32 #define _UNIQUE_PTR_H 1
34 #include <bits/c++config.h>
35 #include <debug/assertions.h>
36 #include <type_traits>
38 #include <bits/stl_function.h>
39 #include <bits/functional_hash.h>
40 #if __cplusplus >= 202002L
47 namespace std
_GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 * @addtogroup pointer_abstractions
56 #if _GLIBCXX_USE_DEPRECATED
57 #pragma GCC diagnostic push
58 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59 template<typename
> class auto_ptr
;
60 #pragma GCC diagnostic pop
63 /** Primary template of default_delete, used by unique_ptr for single objects
68 template<typename _Tp
>
71 /// Default constructor
72 constexpr default_delete() noexcept
= default;
74 /** @brief Converting constructor.
76 * Allows conversion from a deleter for objects of another type, `_Up`,
77 * only if `_Up*` is convertible to `_Tp*`.
79 template<typename _Up
,
80 typename
= _Require
<is_convertible
<_Up
*, _Tp
*>>>
82 default_delete(const default_delete
<_Up
>&) noexcept
{ }
84 /// Calls `delete __ptr`
87 operator()(_Tp
* __ptr
) const
89 static_assert(!is_void
<_Tp
>::value
,
90 "can't delete pointer to incomplete type");
91 static_assert(sizeof(_Tp
)>0,
92 "can't delete pointer to incomplete type");
97 // _GLIBCXX_RESOLVE_LIB_DEFECTS
98 // DR 740 - omit specialization for array objects with a compile time length
100 /** Specialization of default_delete for arrays, used by `unique_ptr<T[]>`
105 template<typename _Tp
>
106 struct default_delete
<_Tp
[]>
109 /// Default constructor
110 constexpr default_delete() noexcept
= default;
112 /** @brief Converting constructor.
114 * Allows conversion from a deleter for arrays of another type, such as
115 * a const-qualified version of `_Tp`.
117 * Conversions from types derived from `_Tp` are not allowed because
118 * it is undefined to `delete[]` an array of derived types through a
119 * pointer to the base type.
121 template<typename _Up
,
122 typename
= _Require
<is_convertible
<_Up(*)[], _Tp(*)[]>>>
124 default_delete(const default_delete
<_Up
[]>&) noexcept
{ }
126 /// Calls `delete[] __ptr`
127 template<typename _Up
>
129 typename enable_if
<is_convertible
<_Up(*)[], _Tp(*)[]>::value
>::type
130 operator()(_Up
* __ptr
) const
132 static_assert(sizeof(_Tp
)>0,
133 "can't delete pointer to incomplete type");
138 /// @cond undocumented
140 // Manages the pointer and deleter of a unique_ptr
141 template <typename _Tp
, typename _Dp
>
142 class __uniq_ptr_impl
144 template <typename _Up
, typename _Ep
, typename
= void>
150 template <typename _Up
, typename _Ep
>
152 _Ptr
<_Up
, _Ep
, __void_t
<typename remove_reference
<_Ep
>::type::pointer
>>
154 using type
= typename remove_reference
<_Ep
>::type::pointer
;
158 using _DeleterConstraint
= enable_if
<
159 __and_
<__not_
<is_pointer
<_Dp
>>,
160 is_default_constructible
<_Dp
>>::value
>;
162 using pointer
= typename _Ptr
<_Tp
, _Dp
>::type
;
164 static_assert( !is_rvalue_reference
<_Dp
>::value
,
165 "unique_ptr's deleter type must be a function object type"
166 " or an lvalue reference type" );
168 __uniq_ptr_impl() = default;
170 __uniq_ptr_impl(pointer __p
) : _M_t() { _M_ptr() = __p
; }
172 template<typename _Del
>
174 __uniq_ptr_impl(pointer __p
, _Del
&& __d
)
175 : _M_t(__p
, std::forward
<_Del
>(__d
)) { }
178 __uniq_ptr_impl(__uniq_ptr_impl
&& __u
) noexcept
179 : _M_t(std::move(__u
._M_t
))
180 { __u
._M_ptr() = nullptr; }
183 __uniq_ptr_impl
& operator=(__uniq_ptr_impl
&& __u
) noexcept
185 reset(__u
.release());
186 _M_deleter() = std::forward
<_Dp
>(__u
._M_deleter());
191 pointer
& _M_ptr() noexcept
{ return std::get
<0>(_M_t
); }
193 pointer
_M_ptr() const noexcept
{ return std::get
<0>(_M_t
); }
195 _Dp
& _M_deleter() noexcept
{ return std::get
<1>(_M_t
); }
197 const _Dp
& _M_deleter() const noexcept
{ return std::get
<1>(_M_t
); }
200 void reset(pointer __p
) noexcept
202 const pointer __old_p
= _M_ptr();
205 _M_deleter()(__old_p
);
209 pointer
release() noexcept
211 pointer __p
= _M_ptr();
218 swap(__uniq_ptr_impl
& __rhs
) noexcept
221 swap(this->_M_ptr(), __rhs
._M_ptr());
222 swap(this->_M_deleter(), __rhs
._M_deleter());
226 tuple
<pointer
, _Dp
> _M_t
;
229 // Defines move construction + assignment as either defaulted or deleted.
230 template <typename _Tp
, typename _Dp
,
231 bool = is_move_constructible
<_Dp
>::value
,
232 bool = is_move_assignable
<_Dp
>::value
>
233 struct __uniq_ptr_data
: __uniq_ptr_impl
<_Tp
, _Dp
>
235 using __uniq_ptr_impl
<_Tp
, _Dp
>::__uniq_ptr_impl
;
236 __uniq_ptr_data(__uniq_ptr_data
&&) = default;
237 __uniq_ptr_data
& operator=(__uniq_ptr_data
&&) = default;
240 template <typename _Tp
, typename _Dp
>
241 struct __uniq_ptr_data
<_Tp
, _Dp
, true, false> : __uniq_ptr_impl
<_Tp
, _Dp
>
243 using __uniq_ptr_impl
<_Tp
, _Dp
>::__uniq_ptr_impl
;
244 __uniq_ptr_data(__uniq_ptr_data
&&) = default;
245 __uniq_ptr_data
& operator=(__uniq_ptr_data
&&) = delete;
248 template <typename _Tp
, typename _Dp
>
249 struct __uniq_ptr_data
<_Tp
, _Dp
, false, true> : __uniq_ptr_impl
<_Tp
, _Dp
>
251 using __uniq_ptr_impl
<_Tp
, _Dp
>::__uniq_ptr_impl
;
252 __uniq_ptr_data(__uniq_ptr_data
&&) = delete;
253 __uniq_ptr_data
& operator=(__uniq_ptr_data
&&) = default;
256 template <typename _Tp
, typename _Dp
>
257 struct __uniq_ptr_data
<_Tp
, _Dp
, false, false> : __uniq_ptr_impl
<_Tp
, _Dp
>
259 using __uniq_ptr_impl
<_Tp
, _Dp
>::__uniq_ptr_impl
;
260 __uniq_ptr_data(__uniq_ptr_data
&&) = delete;
261 __uniq_ptr_data
& operator=(__uniq_ptr_data
&&) = delete;
265 // 20.7.1.2 unique_ptr for single objects.
267 /// A move-only smart pointer that manages unique ownership of a resource.
268 /// @headerfile memory
270 template <typename _Tp
, typename _Dp
= default_delete
<_Tp
>>
273 template <typename _Up
>
274 using _DeleterConstraint
=
275 typename __uniq_ptr_impl
<_Tp
, _Up
>::_DeleterConstraint::type
;
277 __uniq_ptr_data
<_Tp
, _Dp
> _M_t
;
280 using pointer
= typename __uniq_ptr_impl
<_Tp
, _Dp
>::pointer
;
281 using element_type
= _Tp
;
282 using deleter_type
= _Dp
;
285 // helper template for detecting a safe conversion from another
287 template<typename _Up
, typename _Ep
>
288 using __safe_conversion_up
= __and_
<
289 is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
, pointer
>,
290 __not_
<is_array
<_Up
>>
296 /// Default constructor, creates a unique_ptr that owns nothing.
297 template<typename _Del
= _Dp
, typename
= _DeleterConstraint
<_Del
>>
298 constexpr unique_ptr() noexcept
302 /** Takes ownership of a pointer.
304 * @param __p A pointer to an object of @c element_type
306 * The deleter will be value-initialized.
308 template<typename _Del
= _Dp
, typename
= _DeleterConstraint
<_Del
>>
311 unique_ptr(pointer __p
) noexcept
315 /** Takes ownership of a pointer.
317 * @param __p A pointer to an object of @c element_type
318 * @param __d A reference to a deleter.
320 * The deleter will be initialized with @p __d
322 template<typename _Del
= deleter_type
,
323 typename
= _Require
<is_copy_constructible
<_Del
>>>
325 unique_ptr(pointer __p
, const deleter_type
& __d
) noexcept
328 /** Takes ownership of a pointer.
330 * @param __p A pointer to an object of @c element_type
331 * @param __d An rvalue reference to a (non-reference) deleter.
333 * The deleter will be initialized with @p std::move(__d)
335 template<typename _Del
= deleter_type
,
336 typename
= _Require
<is_move_constructible
<_Del
>>>
338 unique_ptr(pointer __p
,
339 __enable_if_t
<!is_lvalue_reference
<_Del
>::value
,
340 _Del
&&> __d
) noexcept
341 : _M_t(__p
, std::move(__d
))
344 template<typename _Del
= deleter_type
,
345 typename _DelUnref
= typename remove_reference
<_Del
>::type
>
348 __enable_if_t
<is_lvalue_reference
<_Del
>::value
,
349 _DelUnref
&&>) = delete;
351 /// Creates a unique_ptr that owns nothing.
352 template<typename _Del
= _Dp
, typename
= _DeleterConstraint
<_Del
>>
353 constexpr unique_ptr(nullptr_t
) noexcept
357 // Move constructors.
359 /// Move constructor.
360 unique_ptr(unique_ptr
&&) = default;
362 /** @brief Converting constructor from another type
364 * Requires that the pointer owned by @p __u is convertible to the
365 * type of pointer owned by this object, @p __u does not own an array,
366 * and @p __u has a compatible deleter type.
368 template<typename _Up
, typename _Ep
, typename
= _Require
<
369 __safe_conversion_up
<_Up
, _Ep
>,
370 __conditional_t
<is_reference
<_Dp
>::value
,
372 is_convertible
<_Ep
, _Dp
>>>>
374 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
375 : _M_t(__u
.release(), std::forward
<_Ep
>(__u
.get_deleter()))
378 #if _GLIBCXX_USE_DEPRECATED
379 #pragma GCC diagnostic push
380 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
381 /// Converting constructor from @c auto_ptr
382 template<typename _Up
, typename
= _Require
<
383 is_convertible
<_Up
*, _Tp
*>, is_same
<_Dp
, default_delete
<_Tp
>>>>
384 unique_ptr(auto_ptr
<_Up
>&& __u
) noexcept
;
385 #pragma GCC diagnostic pop
388 /// Destructor, invokes the deleter if the stored pointer is not null.
389 #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
392 ~unique_ptr() noexcept
394 static_assert(__is_invocable
<deleter_type
&, pointer
>::value
,
395 "unique_ptr's deleter must be invocable with a pointer");
396 auto& __ptr
= _M_t
._M_ptr();
397 if (__ptr
!= nullptr)
398 get_deleter()(std::move(__ptr
));
404 /** @brief Move assignment operator.
406 * Invokes the deleter if this object owns a pointer.
408 unique_ptr
& operator=(unique_ptr
&&) = default;
410 /** @brief Assignment from another type.
412 * @param __u The object to transfer ownership from, which owns a
413 * convertible pointer to a non-array object.
415 * Invokes the deleter if this object owns a pointer.
417 template<typename _Up
, typename _Ep
>
419 typename enable_if
< __and_
<
420 __safe_conversion_up
<_Up
, _Ep
>,
421 is_assignable
<deleter_type
&, _Ep
&&>
424 operator=(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
426 reset(__u
.release());
427 get_deleter() = std::forward
<_Ep
>(__u
.get_deleter());
431 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
434 operator=(nullptr_t
) noexcept
442 /// Dereference the stored pointer.
444 typename add_lvalue_reference
<element_type
>::type
445 operator*() const noexcept(noexcept(*std::declval
<pointer
>()))
447 __glibcxx_assert(get() != pointer());
451 /// Return the stored pointer.
454 operator->() const noexcept
456 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
460 /// Return the stored pointer.
464 { return _M_t
._M_ptr(); }
466 /// Return a reference to the stored deleter.
469 get_deleter() noexcept
470 { return _M_t
._M_deleter(); }
472 /// Return a reference to the stored deleter.
475 get_deleter() const noexcept
476 { return _M_t
._M_deleter(); }
478 /// Return @c true if the stored pointer is not null.
480 explicit operator bool() const noexcept
481 { return get() == pointer() ? false : true; }
485 /// Release ownership of any stored pointer.
489 { return _M_t
.release(); }
491 /** @brief Replace the stored pointer.
493 * @param __p The new pointer to store.
495 * The deleter will be invoked if a pointer is already owned.
499 reset(pointer __p
= pointer()) noexcept
501 static_assert(__is_invocable
<deleter_type
&, pointer
>::value
,
502 "unique_ptr's deleter must be invocable with a pointer");
503 _M_t
.reset(std::move(__p
));
506 /// Exchange the pointer and deleter with another object.
509 swap(unique_ptr
& __u
) noexcept
511 static_assert(__is_swappable
<_Dp
>::value
, "deleter must be swappable");
515 // Disable copy from lvalue.
516 unique_ptr(const unique_ptr
&) = delete;
517 unique_ptr
& operator=(const unique_ptr
&) = delete;
520 #ifdef __glibcxx_out_ptr
521 template<typename
, typename
, typename
...>
522 friend class out_ptr_t
;
523 template<typename
, typename
, typename
...>
524 friend class inout_ptr_t
;
528 // 20.7.1.3 unique_ptr for array objects with a runtime length
529 // [unique.ptr.runtime]
530 // _GLIBCXX_RESOLVE_LIB_DEFECTS
531 // DR 740 - omit specialization for array objects with a compile time length
533 /// A move-only smart pointer that manages unique ownership of an array.
534 /// @headerfile memory
536 template<typename _Tp
, typename _Dp
>
537 class unique_ptr
<_Tp
[], _Dp
>
539 template <typename _Up
>
540 using _DeleterConstraint
=
541 typename __uniq_ptr_impl
<_Tp
, _Up
>::_DeleterConstraint::type
;
543 __uniq_ptr_data
<_Tp
, _Dp
> _M_t
;
545 // like is_base_of<_Tp, _Up> but false if unqualified types are the same
546 template<typename _Up
>
547 using __is_derived_Tp
548 = __and_
< is_base_of
<_Tp
, _Up
>,
549 __not_
<is_same
<__remove_cv_t
<_Tp
>, __remove_cv_t
<_Up
>>> >;
552 using pointer
= typename __uniq_ptr_impl
<_Tp
, _Dp
>::pointer
;
553 using element_type
= _Tp
;
554 using deleter_type
= _Dp
;
556 // helper template for detecting a safe conversion from another
558 template<typename _Up
, typename _Ep
,
559 typename _UPtr
= unique_ptr
<_Up
, _Ep
>,
560 typename _UP_pointer
= typename
_UPtr::pointer
,
561 typename _UP_element_type
= typename
_UPtr::element_type
>
562 using __safe_conversion_up
= __and_
<
564 is_same
<pointer
, element_type
*>,
565 is_same
<_UP_pointer
, _UP_element_type
*>,
566 is_convertible
<_UP_element_type(*)[], element_type(*)[]>
569 // helper template for detecting a safe conversion from a raw pointer
570 template<typename _Up
>
571 using __safe_conversion_raw
= __and_
<
572 __or_
<__or_
<is_same
<_Up
, pointer
>,
573 is_same
<_Up
, nullptr_t
>>,
574 __and_
<is_pointer
<_Up
>,
575 is_same
<pointer
, element_type
*>,
577 typename remove_pointer
<_Up
>::type(*)[],
585 /// Default constructor, creates a unique_ptr that owns nothing.
586 template<typename _Del
= _Dp
, typename
= _DeleterConstraint
<_Del
>>
587 constexpr unique_ptr() noexcept
591 /** Takes ownership of a pointer.
593 * @param __p A pointer to an array of a type safely convertible
594 * to an array of @c element_type
596 * The deleter will be value-initialized.
598 template<typename _Up
,
600 typename
= _DeleterConstraint
<_Vp
>,
601 typename
= typename enable_if
<
602 __safe_conversion_raw
<_Up
>::value
, bool>::type
>
605 unique_ptr(_Up __p
) noexcept
609 /** Takes ownership of a pointer.
611 * @param __p A pointer to an array of a type safely convertible
612 * to an array of @c element_type
613 * @param __d A reference to a deleter.
615 * The deleter will be initialized with @p __d
617 template<typename _Up
, typename _Del
= deleter_type
,
618 typename
= _Require
<__safe_conversion_raw
<_Up
>,
619 is_copy_constructible
<_Del
>>>
621 unique_ptr(_Up __p
, const deleter_type
& __d
) noexcept
624 /** Takes ownership of a pointer.
626 * @param __p A pointer to an array of a type safely convertible
627 * to an array of @c element_type
628 * @param __d A reference to a deleter.
630 * The deleter will be initialized with @p std::move(__d)
632 template<typename _Up
, typename _Del
= deleter_type
,
633 typename
= _Require
<__safe_conversion_raw
<_Up
>,
634 is_move_constructible
<_Del
>>>
637 __enable_if_t
<!is_lvalue_reference
<_Del
>::value
,
638 _Del
&&> __d
) noexcept
639 : _M_t(std::move(__p
), std::move(__d
))
642 template<typename _Up
, typename _Del
= deleter_type
,
643 typename _DelUnref
= typename remove_reference
<_Del
>::type
,
644 typename
= _Require
<__safe_conversion_raw
<_Up
>>>
646 __enable_if_t
<is_lvalue_reference
<_Del
>::value
,
647 _DelUnref
&&>) = delete;
649 /// Move constructor.
650 unique_ptr(unique_ptr
&&) = default;
652 /// Creates a unique_ptr that owns nothing.
653 template<typename _Del
= _Dp
, typename
= _DeleterConstraint
<_Del
>>
654 constexpr unique_ptr(nullptr_t
) noexcept
658 template<typename _Up
, typename _Ep
, typename
= _Require
<
659 __safe_conversion_up
<_Up
, _Ep
>,
660 __conditional_t
<is_reference
<_Dp
>::value
,
662 is_convertible
<_Ep
, _Dp
>>>>
664 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
665 : _M_t(__u
.release(), std::forward
<_Ep
>(__u
.get_deleter()))
668 /// Destructor, invokes the deleter if the stored pointer is not null.
669 #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
674 auto& __ptr
= _M_t
._M_ptr();
675 if (__ptr
!= nullptr)
676 get_deleter()(__ptr
);
682 /** @brief Move assignment operator.
684 * Invokes the deleter if this object owns a pointer.
687 operator=(unique_ptr
&&) = default;
689 /** @brief Assignment from another type.
691 * @param __u The object to transfer ownership from, which owns a
692 * convertible pointer to an array object.
694 * Invokes the deleter if this object owns a pointer.
696 template<typename _Up
, typename _Ep
>
699 enable_if
<__and_
<__safe_conversion_up
<_Up
, _Ep
>,
700 is_assignable
<deleter_type
&, _Ep
&&>
703 operator=(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
705 reset(__u
.release());
706 get_deleter() = std::forward
<_Ep
>(__u
.get_deleter());
710 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
713 operator=(nullptr_t
) noexcept
721 /// Access an element of owned array.
723 typename
std::add_lvalue_reference
<element_type
>::type
724 operator[](size_t __i
) const
726 __glibcxx_assert(get() != pointer());
730 /// Return the stored pointer.
734 { return _M_t
._M_ptr(); }
736 /// Return a reference to the stored deleter.
739 get_deleter() noexcept
740 { return _M_t
._M_deleter(); }
742 /// Return a reference to the stored deleter.
745 get_deleter() const noexcept
746 { return _M_t
._M_deleter(); }
748 /// Return @c true if the stored pointer is not null.
750 explicit operator bool() const noexcept
751 { return get() == pointer() ? false : true; }
755 /// Release ownership of any stored pointer.
759 { return _M_t
.release(); }
761 /** @brief Replace the stored pointer.
763 * @param __p The new pointer to store.
765 * The deleter will be invoked if a pointer is already owned.
767 template <typename _Up
,
769 __or_
<is_same
<_Up
, pointer
>,
770 __and_
<is_same
<pointer
, element_type
*>,
773 typename remove_pointer
<_Up
>::type(*)[],
781 reset(_Up __p
) noexcept
782 { _M_t
.reset(std::move(__p
)); }
785 void reset(nullptr_t
= nullptr) noexcept
786 { reset(pointer()); }
788 /// Exchange the pointer and deleter with another object.
791 swap(unique_ptr
& __u
) noexcept
793 static_assert(__is_swappable
<_Dp
>::value
, "deleter must be swappable");
797 // Disable copy from lvalue.
798 unique_ptr(const unique_ptr
&) = delete;
799 unique_ptr
& operator=(const unique_ptr
&) = delete;
802 #ifdef __glibcxx_out_ptr
803 template<typename
, typename
, typename
...> friend class out_ptr_t
;
804 template<typename
, typename
, typename
...> friend class inout_ptr_t
;
809 /// @relates unique_ptr
811 /// Swap overload for unique_ptr
812 template<typename _Tp
, typename _Dp
>
814 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
815 // Constrained free swap overload, see p0185r1
817 typename enable_if
<__is_swappable
<_Dp
>::value
>::type
821 swap(unique_ptr
<_Tp
, _Dp
>& __x
,
822 unique_ptr
<_Tp
, _Dp
>& __y
) noexcept
825 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
826 template<typename _Tp
, typename _Dp
>
827 typename enable_if
<!__is_swappable
<_Dp
>::value
>::type
828 swap(unique_ptr
<_Tp
, _Dp
>&,
829 unique_ptr
<_Tp
, _Dp
>&) = delete;
832 /// Equality operator for unique_ptr objects, compares the owned pointers
833 template<typename _Tp
, typename _Dp
,
834 typename _Up
, typename _Ep
>
835 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
837 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
,
838 const unique_ptr
<_Up
, _Ep
>& __y
)
839 { return __x
.get() == __y
.get(); }
841 /// unique_ptr comparison with nullptr
842 template<typename _Tp
, typename _Dp
>
843 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
845 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
) noexcept
848 #ifndef __cpp_lib_three_way_comparison
849 /// unique_ptr comparison with nullptr
850 template<typename _Tp
, typename _Dp
>
853 operator==(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
) noexcept
856 /// Inequality operator for unique_ptr objects, compares the owned pointers
857 template<typename _Tp
, typename _Dp
,
858 typename _Up
, typename _Ep
>
861 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
,
862 const unique_ptr
<_Up
, _Ep
>& __y
)
863 { return __x
.get() != __y
.get(); }
865 /// unique_ptr comparison with nullptr
866 template<typename _Tp
, typename _Dp
>
869 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
) noexcept
870 { return (bool)__x
; }
872 /// unique_ptr comparison with nullptr
873 template<typename _Tp
, typename _Dp
>
876 operator!=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
) noexcept
877 { return (bool)__x
; }
878 #endif // three way comparison
880 /// Relational operator for unique_ptr objects, compares the owned pointers
881 template<typename _Tp
, typename _Dp
,
882 typename _Up
, typename _Ep
>
883 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
885 operator<(const unique_ptr
<_Tp
, _Dp
>& __x
,
886 const unique_ptr
<_Up
, _Ep
>& __y
)
889 std::common_type
<typename unique_ptr
<_Tp
, _Dp
>::pointer
,
890 typename unique_ptr
<_Up
, _Ep
>::pointer
>::type _CT
;
891 return std::less
<_CT
>()(__x
.get(), __y
.get());
894 /// unique_ptr comparison with nullptr
895 template<typename _Tp
, typename _Dp
>
896 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
898 operator<(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
900 return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(__x
.get(),
904 /// unique_ptr comparison with nullptr
905 template<typename _Tp
, typename _Dp
>
906 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
908 operator<(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
910 return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(nullptr,
914 /// Relational operator for unique_ptr objects, compares the owned pointers
915 template<typename _Tp
, typename _Dp
,
916 typename _Up
, typename _Ep
>
917 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
919 operator<=(const unique_ptr
<_Tp
, _Dp
>& __x
,
920 const unique_ptr
<_Up
, _Ep
>& __y
)
921 { return !(__y
< __x
); }
923 /// unique_ptr comparison with nullptr
924 template<typename _Tp
, typename _Dp
>
925 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
927 operator<=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
928 { return !(nullptr < __x
); }
930 /// unique_ptr comparison with nullptr
931 template<typename _Tp
, typename _Dp
>
932 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
934 operator<=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
935 { return !(__x
< nullptr); }
937 /// Relational operator for unique_ptr objects, compares the owned pointers
938 template<typename _Tp
, typename _Dp
,
939 typename _Up
, typename _Ep
>
940 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
942 operator>(const unique_ptr
<_Tp
, _Dp
>& __x
,
943 const unique_ptr
<_Up
, _Ep
>& __y
)
944 { return (__y
< __x
); }
946 /// unique_ptr comparison with nullptr
947 template<typename _Tp
, typename _Dp
>
948 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
950 operator>(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
952 return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(nullptr,
956 /// unique_ptr comparison with nullptr
957 template<typename _Tp
, typename _Dp
>
958 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
960 operator>(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
962 return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(__x
.get(),
966 /// Relational operator for unique_ptr objects, compares the owned pointers
967 template<typename _Tp
, typename _Dp
,
968 typename _Up
, typename _Ep
>
969 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
971 operator>=(const unique_ptr
<_Tp
, _Dp
>& __x
,
972 const unique_ptr
<_Up
, _Ep
>& __y
)
973 { return !(__x
< __y
); }
975 /// unique_ptr comparison with nullptr
976 template<typename _Tp
, typename _Dp
>
977 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
979 operator>=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
980 { return !(__x
< nullptr); }
982 /// unique_ptr comparison with nullptr
983 template<typename _Tp
, typename _Dp
>
984 _GLIBCXX_NODISCARD
inline bool
985 operator>=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
986 { return !(nullptr < __x
); }
988 #ifdef __cpp_lib_three_way_comparison
989 template<typename _Tp
, typename _Dp
, typename _Up
, typename _Ep
>
990 requires three_way_comparable_with
<typename unique_ptr
<_Tp
, _Dp
>::pointer
,
991 typename unique_ptr
<_Up
, _Ep
>::pointer
>
994 compare_three_way_result_t
<typename unique_ptr
<_Tp
, _Dp
>::pointer
,
995 typename unique_ptr
<_Up
, _Ep
>::pointer
>
996 operator<=>(const unique_ptr
<_Tp
, _Dp
>& __x
,
997 const unique_ptr
<_Up
, _Ep
>& __y
)
998 { return compare_three_way()(__x
.get(), __y
.get()); }
1000 template<typename _Tp
, typename _Dp
>
1001 requires three_way_comparable
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>
1002 _GLIBCXX23_CONSTEXPR
1004 compare_three_way_result_t
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>
1005 operator<=>(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
1007 using pointer
= typename unique_ptr
<_Tp
, _Dp
>::pointer
;
1008 return compare_three_way()(__x
.get(), static_cast<pointer
>(nullptr));
1011 /// @} relates unique_ptr
1013 /// @cond undocumented
1014 template<typename _Up
, typename _Ptr
= typename
_Up::pointer
,
1015 bool = __poison_hash
<_Ptr
>::__enable_hash_call
>
1016 struct __uniq_ptr_hash
1017 #if ! _GLIBCXX_INLINE_VERSION
1018 : private __poison_hash
<_Ptr
>
1022 operator()(const _Up
& __u
) const
1023 noexcept(noexcept(std::declval
<hash
<_Ptr
>>()(std::declval
<_Ptr
>())))
1024 { return hash
<_Ptr
>()(__u
.get()); }
1027 template<typename _Up
, typename _Ptr
>
1028 struct __uniq_ptr_hash
<_Up
, _Ptr
, false>
1029 : private __poison_hash
<_Ptr
>
1033 /// std::hash specialization for unique_ptr.
1034 template<typename _Tp
, typename _Dp
>
1035 struct hash
<unique_ptr
<_Tp
, _Dp
>>
1036 : public __hash_base
<size_t, unique_ptr
<_Tp
, _Dp
>>,
1037 public __uniq_ptr_hash
<unique_ptr
<_Tp
, _Dp
>>
1040 #ifdef __glibcxx_make_unique // C++ >= 14 && HOSTED
1041 /// @cond undocumented
1044 template<typename _Tp
>
1046 { typedef unique_ptr
<_Tp
> __single_object
; };
1048 template<typename _Tp
>
1049 struct _MakeUniq
<_Tp
[]>
1050 { typedef unique_ptr
<_Tp
[]> __array
; };
1052 template<typename _Tp
, size_t _Bound
>
1053 struct _MakeUniq
<_Tp
[_Bound
]>
1054 { struct __invalid_type
{ }; };
1056 template<typename _Tp
>
1057 using __unique_ptr_t
= typename _MakeUniq
<_Tp
>::__single_object
;
1058 template<typename _Tp
>
1059 using __unique_ptr_array_t
= typename _MakeUniq
<_Tp
>::__array
;
1060 template<typename _Tp
>
1061 using __invalid_make_unique_t
= typename _MakeUniq
<_Tp
>::__invalid_type
;
1065 /** Create an object owned by a `unique_ptr`.
1066 * @tparam _Tp A non-array object type.
1067 * @param __args Constructor arguments for the new object.
1068 * @returns A `unique_ptr<_Tp>` that owns the new object.
1070 * @relates unique_ptr
1072 template<typename _Tp
, typename
... _Args
>
1073 _GLIBCXX23_CONSTEXPR
1074 inline __detail::__unique_ptr_t
<_Tp
>
1075 make_unique(_Args
&&... __args
)
1076 { return unique_ptr
<_Tp
>(new _Tp(std::forward
<_Args
>(__args
)...)); }
1078 /** Create an array owned by a `unique_ptr`.
1079 * @tparam _Tp An array type of unknown bound, such as `U[]`.
1080 * @param __num The number of elements of type `U` in the new array.
1081 * @returns A `unique_ptr<U[]>` that owns the new array.
1083 * @relates unique_ptr
1085 * The array elements are value-initialized.
1087 template<typename _Tp
>
1088 _GLIBCXX23_CONSTEXPR
1089 inline __detail::__unique_ptr_array_t
<_Tp
>
1090 make_unique(size_t __num
)
1091 { return unique_ptr
<_Tp
>(new remove_extent_t
<_Tp
>[__num
]()); }
1093 /** Disable std::make_unique for arrays of known bound.
1094 * @tparam _Tp An array type of known bound, such as `U[N]`.
1096 * @relates unique_ptr
1098 template<typename _Tp
, typename
... _Args
>
1099 __detail::__invalid_make_unique_t
<_Tp
>
1100 make_unique(_Args
&&...) = delete;
1102 #if __cplusplus > 201703L
1103 /** Create a default-initialied object owned by a `unique_ptr`.
1104 * @tparam _Tp A non-array object type.
1105 * @returns A `unique_ptr<_Tp>` that owns the new object.
1107 * @relates unique_ptr
1109 template<typename _Tp
>
1110 _GLIBCXX23_CONSTEXPR
1111 inline __detail::__unique_ptr_t
<_Tp
>
1112 make_unique_for_overwrite()
1113 { return unique_ptr
<_Tp
>(new _Tp
); }
1115 /** Create a default-initialized array owned by a `unique_ptr`.
1116 * @tparam _Tp An array type of unknown bound, such as `U[]`.
1117 * @param __num The number of elements of type `U` in the new array.
1118 * @returns A `unique_ptr<U[]>` that owns the new array.
1120 * @relates unique_ptr
1122 template<typename _Tp
>
1123 _GLIBCXX23_CONSTEXPR
1124 inline __detail::__unique_ptr_array_t
<_Tp
>
1125 make_unique_for_overwrite(size_t __num
)
1126 { return unique_ptr
<_Tp
>(new remove_extent_t
<_Tp
>[__num
]); }
1128 /** Disable std::make_unique_for_overwrite for arrays of known bound.
1129 * @tparam _Tp An array type of known bound, such as `U[N]`.
1131 * @relates unique_ptr
1133 template<typename _Tp
, typename
... _Args
>
1134 __detail::__invalid_make_unique_t
<_Tp
>
1135 make_unique_for_overwrite(_Args
&&...) = delete;
1138 #endif // C++14 && HOSTED
1140 #if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1141 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1142 // 2948. unique_ptr does not define operator<< for stream output
1143 /// Stream output operator for unique_ptr
1144 /// @relates unique_ptr
1146 template<typename _CharT
, typename _Traits
, typename _Tp
, typename _Dp
>
1147 inline basic_ostream
<_CharT
, _Traits
>&
1148 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
1149 const unique_ptr
<_Tp
, _Dp
>& __p
)
1150 requires requires
{ __os
<< __p
.get(); }
1155 #endif // C++20 && HOSTED
1157 #if __cpp_variable_templates
1158 template<typename _Tp
>
1159 static constexpr bool __is_unique_ptr
= false;
1160 template<typename _Tp
, typename _Del
>
1161 static constexpr bool __is_unique_ptr
<unique_ptr
<_Tp
, _Del
>> = true;
1164 /// @} group pointer_abstractions
1166 #if __cplusplus >= 201703L
1167 namespace __detail::__variant
1169 template<typename
> struct _Never_valueless_alt
; // see <variant>
1171 // Provide the strong exception-safety guarantee when emplacing a
1172 // unique_ptr into a variant.
1173 template<typename _Tp
, typename _Del
>
1174 struct _Never_valueless_alt
<std::unique_ptr
<_Tp
, _Del
>>
1177 } // namespace __detail::__variant
1180 _GLIBCXX_END_NAMESPACE_VERSION
1183 #endif /* _UNIQUE_PTR_H */