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