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