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