libstdc++
unique_ptr.h
Go to the documentation of this file.
1 // unique_ptr implementation -*- C++ -*-
2 
3 // Copyright (C) 2008-2018 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
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 arrays of another type, @p _Up,
67  * only if @p _Up* is convertible to @p _Tp*.
68  */
69  template<typename _Up, typename = typename
71  default_delete(const default_delete<_Up>&) noexcept { }
72 
73  /// Calls @c delete @p __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  /// Specialization for arrays, default_delete.
88  template<typename _Tp>
89  struct default_delete<_Tp[]>
90  {
91  public:
92  /// Default constructor
93  constexpr default_delete() noexcept = default;
94 
95  /** @brief Converting constructor.
96  *
97  * Allows conversion from a deleter for arrays of another type, such as
98  * a const-qualified version of @p _Tp.
99  *
100  * Conversions from types derived from @c _Tp are not allowed because
101  * it is unsafe to @c delete[] an array of derived types through a
102  * pointer to the base type.
103  */
104  template<typename _Up, typename = typename
106  default_delete(const default_delete<_Up[]>&) noexcept { }
107 
108  /// Calls @c delete[] @p __ptr
109  template<typename _Up>
111  operator()(_Up* __ptr) const
112  {
113  static_assert(sizeof(_Tp)>0,
114  "can't delete pointer to incomplete type");
115  delete [] __ptr;
116  }
117  };
118 
119  template <typename _Tp, typename _Dp>
120  class __uniq_ptr_impl
121  {
122  template <typename _Up, typename _Ep, typename = void>
123  struct _Ptr
124  {
125  using type = _Up*;
126  };
127 
128  template <typename _Up, typename _Ep>
129  struct
130  _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
131  {
132  using type = typename remove_reference<_Ep>::type::pointer;
133  };
134 
135  public:
136  using _DeleterConstraint = enable_if<
137  __and_<__not_<is_pointer<_Dp>>,
139 
140  using pointer = typename _Ptr<_Tp, _Dp>::type;
141 
142  __uniq_ptr_impl() = default;
143  __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
144 
145  template<typename _Del>
146  __uniq_ptr_impl(pointer __p, _Del&& __d)
147  : _M_t(__p, std::forward<_Del>(__d)) { }
148 
149  pointer& _M_ptr() { return std::get<0>(_M_t); }
150  pointer _M_ptr() const { return std::get<0>(_M_t); }
151  _Dp& _M_deleter() { return std::get<1>(_M_t); }
152  const _Dp& _M_deleter() const { return std::get<1>(_M_t); }
153 
154  private:
155  tuple<pointer, _Dp> _M_t;
156  };
157 
158  /// 20.7.1.2 unique_ptr for single objects.
159  template <typename _Tp, typename _Dp = default_delete<_Tp>>
161  {
162  template <class _Up>
163  using _DeleterConstraint =
164  typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
165 
166  __uniq_ptr_impl<_Tp, _Dp> _M_t;
167 
168  public:
169  using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
170  using element_type = _Tp;
171  using deleter_type = _Dp;
172 
173  // helper template for detecting a safe conversion from another
174  // unique_ptr
175  template<typename _Up, typename _Ep>
176  using __safe_conversion_up = __and_<
178  __not_<is_array<_Up>>
179  >;
180 
181  // Constructors.
182 
183  /// Default constructor, creates a unique_ptr that owns nothing.
184  template <typename _Up = _Dp,
185  typename = _DeleterConstraint<_Up>>
186  constexpr unique_ptr() noexcept
187  : _M_t()
188  { }
189 
190  /** Takes ownership of a pointer.
191  *
192  * @param __p A pointer to an object of @c element_type
193  *
194  * The deleter will be value-initialized.
195  */
196  template <typename _Up = _Dp,
197  typename = _DeleterConstraint<_Up>>
198  explicit
199  unique_ptr(pointer __p) noexcept
200  : _M_t(__p)
201  { }
202 
203  /** Takes ownership of a pointer.
204  *
205  * @param __p A pointer to an object of @c element_type
206  * @param __d A reference to a deleter.
207  *
208  * The deleter will be initialized with @p __d
209  */
210  unique_ptr(pointer __p,
212  deleter_type, const deleter_type&>::type __d) noexcept
213  : _M_t(__p, __d) { }
214 
215  /** Takes ownership of a pointer.
216  *
217  * @param __p A pointer to an object of @c element_type
218  * @param __d An rvalue reference to a deleter.
219  *
220  * The deleter will be initialized with @p std::move(__d)
221  */
222  unique_ptr(pointer __p,
223  typename remove_reference<deleter_type>::type&& __d) noexcept
224  : _M_t(std::move(__p), std::move(__d))
226  "rvalue deleter bound to reference"); }
227 
228  /// Creates a unique_ptr that owns nothing.
229  template <typename _Up = _Dp,
230  typename = _DeleterConstraint<_Up>>
231  constexpr unique_ptr(nullptr_t) noexcept : _M_t() { }
232 
233  // Move constructors.
234 
235  /// Move constructor.
236  unique_ptr(unique_ptr&& __u) noexcept
237  : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
238 
239  /** @brief Converting constructor from another type
240  *
241  * Requires that the pointer owned by @p __u is convertible to the
242  * type of pointer owned by this object, @p __u does not own an array,
243  * and @p __u has a compatible deleter type.
244  */
245  template<typename _Up, typename _Ep, typename = _Require<
246  __safe_conversion_up<_Up, _Ep>,
249  is_convertible<_Ep, _Dp>>::type>>
251  : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
252  { }
253 
254 #if _GLIBCXX_USE_DEPRECATED
255 #pragma GCC diagnostic push
256 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
257  /// Converting constructor from @c auto_ptr
258  template<typename _Up, typename = _Require<
260  unique_ptr(auto_ptr<_Up>&& __u) noexcept;
261 #pragma GCC diagnostic pop
262 #endif
263 
264  /// Destructor, invokes the deleter if the stored pointer is not null.
265  ~unique_ptr() noexcept
266  {
267  auto& __ptr = _M_t._M_ptr();
268  if (__ptr != nullptr)
269  get_deleter()(__ptr);
270  __ptr = pointer();
271  }
272 
273  // Assignment.
274 
275  /** @brief Move assignment operator.
276  *
277  * @param __u The object to transfer ownership from.
278  *
279  * Invokes the deleter first if this object owns a pointer.
280  */
281  unique_ptr&
282  operator=(unique_ptr&& __u) noexcept
283  {
284  reset(__u.release());
285  get_deleter() = std::forward<deleter_type>(__u.get_deleter());
286  return *this;
287  }
288 
289  /** @brief Assignment from another type.
290  *
291  * @param __u The object to transfer ownership from, which owns a
292  * convertible pointer to a non-array object.
293  *
294  * Invokes the deleter first if this object owns a pointer.
295  */
296  template<typename _Up, typename _Ep>
297  typename enable_if< __and_<
298  __safe_conversion_up<_Up, _Ep>,
300  >::value,
301  unique_ptr&>::type
303  {
304  reset(__u.release());
305  get_deleter() = std::forward<_Ep>(__u.get_deleter());
306  return *this;
307  }
308 
309  /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
310  unique_ptr&
311  operator=(nullptr_t) noexcept
312  {
313  reset();
314  return *this;
315  }
316 
317  // Observers.
318 
319  /// Dereference the stored pointer.
320  typename add_lvalue_reference<element_type>::type
321  operator*() const
322  {
323  __glibcxx_assert(get() != pointer());
324  return *get();
325  }
326 
327  /// Return the stored pointer.
328  pointer
329  operator->() const noexcept
330  {
331  _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
332  return get();
333  }
334 
335  /// Return the stored pointer.
336  pointer
337  get() const noexcept
338  { return _M_t._M_ptr(); }
339 
340  /// Return a reference to the stored deleter.
341  deleter_type&
342  get_deleter() noexcept
343  { return _M_t._M_deleter(); }
344 
345  /// Return a reference to the stored deleter.
346  const deleter_type&
347  get_deleter() const noexcept
348  { return _M_t._M_deleter(); }
349 
350  /// Return @c true if the stored pointer is not null.
351  explicit operator bool() const noexcept
352  { return get() == pointer() ? false : true; }
353 
354  // Modifiers.
355 
356  /// Release ownership of any stored pointer.
357  pointer
358  release() noexcept
359  {
360  pointer __p = get();
361  _M_t._M_ptr() = pointer();
362  return __p;
363  }
364 
365  /** @brief Replace the stored pointer.
366  *
367  * @param __p The new pointer to store.
368  *
369  * The deleter will be invoked if a pointer is already owned.
370  */
371  void
372  reset(pointer __p = pointer()) noexcept
373  {
374  using std::swap;
375  swap(_M_t._M_ptr(), __p);
376  if (__p != pointer())
377  get_deleter()(__p);
378  }
379 
380  /// Exchange the pointer and deleter with another object.
381  void
382  swap(unique_ptr& __u) noexcept
383  {
384  using std::swap;
385  swap(_M_t, __u._M_t);
386  }
387 
388  // Disable copy from lvalue.
389  unique_ptr(const unique_ptr&) = delete;
390  unique_ptr& operator=(const unique_ptr&) = delete;
391  };
392 
393  /// 20.7.1.3 unique_ptr for array objects with a runtime length
394  // [unique.ptr.runtime]
395  // _GLIBCXX_RESOLVE_LIB_DEFECTS
396  // DR 740 - omit specialization for array objects with a compile time length
397  template<typename _Tp, typename _Dp>
398  class unique_ptr<_Tp[], _Dp>
399  {
400  template <typename _Up>
401  using _DeleterConstraint =
402  typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
403 
404  __uniq_ptr_impl<_Tp, _Dp> _M_t;
405 
406  template<typename _Up>
407  using __remove_cv = typename remove_cv<_Up>::type;
408 
409  // like is_base_of<_Tp, _Up> but false if unqualified types are the same
410  template<typename _Up>
411  using __is_derived_Tp
412  = __and_< is_base_of<_Tp, _Up>,
413  __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
414 
415  public:
416  using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
417  using element_type = _Tp;
418  using deleter_type = _Dp;
419 
420  // helper template for detecting a safe conversion from another
421  // unique_ptr
422  template<typename _Up, typename _Ep,
423  typename _UPtr = unique_ptr<_Up, _Ep>,
424  typename _UP_pointer = typename _UPtr::pointer,
425  typename _UP_element_type = typename _UPtr::element_type>
426  using __safe_conversion_up = __and_<
431  >;
432 
433  // helper template for detecting a safe conversion from a raw pointer
434  template<typename _Up>
435  using __safe_conversion_raw = __and_<
436  __or_<__or_<is_same<_Up, pointer>,
438  __and_<is_pointer<_Up>,
439  is_same<pointer, element_type*>,
441  typename remove_pointer<_Up>::type(*)[],
442  element_type(*)[]>
443  >
444  >
445  >;
446 
447  // Constructors.
448 
449  /// Default constructor, creates a unique_ptr that owns nothing.
450  template <typename _Up = _Dp,
451  typename = _DeleterConstraint<_Up>>
452  constexpr unique_ptr() noexcept
453  : _M_t()
454  { }
455 
456  /** Takes ownership of a pointer.
457  *
458  * @param __p A pointer to an array of a type safely convertible
459  * to an array of @c element_type
460  *
461  * The deleter will be value-initialized.
462  */
463  template<typename _Up,
464  typename _Vp = _Dp,
465  typename = _DeleterConstraint<_Vp>,
466  typename = typename enable_if<
467  __safe_conversion_raw<_Up>::value, bool>::type>
468  explicit
469  unique_ptr(_Up __p) noexcept
470  : _M_t(__p)
471  { }
472 
473  /** Takes ownership of a pointer.
474  *
475  * @param __p A pointer to an array of a type safely convertible
476  * to an array of @c element_type
477  * @param __d A reference to a deleter.
478  *
479  * The deleter will be initialized with @p __d
480  */
481  template<typename _Up,
482  typename = typename enable_if<
483  __safe_conversion_raw<_Up>::value, bool>::type>
484  unique_ptr(_Up __p,
486  deleter_type, const deleter_type&>::type __d) noexcept
487  : _M_t(__p, __d) { }
488 
489  /** Takes ownership of a pointer.
490  *
491  * @param __p A pointer to an array of a type safely convertible
492  * to an array of @c element_type
493  * @param __d A reference to a deleter.
494  *
495  * The deleter will be initialized with @p std::move(__d)
496  */
497  template<typename _Up,
498  typename = typename enable_if<
499  __safe_conversion_raw<_Up>::value, bool>::type>
500  unique_ptr(_Up __p, typename
501  remove_reference<deleter_type>::type&& __d) noexcept
502  : _M_t(std::move(__p), std::move(__d))
503  { static_assert(!is_reference<deleter_type>::value,
504  "rvalue deleter bound to reference"); }
505 
506  /// Move constructor.
507  unique_ptr(unique_ptr&& __u) noexcept
508  : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
509 
510  /// Creates a unique_ptr that owns nothing.
511  template <typename _Up = _Dp,
512  typename = _DeleterConstraint<_Up>>
513  constexpr unique_ptr(nullptr_t) noexcept : _M_t() { }
514 
515  template<typename _Up, typename _Ep, typename = _Require<
516  __safe_conversion_up<_Up, _Ep>,
517  typename conditional<is_reference<_Dp>::value,
518  is_same<_Ep, _Dp>,
519  is_convertible<_Ep, _Dp>>::type>>
520  unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
521  : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
522  { }
523 
524  /// Destructor, invokes the deleter if the stored pointer is not null.
526  {
527  auto& __ptr = _M_t._M_ptr();
528  if (__ptr != nullptr)
529  get_deleter()(__ptr);
530  __ptr = pointer();
531  }
532 
533  // Assignment.
534 
535  /** @brief Move assignment operator.
536  *
537  * @param __u The object to transfer ownership from.
538  *
539  * Invokes the deleter first if this object owns a pointer.
540  */
541  unique_ptr&
542  operator=(unique_ptr&& __u) noexcept
543  {
544  reset(__u.release());
545  get_deleter() = std::forward<deleter_type>(__u.get_deleter());
546  return *this;
547  }
548 
549  /** @brief Assignment from another type.
550  *
551  * @param __u The object to transfer ownership from, which owns a
552  * convertible pointer to an array object.
553  *
554  * Invokes the deleter first if this object owns a pointer.
555  */
556  template<typename _Up, typename _Ep>
557  typename
560  >::value,
561  unique_ptr&>::type
563  {
564  reset(__u.release());
565  get_deleter() = std::forward<_Ep>(__u.get_deleter());
566  return *this;
567  }
568 
569  /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
570  unique_ptr&
571  operator=(nullptr_t) noexcept
572  {
573  reset();
574  return *this;
575  }
576 
577  // Observers.
578 
579  /// Access an element of owned array.
580  typename std::add_lvalue_reference<element_type>::type
581  operator[](size_t __i) const
582  {
583  __glibcxx_assert(get() != pointer());
584  return get()[__i];
585  }
586 
587  /// Return the stored pointer.
588  pointer
589  get() const noexcept
590  { return _M_t._M_ptr(); }
591 
592  /// Return a reference to the stored deleter.
593  deleter_type&
594  get_deleter() noexcept
595  { return _M_t._M_deleter(); }
596 
597  /// Return a reference to the stored deleter.
598  const deleter_type&
599  get_deleter() const noexcept
600  { return _M_t._M_deleter(); }
601 
602  /// Return @c true if the stored pointer is not null.
603  explicit operator bool() const noexcept
604  { return get() == pointer() ? false : true; }
605 
606  // Modifiers.
607 
608  /// Release ownership of any stored pointer.
609  pointer
610  release() noexcept
611  {
612  pointer __p = get();
613  _M_t._M_ptr() = pointer();
614  return __p;
615  }
616 
617  /** @brief Replace the stored pointer.
618  *
619  * @param __p The new pointer to store.
620  *
621  * The deleter will be invoked if a pointer is already owned.
622  */
623  template <typename _Up,
624  typename = _Require<
625  __or_<is_same<_Up, pointer>,
626  __and_<is_same<pointer, element_type*>,
629  typename remove_pointer<_Up>::type(*)[],
630  element_type(*)[]
631  >
632  >
633  >
634  >>
635  void
636  reset(_Up __p) noexcept
637  {
638  pointer __ptr = __p;
639  using std::swap;
640  swap(_M_t._M_ptr(), __ptr);
641  if (__ptr != nullptr)
642  get_deleter()(__ptr);
643  }
644 
645  void reset(nullptr_t = nullptr) noexcept
646  {
647  reset(pointer());
648  }
649 
650  /// Exchange the pointer and deleter with another object.
651  void
652  swap(unique_ptr& __u) noexcept
653  {
654  using std::swap;
655  swap(_M_t, __u._M_t);
656  }
657 
658  // Disable copy from lvalue.
659  unique_ptr(const unique_ptr&) = delete;
660  unique_ptr& operator=(const unique_ptr&) = delete;
661  };
662 
663  template<typename _Tp, typename _Dp>
664  inline
665 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
666  // Constrained free swap overload, see p0185r1
668 #else
669  void
670 #endif
671  swap(unique_ptr<_Tp, _Dp>& __x,
672  unique_ptr<_Tp, _Dp>& __y) noexcept
673  { __x.swap(__y); }
674 
675 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
676  template<typename _Tp, typename _Dp>
678  swap(unique_ptr<_Tp, _Dp>&,
679  unique_ptr<_Tp, _Dp>&) = delete;
680 #endif
681 
682  template<typename _Tp, typename _Dp,
683  typename _Up, typename _Ep>
684  inline bool
685  operator==(const unique_ptr<_Tp, _Dp>& __x,
686  const unique_ptr<_Up, _Ep>& __y)
687  { return __x.get() == __y.get(); }
688 
689  template<typename _Tp, typename _Dp>
690  inline bool
691  operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
692  { return !__x; }
693 
694  template<typename _Tp, typename _Dp>
695  inline bool
696  operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
697  { return !__x; }
698 
699  template<typename _Tp, typename _Dp,
700  typename _Up, typename _Ep>
701  inline bool
702  operator!=(const unique_ptr<_Tp, _Dp>& __x,
703  const unique_ptr<_Up, _Ep>& __y)
704  { return __x.get() != __y.get(); }
705 
706  template<typename _Tp, typename _Dp>
707  inline bool
708  operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
709  { return (bool)__x; }
710 
711  template<typename _Tp, typename _Dp>
712  inline bool
713  operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
714  { return (bool)__x; }
715 
716  template<typename _Tp, typename _Dp,
717  typename _Up, typename _Ep>
718  inline bool
719  operator<(const unique_ptr<_Tp, _Dp>& __x,
720  const unique_ptr<_Up, _Ep>& __y)
721  {
722  typedef typename
724  typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
725  return std::less<_CT>()(__x.get(), __y.get());
726  }
727 
728  template<typename _Tp, typename _Dp>
729  inline bool
730  operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
732  nullptr); }
733 
734  template<typename _Tp, typename _Dp>
735  inline bool
736  operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
738  __x.get()); }
739 
740  template<typename _Tp, typename _Dp,
741  typename _Up, typename _Ep>
742  inline bool
743  operator<=(const unique_ptr<_Tp, _Dp>& __x,
744  const unique_ptr<_Up, _Ep>& __y)
745  { return !(__y < __x); }
746 
747  template<typename _Tp, typename _Dp>
748  inline bool
749  operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
750  { return !(nullptr < __x); }
751 
752  template<typename _Tp, typename _Dp>
753  inline bool
754  operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
755  { return !(__x < nullptr); }
756 
757  template<typename _Tp, typename _Dp,
758  typename _Up, typename _Ep>
759  inline bool
760  operator>(const unique_ptr<_Tp, _Dp>& __x,
761  const unique_ptr<_Up, _Ep>& __y)
762  { return (__y < __x); }
763 
764  template<typename _Tp, typename _Dp>
765  inline bool
766  operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
768  __x.get()); }
769 
770  template<typename _Tp, typename _Dp>
771  inline bool
772  operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
774  nullptr); }
775 
776  template<typename _Tp, typename _Dp,
777  typename _Up, typename _Ep>
778  inline bool
779  operator>=(const unique_ptr<_Tp, _Dp>& __x,
780  const unique_ptr<_Up, _Ep>& __y)
781  { return !(__x < __y); }
782 
783  template<typename _Tp, typename _Dp>
784  inline bool
785  operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
786  { return !(__x < nullptr); }
787 
788  template<typename _Tp, typename _Dp>
789  inline bool
790  operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
791  { return !(nullptr < __x); }
792 
793  /// std::hash specialization for unique_ptr.
794  template<typename _Tp, typename _Dp>
795  struct hash<unique_ptr<_Tp, _Dp>>
796  : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
797  private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
798  {
799  size_t
800  operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
801  {
802  typedef unique_ptr<_Tp, _Dp> _UP;
803  return std::hash<typename _UP::pointer>()(__u.get());
804  }
805  };
806 
807 #if __cplusplus > 201103L
808 
809 #define __cpp_lib_make_unique 201304
810 
811  template<typename _Tp>
812  struct _MakeUniq
813  { typedef unique_ptr<_Tp> __single_object; };
814 
815  template<typename _Tp>
816  struct _MakeUniq<_Tp[]>
817  { typedef unique_ptr<_Tp[]> __array; };
818 
819  template<typename _Tp, size_t _Bound>
820  struct _MakeUniq<_Tp[_Bound]>
821  { struct __invalid_type { }; };
822 
823  /// std::make_unique for single objects
824  template<typename _Tp, typename... _Args>
825  inline typename _MakeUniq<_Tp>::__single_object
826  make_unique(_Args&&... __args)
827  { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
828 
829  /// std::make_unique for arrays of unknown bound
830  template<typename _Tp>
831  inline typename _MakeUniq<_Tp>::__array
832  make_unique(size_t __num)
833  { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
834 
835  /// Disable std::make_unique for arrays of known bound
836  template<typename _Tp, typename... _Args>
837  inline typename _MakeUniq<_Tp>::__invalid_type
838  make_unique(_Args&&...) = delete;
839 #endif
840 
841  // @} group pointer_abstractions
842 
843 _GLIBCXX_END_NAMESPACE_VERSION
844 } // namespace
845 
846 #endif /* _UNIQUE_PTR_H */
pointer release() noexcept
Release ownership of any stored pointer.
Definition: unique_ptr.h:358
default_delete(const default_delete< _Up > &) noexcept
Converting constructor.
Definition: unique_ptr.h:71
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:186
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
Definition: unique_ptr.h:282
is_pointer
Definition: type_traits:368
Primary class template hash.
Definition: system_error:142
Define a member typedef type to one of two argument types.
Definition: type_traits:92
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
Definition: unique_ptr.h:265
void reset(_Up __p) noexcept
Replace the stored pointer.
Definition: unique_ptr.h:636
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:1913
is_default_constructible
Definition: type_traits:878
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:599
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
Definition: unique_ptr.h:507
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
Definition: unique_ptr.h:321
pointer release() noexcept
Release ownership of any stored pointer.
Definition: unique_ptr.h:610
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:452
pointer get() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:337
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
Definition: unique_ptr.h:111
is_reference
Definition: type_traits:568
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:302
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:347
_MakeUniq< _Tp >::__single_object make_unique(_Args &&... __args)
std::make_unique for single objects
Definition: unique_ptr.h:826
A simple smart pointer providing strict ownership semantics.
Definition: auto_ptr.h:89
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
Definition: unique_ptr.h:525
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:562
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
Definition: unique_ptr.h:106
ISO C++ entities toplevel namespace is std.
is_void
Definition: type_traits:202
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
Definition: unique_ptr.h:222
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
Definition: type_traits:1730
is_convertible
Definition: type_traits:1327
common_type
Definition: type_traits:1937
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
Definition: unique_ptr.h:250
remove_reference
Definition: type_traits:1411
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
Definition: unique_ptr.h:210
std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.
Definition: unique_ptr.h:581
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
Definition: unique_ptr.h:236
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
Definition: unique_ptr.h:372
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:231
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:342
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept
Definition: unique_ptr.h:500
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Definition: unique_ptr.h:594
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
Definition: unique_ptr.h:542
constexpr default_delete() noexcept=default
Default constructor.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
Definition: unique_ptr.h:311
void operator()(_Tp *__ptr) const
Calls delete __ptr.
Definition: unique_ptr.h:75
pointer operator->() const noexcept
Return the stored pointer.
Definition: unique_ptr.h:329
unique_ptr(pointer __p) noexcept
Definition: unique_ptr.h:199
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
Definition: unique_ptr.h:513
unique_ptr(_Up __p) noexcept
Definition: unique_ptr.h:469
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Definition: unique_ptr.h:652
unique_ptr(_Up __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
Definition: unique_ptr.h:484
Primary template of default_delete, used by unique_ptr.
Definition: unique_ptr.h:59
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
Definition: unique_ptr.h:382
One of the comparison functors.
Definition: stl_function.h:340
is_same
Definition: type_traits:1286
is_assignable
Definition: type_traits:1006
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
Definition: unique_ptr.h:571
20.7.1.2 unique_ptr for single objects.
Definition: unique_ptr.h:160
is_array
Definition: type_traits:347