From fb2bd4a9398b35ee4f732ea0847d9c1226fc4cf3 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sun, 23 Jun 2019 20:47:21 +0000 Subject: [PATCH] Use C++11 implementation of unique_ptr in C++03. llvm-svn: 364161 --- libcxx/include/memory | 208 +++++------------- .../unique.ptr.ctor/pointer_deleter.fail.cpp | 4 - 2 files changed, 51 insertions(+), 161 deletions(-) diff --git a/libcxx/include/memory b/libcxx/include/memory index cabf3d7cd743..d9222b3ada7e 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -548,7 +548,7 @@ template void swap(weak_ptr& a, weak_ptr& b) noexcept; template struct owner_less; template -struct owner_less> +struct owner_less > : binary_function, shared_ptr, bool> { typedef bool result_type; @@ -558,7 +558,7 @@ struct owner_less> }; template -struct owner_less> +struct owner_less > : binary_function, weak_ptr, bool> { typedef bool result_type; @@ -1533,7 +1533,7 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits #ifndef _LIBCPP_CXX03_LANG template using rebind_alloc = typename __allocator_traits_rebind::type; - template using rebind_traits = allocator_traits>; + template using rebind_traits = allocator_traits >; #else // _LIBCPP_CXX03_LANG template struct rebind_alloc {typedef typename __allocator_traits_rebind::type other;}; @@ -2320,7 +2320,7 @@ struct _LIBCPP_TEMPLATE_VIS default_delete { static_assert(!is_function<_Tp>::value, "default_delete cannot be instantiated for function types"); #ifndef _LIBCPP_CXX03_LANG - _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default; + _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default; #else _LIBCPP_INLINE_VISIBILITY default_delete() {} #endif @@ -2348,7 +2348,7 @@ private: public: #ifndef _LIBCPP_CXX03_LANG - _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default; + _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default; #else _LIBCPP_INLINE_VISIBILITY default_delete() {} #endif @@ -2370,9 +2370,6 @@ public: } }; - - -#ifndef _LIBCPP_CXX03_LANG template struct __unique_ptr_deleter_sfinae { static_assert(!is_reference<_Deleter>::value, "incorrect specialization"); @@ -2394,7 +2391,6 @@ struct __unique_ptr_deleter_sfinae<_Deleter&> { typedef _Deleter&& __bad_rval_ref_type; typedef false_type __enable_rval_overload; }; -#endif // !defined(_LIBCPP_CXX03_LANG) template > class _LIBCPP_TEMPLATE_VIS unique_ptr { @@ -2411,7 +2407,6 @@ private: struct __nat { int __for_bool_; }; -#ifndef _LIBCPP_CXX03_LANG typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE; template @@ -2455,42 +2450,42 @@ private: public: template > + class = _EnableIfDeleterDefaultConstructible<_Dummy> > _LIBCPP_INLINE_VISIBILITY - constexpr unique_ptr() noexcept : __ptr_(pointer()) {} + _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {} template > + class = _EnableIfDeleterDefaultConstructible<_Dummy> > _LIBCPP_INLINE_VISIBILITY - constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {} + _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {} template > + class = _EnableIfDeleterDefaultConstructible<_Dummy> > _LIBCPP_INLINE_VISIBILITY - explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {} + explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {} template >> + class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > > _LIBCPP_INLINE_VISIBILITY - unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept + unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT : __ptr_(__p, __d) {} template >> + class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > > _LIBCPP_INLINE_VISIBILITY - unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept + unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT : __ptr_(__p, _VSTD::move(__d)) { static_assert(!is_reference::value, "rvalue deleter bound to reference"); } template >> + class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > > _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete; _LIBCPP_INLINE_VISIBILITY - unique_ptr(unique_ptr&& __u) noexcept + unique_ptr(unique_ptr&& __u) _NOEXCEPT : __ptr_(__u.release(), _VSTD::forward(__u.get_deleter())) { } @@ -2507,7 +2502,7 @@ public: _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p, typename enable_if::value && - is_same<_Dp, default_delete<_Tp>>::value, + is_same<_Dp, default_delete<_Tp> >::value, __nat>::type = __nat()) _NOEXCEPT : __ptr_(__p.release()) {} #endif @@ -2530,60 +2525,6 @@ public: return *this; } -#else // _LIBCPP_CXX03_LANG -private: - unique_ptr(unique_ptr&); - template unique_ptr(unique_ptr<_Up, _Ep>&); - - unique_ptr& operator=(unique_ptr&); - template unique_ptr& operator=(unique_ptr<_Up, _Ep>&); - -public: - _LIBCPP_INLINE_VISIBILITY - unique_ptr() : __ptr_(pointer()) - { - static_assert(!is_pointer::value, - "unique_ptr constructed with null function pointer deleter"); - static_assert(is_default_constructible::value, - "unique_ptr::deleter_type is not default constructible"); - } - _LIBCPP_INLINE_VISIBILITY - unique_ptr(nullptr_t) : __ptr_(pointer()) - { - static_assert(!is_pointer::value, - "unique_ptr constructed with null function pointer deleter"); - } - _LIBCPP_INLINE_VISIBILITY - explicit unique_ptr(pointer __p) - : __ptr_(_VSTD::move(__p)) { - static_assert(!is_pointer::value, - "unique_ptr constructed with null function pointer deleter"); - } - - _LIBCPP_INLINE_VISIBILITY - unique_ptr(unique_ptr&& __u) - : __ptr_(__u.release(), - _VSTD::forward(__u.get_deleter())) {} - - template - _LIBCPP_INLINE_VISIBILITY - typename enable_if< - !is_array<_Up>::value && - is_convertible::pointer, - pointer>::value && - is_assignable::value, - unique_ptr&>::type - operator=(unique_ptr<_Up, _Ep> __u) { - reset(__u.release()); - __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter()); - return *this; - } - - _LIBCPP_INLINE_VISIBILITY - unique_ptr(pointer __p, deleter_type __d) - : __ptr_(__p, _VSTD::forward(__d)) {} -#endif // _LIBCPP_CXX03_LANG - #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) template _LIBCPP_INLINE_VISIBILITY @@ -2596,6 +2537,12 @@ public: } #endif +#ifdef _LIBCPP_CXX03_LANG + unique_ptr(unique_ptr const&) = delete; + unique_ptr& operator=(unique_ptr const&) = delete; +#endif + + _LIBCPP_INLINE_VISIBILITY ~unique_ptr() { reset(); } @@ -2675,7 +2622,6 @@ private: > {}; -#ifndef _LIBCPP_CXX03_LANG typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE; template @@ -2727,67 +2673,67 @@ private: public: template > + class = _EnableIfDeleterDefaultConstructible<_Dummy> > _LIBCPP_INLINE_VISIBILITY - constexpr unique_ptr() noexcept : __ptr_(pointer()) {} + _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {} template > + class = _EnableIfDeleterDefaultConstructible<_Dummy> > _LIBCPP_INLINE_VISIBILITY - constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {} + _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {} template , - class = _EnableIfPointerConvertible<_Pp>> + class = _EnableIfPointerConvertible<_Pp> > _LIBCPP_INLINE_VISIBILITY - explicit unique_ptr(_Pp __p) noexcept + explicit unique_ptr(_Pp __p) _NOEXCEPT : __ptr_(__p) {} template >, - class = _EnableIfPointerConvertible<_Pp>> + class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >, + class = _EnableIfPointerConvertible<_Pp> > _LIBCPP_INLINE_VISIBILITY - unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept + unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT : __ptr_(__p, __d) {} template >> + class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > > _LIBCPP_INLINE_VISIBILITY - unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept + unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT : __ptr_(nullptr, __d) {} template >, - class = _EnableIfPointerConvertible<_Pp>> + class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >, + class = _EnableIfPointerConvertible<_Pp> > _LIBCPP_INLINE_VISIBILITY - unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept + unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT : __ptr_(__p, _VSTD::move(__d)) { static_assert(!is_reference::value, "rvalue deleter bound to reference"); } template >> + class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > > _LIBCPP_INLINE_VISIBILITY - unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept + unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT : __ptr_(nullptr, _VSTD::move(__d)) { static_assert(!is_reference::value, "rvalue deleter bound to reference"); } template >, - class = _EnableIfPointerConvertible<_Pp>> + class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >, + class = _EnableIfPointerConvertible<_Pp> > _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete; _LIBCPP_INLINE_VISIBILITY - unique_ptr(unique_ptr&& __u) noexcept + unique_ptr(unique_ptr&& __u) _NOEXCEPT : __ptr_(__u.release(), _VSTD::forward(__u.get_deleter())) { } _LIBCPP_INLINE_VISIBILITY - unique_ptr& operator=(unique_ptr&& __u) noexcept { + unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT { reset(__u.release()); __ptr_.second() = _VSTD::forward(__u.get_deleter()); return *this; @@ -2798,7 +2744,7 @@ public: class = _EnableIfDeleterConvertible<_Ep> > _LIBCPP_INLINE_VISIBILITY - unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept + unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) { } @@ -2808,68 +2754,16 @@ public: > _LIBCPP_INLINE_VISIBILITY unique_ptr& - operator=(unique_ptr<_Up, _Ep>&& __u) noexcept { + operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT { reset(__u.release()); __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter()); return *this; } -#else // _LIBCPP_CXX03_LANG -private: - template explicit unique_ptr(_Up); - - unique_ptr(unique_ptr&); - template unique_ptr(unique_ptr<_Up>&); - - unique_ptr& operator=(unique_ptr&); - template unique_ptr& operator=(unique_ptr<_Up>&); - - template - unique_ptr(_Up __u, - typename conditional< - is_reference::value, deleter_type, - typename add_lvalue_reference::type>::type, - typename enable_if::value, - __nat>::type = __nat()); -public: - _LIBCPP_INLINE_VISIBILITY - unique_ptr() : __ptr_(pointer()) { - static_assert(!is_pointer::value, - "unique_ptr constructed with null function pointer deleter"); - } - _LIBCPP_INLINE_VISIBILITY - unique_ptr(nullptr_t) : __ptr_(pointer()) { - static_assert(!is_pointer::value, - "unique_ptr constructed with null function pointer deleter"); - } - - _LIBCPP_INLINE_VISIBILITY - explicit unique_ptr(pointer __p) : __ptr_(__p) { - static_assert(!is_pointer::value, - "unique_ptr constructed with null function pointer deleter"); - } - - _LIBCPP_INLINE_VISIBILITY - unique_ptr(pointer __p, deleter_type __d) - : __ptr_(__p, _VSTD::forward(__d)) {} - - _LIBCPP_INLINE_VISIBILITY - unique_ptr(nullptr_t, deleter_type __d) - : __ptr_(pointer(), _VSTD::forward(__d)) {} - - _LIBCPP_INLINE_VISIBILITY - unique_ptr(unique_ptr&& __u) - : __ptr_(__u.release(), - _VSTD::forward(__u.get_deleter())) {} - - _LIBCPP_INLINE_VISIBILITY - unique_ptr& operator=(unique_ptr&& __u) { - reset(__u.release()); - __ptr_.second() = _VSTD::forward(__u.get_deleter()); - return *this; - } - -#endif // _LIBCPP_CXX03_LANG +#ifdef _LIBCPP_CXX03_LANG + unique_ptr(unique_ptr const&) = delete; + unique_ptr& operator=(unique_ptr const&) = delete; +#endif public: _LIBCPP_INLINE_VISIBILITY @@ -3129,7 +3023,7 @@ template struct _LIBCPP_TEMPLATE_VIS hash > #else struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper< - unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>> + unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> > #endif { typedef unique_ptr<_Tp, _Dp> argument_type; diff --git a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/pointer_deleter.fail.cpp b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/pointer_deleter.fail.cpp index ccb4924d0240..486a60367bc0 100644 --- a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/pointer_deleter.fail.cpp +++ b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/pointer_deleter.fail.cpp @@ -6,10 +6,6 @@ // //===----------------------------------------------------------------------===// -// Without rvalue references it is impossible to detect when a rvalue deleter -// is given. -// XFAIL: c++98, c++03 - // // unique_ptr