libstdc++
unique_ptr.h
Go to the documentation of this file.
1 // unique_ptr implementation -*- C++ -*-
2 
3 // Copyright (C) 2008-2021 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 <utility>
37 #include <tuple>
38 #include <bits/stl_function.h>
39 #include <bits/functional_hash.h>
40 #if __cplusplus > 201703L
41 # include <compare>
42 # include <ostream>
43 #endif
44 
45 namespace std _GLIBCXX_VISIBILITY(default)
46 {
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 
49  /**
50  * @addtogroup pointer_abstractions
51  * @{
52  */
53 
54 #if _GLIBCXX_USE_DEPRECATED
55 #pragma GCC diagnostic push
56 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
57  template<typename> class auto_ptr;
58 #pragma GCC diagnostic pop
59 #endif
60 
61  /// Primary template of default_delete, used by unique_ptr for single objects
62  template<typename _Tp>
64  {
65  /// Default constructor
66  constexpr default_delete() noexcept = default;
67 
68  /** @brief Converting constructor.
69  *
70  * Allows conversion from a deleter for objects of another type, `_Up`,
71  * only if `_Up*` is convertible to `_Tp*`.
72  */
73  template<typename _Up,
74  typename = _Require<is_convertible<_Up*, _Tp*>>>
75  default_delete(const default_delete<_Up>&) noexcept { }
76 
77  /// Calls `delete __ptr`
78  void
79  operator()(_Tp* __ptr) const
80  {
81  static_assert(!is_void<_Tp>::value,
82  "can't delete pointer to incomplete type");
83  static_assert(sizeof(_Tp)>0,
84  "can't delete pointer to incomplete type");
85  delete __ptr;
86  }
87  };
88 
89  // _GLIBCXX_RESOLVE_LIB_DEFECTS
90  // DR 740 - omit specialization for array objects with a compile time length
91 
92  /// Specialization of default_delete for arrays, used by `unique_ptr<T[]>`
93  template<typename _Tp>
94  struct default_delete<_Tp[]>
95  {
96  public:
97  /// Default constructor
98  constexpr default_delete() noexcept = default;
99 
100  /** @brief Converting constructor.
101  *
102  * Allows conversion from a deleter for arrays of another type, such as
103  * a const-qualified version of `_Tp`.
104  *
105  * Conversions from types derived from `_Tp` are not allowed because
106  * it is undefined to `delete[]` an array of derived types through a
107  * pointer to the base type.
108  */
109  template<typename _Up,
110  typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>>
111  default_delete(const default_delete<_Up[]>&) noexcept { }
112 
113  /// Calls `delete[] __ptr`
114  template<typename _Up>
115  typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
116  operator()(_Up* __ptr) const
117  {
118  static_assert(sizeof(_Tp)>0,
119  "can't delete pointer to incomplete type");
120  delete [] __ptr;
121  }
122  };
123 
124  /// @cond undocumented
125 
126  // Manages the pointer and deleter of a unique_ptr
127  template <typename _Tp, typename _Dp>
128  class __uniq_ptr_impl
129  {
130  template <typename _Up, typename _Ep, typename = void>
131  struct _Ptr
132  {
133  using type = _Up*;
134  };
135 
136  template <typename _Up, typename _Ep>
137  struct
138  _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
139  {
140  using type = typename remove_reference<_Ep>::type::pointer;
141  };
142 
143  public:
144  using _DeleterConstraint = enable_if<
145  __and_<__not_<is_pointer<_Dp>>,
146  is_default_constructible<_Dp>>::value>;
147 
148  using pointer = typename _Ptr<_Tp, _Dp>::type;
149 
150  static_assert( !is_rvalue_reference<_Dp>::value,
151  "unique_ptr's deleter type must be a function object type"
152  " or an lvalue reference type" );
153 
154  __uniq_ptr_impl() = default;
155  __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
156 
157  template<typename _Del>
158  __uniq_ptr_impl(pointer __p, _Del&& __d)
159  : _M_t(__p, std::forward<_Del>(__d)) { }
160 
161  __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
162  : _M_t(std::move(__u._M_t))
163  { __u._M_ptr() = nullptr; }
164 
165  __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u) noexcept
166  {
167  reset(__u.release());
168  _M_deleter() = std::forward<_Dp>(__u._M_deleter());
169  return *this;
170  }
171 
172  pointer& _M_ptr() { return std::get<0>(_M_t); }
173  pointer _M_ptr() const { return std::get<0>(_M_t); }
174  _Dp& _M_deleter() { return std::get<1>(_M_t); }
175  const _Dp& _M_deleter() const { return std::get<1>(_M_t); }
176 
177  void reset(pointer __p) noexcept
178  {
179  const pointer __old_p = _M_ptr();
180  _M_ptr() = __p;
181  if (__old_p)
182  _M_deleter()(__old_p);
183  }
184 
185  pointer release() noexcept
186  {
187  pointer __p = _M_ptr();
188  _M_ptr() = nullptr;
189  return __p;
190  }
191 
192  void
193  swap(__uniq_ptr_impl& __rhs) noexcept
194  {
195  using std::swap;
196  swap(this->_M_ptr(), __rhs._M_ptr());
197  swap(this->_M_deleter(), __rhs._M_deleter());
198  }
199 
200  private:
201  tuple<pointer, _Dp> _M_t;
202  };
203 
204  // Defines move construction + assignment as either defaulted or deleted.
205  template <typename _Tp, typename _Dp,
206  bool = is_move_constructible<_Dp>::value,
207  bool = is_move_assignable<_Dp>::value>
208  struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
209  {
210  using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
211  __uniq_ptr_data(__uniq_ptr_data&&) = default;
212  __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
213  };
214 
215  template <typename _Tp, typename _Dp>
216  struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
217  {
218  using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
219  __uniq_ptr_data(__uniq_ptr_data&&) = default;
220  __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
221  };
222 
223  template <typename _Tp, typename _Dp>
224  struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
225  {
226  using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
227  __uniq_ptr_data(__uniq_ptr_data&&) = delete;
228  __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
229  };
230 
231  template <typename _Tp, typename _Dp>
232  struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
233  {
234  using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
235  __uniq_ptr_data(__uniq_ptr_data&&) = delete;
236  __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
237  };
238  /// @endcond
239 
240  /// 20.7.1.2 unique_ptr for single objects.
241  template <typename _Tp, typename _Dp = default_delete<_Tp>>
243  {
244  template <typename _Up>
245  using _DeleterConstraint =
246  typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
247 
248  __uniq_ptr_data<_Tp, _Dp> _M_t;
249 
250  public:
251  using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
252  using element_type = _Tp;
253  using deleter_type = _Dp;
254 
255  private:
256  // helper template for detecting a safe conversion from another
257  // unique_ptr
258  template<typename _Up, typename _Ep>
259  using __safe_conversion_up = __and_<
261  __not_<is_array<_Up>>
262  >;
263 
264  public:
265  // Constructors.
266 
267  /// Default constructor, creates a unique_ptr that owns nothing.
268  template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
269  constexpr unique_ptr() noexcept
270  : _M_t()
271  { }
272 
273  /** Takes ownership of a pointer.
274  *
275  * @param __p A pointer to an object of @c element_type
276  *
277  * The deleter will be value-initialized.
278  */
279  template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
280  explicit
281  unique_ptr(pointer __p) noexcept
282  : _M_t(__p)
283  { }
284 
285  /** Takes ownership of a pointer.
286  *
287  * @param __p A pointer to an object of @c element_type
288  * @param __d A reference to a deleter.
289  *
290  * The deleter will be initialized with @p __d
291  */
292  template<typename _Del = deleter_type,
293  typename = _Require<is_copy_constructible<_Del>>>
294  unique_ptr(pointer __p, const deleter_type& __d) noexcept
295  : _M_t(__p, __d) { }
296 
297  /** Takes ownership of a pointer.
298  *
299  * @param __p A pointer to an object of @c element_type
300  * @param __d An rvalue reference to a (non-reference) deleter.
301  *
302  * The deleter will be initialized with @p std::move(__d)
303  */
304  template<typename _Del = deleter_type,
305  typename = _Require<is_move_constructible<_Del>>>
306  unique_ptr(pointer __p,
307  __enable_if_t<!is_lvalue_reference<_Del>::value,
308  _Del&&> __d) noexcept
309  : _M_t(__p, std::move(__d))
310  { }
311 
312  template<typename _Del = deleter_type,
313  typename _DelUnref = typename remove_reference<_Del>::type>
314  unique_ptr(pointer,
315  __enable_if_t<is_lvalue_reference<_Del>::value,
316  _DelUnref&&>) = delete;
317 
318  /// Creates a unique_ptr that owns nothing.
319  template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
320  constexpr unique_ptr(nullptr_t) noexcept
321  : _M_t()
322  { }
323 
324  // Move constructors.
325 
326  /// Move constructor.
327  unique_ptr(unique_ptr&&) = default;
328 
329  /** @brief Converting constructor from another type
330  *
331  * Requires that the pointer owned by @p __u is convertible to the
332  * type of pointer owned by this object, @p __u does not own an array,
333  * and @p __u has a compatible deleter type.
334  */
335  template<typename _Up, typename _Ep, typename = _Require<
336  __safe_conversion_up<_Up, _Ep>,
339  is_convertible<_Ep, _Dp>>::type>>
341  : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
342  { }
343 
344 #if _GLIBCXX_USE_DEPRECATED
345 #pragma GCC diagnostic push
346 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
347  /// Converting constructor from @c auto_ptr
348  template<typename _Up, typename = _Require<
350  unique_ptr(auto_ptr<_Up>&& __u) noexcept;
351 #pragma GCC diagnostic pop
352 #endif
353 
354  /// Destructor, invokes the deleter if the stored pointer is not null.
355  ~unique_ptr() noexcept
356  {
357  static_assert(__is_invocable<deleter_type&, pointer>::value,
358  "unique_ptr's deleter must be invocable with a pointer");
359  auto& __ptr = _M_t._M_ptr();
360  if (__ptr != nullptr)
361  get_deleter()(std::move(__ptr));
362  __ptr = pointer();
363  }
364 
365  // Assignment.
366 
367  /** @brief Move assignment operator.
368  *
369  * Invokes the deleter if this object owns a pointer.
370  */
372 
373  /** @brief Assignment from another type.
374  *
375  * @param __u The object to transfer ownership from, which owns a
376  * convertible pointer to a non-array object.
377  *
378  * Invokes the deleter if this object owns a pointer.
379  */
380  template<typename _Up, typename _Ep>
381  typename enable_if< __and_<
382  __safe_conversion_up<_Up, _Ep>,
384  >::value,
385  unique_ptr&>::type
387  {
388  reset(__u.release());
389  get_deleter() = std::forward<_Ep>(__u.get_deleter());
390  return *this;
391  }
392 
393  /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
394  unique_ptr&
395  operator=(nullptr_t) noexcept
396  {
397  reset();
398  return *this;
399  }
400 
401  // Observers.
402 
403  /// Dereference the stored pointer.
404  typename add_lvalue_reference<element_type>::type
405  operator*() const noexcept(noexcept(*std::declval<pointer>()))
406  {
407  __glibcxx_assert(get() != pointer());
408  return *get();
409  }
410 
411  /// Return the stored pointer.
412  pointer
413  operator->() const noexcept
414  {
415  _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
416  return get();
417  }
418 
419  /// Return the stored pointer.
420  pointer
421  get() const noexcept
422  { return _M_t._M_ptr(); }
423 
424  /// Return a reference to the stored deleter.
425  deleter_type&
426  get_deleter() noexcept
427  { return _M_t._M_deleter(); }
428 
429  /// Return a reference to the stored deleter.
430  const deleter_type&
431  get_deleter() const noexcept
432  { return _M_t._M_deleter(); }
433 
434  /// Return @c true if the stored pointer is not null.
435  explicit operator bool() const noexcept
436  { return get() == pointer() ? false : true; }
437 
438  // Modifiers.
439 
440  /// Release ownership of any stored pointer.
441  pointer
442  release() noexcept
443  { return _M_t.release(); }
444 
445  /** @brief Replace the stored pointer.
446  *
447  * @param __p The new pointer to store.
448  *
449  * The deleter will be invoked if a pointer is already owned.
450  */
451  void
452  reset(pointer __p = pointer()) noexcept
453  {
454  static_assert(__is_invocable<deleter_type&, pointer>::value,
455  "unique_ptr's deleter must be invocable with a pointer");
456  _M_t.reset(std::move(__p));
457  }
458 
459  /// Exchange the pointer and deleter with another object.
460  void
461  swap(unique_ptr& __u) noexcept
462  {
463  static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
464  _M_t.swap(__u._M_t);
465  }
466 
467  // Disable copy from lvalue.
468  unique_ptr(const unique_ptr&) = delete;
469  unique_ptr& operator=(const unique_ptr&) = delete;
470  };
471 
472  /// 20.7.1.3 unique_ptr for array objects with a runtime length
473  // [unique.ptr.runtime]
474  // _GLIBCXX_RESOLVE_LIB_DEFECTS
475  // DR 740 - omit specialization for array objects with a compile time length
476  template<typename _Tp, typename _Dp>
477  class unique_ptr<_Tp[], _Dp>
478  {
479  template <typename _Up>
480  using _DeleterConstraint =
481  typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
482 
483  __uniq_ptr_data<_Tp, _Dp> _M_t;
484 
485  template<typename _Up>
486  using __remove_cv = typename remove_cv<_Up>::type;
487 
488  // like is_base_of<_Tp, _Up> but false if unqualified types are the same
489  template<typename _Up>
490  using __is_derived_Tp
491  = __and_< is_base_of<_Tp, _Up>,
492  __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
493 
494  // This checks whether p[n] is noexcept, but fails gracefully when
495  // element_type is incomplete. The standard requires a complete type
496  // for unique_ptr<T[], D>, but we try to support it anyway (PR 101236).
497  template<typename _Ptr, typename _Elt>
498  static constexpr auto
499  _S_nothrow_deref(size_t __n)
500  -> decltype(sizeof(_Elt) != 0) // PR c++/101239
501  { return noexcept(std::declval<_Ptr>()[__n]); }
502 
503  template<typename _Ptr, typename _Elt>
504  static constexpr bool
505  _S_nothrow_deref(...)
506  { return false; }
507 
508  public:
509  using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
510  using element_type = _Tp;
511  using deleter_type = _Dp;
512 
513  // helper template for detecting a safe conversion from another
514  // unique_ptr
515  template<typename _Up, typename _Ep,
516  typename _UPtr = unique_ptr<_Up, _Ep>,
517  typename _UP_pointer = typename _UPtr::pointer,
518  typename _UP_element_type = typename _UPtr::element_type>
519  using __safe_conversion_up = __and_<
523  is_convertible<_UP_element_type(*)[], element_type(*)[]>
524  >;
525 
526  // helper template for detecting a safe conversion from a raw pointer
527  template<typename _Up>
528  using __safe_conversion_raw = __and_<
529  __or_<__or_<is_same<_Up, pointer>,
531  __and_<is_pointer<_Up>,
534  typename remove_pointer<_Up>::type(*)[],
535  element_type(*)[]>
536  >
537  >
538  >;
539 
540  // Constructors.
541 
542  /// Default constructor, creates a unique_ptr that owns nothing.
543  template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
544  constexpr unique_ptr() noexcept
545  : _M_t()
546  { }
547 
548  /** Takes ownership of a pointer.
549  *
550  * @param __p A pointer to an array of a type safely convertible
551  * to an array of @c element_type
552  *
553  * The deleter will be value-initialized.
554  */
555  template<typename _Up,
556  typename _Vp = _Dp,
557  typename = _DeleterConstraint<_Vp>,
558  typename = typename enable_if<
559  __safe_conversion_raw<_Up>::value, bool>::type>
560  explicit
561  unique_ptr(_Up __p) noexcept
562  : _M_t(__p)
563  { }
564 
565  /** Takes ownership of a pointer.
566  *
567  * @param __p A pointer to an array of a type safely convertible
568  * to an array of @c element_type
569  * @param __d A reference to a deleter.
570  *
571  * The deleter will be initialized with @p __d
572  */
573  template<typename _Up, typename _Del = deleter_type,
574  typename = _Require<__safe_conversion_raw<_Up>,
576  unique_ptr(_Up __p, const deleter_type& __d) noexcept
577  : _M_t(__p, __d) { }
578 
579  /** Takes ownership of a pointer.
580  *
581  * @param __p A pointer to an array of a type safely convertible
582  * to an array of @c element_type
583  * @param __d A reference to a deleter.
584  *
585  * The deleter will be initialized with @p std::move(__d)
586  */
587  template<typename _Up, typename _Del = deleter_type,
588  typename = _Require<__safe_conversion_raw<_Up>,
590  unique_ptr(_Up __p,
591  __enable_if_t<!is_lvalue_reference<_Del>::value,
592  _Del&&> __d) noexcept
593  : _M_t(std::move(__p), std::move(__d))
594  { }
595 
596  template<typename _Up, typename _Del = deleter_type,
597  typename _DelUnref = typename remove_reference<_Del>::type,
598  typename = _Require<__safe_conversion_raw<_Up>>>
599  unique_ptr(_Up,
600  __enable_if_t<is_lvalue_reference<_Del>::value,
601  _DelUnref&&>) = delete;
602 
603  /// Move constructor.
604  unique_ptr(unique_ptr&&) = default;
605 
606  /// Creates a unique_ptr that owns nothing.
607  template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
608  constexpr unique_ptr(nullptr_t) noexcept
609  : _M_t()
610  { }
611 
612  template<typename _Up, typename _Ep, typename = _Require<
613  __safe_conversion_up<_Up, _Ep>,
616  is_convertible<_Ep, _Dp>>::type>>
617  unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
618  : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
619  { }
620 
621  /// Destructor, invokes the deleter if the stored pointer is not null.
623  {
624  auto& __ptr = _M_t._M_ptr();
625  if (__ptr != nullptr)
626  get_deleter()(__ptr);
627  __ptr = pointer();
628  }
629 
630  // Assignment.
631 
632  /** @brief Move assignment operator.
633  *
634  * Invokes the deleter if this object owns a pointer.
635  */
636  unique_ptr&
637  operator=(unique_ptr&&) = default;
638 
639  /** @brief Assignment from another type.
640  *
641  * @param __u The object to transfer ownership from, which owns a
642  * convertible pointer to an array object.
643  *
644  * Invokes the deleter if this object owns a pointer.
645  */
646  template<typename _Up, typename _Ep>
647  typename
650  >::value,
651  unique_ptr&>::type
653  {
654  reset(__u.release());
655  get_deleter() = std::forward<_Ep>(__u.get_deleter());
656  return *this;
657  }
658 
659  /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
660  unique_ptr&
661  operator=(nullptr_t) noexcept
662  {
663  reset();
664  return *this;
665  }
666 
667  // Observers.
668 
669  /// Access an element of owned array.
670  typename std::add_lvalue_reference<element_type>::type
671  operator[](size_t __i) const
672  noexcept(_S_nothrow_deref<pointer, element_type>(0))
673  {
674  __glibcxx_assert(get() != pointer());
675  return get()[__i];
676  }
677 
678  /// Return the stored pointer.
679  pointer
680  get() const noexcept
681  { return _M_t._M_ptr(); }
682 
683  /// Return a reference to the stored deleter.
684  deleter_type&
685  get_deleter() noexcept
686  { return _M_t._M_deleter(); }
687 
688  /// Return a reference to the stored deleter.
689  const deleter_type&
690  get_deleter() const noexcept
691  { return _M_t._M_deleter(); }
692 
693  /// Return @c true if the stored pointer is not null.
694  explicit operator bool() const noexcept
695  { return get() == pointer() ? false : true; }
696 
697  // Modifiers.
698 
699  /// Release ownership of any stored pointer.
700  pointer
701  release() noexcept
702  { return _M_t.release(); }
703 
704  /** @brief Replace the stored pointer.
705  *
706  * @param __p The new pointer to store.
707  *
708  * The deleter will be invoked if a pointer is already owned.
709  */
710  template <typename _Up,
711  typename = _Require<
712  __or_<is_same<_Up, pointer>,
713  __and_<is_same<pointer, element_type*>,
716  typename remove_pointer<_Up>::type(*)[],
717  element_type(*)[]
718  >
719  >
720  >
721  >>
722  void
723  reset(_Up __p) noexcept
724  { _M_t.reset(std::move(__p)); }
725 
726  void reset(nullptr_t = nullptr) noexcept
727  { reset(pointer()); }
728 
729  /// Exchange the pointer and deleter with another object.
730  void
731  swap(unique_ptr& __u) noexcept
732  {
733  static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
734  _M_t.swap(__u._M_t);
735  }
736 
737  // Disable copy from lvalue.
738  unique_ptr(const unique_ptr&) = delete;
739  unique_ptr& operator=(const unique_ptr&) = delete;
740  };
741 
742  /// @relates unique_ptr @{
743 
744  /// Swap overload for unique_ptr
745  template<typename _Tp, typename _Dp>
746  inline
747 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
748  // Constrained free swap overload, see p0185r1
749  typename enable_if<__is_swappable<_Dp>::value>::type
750 #else
751  void
752 #endif
754  unique_ptr<_Tp, _Dp>& __y) noexcept
755  { __x.swap(__y); }
756 
757 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
758  template<typename _Tp, typename _Dp>
761  unique_ptr<_Tp, _Dp>&) = delete;
762 #endif
763 
764  /// Equality operator for unique_ptr objects, compares the owned pointers
765  template<typename _Tp, typename _Dp,
766  typename _Up, typename _Ep>
767  _GLIBCXX_NODISCARD inline bool
769  const unique_ptr<_Up, _Ep>& __y)
770  { return __x.get() == __y.get(); }
771 
772  /// unique_ptr comparison with nullptr
773  template<typename _Tp, typename _Dp>
774  _GLIBCXX_NODISCARD inline bool
775  operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
776  { return !__x; }
777 
778 #ifndef __cpp_lib_three_way_comparison
779  /// unique_ptr comparison with nullptr
780  template<typename _Tp, typename _Dp>
781  _GLIBCXX_NODISCARD inline bool
782  operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
783  { return !__x; }
784 
785  /// Inequality operator for unique_ptr objects, compares the owned pointers
786  template<typename _Tp, typename _Dp,
787  typename _Up, typename _Ep>
788  _GLIBCXX_NODISCARD inline bool
790  const unique_ptr<_Up, _Ep>& __y)
791  { return __x.get() != __y.get(); }
792 
793  /// unique_ptr comparison with nullptr
794  template<typename _Tp, typename _Dp>
795  _GLIBCXX_NODISCARD inline bool
796  operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
797  { return (bool)__x; }
798 
799  /// unique_ptr comparison with nullptr
800  template<typename _Tp, typename _Dp>
801  _GLIBCXX_NODISCARD inline bool
802  operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
803  { return (bool)__x; }
804 #endif // three way comparison
805 
806  /// Relational operator for unique_ptr objects, compares the owned pointers
807  template<typename _Tp, typename _Dp,
808  typename _Up, typename _Ep>
809  _GLIBCXX_NODISCARD inline bool
810  operator<(const unique_ptr<_Tp, _Dp>& __x,
811  const unique_ptr<_Up, _Ep>& __y)
812  {
813  typedef typename
815  typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
816  return std::less<_CT>()(__x.get(), __y.get());
817  }
818 
819  /// unique_ptr comparison with nullptr
820  template<typename _Tp, typename _Dp>
821  _GLIBCXX_NODISCARD inline bool
822  operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
823  {
825  nullptr);
826  }
827 
828  /// unique_ptr comparison with nullptr
829  template<typename _Tp, typename _Dp>
830  _GLIBCXX_NODISCARD inline bool
831  operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
832  {
834  __x.get());
835  }
836 
837  /// Relational operator for unique_ptr objects, compares the owned pointers
838  template<typename _Tp, typename _Dp,
839  typename _Up, typename _Ep>
840  _GLIBCXX_NODISCARD inline bool
841  operator<=(const unique_ptr<_Tp, _Dp>& __x,
842  const unique_ptr<_Up, _Ep>& __y)
843  { return !(__y < __x); }
844 
845  /// unique_ptr comparison with nullptr
846  template<typename _Tp, typename _Dp>
847  _GLIBCXX_NODISCARD inline bool
848  operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
849  { return !(nullptr < __x); }
850 
851  /// unique_ptr comparison with nullptr
852  template<typename _Tp, typename _Dp>
853  _GLIBCXX_NODISCARD inline bool
854  operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
855  { return !(__x < nullptr); }
856 
857  /// Relational operator for unique_ptr objects, compares the owned pointers
858  template<typename _Tp, typename _Dp,
859  typename _Up, typename _Ep>
860  _GLIBCXX_NODISCARD inline bool
862  const unique_ptr<_Up, _Ep>& __y)
863  { return (__y < __x); }
864 
865  /// unique_ptr comparison with nullptr
866  template<typename _Tp, typename _Dp>
867  _GLIBCXX_NODISCARD inline bool
868  operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
869  {
871  __x.get());
872  }
873 
874  /// unique_ptr comparison with nullptr
875  template<typename _Tp, typename _Dp>
876  _GLIBCXX_NODISCARD inline bool
877  operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
878  {
880  nullptr);
881  }
882 
883  /// Relational operator for unique_ptr objects, compares the owned pointers
884  template<typename _Tp, typename _Dp,
885  typename _Up, typename _Ep>
886  _GLIBCXX_NODISCARD inline bool
888  const unique_ptr<_Up, _Ep>& __y)
889  { return !(__x < __y); }
890 
891  /// unique_ptr comparison with nullptr
892  template<typename _Tp, typename _Dp>
893  _GLIBCXX_NODISCARD inline bool
894  operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
895  { return !(__x < nullptr); }
896 
897  /// unique_ptr comparison with nullptr
898  template<typename _Tp, typename _Dp>
899  _GLIBCXX_NODISCARD inline bool
900  operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
901  { return !(nullptr < __x); }
902 
903 #ifdef __cpp_lib_three_way_comparison
904  template<typename _Tp, typename _Dp, typename _Up, typename _Ep>
905  requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
906  typename unique_ptr<_Up, _Ep>::pointer>
907  inline
908  compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
909  typename unique_ptr<_Up, _Ep>::pointer>
910  operator<=>(const unique_ptr<_Tp, _Dp>& __x,
911  const unique_ptr<_Up, _Ep>& __y)
912  { return compare_three_way()(__x.get(), __y.get()); }
913 
914  template<typename _Tp, typename _Dp>
915  requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
916  inline
917  compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
918  operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
919  {
920  using pointer = typename unique_ptr<_Tp, _Dp>::pointer;
921  return compare_three_way()(__x.get(), static_cast<pointer>(nullptr));
922  }
923 #endif
924  /// @} relates unique_ptr
925 
926  /// @cond undocumented
927  template<typename _Up, typename _Ptr = typename _Up::pointer,
928  bool = __poison_hash<_Ptr>::__enable_hash_call>
929  struct __uniq_ptr_hash
930 #if ! _GLIBCXX_INLINE_VERSION
931  : private __poison_hash<_Ptr>
932 #endif
933  {
934  size_t
935  operator()(const _Up& __u) const
936  noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>())))
937  { return hash<_Ptr>()(__u.get()); }
938  };
939 
940  template<typename _Up, typename _Ptr>
941  struct __uniq_ptr_hash<_Up, _Ptr, false>
942  : private __poison_hash<_Ptr>
943  { };
944  /// @endcond
945 
946  /// std::hash specialization for unique_ptr.
947  template<typename _Tp, typename _Dp>
948  struct hash<unique_ptr<_Tp, _Dp>>
949  : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
950  public __uniq_ptr_hash<unique_ptr<_Tp, _Dp>>
951  { };
952 
953 #if __cplusplus >= 201402L
954  /// @relates unique_ptr @{
955 #define __cpp_lib_make_unique 201304
956 
957  /// @cond undocumented
958 
959  template<typename _Tp>
960  struct _MakeUniq
961  { typedef unique_ptr<_Tp> __single_object; };
962 
963  template<typename _Tp>
964  struct _MakeUniq<_Tp[]>
965  { typedef unique_ptr<_Tp[]> __array; };
966 
967  template<typename _Tp, size_t _Bound>
968  struct _MakeUniq<_Tp[_Bound]>
969  { struct __invalid_type { }; };
970 
971  /// @endcond
972 
973  /// std::make_unique for single objects
974  template<typename _Tp, typename... _Args>
975  inline typename _MakeUniq<_Tp>::__single_object
976  make_unique(_Args&&... __args)
977  { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
978 
979  /// std::make_unique for arrays of unknown bound
980  template<typename _Tp>
981  inline typename _MakeUniq<_Tp>::__array
982  make_unique(size_t __num)
983  { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
984 
985  /// Disable std::make_unique for arrays of known bound
986  template<typename _Tp, typename... _Args>
987  typename _MakeUniq<_Tp>::__invalid_type
988  make_unique(_Args&&...) = delete;
989 
990 #if __cplusplus > 201703L
991  /// std::make_unique_for_overwrite for single objects
992  template<typename _Tp>
993  inline typename _MakeUniq<_Tp>::__single_object
994  make_unique_for_overwrite()
995  { return unique_ptr<_Tp>(new _Tp); }
996 
997  /// std::make_unique_for_overwrite for arrays of unknown bound
998  template<typename _Tp>
999  inline typename _MakeUniq<_Tp>::__array
1000  make_unique_for_overwrite(size_t __n)
1001  { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__n]); }
1002 
1003  /// Disable std::make_unique_for_overwrite for arrays of known bound
1004  template<typename _Tp, typename... _Args>
1005  typename _MakeUniq<_Tp>::__invalid_type
1006  make_unique_for_overwrite(_Args&&...) = delete;
1007 #endif // C++20
1008 
1009  /// @} relates unique_ptr
1010 #endif // C++14
1011 
1012 #if __cplusplus > 201703L && __cpp_concepts
1013  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1014  // 2948. unique_ptr does not define operator<< for stream output
1015  /// Stream output operator for unique_ptr
1016  template<typename _CharT, typename _Traits, typename _Tp, typename _Dp>
1017  inline basic_ostream<_CharT, _Traits>&
1018  operator<<(basic_ostream<_CharT, _Traits>& __os,
1019  const unique_ptr<_Tp, _Dp>& __p)
1020  requires requires { __os << __p.get(); }
1021  {
1022  __os << __p.get();
1023  return __os;
1024  }
1025 #endif // C++20
1026 
1027  /// @} group pointer_abstractions
1028 
1029 #if __cplusplus >= 201703L
1030  namespace __detail::__variant
1031  {
1032  template<typename> struct _Never_valueless_alt; // see <variant>
1033 
1034  // Provide the strong exception-safety guarantee when emplacing a
1035  // unique_ptr into a variant.
1036  template<typename _Tp, typename _Del>
1037  struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
1038  : std::true_type
1039  { };
1040  } // namespace __detail::__variant
1041 #endif // C++17
1042 
1043 _GLIBCXX_END_NAMESPACE_VERSION
1044 } // namespace
1045 
1046 #endif /* _UNIQUE_PTR_H */
bool operator==(const unique_ptr< _Tp, _Dp > &__x, const unique_ptr< _Up, _Ep > &__y)
Equality operator for unique_ptr objects, compares the owned pointers.
Definition: unique_ptr.h:768
bool operator==(nullptr_t, const unique_ptr< _Tp, _Dp > &__x) noexcept
unique_ptr comparison with nullptr
Definition: unique_ptr.h:782
bool operator>=(const unique_ptr< _Tp, _Dp > &__x, const unique_ptr< _Up, _Ep > &__y)
Relational operator for unique_ptr objects, compares the owned pointers.
Definition: unique_ptr.h:887
bool operator==(const unique_ptr< _Tp, _Dp > &__x, nullptr_t) noexcept
unique_ptr comparison with nullptr
Definition: unique_ptr.h:775
bool operator!=(nullptr_t, const unique_ptr< _Tp, _Dp > &__x) noexcept
unique_ptr comparison with nullptr
Definition: unique_ptr.h:802
bool operator!=(const unique_ptr< _Tp, _Dp > &__x, nullptr_t) noexcept
unique_ptr comparison with nullptr
Definition: unique_ptr.h:796
enable_if< __is_swappable< _Dp >::value >::type swap(unique_ptr< _Tp, _Dp > &__x, unique_ptr< _Tp, _Dp > &__y) noexcept
Definition: unique_ptr.h:753
_MakeUniq< _Tp >::__array make_unique(size_t __num)
std::make_unique for arrays of unknown bound
Definition: unique_ptr.h:982
bool operator>(nullptr_t, const unique_ptr< _Tp, _Dp > &__x)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:877
bool operator>(const unique_ptr< _Tp, _Dp > &__x, nullptr_t)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:868
bool operator>=(const unique_ptr< _Tp, _Dp > &__x, nullptr_t)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:894
bool operator!=(const unique_ptr< _Tp, _Dp > &__x, const unique_ptr< _Up, _Ep > &__y)
Inequality operator for unique_ptr objects, compares the owned pointers.
Definition: unique_ptr.h:789
bool operator>=(nullptr_t, const unique_ptr< _Tp, _Dp > &__x)
unique_ptr comparison with nullptr
Definition: unique_ptr.h:900
_MakeUniq< _Tp >::__invalid_type make_unique(_Args &&...)=delete
Disable std::make_unique for arrays of known bound.
bool operator>(const unique_ptr< _Tp, _Dp > &__x, const unique_ptr< _Up, _Ep > &__y)
Relational operator for unique_ptr objects, compares the owned pointers.
Definition: unique_ptr.h:861
_MakeUniq< _Tp >::__single_object make_unique(_Args &&... __args)
std::make_unique for single objects
Definition: unique_ptr.h:976
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
Definition: type_traits:2014
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition: type_traits:2364
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:422
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:1540
Primary class template hash.
integral_constant
Definition: type_traits:66
Define a member typedef type to one of two argument types.
Definition: type_traits:2227
is_void
Definition: type_traits:272
is_array
Definition: type_traits:429
is_pointer
Definition: type_traits:451
is_lvalue_reference
Definition: type_traits:456
is_same
Definition: type_traits:1410
is_copy_constructible
Definition: type_traits:986
is_move_constructible
Definition: type_traits:1009
is_assignable
Definition: type_traits:1088
is_convertible
Definition: type_traits:1459
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:2200
common_type
Definition: type_traits:2236
One of the comparison functors.
Definition: stl_function.h:382
Primary template of default_delete, used by unique_ptr for single objects.
Definition: unique_ptr.h:64
constexpr default_delete() noexcept=default
Default constructor.
void operator()(_Tp *__ptr) const
Calls delete __ptr
Definition: unique_ptr.h:79
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr
Definition: unique_ptr.h:116
constexpr default_delete() noexcept=default
Default constructor.
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:243
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
Definition: unique_ptr.h:452
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:431
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Definition: unique_ptr.h:461
unique_ptr(pointer __p, const deleter_type &__d) noexcept
Definition: unique_ptr.h:294
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
Definition: unique_ptr.h:340
add_lvalue_reference< element_type >::type operator*() const noexcept(noexcept(*std::declval< pointer >()))
Dereference the stored pointer.
Definition: unique_ptr.h:405
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:269
unique_ptr(unique_ptr &&)=default
Move constructor.
unique_ptr(pointer __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __d) noexcept
Definition: unique_ptr.h:306
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
Definition: unique_ptr.h:395
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:426
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
unique_ptr(pointer __p) noexcept
Definition: unique_ptr.h:281
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
Definition: unique_ptr.h:355
pointer operator->() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:413
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:386
pointer get() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:421
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:320
pointer release() noexcept
Release ownership of any stored pointer.
Definition: unique_ptr.h:442
pointer release() noexcept
Release ownership of any stored pointer.
Definition: unique_ptr.h:701
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:608
void reset(_Up __p) noexcept
Replace the stored pointer.
Definition: unique_ptr.h:723
unique_ptr & operator=(unique_ptr &&)=default
Move assignment operator.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:690
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Definition: unique_ptr.h:731
unique_ptr(_Up __p, const deleter_type &__d) noexcept
Definition: unique_ptr.h:576
std::add_lvalue_reference< element_type >::type operator[](size_t __i) const noexcept(_S_nothrow_deref< pointer, element_type >(0))
Access an element of owned array.
Definition: unique_ptr.h:671
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:544
unique_ptr(_Up __p) noexcept
Definition: unique_ptr.h:561
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:652
unique_ptr(unique_ptr &&)=default
Move constructor.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
Definition: unique_ptr.h:661
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:685
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
Definition: unique_ptr.h:622
pointer get() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:680
unique_ptr(_Up __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __d) noexcept
Definition: unique_ptr.h:590
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:90