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