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