1 // unique_ptr implementation -*- C++ -*-
3 // Copyright (C) 2008-2013 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
31 #define _UNIQUE_PTR_H 1
33 #include <bits/c++config.h>
34 #include <debug/debug.h>
35 #include <type_traits>
39 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @addtogroup pointer_abstractions
48 #if _GLIBCXX_USE_DEPRECATED
49 template<typename
> class auto_ptr
;
52 /// Primary template of default_delete, used by unique_ptr
53 template<typename _Tp
>
56 /// Default constructor
57 constexpr default_delete() noexcept
= default;
59 /** @brief Converting constructor.
61 * Allows conversion from a deleter for arrays of another type, @p _Up,
62 * only if @p _Up* is convertible to @p _Tp*.
64 template<typename _Up
, typename
= typename
65 enable_if
<is_convertible
<_Up
*, _Tp
*>::value
>::type
>
66 default_delete(const default_delete
<_Up
>&) noexcept
{ }
68 /// Calls @c delete @p __ptr
70 operator()(_Tp
* __ptr
) const
72 static_assert(sizeof(_Tp
)>0,
73 "can't delete pointer to incomplete type");
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
[]>
85 template<typename _Up
>
86 using __remove_cv
= typename remove_cv
<_Up
>::type
;
88 // Like is_base_of<_Tp, _Up> but false if unqualified types are the same
89 template<typename _Up
>
91 = __and_
< is_base_of
<_Tp
, _Up
>,
92 __not_
<is_same
<__remove_cv
<_Tp
>, __remove_cv
<_Up
>>> >;
95 /// Default constructor
96 constexpr default_delete() noexcept
= default;
98 /** @brief Converting constructor.
100 * Allows conversion from a deleter for arrays of another type, such as
101 * a const-qualified version of @p _Tp.
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.
107 template<typename _Up
, typename
= typename
108 enable_if
<!__is_derived_Tp
<_Up
>::value
>::type
>
109 default_delete(const default_delete
<_Up
[]>&) noexcept
{ }
111 /// Calls @c delete[] @p __ptr
113 operator()(_Tp
* __ptr
) const
115 static_assert(sizeof(_Tp
)>0,
116 "can't delete pointer to incomplete type");
120 template<typename _Up
>
121 typename enable_if
<__is_derived_Tp
<_Up
>::value
>::type
122 operator()(_Up
*) const = delete;
125 /// 20.7.1.2 unique_ptr for single objects.
126 template <typename _Tp
, typename _Dp
= default_delete
<_Tp
> >
129 // use SFINAE to determine whether _Del::pointer exists
132 template<typename _Up
>
133 static typename
_Up::pointer
__test(typename
_Up::pointer
*);
135 template<typename _Up
>
136 static _Tp
* __test(...);
138 typedef typename remove_reference
<_Dp
>::type _Del
;
141 typedef decltype(__test
<_Del
>(0)) type
;
144 typedef std::tuple
<typename
_Pointer::type
, _Dp
> __tuple_type
;
148 typedef typename
_Pointer::type pointer
;
149 typedef _Tp element_type
;
150 typedef _Dp deleter_type
;
154 /// Default constructor, creates a unique_ptr that owns nothing.
155 constexpr unique_ptr() noexcept
157 { static_assert(!is_pointer
<deleter_type
>::value
,
158 "constructed with null function pointer deleter"); }
160 /** Takes ownership of a pointer.
162 * @param __p A pointer to an object of @c element_type
164 * The deleter will be value-initialized.
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"); }
172 /** Takes ownership of a pointer.
174 * @param __p A pointer to an object of @c element_type
175 * @param __d A reference to a deleter.
177 * The deleter will be initialized with @p __d
179 unique_ptr(pointer __p
,
180 typename conditional
<is_reference
<deleter_type
>::value
,
181 deleter_type
, const deleter_type
&>::type __d
) noexcept
184 /** Takes ownership of a pointer.
186 * @param __p A pointer to an object of @c element_type
187 * @param __d An rvalue reference to a deleter.
189 * The deleter will be initialized with @p std::move(__d)
191 unique_ptr(pointer __p
,
192 typename remove_reference
<deleter_type
>::type
&& __d
) noexcept
193 : _M_t(std::move(__p
), std::move(__d
))
194 { static_assert(!std::is_reference
<deleter_type
>::value
,
195 "rvalue deleter bound to reference"); }
197 /// Creates a unique_ptr that owns nothing.
198 constexpr unique_ptr(nullptr_t
) noexcept
: unique_ptr() { }
200 // Move constructors.
202 /// Move constructor.
203 unique_ptr(unique_ptr
&& __u
) noexcept
204 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
206 /** @brief Converting constructor from another type
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.
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
,
217 is_convertible
<_Ep
, _Dp
>>::type
>>
218 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
219 : _M_t(__u
.release(), std::forward
<_Ep
>(__u
.get_deleter()))
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
;
229 /// Destructor, invokes the deleter if the stored pointer is not null.
230 ~unique_ptr() noexcept
232 auto& __ptr
= std::get
<0>(_M_t
);
233 if (__ptr
!= nullptr)
234 get_deleter()(__ptr
);
240 /** @brief Move assignment operator.
242 * @param __u The object to transfer ownership from.
244 * Invokes the deleter first if this object owns a pointer.
247 operator=(unique_ptr
&& __u
) noexcept
249 reset(__u
.release());
250 get_deleter() = std::forward
<deleter_type
>(__u
.get_deleter());
254 /** @brief Assignment from another type.
256 * @param __u The object to transfer ownership from, which owns a
257 * convertible pointer to a non-array object.
259 * Invokes the deleter first if this object owns a pointer.
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
>>
267 operator=(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
269 reset(__u
.release());
270 get_deleter() = std::forward
<_Ep
>(__u
.get_deleter());
274 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
276 operator=(nullptr_t
) noexcept
284 /// Dereference the stored pointer.
285 typename add_lvalue_reference
<element_type
>::type
288 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
292 /// Return the stored pointer.
294 operator->() const noexcept
296 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
300 /// Return the stored pointer.
303 { return std::get
<0>(_M_t
); }
305 /// Return a reference to the stored deleter.
307 get_deleter() noexcept
308 { return std::get
<1>(_M_t
); }
310 /// Return a reference to the stored deleter.
312 get_deleter() const noexcept
313 { return std::get
<1>(_M_t
); }
315 /// Return @c true if the stored pointer is not null.
316 explicit operator bool() const noexcept
317 { return get() == pointer() ? false : true; }
321 /// Release ownership of any stored pointer.
326 std::get
<0>(_M_t
) = pointer();
330 /** @brief Replace the stored pointer.
332 * @param __p The new pointer to store.
334 * The deleter will be invoked if a pointer is already owned.
337 reset(pointer __p
= pointer()) noexcept
340 swap(std::get
<0>(_M_t
), __p
);
341 if (__p
!= pointer())
345 /// Exchange the pointer and deleter with another object.
347 swap(unique_ptr
& __u
) noexcept
350 swap(_M_t
, __u
._M_t
);
353 // Disable copy from lvalue.
354 unique_ptr(const unique_ptr
&) = delete;
355 unique_ptr
& operator=(const unique_ptr
&) = delete;
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
>
365 // use SFINAE to determine whether _Del::pointer exists
368 template<typename _Up
>
369 static typename
_Up::pointer
__test(typename
_Up::pointer
*);
371 template<typename _Up
>
372 static _Tp
* __test(...);
374 typedef typename remove_reference
<_Dp
>::type _Del
;
377 typedef decltype(__test
<_Del
>(0)) type
;
380 typedef std::tuple
<typename
_Pointer::type
, _Dp
> __tuple_type
;
383 template<typename _Up
>
384 using __remove_cv
= typename remove_cv
<_Up
>::type
;
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
>>> >;
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
>>
405 typedef typename
_Pointer::type pointer
;
406 typedef _Tp element_type
;
407 typedef _Dp deleter_type
;
411 /// Default constructor, creates a unique_ptr that owns nothing.
412 constexpr unique_ptr() noexcept
414 { static_assert(!std::is_pointer
<deleter_type
>::value
,
415 "constructed with null function pointer deleter"); }
417 /** Takes ownership of a pointer.
419 * @param __p A pointer to an array of @c element_type
421 * The deleter will be value-initialized.
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"); }
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
>>>
433 unique_ptr(_Up
* __p
) = delete;
435 /** Takes ownership of a pointer.
437 * @param __p A pointer to an array of @c element_type
438 * @param __d A reference to a deleter.
440 * The deleter will be initialized with @p __d
442 unique_ptr(pointer __p
,
443 typename conditional
<is_reference
<deleter_type
>::value
,
444 deleter_type
, const deleter_type
&>::type __d
) noexcept
447 /** Takes ownership of a pointer.
449 * @param __p A pointer to an array of @c element_type
450 * @param __d A reference to a deleter.
452 * The deleter will be initialized with @p std::move(__d)
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"); }
460 /// Move constructor.
461 unique_ptr(unique_ptr
&& __u
) noexcept
462 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
464 /// Creates a unique_ptr that owns nothing.
465 constexpr unique_ptr(nullptr_t
) noexcept
: unique_ptr() { }
467 template<typename _Up
, typename _Ep
,
468 typename
= _Require
<__safe_conversion
<_Up
, _Ep
>,
469 typename conditional
<is_reference
<_Dp
>::value
,
471 is_convertible
<_Ep
, _Dp
>>::type
473 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
474 : _M_t(__u
.release(), std::forward
<_Ep
>(__u
.get_deleter()))
477 /// Destructor, invokes the deleter if the stored pointer is not null.
480 auto& __ptr
= std::get
<0>(_M_t
);
481 if (__ptr
!= nullptr)
482 get_deleter()(__ptr
);
488 /** @brief Move assignment operator.
490 * @param __u The object to transfer ownership from.
492 * Invokes the deleter first if this object owns a pointer.
495 operator=(unique_ptr
&& __u
) noexcept
497 reset(__u
.release());
498 get_deleter() = std::forward
<deleter_type
>(__u
.get_deleter());
502 /** @brief Assignment from another type.
504 * @param __u The object to transfer ownership from, which owns a
505 * convertible pointer to an array object.
507 * Invokes the deleter first if this object owns a pointer.
509 template<typename _Up
, typename _Ep
>
511 enable_if
<__safe_conversion
<_Up
, _Ep
>::value
, unique_ptr
&>::type
512 operator=(unique_ptr
<_Up
, _Ep
>&& __u
) noexcept
514 reset(__u
.release());
515 get_deleter() = std::forward
<_Ep
>(__u
.get_deleter());
519 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
521 operator=(nullptr_t
) noexcept
529 /// Access an element of owned array.
530 typename
std::add_lvalue_reference
<element_type
>::type
531 operator[](size_t __i
) const
533 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
537 /// Return the stored pointer.
540 { return std::get
<0>(_M_t
); }
542 /// Return a reference to the stored deleter.
544 get_deleter() noexcept
545 { return std::get
<1>(_M_t
); }
547 /// Return a reference to the stored deleter.
549 get_deleter() const noexcept
550 { return std::get
<1>(_M_t
); }
552 /// Return @c true if the stored pointer is not null.
553 explicit operator bool() const noexcept
554 { return get() == pointer() ? false : true; }
558 /// Release ownership of any stored pointer.
563 std::get
<0>(_M_t
) = pointer();
567 /** @brief Replace the stored pointer.
569 * @param __p The new pointer to store.
571 * The deleter will be invoked if a pointer is already owned.
574 reset(pointer __p
= pointer()) noexcept
577 swap(std::get
<0>(_M_t
), __p
);
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;
587 /// Exchange the pointer and deleter with another object.
589 swap(unique_ptr
& __u
) noexcept
592 swap(_M_t
, __u
._M_t
);
595 // Disable copy from lvalue.
596 unique_ptr(const unique_ptr
&) = delete;
597 unique_ptr
& operator=(const unique_ptr
&) = delete;
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
603 conditional
<is_reference
<deleter_type
>::value
,
604 deleter_type
, const deleter_type
&>::type
) = delete;
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;
613 template<typename _Tp
, typename _Dp
>
615 swap(unique_ptr
<_Tp
, _Dp
>& __x
,
616 unique_ptr
<_Tp
, _Dp
>& __y
) noexcept
619 template<typename _Tp
, typename _Dp
,
620 typename _Up
, typename _Ep
>
622 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
,
623 const unique_ptr
<_Up
, _Ep
>& __y
)
624 { return __x
.get() == __y
.get(); }
626 template<typename _Tp
, typename _Dp
>
628 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
) noexcept
631 template<typename _Tp
, typename _Dp
>
633 operator==(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
) noexcept
636 template<typename _Tp
, typename _Dp
,
637 typename _Up
, typename _Ep
>
639 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
,
640 const unique_ptr
<_Up
, _Ep
>& __y
)
641 { return __x
.get() != __y
.get(); }
643 template<typename _Tp
, typename _Dp
>
645 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
) noexcept
646 { return (bool)__x
; }
648 template<typename _Tp
, typename _Dp
>
650 operator!=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
) noexcept
651 { return (bool)__x
; }
653 template<typename _Tp
, typename _Dp
,
654 typename _Up
, typename _Ep
>
656 operator<(const unique_ptr
<_Tp
, _Dp
>& __x
,
657 const unique_ptr
<_Up
, _Ep
>& __y
)
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());
665 template<typename _Tp
, typename _Dp
>
667 operator<(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
668 { return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(__x
.get(),
671 template<typename _Tp
, typename _Dp
>
673 operator<(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
674 { return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(nullptr,
677 template<typename _Tp
, typename _Dp
,
678 typename _Up
, typename _Ep
>
680 operator<=(const unique_ptr
<_Tp
, _Dp
>& __x
,
681 const unique_ptr
<_Up
, _Ep
>& __y
)
682 { return !(__y
< __x
); }
684 template<typename _Tp
, typename _Dp
>
686 operator<=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
687 { return !(nullptr < __x
); }
689 template<typename _Tp
, typename _Dp
>
691 operator<=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
692 { return !(__x
< nullptr); }
694 template<typename _Tp
, typename _Dp
,
695 typename _Up
, typename _Ep
>
697 operator>(const unique_ptr
<_Tp
, _Dp
>& __x
,
698 const unique_ptr
<_Up
, _Ep
>& __y
)
699 { return (__y
< __x
); }
701 template<typename _Tp
, typename _Dp
>
703 operator>(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
704 { return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(nullptr,
707 template<typename _Tp
, typename _Dp
>
709 operator>(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
710 { return std::less
<typename unique_ptr
<_Tp
, _Dp
>::pointer
>()(__x
.get(),
713 template<typename _Tp
, typename _Dp
,
714 typename _Up
, typename _Ep
>
716 operator>=(const unique_ptr
<_Tp
, _Dp
>& __x
,
717 const unique_ptr
<_Up
, _Ep
>& __y
)
718 { return !(__x
< __y
); }
720 template<typename _Tp
, typename _Dp
>
722 operator>=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
723 { return !(__x
< nullptr); }
725 template<typename _Tp
, typename _Dp
>
727 operator>=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __x
)
728 { return !(nullptr < __x
); }
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
>>
736 operator()(const unique_ptr
<_Tp
, _Dp
>& __u
) const noexcept
738 typedef unique_ptr
<_Tp
, _Dp
> _UP
;
739 return std::hash
<typename
_UP::pointer
>()(__u
.get());
743 #if __cplusplus > 201103L
744 template<typename _Tp
>
746 { typedef unique_ptr
<_Tp
> __single_object
; };
748 template<typename _Tp
>
749 struct _MakeUniq
<_Tp
[]>
750 { typedef unique_ptr
<_Tp
[]> __array
; };
752 template<typename _Tp
, size_t _Bound
>
753 struct _MakeUniq
<_Tp
[_Bound
]>
754 { struct __invalid_type
{ }; };
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
)...)); }
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
]()); }
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;
774 // @} group pointer_abstractions
776 _GLIBCXX_END_NAMESPACE_VERSION
779 #endif /* _UNIQUE_PTR_H */