libstdc++
unique_ptr.h
Go to the documentation of this file.
1 // unique_ptr implementation -*- C++ -*-
2 
3 // Copyright (C) 2008-2026 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>
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>>
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,
381  is_convertible<_Ep, _Dp>>>>
382  _GLIBCXX23_CONSTEXPR
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>,
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  */
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>,
432  >::value,
433  unique_ptr&>::type
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>
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_<
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>,
601  __and_<is_pointer<_Up>,
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>,
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>,
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,
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
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
728  >::value,
729  unique_ptr&>::type
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*>,
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
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  // _GLIBCXX_RESOLVE_LIB_DEFECTS
854  // 2766. Swapping non-swappable types
855  template<typename _Tp, typename _Dp>
857  swap(unique_ptr<_Tp, _Dp>&,
858  unique_ptr<_Tp, _Dp>&) = delete;
859 #endif
860 
861  /// Equality operator for unique_ptr objects, compares the owned pointers
862  template<typename _Tp, typename _Dp,
863  typename _Up, typename _Ep>
864  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
865  inline bool
867  const unique_ptr<_Up, _Ep>& __y)
868  { return __x.get() == __y.get(); }
869 
870  /// unique_ptr comparison with nullptr
871  template<typename _Tp, typename _Dp>
872  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
873  inline bool
874  operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
875  { return !__x; }
876 
877 #ifndef __cpp_lib_three_way_comparison
878  /// unique_ptr comparison with nullptr
879  template<typename _Tp, typename _Dp>
880  _GLIBCXX_NODISCARD
881  inline bool
882  operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
883  { return !__x; }
884 
885  /// Inequality operator for unique_ptr objects, compares the owned pointers
886  template<typename _Tp, typename _Dp,
887  typename _Up, typename _Ep>
888  _GLIBCXX_NODISCARD
889  inline bool
890  operator!=(const unique_ptr<_Tp, _Dp>& __x,
891  const unique_ptr<_Up, _Ep>& __y)
892  { return __x.get() != __y.get(); }
893 
894  /// unique_ptr comparison with nullptr
895  template<typename _Tp, typename _Dp>
896  _GLIBCXX_NODISCARD
897  inline bool
898  operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
899  { return (bool)__x; }
900 
901  /// unique_ptr comparison with nullptr
902  template<typename _Tp, typename _Dp>
903  _GLIBCXX_NODISCARD
904  inline bool
905  operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
906  { return (bool)__x; }
907 #endif // three way comparison
908 
909  /// Relational operator for unique_ptr objects, compares the owned pointers
910  template<typename _Tp, typename _Dp,
911  typename _Up, typename _Ep>
912  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
913  inline bool
914  operator<(const unique_ptr<_Tp, _Dp>& __x,
915  const unique_ptr<_Up, _Ep>& __y)
916  {
917  typedef typename
919  typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
920  return std::less<_CT>()(__x.get(), __y.get());
921  }
922 
923  /// unique_ptr comparison with nullptr
924  template<typename _Tp, typename _Dp>
925  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
926  inline bool
927  operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
928  {
930  nullptr);
931  }
932 
933  /// unique_ptr comparison with nullptr
934  template<typename _Tp, typename _Dp>
935  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
936  inline bool
937  operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
938  {
940  __x.get());
941  }
942 
943  /// Relational operator for unique_ptr objects, compares the owned pointers
944  template<typename _Tp, typename _Dp,
945  typename _Up, typename _Ep>
946  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
947  inline bool
948  operator<=(const unique_ptr<_Tp, _Dp>& __x,
949  const unique_ptr<_Up, _Ep>& __y)
950  { return !(__y < __x); }
951 
952  /// unique_ptr comparison with nullptr
953  template<typename _Tp, typename _Dp>
954  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
955  inline bool
956  operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
957  { return !(nullptr < __x); }
958 
959  /// unique_ptr comparison with nullptr
960  template<typename _Tp, typename _Dp>
961  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
962  inline bool
963  operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
964  { return !(__x < nullptr); }
965 
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
970  inline bool
972  const unique_ptr<_Up, _Ep>& __y)
973  { return (__y < __x); }
974 
975  /// unique_ptr comparison with nullptr
976  template<typename _Tp, typename _Dp>
977  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
978  inline bool
979  operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
980  {
982  __x.get());
983  }
984 
985  /// unique_ptr comparison with nullptr
986  template<typename _Tp, typename _Dp>
987  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
988  inline bool
989  operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
990  {
992  nullptr);
993  }
994 
995  /// Relational operator for unique_ptr objects, compares the owned pointers
996  template<typename _Tp, typename _Dp,
997  typename _Up, typename _Ep>
998  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
999  inline bool
1001  const unique_ptr<_Up, _Ep>& __y)
1002  { return !(__x < __y); }
1003 
1004  /// unique_ptr comparison with nullptr
1005  template<typename _Tp, typename _Dp>
1006  _GLIBCXX_NODISCARD _GLIBCXX23_CONSTEXPR
1007  inline bool
1008  operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1009  { return !(__x < nullptr); }
1010 
1011  /// unique_ptr comparison with nullptr
1012  template<typename _Tp, typename _Dp>
1013  _GLIBCXX_NODISCARD inline bool
1014  operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
1015  { return !(nullptr < __x); }
1016 
1017 #ifdef __cpp_lib_three_way_comparison
1018  template<typename _Tp, typename _Dp, typename _Up, typename _Ep>
1019  requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
1020  typename unique_ptr<_Up, _Ep>::pointer>
1021  _GLIBCXX23_CONSTEXPR
1022  inline
1023  compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
1024  typename unique_ptr<_Up, _Ep>::pointer>
1025  operator<=>(const unique_ptr<_Tp, _Dp>& __x,
1026  const unique_ptr<_Up, _Ep>& __y)
1027  { return compare_three_way()(__x.get(), __y.get()); }
1028 
1029  template<typename _Tp, typename _Dp>
1030  requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
1031  _GLIBCXX23_CONSTEXPR
1032  inline
1033  compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
1034  operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
1035  {
1036  using pointer = typename unique_ptr<_Tp, _Dp>::pointer;
1037  return compare_three_way()(__x.get(), static_cast<pointer>(nullptr));
1038  }
1039 #endif
1040  /// @} relates unique_ptr
1041 
1042  /// @cond undocumented
1043  template<typename _Up, typename _Ptr = typename _Up::pointer>
1044  struct __uniq_ptr_hash
1045  : public __hash_base<size_t, _Up>
1046 #if ! _GLIBCXX_INLINE_VERSION
1047  , private __hash_empty_base<_Ptr>
1048 #endif
1049  {
1050  size_t
1051  operator()(const _Up& __u) const
1052  noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>())))
1053  { return hash<_Ptr>()(__u.get()); }
1054  };
1055 
1056  template<typename _Up>
1057  using __uniq_ptr_hash_base
1058  = __conditional_t<__is_hash_enabled_for<typename _Up::pointer>,
1059  __uniq_ptr_hash<_Up>,
1060  __hash_not_enabled<typename _Up::pointer>>;
1061  /// @endcond
1062 
1063  /// std::hash specialization for unique_ptr.
1064  template<typename _Tp, typename _Dp>
1065  struct hash<unique_ptr<_Tp, _Dp>>
1066  : public __uniq_ptr_hash_base<unique_ptr<_Tp, _Dp>>
1067  { };
1068 
1069 #ifdef __glibcxx_make_unique // C++ >= 14 && HOSTED
1070  /// @cond undocumented
1071 namespace __detail
1072 {
1073  template<typename _Tp>
1074  struct _MakeUniq
1075  { typedef unique_ptr<_Tp> __single_object; };
1076 
1077  template<typename _Tp>
1078  struct _MakeUniq<_Tp[]>
1079  { typedef unique_ptr<_Tp[]> __array; };
1080 
1081  template<typename _Tp, size_t _Bound>
1082  struct _MakeUniq<_Tp[_Bound]>
1083  { struct __invalid_type { }; };
1084 
1085  template<typename _Tp>
1086  using __unique_ptr_t = typename _MakeUniq<_Tp>::__single_object;
1087  template<typename _Tp>
1088  using __unique_ptr_array_t = typename _MakeUniq<_Tp>::__array;
1089  template<typename _Tp>
1090  using __invalid_make_unique_t = typename _MakeUniq<_Tp>::__invalid_type;
1091 }
1092  /// @endcond
1093 
1094  /** Create an object owned by a `unique_ptr`.
1095  * @tparam _Tp A non-array object type.
1096  * @param __args Constructor arguments for the new object.
1097  * @returns A `unique_ptr<_Tp>` that owns the new object.
1098  * @since C++14
1099  * @relates unique_ptr
1100  */
1101  template<typename _Tp, typename... _Args>
1102  _GLIBCXX23_CONSTEXPR
1103  inline __detail::__unique_ptr_t<_Tp>
1104  make_unique(_Args&&... __args)
1105  { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
1106 
1107  /** Create an array owned by a `unique_ptr`.
1108  * @tparam _Tp An array type of unknown bound, such as `U[]`.
1109  * @param __num The number of elements of type `U` in the new array.
1110  * @returns A `unique_ptr<U[]>` that owns the new array.
1111  * @since C++14
1112  * @relates unique_ptr
1113  *
1114  * The array elements are value-initialized.
1115  */
1116  template<typename _Tp>
1117  _GLIBCXX23_CONSTEXPR
1118  inline __detail::__unique_ptr_array_t<_Tp>
1119  make_unique(size_t __num)
1120  { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
1121 
1122  /** Disable std::make_unique for arrays of known bound.
1123  * @tparam _Tp An array type of known bound, such as `U[N]`.
1124  * @since C++14
1125  * @relates unique_ptr
1126  */
1127  template<typename _Tp, typename... _Args>
1128  __detail::__invalid_make_unique_t<_Tp>
1129  make_unique(_Args&&...) = delete;
1130 
1131 #if __cplusplus > 201703L
1132  /** Create a default-initialied object owned by a `unique_ptr`.
1133  * @tparam _Tp A non-array object type.
1134  * @returns A `unique_ptr<_Tp>` that owns the new object.
1135  * @since C++20
1136  * @relates unique_ptr
1137  */
1138  template<typename _Tp>
1139  _GLIBCXX23_CONSTEXPR
1140  inline __detail::__unique_ptr_t<_Tp>
1142  { return unique_ptr<_Tp>(new _Tp); }
1143 
1144  /** Create a default-initialized array owned by a `unique_ptr`.
1145  * @tparam _Tp An array type of unknown bound, such as `U[]`.
1146  * @param __num The number of elements of type `U` in the new array.
1147  * @returns A `unique_ptr<U[]>` that owns the new array.
1148  * @since C++20
1149  * @relates unique_ptr
1150  */
1151  template<typename _Tp>
1152  _GLIBCXX23_CONSTEXPR
1153  inline __detail::__unique_ptr_array_t<_Tp>
1155  { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]); }
1156 
1157  /** Disable std::make_unique_for_overwrite for arrays of known bound.
1158  * @tparam _Tp An array type of known bound, such as `U[N]`.
1159  * @since C++20
1160  * @relates unique_ptr
1161  */
1162  template<typename _Tp, typename... _Args>
1163  __detail::__invalid_make_unique_t<_Tp>
1164  make_unique_for_overwrite(_Args&&...) = delete;
1165 #endif // C++20
1166 
1167 #endif // C++14 && HOSTED
1168 
1169 #if __cplusplus > 201703L && __cpp_concepts && _GLIBCXX_HOSTED
1170  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1171  // 2948. unique_ptr does not define operator<< for stream output
1172  /// Stream output operator for unique_ptr
1173  /// @relates unique_ptr
1174  /// @since C++20
1175  template<typename _CharT, typename _Traits, typename _Tp, typename _Dp>
1178  const unique_ptr<_Tp, _Dp>& __p)
1179  requires requires { __os << __p.get(); }
1180  {
1181  __os << __p.get();
1182  return __os;
1183  }
1184 #endif // C++20 && HOSTED
1185 
1186 #if __cpp_variable_templates
1187  template<typename _Tp>
1188  constexpr bool __is_unique_ptr = false;
1189  template<typename _Tp, typename _Del>
1190  constexpr bool __is_unique_ptr<unique_ptr<_Tp, _Del>> = true;
1191 #endif
1192 
1193  /// @} group pointer_abstractions
1194 
1195 #if __cplusplus >= 201703L
1196  namespace __detail::__variant
1197  {
1198  template<typename> struct _Never_valueless_alt; // see <variant>
1199 
1200  // Provide the strong exception-safety guarantee when emplacing a
1201  // unique_ptr into a variant.
1202  template<typename _Tp, typename _Del>
1203  struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1204  : std::true_type
1205  { };
1206  } // namespace __detail::__variant
1207 #endif // C++17
1208 
1209 _GLIBCXX_END_NAMESPACE_VERSION
1210 } // namespace
1211 
1212 #endif /* _UNIQUE_PTR_H */
requires requires
Definition: complex:1948
__detail::__invalid_make_unique_t< _Tp > make_unique_for_overwrite(_Args &&...)=delete
constexpr __detail::__unique_ptr_array_t< _Tp > make_unique_for_overwrite(size_t __num)
Definition: unique_ptr.h:1154
constexpr __detail::__unique_ptr_array_t< _Tp > make_unique(size_t __num)
Definition: unique_ptr.h:1119
constexpr bool operator<(nullptr_t, const unique_ptr< _Tp, _Dp > &__x)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:937
constexpr bool operator>(nullptr_t, const unique_ptr< _Tp, _Dp > &__x)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:989
constexpr enable_if< __is_swappable< _Dp >::value >::type swap(unique_ptr< _Tp, _Dp > &__x, unique_ptr< _Tp, _Dp > &__y) noexcept
Definition: unique_ptr.h:848
constexpr bool operator<=(nullptr_t, const unique_ptr< _Tp, _Dp > &__x)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:963
constexpr bool operator==(const unique_ptr< _Tp, _Dp > &__x, nullptr_t) noexcept
unique_ptr comparison with nullptr
Definition: unique_ptr.h:874
__detail::__invalid_make_unique_t< _Tp > make_unique(_Args &&...)=delete
bool operator>=(nullptr_t, const unique_ptr< _Tp, _Dp > &__x)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:1014
constexpr __detail::__unique_ptr_t< _Tp > make_unique_for_overwrite()
Definition: unique_ptr.h:1141
constexpr __detail::__unique_ptr_t< _Tp > make_unique(_Args &&... __args)
Definition: unique_ptr.h:1104
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:119
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:122
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
Definition: type_traits:2298
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition: type_traits:2720
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:72
ISO C++ entities toplevel namespace is std.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1754
Template class basic_ostream.
Definition: ostream.h:67
Primary class template hash.
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:137
is_void
Definition: type_traits:332
is_array
Definition: type_traits:609
is_pointer
Definition: type_traits:629
is_lvalue_reference
Definition: type_traits:655
is_same
Definition: type_traits:1627
is_copy_constructible
Definition: type_traits:1279
is_move_constructible
Definition: type_traits:1306
is_assignable
Definition: type_traits:1362
common_type
Definition: type_traits:2579
Smart pointer adaptor for functions taking an output pointer parameter.
Definition: out_ptr.h:58
Smart pointer adaptor for functions taking an inout pointer parameter.
Definition: out_ptr.h:301
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:94
One of the comparison functors.
Definition: stl_function.h:404
constexpr void operator()(_Tp *__ptr) const
Calls delete __ptr
Definition: unique_ptr.h:86
constexpr default_delete() noexcept=default
Default constructor.
constexpr default_delete() noexcept=default
Default constructor.
constexpr enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr
Definition: unique_ptr.h:129
A move-only smart pointer that manages unique ownership of a resource.
Definition: unique_ptr.h:271
constexpr pointer operator->() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:481
constexpr deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:496
constexpr unique_ptr(pointer __p) noexcept
Definition: unique_ptr.h:320
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:307
constexpr unique_ptr(pointer __p, const deleter_type &__d) noexcept
Definition: unique_ptr.h:334
unique_ptr(unique_ptr &&)=default
Move constructor.
constexpr void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
Definition: unique_ptr.h:526
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
constexpr const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:502
constexpr unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
Definition: unique_ptr.h:383
constexpr ~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
Definition: unique_ptr.h:402
constexpr void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Definition: unique_ptr.h:536
constexpr add_lvalue_reference< element_type >::type operator*() const noexcept(noexcept(*std::declval< pointer >())) requires requires
Dereference the stored pointer.
Definition: unique_ptr.h:455
constexpr pointer get() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:490
constexpr unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
Definition: unique_ptr.h:444
constexpr enable_if< __and_< __safe_conversion_up< _Up, _Ep >, is_assignable< deleter_type &, _Ep && > >::value, unique_ptr & >::type operator=(unique_ptr< _Up, _Ep > &&__u) noexcept
Assignment from another type.
Definition: unique_ptr.h:434
constexpr unique_ptr(pointer __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __d) noexcept
Definition: unique_ptr.h:347
constexpr pointer release() noexcept
Release ownership of any stored pointer.
Definition: unique_ptr.h:515
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:362
A move-only smart pointer that manages unique ownership of an array.
Definition: unique_ptr.h:565
constexpr pointer release() noexcept
Release ownership of any stored pointer.
Definition: unique_ptr.h:785
constexpr unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
Definition: unique_ptr.h:740
constexpr std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.
Definition: unique_ptr.h:751
constexpr ~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
Definition: unique_ptr.h:699
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:681
constexpr void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Definition: unique_ptr.h:818
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:614
constexpr void reset(_Up __p) noexcept
Replace the stored pointer.
Definition: unique_ptr.h:808
constexpr pointer get() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:760
constexpr enable_if< __and_< __safe_conversion_up< _Up, _Ep >, is_assignable< deleter_type &, _Ep && > >::value, unique_ptr & >::type operator=(unique_ptr< _Up, _Ep > &&__u) noexcept
Assignment from another type.
Definition: unique_ptr.h:730
constexpr unique_ptr(_Up __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __d) noexcept
Definition: unique_ptr.h:663
constexpr unique_ptr(_Up __p) noexcept
Definition: unique_ptr.h:632
unique_ptr(unique_ptr &&)=default
Move constructor.
constexpr unique_ptr(_Up __p, const deleter_type &__d) noexcept
Definition: unique_ptr.h:648
constexpr const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:772
constexpr deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:766