Debian Codegraph @

libstdc++-v3/include/bits/unique_ptr.h

raw
1 // unique_ptr implementation -*- C++ -*-
2
3 // Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file bits/unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30 #ifndef _UNIQUE_PTR_H
31 #define _UNIQUE_PTR_H 1
32
33 #include <bits/c++config.h>
34 #include <debug/assertions.h>
35 #include <type_traits>
36 #include <tuple>
37 #include <bits/stl_function.h>
38 #include <bits/functional_hash.h>
39 #if __cplusplus >= 202002L
40 # include <compare>
41 # if _GLIBCXX_HOSTED
42 # include <bits/ostream.h>
43 # endif
44 #endif
45
46 namespace std _GLIBCXX_VISIBILITY(default)
47 {
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49
50 /**
51 * @addtogroup pointer_abstractions
52 * @{
53 */
54
55 #if _GLIBCXX_USE_DEPRECATED
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
58 template<typename> class auto_ptr;
59 #pragma GCC diagnostic pop
60 #endif
61
62 /** Primary template of default_delete, used by unique_ptr for single objects
63 *
64 * @headerfile memory
65 * @since C++11
66 */
67 template<typename _Tp>
68 struct default_delete
69 {
70 /// Default constructor
71 constexpr default_delete() noexcept = default;
72
73 /** @brief Converting constructor.
74 *
75 * Allows conversion from a deleter for objects of another type, `_Up`,
76 * only if `_Up*` is convertible to `_Tp*`.
77 */
78 template<typename _Up,
79 typename = _Require<is_convertible<_Up*, _Tp*>>>
80 _GLIBCXX23_CONSTEXPR
81 default_delete(const default_delete<_Up>&) noexcept { }
82
83 /// Calls `delete __ptr`
84 _GLIBCXX23_CONSTEXPR
85 void
86 operator()(_Tp* __ptr) const
87 {
88 static_assert(!is_void<_Tp>::value,
89 "can't delete pointer to incomplete type");
90 static_assert(sizeof(_Tp)>0,
91 "can't delete pointer to incomplete type");
92 delete __ptr;
93 }
94 };
95
96 // _GLIBCXX_RESOLVE_LIB_DEFECTS
97 // DR 740 - omit specialization for array objects with a compile time length
98
99 /** Specialization of default_delete for arrays, used by `unique_ptr<T[]>`
100 *
101 * @headerfile memory
102 * @since C++11
103 */
104 template<typename _Tp>
105 struct default_delete<_Tp[]>
106 {
107 public:
108 /// Default constructor
109 constexpr default_delete() noexcept = default;
110
111 /** @brief Converting constructor.
112 *
113 * Allows conversion from a deleter for arrays of another type, such as
114 * a const-qualified version of `_Tp`.
115 *
116 * Conversions from types derived from `_Tp` are not allowed because
117 * it is undefined to `delete[]` an array of derived types through a
118 * pointer to the base type.
119 */
120 template<typename _Up,
121 typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>>
122 _GLIBCXX23_CONSTEXPR
123 default_delete(const default_delete<_Up[]>&) noexcept { }
124
125 /// Calls `delete[] __ptr`
126 template<typename _Up>
127 _GLIBCXX23_CONSTEXPR
128 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
129 operator()(_Up* __ptr) const
130 {
131 static_assert(sizeof(_Tp)>0,
132 "can't delete pointer to incomplete type");
133 delete [] __ptr;
134 }
135 };
136
137 /// @cond undocumented
138
139 // Manages the pointer and deleter of a unique_ptr
140 template <typename _Tp, typename _Dp>
141 class __uniq_ptr_impl
142 {
143 template <typename _Up, typename _Ep, typename = void>
144 struct _Ptr
145 {
146 using type = _Up*;
147 };
148
149 template <typename _Up, typename _Ep>
150 struct
151 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
152 {
153 using type = typename remove_reference<_Ep>::type::pointer;
154 };
155
156 public:
157 using _DeleterConstraint = enable_if<
158 __and_<__not_<is_pointer<_Dp>>,
159 is_default_constructible<_Dp>>::value>;
160
161 using pointer = typename _Ptr<_Tp, _Dp>::type;
162
163 static_assert( !is_rvalue_reference<_Dp>::value,
164 "unique_ptr's deleter type must be a function object type"
165 " or an lvalue reference type" );
166
167 __uniq_ptr_impl() = default;
168 _GLIBCXX23_CONSTEXPR
169 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
170
171 template<typename _Del>
172 _GLIBCXX23_CONSTEXPR
173 __uniq_ptr_impl(pointer __p, _Del&& __d)
174 : _M_t(__p, std::forward<_Del>(__d)) { }
175
176 _GLIBCXX23_CONSTEXPR
177 __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
178 : _M_t(std::move(__u._M_t))
179 { __u._M_ptr() = nullptr; }
180
181 _GLIBCXX23_CONSTEXPR
182 __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u) noexcept
183 {
184 reset(__u.release());
185 _M_deleter() = std::forward<_Dp>(__u._M_deleter());
186 return *this;
187 }
188
189 _GLIBCXX23_CONSTEXPR
190 pointer& _M_ptr() noexcept { return std::get<0>(_M_t); }
191 _GLIBCXX23_CONSTEXPR
192 pointer _M_ptr() const noexcept { return std::get<0>(_M_t); }
193 _GLIBCXX23_CONSTEXPR
194 _Dp& _M_deleter() noexcept { return std::get<1>(_M_t); }
195 _GLIBCXX23_CONSTEXPR
196 const _Dp& _M_deleter() const noexcept { return std::get<1>(_M_t); }
197
198 _GLIBCXX23_CONSTEXPR
199 void reset(pointer __p) noexcept
200 {
201 const pointer __old_p = _M_ptr();
202 _M_ptr() = __p;
203 if (__old_p)
204 _M_deleter()(__old_p);
205 }
206
207 _GLIBCXX23_CONSTEXPR
208 pointer release() noexcept
209 {
210 pointer __p = _M_ptr();
211 _M_ptr() = nullptr;
212 return __p;
213 }
214
215 _GLIBCXX23_CONSTEXPR
216 void
217 swap(__uniq_ptr_impl& __rhs) noexcept
218 {
219 using std::swap;
220 swap(this->_M_ptr(), __rhs._M_ptr());
221 swap(this->_M_deleter(), __rhs._M_deleter());
222 }
223
224 private:
225 tuple<pointer, _Dp> _M_t;
226 };
227
228 // Defines move construction + assignment as either defaulted or deleted.
229 template <typename _Tp, typename _Dp,
230 bool = is_move_constructible<_Dp>::value,
231 bool = is_move_assignable<_Dp>::value>
232 struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
233 {
234 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
235 __uniq_ptr_data(__uniq_ptr_data&&) = default;
236 __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
237 };
238
239 template <typename _Tp, typename _Dp>
240 struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
241 {
242 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
243 __uniq_ptr_data(__uniq_ptr_data&&) = default;
244 __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
245 };
246
247 template <typename _Tp, typename _Dp>
248 struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
249 {
250 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
251 __uniq_ptr_data(__uniq_ptr_data&&) = delete;
252 __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
253 };
254
255 template <typename _Tp, typename _Dp>
256 struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
257 {
258 using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
259 __uniq_ptr_data(__uniq_ptr_data&&) = delete;
260 __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
261 };
262 /// @endcond
263
264 // 20.7.1.2 unique_ptr for single objects.
265
266 /// A move-only smart pointer that manages unique ownership of a resource.
267 /// @headerfile memory
268 /// @since C++11
269 template <typename _Tp, typename _Dp = default_delete<_Tp>>
270 class unique_ptr
271 {
272 template <typename _Up>
273 using _DeleterConstraint =
274 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
275
276 __uniq_ptr_data<_Tp, _Dp> _M_t;
277
278 public:
279 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
280 using element_type = _Tp;
281 using deleter_type = _Dp;
282
283 private:
284 // helper template for detecting a safe conversion from another
285 // unique_ptr
286 template<typename _Up, typename _Ep>
287 using __safe_conversion_up = __and_<
288 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
289 __not_<is_array<_Up>>
290 >;
291
292 #if ! __cpp_concepts
293 template<typename _Ptr, typename = void>
294 struct _Nothrow_deref
295 : false_type { };
296
297 template<typename _Ptr>
298 struct _Nothrow_deref<_Ptr, __void_t<decltype(*std::declval<_Ptr>())>>
299 : __bool_constant<noexcept(*std::declval<_Ptr>())> { };
300 #endif
301
302 public:
303 // Constructors.
304
305 /// Default constructor, creates a unique_ptr that owns nothing.
306 template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
307 constexpr unique_ptr() noexcept
308 : _M_t()
309 { }
310
311 /** Takes ownership of a pointer.
312 *
313 * @param __p A pointer to an object of @c element_type
314 *
315 * The deleter will be value-initialized.
316 */
317 template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
318 _GLIBCXX23_CONSTEXPR
319 explicit
320 unique_ptr(pointer __p) noexcept
321 : _M_t(__p)
322 { }
323
324 /** Takes ownership of a pointer.
325 *
326 * @param __p A pointer to an object of @c element_type
327 * @param __d A reference to a deleter.
328 *
329 * The deleter will be initialized with @p __d
330 */
331 template<typename _Del = deleter_type,
332 typename = _Require<is_copy_constructible<_Del>>>
333 _GLIBCXX23_CONSTEXPR
334 unique_ptr(pointer __p, const deleter_type& __d) noexcept
335 : _M_t(__p, __d) { }
336
337 /** Takes ownership of a pointer.
338 *
339 * @param __p A pointer to an object of @c element_type
340 * @param __d An rvalue reference to a (non-reference) deleter.
341 *
342 * The deleter will be initialized with @p std::move(__d)
343 */
344 template<typename _Del = deleter_type,
345 typename = _Require<is_move_constructible<_Del>>>
346 _GLIBCXX23_CONSTEXPR
347 unique_ptr(pointer __p,
348 __enable_if_t<!is_lvalue_reference<_Del>::value,
349 _Del&&> __d) noexcept
350 : _M_t(__p, std::move(__d))
351 { }
352
353 template<typename _Del = deleter_type,
354 typename _DelUnref = typename remove_reference<_Del>::type>
355 _GLIBCXX23_CONSTEXPR
356 unique_ptr(pointer,
357 __enable_if_t<is_lvalue_reference<_Del>::value,
358 _DelUnref&&>) = delete;
359
360 /// Creates a unique_ptr that owns nothing.
361 template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
362 constexpr unique_ptr(nullptr_t) noexcept
363 : _M_t()
364 { }
365
366 // Move constructors.
367
368 /// Move constructor.
369 unique_ptr(unique_ptr&&) = default;
370
371 /** @brief Converting constructor from another type
372 *
373 * Requires that the pointer owned by @p __u is convertible to the
374 * type of pointer owned by this object, @p __u does not own an array,
375 * and @p __u has a compatible deleter type.
376 */
377 template<typename _Up, typename _Ep, typename = _Require<
378 __safe_conversion_up<_Up, _Ep>,
379 __conditional_t<is_reference<_Dp>::value,
380 is_same<_Ep, _Dp>,
381 is_convertible<_Ep, _Dp>>>>
382 _GLIBCXX23_CONSTEXPR
383 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
384 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
385 { }
386
387 #if _GLIBCXX_USE_DEPRECATED
388 #pragma GCC diagnostic push
389 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
390 /// Converting constructor from @c auto_ptr
391 template<typename _Up,
392 typename = _Require<is_convertible<_Up*, pointer>,
393 is_same<_Dp, default_delete<_Tp>>>>
394 unique_ptr(auto_ptr<_Up>&& __u) noexcept;
395 #pragma GCC diagnostic pop
396 #endif
397
398 /// Destructor, invokes the deleter if the stored pointer is not null.
399 #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
400 constexpr
401 #endif
402 ~unique_ptr() noexcept
403 {
404 static_assert(__is_invocable<deleter_type&, pointer>::value,
405 "unique_ptr's deleter must be invocable with a pointer");
406 auto& __ptr = _M_t._M_ptr();
407 if (__ptr != nullptr)
408 get_deleter()(std::move(__ptr));
409 __ptr = pointer();
410 }
411
412 // Assignment.
413
414 /** @brief Move assignment operator.
415 *
416 * Invokes the deleter if this object owns a pointer.
417 */
418 unique_ptr& operator=(unique_ptr&&) = default;
419
420 /** @brief Assignment from another type.
421 *
422 * @param __u The object to transfer ownership from, which owns a
423 * convertible pointer to a non-array object.
424 *
425 * Invokes the deleter if this object owns a pointer.
426 */
427 template<typename _Up, typename _Ep>
428 _GLIBCXX23_CONSTEXPR
429 typename enable_if< __and_<
430 __safe_conversion_up<_Up, _Ep>,
431 is_assignable<deleter_type&, _Ep&&>
432 >::value,
433 unique_ptr&>::type
434 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
435 {
436 reset(__u.release());
437 get_deleter() = std::forward<_Ep>(__u.get_deleter());
438 return *this;
439 }
440
441 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
442 _GLIBCXX23_CONSTEXPR
443 unique_ptr&
444 operator=(nullptr_t) noexcept
445 {
446 reset();
447 return *this;
448 }
449
450 // Observers.
451
452 /// Dereference the stored pointer.
453 _GLIBCXX23_CONSTEXPR
454 typename add_lvalue_reference<element_type>::type
455 operator*() const
456 // _GLIBCXX_RESOLVE_LIB_DEFECTS
457 // 2762. unique_ptr operator*() should be noexcept
458 // 4324. unique_ptr<void>::operator* is not SFINAE-friendly
459 #if __cpp_concepts
460 noexcept(noexcept(*std::declval<pointer>()))
461 requires requires { *std::declval<pointer>(); }
462 #else
463 noexcept(_Nothrow_deref<pointer>::value)
464 #endif
465 {
466 #if _GLIBCXX_USE_BUILTIN_TRAIT(__reference_converts_from_temporary)
467 // _GLIBCXX_RESOLVE_LIB_DEFECTS
468 // 4148. unique_ptr::operator* should not allow dangling references
469 using _ResT = typename add_lvalue_reference<element_type>::type;
470 using _DerefT = decltype(*get());
471 static_assert(!__reference_converts_from_temporary(_ResT, _DerefT),
472 "operator* must not return a dangling reference");
473 #endif
474 __glibcxx_assert(get() != pointer());
475 return *get();
476 }
477
478 /// Return the stored pointer.
479 _GLIBCXX23_CONSTEXPR
480 pointer
481 operator->() const noexcept
482 {
483 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
484 return get();
485 }
486
487 /// Return the stored pointer.
488 _GLIBCXX23_CONSTEXPR
489 pointer
490 get() const noexcept
491 { return _M_t._M_ptr(); }
492
493 /// Return a reference to the stored deleter.
494 _GLIBCXX23_CONSTEXPR
495 deleter_type&
496 get_deleter() noexcept
497 { return _M_t._M_deleter(); }
498
499 /// Return a reference to the stored deleter.
500 _GLIBCXX23_CONSTEXPR
501 const deleter_type&
502 get_deleter() const noexcept
503 { return _M_t._M_deleter(); }
504
505 /// Return @c true if the stored pointer is not null.
506 _GLIBCXX23_CONSTEXPR
507 explicit operator bool() const noexcept
508 { return get() == pointer() ? false : true; }
509
510 // Modifiers.
511
512 /// Release ownership of any stored pointer.
513 _GLIBCXX23_CONSTEXPR
514 pointer
515 release() noexcept
516 { return _M_t.release(); }
517
518 /** @brief Replace the stored pointer.
519 *
520 * @param __p The new pointer to store.
521 *
522 * The deleter will be invoked if a pointer is already owned.
523 */
524 _GLIBCXX23_CONSTEXPR
525 void
526 reset(pointer __p = pointer()) noexcept
527 {
528 static_assert(__is_invocable<deleter_type&, pointer>::value,
529 "unique_ptr's deleter must be invocable with a pointer");
530 _M_t.reset(std::move(__p));
531 }
532
533 /// Exchange the pointer and deleter with another object.
534 _GLIBCXX23_CONSTEXPR
535 void
536 swap(unique_ptr& __u) noexcept
537 {
538 static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
539 _M_t.swap(__u._M_t);
540 }
541
542 // Disable copy from lvalue.
543 unique_ptr(const unique_ptr&) = delete;
544 unique_ptr& operator=(const unique_ptr&) = delete;
545
546 private:
547 #ifdef __glibcxx_out_ptr
548 template<typename, typename, typename...>
549 friend class out_ptr_t;
550 template<typename, typename, typename...>
551 friend class inout_ptr_t;
552 #endif
553 };
554
555 // 20.7.1.3 unique_ptr for array objects with a runtime length
556 // [unique.ptr.runtime]
557 // _GLIBCXX_RESOLVE_LIB_DEFECTS
558 // DR 740 - omit specialization for array objects with a compile time length
559
560 /// A move-only smart pointer that manages unique ownership of an array.
561 /// @headerfile memory
562 /// @since C++11
563 template<typename _Tp, typename _Dp>
564 class unique_ptr<_Tp[], _Dp>
565 {
566 template <typename _Up>
567 using _DeleterConstraint =
568 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
569
570 __uniq_ptr_data<_Tp, _Dp> _M_t;
571
572 // like is_base_of<_Tp, _Up> but false if unqualified types are the same
573 template<typename _Up>
574 using __is_derived_Tp
575 = __and_< is_base_of<_Tp, _Up>,
576 __not_<is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up>>> >;
577
578 public:
579 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
580 using element_type = _Tp;
581 using deleter_type = _Dp;
582
583 // helper template for detecting a safe conversion from another
584 // unique_ptr
585 template<typename _Up, typename _Ep,
586 typename _UPtr = unique_ptr<_Up, _Ep>,
587 typename _UP_pointer = typename _UPtr::pointer,
588 typename _UP_element_type = typename _UPtr::element_type>
589 using __safe_conversion_up = __and_<
590 is_array<_Up>,
591 is_same<pointer, element_type*>,
592 is_same<_UP_pointer, _UP_element_type*>,
593 is_convertible<_UP_element_type(*)[], element_type(*)[]>
594 >;
595
596 // helper template for detecting a safe conversion from a raw pointer
597 template<typename _Up>
598 using __safe_conversion_raw = __and_<
599 __or_<__or_<is_same<_Up, pointer>,
600 is_same<_Up, nullptr_t>>,
601 __and_<is_pointer<_Up>,
602 is_same<pointer, element_type*>,
603 is_convertible<
604 typename remove_pointer<_Up>::type(*)[],
605 element_type(*)[]>
606 >
607 >
608 >;
609
610 // Constructors.
611
612 /// Default constructor, creates a unique_ptr that owns nothing.
613 template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
614 constexpr unique_ptr() noexcept
615 : _M_t()
616 { }
617
618 /** Takes ownership of a pointer.
619 *
620 * @param __p A pointer to an array of a type safely convertible
621 * to an array of @c element_type
622 *
623 * The deleter will be value-initialized.
624 */
625 template<typename _Up,
626 typename _Vp = _Dp,
627 typename = _DeleterConstraint<_Vp>,
628 typename = typename enable_if<
629 __safe_conversion_raw<_Up>::value, bool>::type>
630 _GLIBCXX23_CONSTEXPR
631 explicit
632 unique_ptr(_Up __p) noexcept
633 : _M_t(__p)
634 { }
635
636 /** Takes ownership of a pointer.
637 *
638 * @param __p A pointer to an array of a type safely convertible
639 * to an array of @c element_type
640 * @param __d A reference to a deleter.
641 *
642 * The deleter will be initialized with @p __d
643 */
644 template<typename _Up, typename _Del = deleter_type,
645 typename = _Require<__safe_conversion_raw<_Up>,
646 is_copy_constructible<_Del>>>
647 _GLIBCXX23_CONSTEXPR
648 unique_ptr(_Up __p, const deleter_type& __d) noexcept
649 : _M_t(__p, __d) { }
650
651 /** Takes ownership of a pointer.
652 *
653 * @param __p A pointer to an array of a type safely convertible
654 * to an array of @c element_type
655 * @param __d A reference to a deleter.
656 *
657 * The deleter will be initialized with @p std::move(__d)
658 */
659 template<typename _Up, typename _Del = deleter_type,
660 typename = _Require<__safe_conversion_raw<_Up>,
661 is_move_constructible<_Del>>>
662 _GLIBCXX23_CONSTEXPR
663 unique_ptr(_Up __p,
664 __enable_if_t<!is_lvalue_reference<_Del>::value,
665 _Del&&> __d) noexcept
666 : _M_t(std::move(__p), std::move(__d))
667 { }
668
669 template<typename _Up, typename _Del = deleter_type,
670 typename _DelUnref = typename remove_reference<_Del>::type,
671 typename = _Require<__safe_conversion_raw<_Up>>>
672 unique_ptr(_Up,
673 __enable_if_t<is_lvalue_reference<_Del>::value,
674 _DelUnref&&>) = delete;
675
676 /// Move constructor.
677 unique_ptr(unique_ptr&&) = default;
678
679 /// Creates a unique_ptr that owns nothing.
680 template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
681 constexpr unique_ptr(nullptr_t) noexcept
682 : _M_t()
683 { }
684
685 template<typename _Up, typename _Ep, typename = _Require<
686 __safe_conversion_up<_Up, _Ep>,
687 __conditional_t<is_reference<_Dp>::value,
688 is_same<_Ep, _Dp>,
689 is_convertible<_Ep, _Dp>>>>
690 _GLIBCXX23_CONSTEXPR
691 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
692 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
693 { }
694
695 /// Destructor, invokes the deleter if the stored pointer is not null.
696 #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
697 constexpr
698 #endif
699 ~unique_ptr()
700 {
701 auto& __ptr = _M_t._M_ptr();
702 if (__ptr != nullptr)
703 get_deleter()(__ptr);
704 __ptr = pointer();
705 }
706
707 // Assignment.
708
709 /** @brief Move assignment operator.
710 *
711 * Invokes the deleter if this object owns a pointer.
712 */
713 unique_ptr&
714 operator=(unique_ptr&&) = default;
715
716 /** @brief Assignment from another type.
717 *
718 * @param __u The object to transfer ownership from, which owns a
719 * convertible pointer to an array object.
720 *
721 * Invokes the deleter if this object owns a pointer.
722 */
723 template<typename _Up, typename _Ep>
724 _GLIBCXX23_CONSTEXPR
725 typename
726 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
727 is_assignable<deleter_type&, _Ep&&>
728 >::value,
729 unique_ptr&>::type
730 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
731 {
732 reset(__u.release());
733 get_deleter() = std::forward<_Ep>(__u.get_deleter());
734 return *this;
735 }
736
737 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
738 _GLIBCXX23_CONSTEXPR
739 unique_ptr&
740 operator=(nullptr_t) noexcept
741 {
742 reset();
743 return *this;
744 }
745
746 // Observers.
747
748 /// Access an element of owned array.
749 _GLIBCXX23_CONSTEXPR
750 typename std::add_lvalue_reference<element_type>::type
751 operator[](size_t __i) const
752 {
753 __glibcxx_assert(get() != pointer());
754 return get()[__i];
755 }
756
757 /// Return the stored pointer.
758 _GLIBCXX23_CONSTEXPR
759 pointer
760 get() const noexcept
761 { return _M_t._M_ptr(); }
762
763 /// Return a reference to the stored deleter.
764 _GLIBCXX23_CONSTEXPR
765 deleter_type&
766 get_deleter() noexcept
767 { return _M_t._M_deleter(); }
768
769 /// Return a reference to the stored deleter.
770 _GLIBCXX23_CONSTEXPR
771 const deleter_type&
772 get_deleter() const noexcept
773 { return _M_t._M_deleter(); }
774
775 /// Return @c true if the stored pointer is not null.
776 _GLIBCXX23_CONSTEXPR
777 explicit operator bool() const noexcept
778 { return get() == pointer() ? false : true; }
779
780 // Modifiers.
781
782 /// Release ownership of any stored pointer.
783 _GLIBCXX23_CONSTEXPR
784 pointer
785 release() noexcept
786 { return _M_t.release(); }
787
788 /** @brief Replace the stored pointer.
789 *
790 * @param __p The new pointer to store.
791 *
792 * The deleter will be invoked if a pointer is already owned.
793 */
794 template <typename _Up,
795 typename = _Require<
796 __or_<is_same<_Up, pointer>,
797 __and_<is_same<pointer, element_type*>,
798 is_pointer<_Up>,
799 is_convertible<
800 typename remove_pointer<_Up>::type(*)[],
801 element_type(*)[]
802 >
803 >
804 >
805 >>
806 _GLIBCXX23_CONSTEXPR
807 void
808 reset(_Up __p) noexcept
809 { _M_t.reset(std::move(__p)); }
810
811 _GLIBCXX23_CONSTEXPR
812 void reset(nullptr_t = nullptr) noexcept
813 { reset(pointer()); }
814
815 /// Exchange the pointer and deleter with another object.
816 _GLIBCXX23_CONSTEXPR
817 void
818 swap(unique_ptr& __u) noexcept
819 {
820 static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
821 _M_t.swap(__u._M_t);
822 }
823
824 // Disable copy from lvalue.
825 unique_ptr(const unique_ptr&) = delete;
826 unique_ptr& operator=(const unique_ptr&) = delete;
827
828 private:
829 #ifdef __glibcxx_out_ptr
830 template<typename, typename, typename...> friend class out_ptr_t;
831 template<typename, typename, typename...> friend class inout_ptr_t;
832 #endif
833 };
834
835 /// @{
836 /// @relates unique_ptr
837
838 /// Swap overload for unique_ptr
839 template<typename _Tp, typename _Dp>
840 inline
841 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
842 // Constrained free swap overload, see p0185r1
843 _GLIBCXX23_CONSTEXPR
844 typename enable_if<__is_swappable<_Dp>::value>::type
845 #else
846 void
847 #endif
848 swap(unique_ptr<_Tp, _Dp>& __x,
849 unique_ptr<_Tp, _Dp>& __y) noexcept
850 { __x.swap(__y); }
851
852 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
853 template<typename _Tp, typename _Dp>
854 typename enable_if<!__is_swappable<_Dp>::value>::type
855 swap(unique_ptr<_Tp, _Dp>&,
856 unique_ptr<_Tp, _Dp>&) = delete;
857 #endif
858
859 /// Equality operator for unique_ptr objects, compares the owned pointers
860 template<typename _Tp, typename _Dp,
861 typename _Up, typename _Ep>
862 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
863 inline bool
864 operator==(const unique_ptr<_Tp, _Dp>& __x,
865 const unique_ptr<_Up, _Ep>& __y)
866 { return __x.get() == __y.get(); }
867
868 /// unique_ptr comparison with nullptr
869 template<typename _Tp, typename _Dp>
870 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
871 inline bool
872 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
873 { return !__x; }
874
875 #ifndef __cpp_lib_three_way_comparison
876 /// unique_ptr comparison with nullptr
877 template<typename _Tp, typename _Dp>
878 _GLIBCXX_NODISCARD
879 inline bool
880 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
881 { return !__x; }
882
883 /// Inequality operator for unique_ptr objects, compares the owned pointers
884 template<typename _Tp, typename _Dp,
885 typename _Up, typename _Ep>
886 _GLIBCXX_NODISCARD
887 inline bool
888 operator!=(const unique_ptr<_Tp, _Dp>& __x,
889 const unique_ptr<_Up, _Ep>& __y)
890 { return __x.get() != __y.get(); }
891
892 /// unique_ptr comparison with nullptr
893 template<typename _Tp, typename _Dp>
894 _GLIBCXX_NODISCARD
895 inline bool
896 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
897 { return (bool)__x; }
898
899 /// unique_ptr comparison with nullptr
900 template<typename _Tp, typename _Dp>
901 _GLIBCXX_NODISCARD
902 inline bool
903 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
904 { return (bool)__x; }
905 #endif // three way comparison
906
907 /// Relational operator for unique_ptr objects, compares the owned pointers
908 template<typename _Tp, typename _Dp,
909 typename _Up, typename _Ep>
910 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
911 inline bool
912 operator<(const unique_ptr<_Tp, _Dp>& __x,
913 const unique_ptr<_Up, _Ep>& __y)
914 {
915 typedef typename
916 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
917 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
918 return std::less<_CT>()(__x.get(), __y.get());
919 }
920
921 /// unique_ptr comparison with nullptr
922 template<typename _Tp, typename _Dp>
923 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
924 inline bool
925 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
926 {
927 return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
928 nullptr);
929 }
930
931 /// unique_ptr comparison with nullptr
932 template<typename _Tp, typename _Dp>
933 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
934 inline bool
935 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
936 {
937 return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
938 __x.get());
939 }
940
941 /// Relational operator for unique_ptr objects, compares the owned pointers
942 template<typename _Tp, typename _Dp,
943 typename _Up, typename _Ep>
944 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
945 inline bool
946 operator<=(const unique_ptr<_Tp, _Dp>& __x,
947 const unique_ptr<_Up, _Ep>& __y)
948 { return !(__y < __x); }
949
950 /// unique_ptr comparison with nullptr
951 template<typename _Tp, typename _Dp>
952 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
953 inline bool
954 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
955 { return !(nullptr < __x); }
956
957 /// unique_ptr comparison with nullptr
958 template<typename _Tp, typename _Dp>
959 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
960 inline bool
961 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
962 { return !(__x < nullptr); }
963
964 /// Relational operator for unique_ptr objects, compares the owned pointers
965 template<typename _Tp, typename _Dp,
966 typename _Up, typename _Ep>
967 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
968 inline bool
969 operator>(const unique_ptr<_Tp, _Dp>& __x,
970 const unique_ptr<_Up, _Ep>& __y)
971 { return (__y < __x); }
972
973 /// unique_ptr comparison with nullptr
974 template<typename _Tp, typename _Dp>
975 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
976 inline bool
977 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
978 {
979 return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
980 __x.get());
981 }
982
983 /// unique_ptr comparison with nullptr
984 template<typename _Tp, typename _Dp>
985 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
986 inline bool
987 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
988 {
989 return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
990 nullptr);
991 }
992
993 /// Relational operator for unique_ptr objects, compares the owned pointers
994 template<typename _Tp, typename _Dp,
995 typename _Up, typename _Ep>
996 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
997 inline bool
998 operator>=(const unique_ptr<_Tp, _Dp>& __x,
999 const unique_ptr<_Up, _Ep>& __y)
1000 { return !(__x < __y); }
1001
1002 /// unique_ptr comparison with nullptr
1003 template<typename _Tp, typename _Dp>
1004 _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
1005 inline bool
1006 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1007 { return !(__x < nullptr); }
1008
1009 /// unique_ptr comparison with nullptr
1010 template<typename _Tp, typename _Dp>
1011 _GLIBCXX_NODISCARD inline bool
1012 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
1013 { return !(nullptr < __x); }
1014
1015 #ifdef __cpp_lib_three_way_comparison
1016 template<typename _Tp, typename _Dp, typename _Up, typename _Ep>
1017 requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1018 typename unique_ptr<_Up, _Ep>::pointer>
1019 _GLIBCXX23_CONSTEXPR
1020 inline
1021 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1022 typename unique_ptr<_Up, _Ep>::pointer>
1023 operator<=>(const unique_ptr<_Tp, _Dp>& __x,
1024 const unique_ptr<_Up, _Ep>& __y)
1025 { return compare_three_way()(__x.get(), __y.get()); }
1026
1027 template<typename _Tp, typename _Dp>
1028 requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1029 _GLIBCXX23_CONSTEXPR
1030 inline
1031 compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
1032 operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1033 {
1034 using pointer = typename unique_ptr<_Tp, _Dp>::pointer;
1035 return compare_three_way()(__x.get(), static_cast<pointer>(nullptr));
1036 }
1037 #endif
1038 /// @} relates unique_ptr
1039
1040 /// @cond undocumented
1041 template<typename _Up, typename _Ptr = typename _Up::pointer>
1042 struct __uniq_ptr_hash
1043 : public __hash_base<size_t, _Up>
1044 #if ! _GLIBCXX_INLINE_VERSION
1045 , private __hash_empty_base<_Ptr>
1046 #endif
1047 {
1048 size_t
1049 operator()(const _Up& __u) const
1050 noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>())))
1051 { return hash<_Ptr>()(__u.get()); }
1052 };
1053
1054 template<typename _Up>
1055 using __uniq_ptr_hash_base
1056 = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1057 __uniq_ptr_hash<_Up>,
1058 __hash_not_enabled<typename _Up::pointer>>;
1059 /// @endcond
1060
1061 /// std::hash specialization for unique_ptr.
1062 template<typename _Tp, typename _Dp>
1063 struct hash<unique_ptr<_Tp, _Dp>>
1064 : public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1065 { };
1066
1067 #ifdef __glibcxx_make_unique // C++ >= 14 && HOSTED
1068 /// @cond undocumented
1069 namespace __detail
1070 {
1071 template<typename _Tp>
1072 struct _MakeUniq
1073 { typedef unique_ptr<_Tp> __single_object; };
1074
1075 template<typename _Tp>
1076 struct _MakeUniq<_Tp[]>
1077 { typedef unique_ptr<_Tp[]> __array; };
1078
1079 template<typename _Tp, size_t _Bound>
1080 struct _MakeUniq<_Tp[_Bound]>
1081 { struct __invalid_type { }; };
1082
1083 template<typename _Tp>
1084 using __unique_ptr_t = typename _MakeUniq<_Tp>::__single_object;
1085 template<typename _Tp>
1086 using __unique_ptr_array_t = typename _MakeUniq<_Tp>::__array;
1087 template<typename _Tp>
1088 using __invalid_make_unique_t = typename _MakeUniq<_Tp>::__invalid_type;
1089 }
1090 /// @endcond
1091
1092 /** Create an object owned by a `unique_ptr`.
1093 * @tparam _Tp A non-array object type.
1094 * @param __args Constructor arguments for the new object.
1095 * @returns A `unique_ptr<_Tp>` that owns the new object.
1096 * @since C++14
1097 * @relates unique_ptr
1098 */
1099 template<typename _Tp, typename... _Args>
1100 _GLIBCXX23_CONSTEXPR
1101 inline __detail::__unique_ptr_t<_Tp>
1102 make_unique(_Args&&... __args)
1103 { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
1104
1105 /** Create an array owned by a `unique_ptr`.
1106 * @tparam _Tp An array type of unknown bound, such as `U[]`.
1107 * @param __num The number of elements of type `U` in the new array.
1108 * @returns A `unique_ptr<U[]>` that owns the new array.
1109 * @since C++14
1110 * @relates unique_ptr
1111 *
1112 * The array elements are value-initialized.
1113 */
1114 template<typename _Tp>
1115 _GLIBCXX23_CONSTEXPR
1116 inline __detail::__unique_ptr_array_t<_Tp>
1117 make_unique(size_t __num)
1118 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
1119
1120 /** Disable std::make_unique for arrays of known bound.
1121 * @tparam _Tp An array type of known bound, such as `U[N]`.
1122 * @since C++14
1123 * @relates unique_ptr
1124 */
1125 template<typename _Tp, typename... _Args>
1126 __detail::__invalid_make_unique_t<_Tp>
1127 make_unique(_Args&&...) = delete;
1128
1129 #if __cplusplus > 201703L
1130 /** Create a default-initialied object owned by a `unique_ptr`.
1131 * @tparam _Tp A non-array object type.
1132 * @returns A `unique_ptr<_Tp>` that owns the new object.
1133 * @since C++20
1134 * @relates unique_ptr
1135 */
1136 template<typename _Tp>
1137 _GLIBCXX23_CONSTEXPR
1138 inline __detail::__unique_ptr_t<_Tp>
1139 make_unique_for_overwrite()
1140 { return unique_ptr<_Tp>(new _Tp); }
1141
1142 /** Create a default-initialized array owned by a `unique_ptr`.
1143 * @tparam _Tp An array type of unknown bound, such as `U[]`.
1144 * @param __num The number of elements of type `U` in the new array.
1145 * @returns A `unique_ptr<U[]>` that owns the new array.
1146 * @since C++20
1147 * @relates unique_ptr
1148 */
1149 template<typename _Tp>
1150 _GLIBCXX23_CONSTEXPR
1151 inline __detail::__unique_ptr_array_t<_Tp>
1152 make_unique_for_overwrite(size_t __num)
1153 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]); }
1154
1155 /** Disable std::make_unique_for_overwrite for arrays of known bound.
1156 * @tparam _Tp An array type of known bound, such as `U[N]`.
1157 * @since C++20
1158 * @relates unique_ptr
1159 */
1160 template<typename _Tp, typename... _Args>
1161 __detail::__invalid_make_unique_t<_Tp>
1162 make_unique_for_overwrite(_Args&&...) = delete;
1163 #endif // C++20
1164
1165 #endif // C++14 && HOSTED
1166
1167 #if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1168 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1169 // 2948. unique_ptr does not define operator<< for stream output
1170 /// Stream output operator for unique_ptr
1171 /// @relates unique_ptr
1172 /// @since C++20
1173 template<typename _CharT, typename _Traits, typename _Tp, typename _Dp>
1174 inline basic_ostream<_CharT, _Traits>&
1175 operator<<(basic_ostream<_CharT, _Traits>& __os,
1176 const unique_ptr<_Tp, _Dp>& __p)
1177 requires requires { __os << __p.get(); }
1178 {
1179 __os << __p.get();
1180 return __os;
1181 }
1182 #endif // C++20 && HOSTED
1183
1184 #if __cpp_variable_templates
1185 template<typename _Tp>
1186 constexpr bool __is_unique_ptr = false;
1187 template<typename _Tp, typename _Del>
1188 constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> = true;
1189 #endif
1190
1191 /// @} group pointer_abstractions
1192
1193 #if __cplusplus >= 201703L
1194 namespace __detail::__variant
1195 {
1196 template<typename> struct _Never_valueless_alt; // see <variant>
1197
1198 // Provide the strong exception-safety guarantee when emplacing a
1199 // unique_ptr into a variant.
1200 template<typename _Tp, typename _Del>
1201 struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1202 : std::true_type
1203 { };
1204 } // namespace __detail::__variant
1205 #endif // C++17
1206
1207 _GLIBCXX_END_NAMESPACE_VERSION
1208 } // namespace
1209
1210 #endif /* _UNIQUE_PTR_H */