diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 2ea1daed94f0..7a6db7abd26d 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -630,7 +630,7 @@ template #include #include #include -#include +#include // needed to provide swap_ranges. #include #include #include diff --git a/libcxx/include/array b/libcxx/include/array index f0350ea216e0..719286d52ea3 100644 --- a/libcxx/include/array +++ b/libcxx/include/array @@ -34,7 +34,7 @@ struct array // No explicit construct/copy/destroy for aggregate type void fill(const T& u); - void swap(array& a) noexcept(noexcept(swap(declval(), declval()))); + void swap(array& a) noexcept(is_nothrow_swappable_v); // iterators: iterator begin() noexcept; @@ -141,8 +141,15 @@ struct _LIBCPP_TYPE_VIS_ONLY array _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {_VSTD::fill_n(__elems_, _Size, __u);} _LIBCPP_INLINE_VISIBILITY - void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) - {_VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);} + void swap(array& __a) _NOEXCEPT_(_Size == 0 || __is_nothrow_swappable<_Tp>::value) + { __swap_dispatch((std::integral_constant()), __a); } + + _LIBCPP_INLINE_VISIBILITY + void __swap_dispatch(std::true_type, array&) {} + + _LIBCPP_INLINE_VISIBILITY + void __swap_dispatch(std::false_type, array& __a) + { _VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);} // iterators: _LIBCPP_INLINE_VISIBILITY @@ -276,11 +283,12 @@ template inline _LIBCPP_INLINE_VISIBILITY typename enable_if < + _Size == 0 || __is_swappable<_Tp>::value, void >::type swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) - _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) + _NOEXCEPT_(noexcept(__x.swap(__y))) { __x.swap(__y); } diff --git a/libcxx/include/map b/libcxx/include/map index d04b803ce672..8dc84e73cbf2 100644 --- a/libcxx/include/map +++ b/libcxx/include/map @@ -162,7 +162,7 @@ public: void swap(map& m) noexcept(allocator_traits::is_always_equal::value && - __is_nothrow_swappable::value); // C++17 + is_nothrow_swappable::value); // C++17 // observers: allocator_type get_allocator() const noexcept; @@ -357,7 +357,7 @@ public: void swap(multimap& m) noexcept(allocator_traits::is_always_equal::value && - __is_nothrow_swappable::value); // C++17 + is_nothrow_swappable::value); // C++17 // observers: allocator_type get_allocator() const noexcept; diff --git a/libcxx/include/memory b/libcxx/include/memory index e59fd5d38a41..788830329d63 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -2974,7 +2974,10 @@ private: template inline _LIBCPP_INLINE_VISIBILITY -void +typename enable_if< + __is_swappable<_Dp>::value, + void +>::type swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} template diff --git a/libcxx/include/queue b/libcxx/include/queue index 81b83a7701ea..2509b93ede66 100644 --- a/libcxx/include/queue +++ b/libcxx/include/queue @@ -66,7 +66,7 @@ public: template void emplace(Args&&... args); void pop(); - void swap(queue& q) noexcept(noexcept(swap(c, q.c))); + void swap(queue& q) noexcept(is_nothrow_swappable_v) }; template @@ -153,7 +153,8 @@ public: void pop(); void swap(priority_queue& q) - noexcept(noexcept(swap(c, q.c)) && noexcept(swap(comp.q.comp))); + noexcept(is_nothrow_swappable_v && + is_nothrow_swappable_v) }; template @@ -369,7 +370,10 @@ operator<=(const queue<_Tp, _Container>& __x,const queue<_Tp, _Container>& __y) template inline _LIBCPP_INLINE_VISIBILITY -void +typename enable_if< + __is_swappable<_Container>::value, + void +>::type swap(queue<_Tp, _Container>& __x, queue<_Tp, _Container>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) { @@ -700,7 +704,11 @@ priority_queue<_Tp, _Container, _Compare>::swap(priority_queue& __q) template inline _LIBCPP_INLINE_VISIBILITY -void +typename enable_if< + __is_swappable<_Container>::value + && __is_swappable<_Compare>::value, + void +>::type swap(priority_queue<_Tp, _Container, _Compare>& __x, priority_queue<_Tp, _Container, _Compare>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) diff --git a/libcxx/include/stack b/libcxx/include/stack index 64fd65215d99..48b3b0d16ec2 100644 --- a/libcxx/include/stack +++ b/libcxx/include/stack @@ -58,7 +58,7 @@ public: template void emplace(Args&&... args); void pop(); - void swap(stack& c) noexcept(noexcept(swap(c, q.c))); + void swap(stack& c) noexcept(is_nothrow_swappable_v) }; template @@ -275,7 +275,10 @@ operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) template inline _LIBCPP_INLINE_VISIBILITY -void +typename enable_if< + __is_swappable<_Container>::value, + void +>::type swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) { diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index 1ebdafbc69d3..99dd679b2829 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -105,6 +105,8 @@ namespace std template struct is_assignable; template struct is_copy_assignable; template struct is_move_assignable; + template struct is_swappable_with; // C++17 + template struct is_swappable; // C++17 template struct is_destructible; template struct is_trivially_constructible; @@ -123,6 +125,8 @@ namespace std template struct is_nothrow_assignable; template struct is_nothrow_copy_assignable; template struct is_nothrow_move_assignable; + template struct is_nothrow_swappable_with; // C++17 + template struct is_nothrow_swappable; // C++17 template struct is_nothrow_destructible; template struct has_virtual_destructor; @@ -300,6 +304,10 @@ namespace std = is_copy_assignable::value; // C++17 template constexpr bool is_move_assignable_v = is_move_assignable::value; // C++17 + template constexpr bool is_swappable_with_v + = is_swappable_with::value; // C++17 + template constexpr bool is_swappable_v + = is_swappable::value; // C++17 template constexpr bool is_destructible_v = is_destructible::value; // C++17 template constexpr bool is_trivially_constructible_v @@ -332,6 +340,10 @@ namespace std = is_nothrow_copy_assignable::value; // C++17 template constexpr bool is_nothrow_move_assignable_v = is_nothrow_move_assignable::value; // C++17 + template constexpr bool is_nothrow_swappable_with_v + = is_nothrow_swappable_with::value; // C++17 + template constexpr bool is_nothrow_swappable_v + = is_nothrow_swappable::value; // C++17 template constexpr bool is_nothrow_destructible_v = is_nothrow_destructible::value; // C++17 template constexpr bool has_virtual_destructor_v @@ -4421,6 +4433,9 @@ constexpr bool is_nothrow_callable_v = is_nothrow_callable<_Fn, _Ret>::value; #endif // !defined(_LIBCPP_CXX03_LANG) +template struct __is_swappable; +template struct __is_nothrow_swappable; + template inline _LIBCPP_INLINE_VISIBILITY #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE @@ -4440,6 +4455,13 @@ swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value && __y = _VSTD::move(__t); } +template +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if< + __is_swappable<_Tp>::value +>::type +swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value); + template inline _LIBCPP_INLINE_VISIBILITY void @@ -4455,55 +4477,103 @@ iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) namespace __detail { - +// ALL generic swap overloads MUST already have a declaration available at this point. using _VSTD::swap; __nat swap(__any, __any); -template -struct __swappable +template ::value && !is_void<_Up>::value> +struct __swappable_with { - typedef decltype(swap(_VSTD::declval<_Tp&>(), _VSTD::declval<_Tp&>())) type; - static const bool value = !is_same::value; + typedef decltype(swap(_VSTD::declval<_Tp>(), _VSTD::declval<_Up>())) __swap1; + typedef decltype(swap(_VSTD::declval<_Up>(), _VSTD::declval<_Tp>())) __swap2; + + static const bool value = !is_same<__swap1, __nat>::value + && !is_same<__swap2, __nat>::value; }; +template +struct __swappable_with<_Tp, _Up, false> : false_type {}; + +template ::value> +struct __nothrow_swappable_with { + static const bool value = +#ifndef _LIBCPP_HAS_NO_NOEXCEPT + noexcept(swap(_VSTD::declval<_Tp>(), _VSTD::declval<_Up>())) + && noexcept(swap(_VSTD::declval<_Up>(), _VSTD::declval<_Tp>())); +#else + false; +#endif +}; + +template +struct __nothrow_swappable_with<_Tp, _Up, false> : false_type {}; + } // __detail template struct __is_swappable - : public integral_constant::value> -{ -}; - -#if __has_feature(cxx_noexcept) || (_GNUC_VER >= 407 && __cplusplus >= 201103L) - -template -struct __is_nothrow_swappable_imp - : public integral_constant(), - _VSTD::declval<_Tp&>()))> -{ -}; - -template -struct __is_nothrow_swappable_imp - : public false_type + : public integral_constant::value> { }; template struct __is_nothrow_swappable - : public __is_nothrow_swappable_imp<__is_swappable<_Tp>::value, _Tp> + : public integral_constant::value> { }; -#else // __has_feature(cxx_noexcept) +#if _LIBCPP_STD_VER > 14 + +template +struct _LIBCPP_TYPE_VIS_ONLY is_swappable_with + : public integral_constant::value> +{ +}; template -struct __is_nothrow_swappable - : public false_type +struct _LIBCPP_TYPE_VIS_ONLY is_swappable + : public conditional< + __is_referenceable<_Tp>::value, + is_swappable_with< + typename add_lvalue_reference<_Tp>::type, + typename add_lvalue_reference<_Tp>::type>, + false_type + >::type { }; -#endif // __has_feature(cxx_noexcept) +template +struct _LIBCPP_TYPE_VIS_ONLY is_nothrow_swappable_with + : public integral_constant::value> +{ +}; + +template +struct _LIBCPP_TYPE_VIS_ONLY is_nothrow_swappable + : public conditional< + __is_referenceable<_Tp>::value, + is_nothrow_swappable_with< + typename add_lvalue_reference<_Tp>::type, + typename add_lvalue_reference<_Tp>::type>, + false_type + >::type +{ +}; + +template +constexpr bool is_swappable_with_v = is_swappable_with<_Tp, _Up>::value; + +template +constexpr bool is_swappable_v = is_swappable<_Tp>::value; + +template +constexpr bool is_nothrow_swappable_with_v = is_nothrow_swappable_with<_Tp, _Up>::value; + +template +constexpr bool is_nothrow_swappable_v = is_nothrow_swappable<_Tp>::value; + +#endif // _LIBCPP_STD_VER > 14 #ifdef _LIBCPP_UNDERLYING_TYPE diff --git a/libcxx/include/utility b/libcxx/include/utility index e9db2383a04a..27b81a0305e5 100644 --- a/libcxx/include/utility +++ b/libcxx/include/utility @@ -82,8 +82,8 @@ struct pair is_nothrow_move_assignable::value); template pair& operator=(pair&& p); - void swap(pair& p) noexcept(noexcept(swap(first, p.first)) && - noexcept(swap(second, p.second))); + void swap(pair& p) noexcept(is_nothrow_swappable_v && + is_nothrow_swappable_v); }; template bool operator==(const pair&, const pair&); // constexpr in C++14 @@ -225,10 +225,6 @@ operator>=(const _Tp& __x, const _Tp& __y) // swap_ranges -// forward -template -inline _LIBCPP_INLINE_VISIBILITY -void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value); template inline _LIBCPP_INLINE_VISIBILITY @@ -240,9 +236,12 @@ swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardItera return __first2; } +// forward declared in template inline _LIBCPP_INLINE_VISIBILITY -void +typename enable_if< + __is_swappable<_Tp>::value +>::type swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) { _VSTD::swap_ranges(__a, __a + _Np, __b); diff --git a/libcxx/test/std/containers/sequences/array/array.special/swap.pass.cpp b/libcxx/test/std/containers/sequences/array/array.special/swap.pass.cpp index c1b0b235ab34..d8532d7ba7fe 100644 --- a/libcxx/test/std/containers/sequences/array/array.special/swap.pass.cpp +++ b/libcxx/test/std/containers/sequences/array/array.special/swap.pass.cpp @@ -14,10 +14,28 @@ #include #include +#include "test_macros.h" // std::array is explicitly allowed to be initialized with A a = { init-list };. // Disable the missing braces warning for this reason. #include "disable_missing_braces_warning.h" +struct NonSwappable { + NonSwappable() {} +private: + NonSwappable(NonSwappable const&); + NonSwappable& operator=(NonSwappable const&); +}; + +template +decltype(swap(std::declval(), std::declval())) +can_swap_imp(int); + +template +std::false_type can_swap_imp(...); + +template +struct can_swap : std::is_same(0)), void> {}; + int main() { { @@ -44,4 +62,17 @@ int main() assert(c1.size() == 0); assert(c2.size() == 0); } + { + typedef NonSwappable T; + typedef std::array C1; + typedef std::array C0; + static_assert(!can_swap::value, ""); + static_assert(can_swap::value, ""); + C0 l = {}; + C0 r = {}; + swap(l, r); +#if TEST_STD_VER >= 11 + static_assert(noexcept(swap(l, r)), ""); +#endif + } } diff --git a/libcxx/test/std/containers/sequences/array/array.swap/swap.pass.cpp b/libcxx/test/std/containers/sequences/array/array.swap/swap.pass.cpp index 91ebe419a0af..8d01dbf959ce 100644 --- a/libcxx/test/std/containers/sequences/array/array.swap/swap.pass.cpp +++ b/libcxx/test/std/containers/sequences/array/array.swap/swap.pass.cpp @@ -15,10 +15,19 @@ #include #include +#include "test_macros.h" + // std::array is explicitly allowed to be initialized with A a = { init-list };. // Disable the missing braces warning for this reason. #include "disable_missing_braces_warning.h" +struct NonSwappable { + NonSwappable() {} +private: + NonSwappable(NonSwappable const&); + NonSwappable& operator=(NonSwappable const&); +}; + int main() { { @@ -70,5 +79,15 @@ int main() assert(c1.size() == 0); assert(c2.size() == 0); } + { + typedef NonSwappable T; + typedef std::array C0; + C0 l = {}; + C0 r = {}; + l.swap(r); +#if TEST_STD_VER >= 11 + static_assert(noexcept(l.swap(r)), ""); +#endif + } } diff --git a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.special/swap.pass.cpp b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.special/swap.pass.cpp index 44b746fbcfd8..c525137d4841 100644 --- a/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.special/swap.pass.cpp +++ b/libcxx/test/std/utilities/memory/unique.ptr/unique.ptr.special/swap.pass.cpp @@ -16,6 +16,7 @@ #include #include +#include "test_macros.h" #include "../deleter.h" struct A @@ -34,6 +35,16 @@ struct A int A::count = 0; +template +struct NonSwappableDeleter { + explicit NonSwappableDeleter(int) {} + NonSwappableDeleter& operator=(NonSwappableDeleter const&) { return *this; } + void operator()(T*) const {} +private: + NonSwappableDeleter(NonSwappableDeleter const&); + +}; + int main() { { @@ -74,4 +85,18 @@ int main() assert(A::count == 6); } assert(A::count == 0); +#if TEST_STD_VER >= 11 + { + // test that unique_ptr's specialized swap is disabled when the deleter + // is non-swappable. Instead we should pick up the generic swap(T, T) + // and perform 3 move constructions. + typedef NonSwappableDeleter D; + D d(42); + int x = 42; + int y = 43; + std::unique_ptr p(&x, d); + std::unique_ptr p2(&y, d); + std::swap(p, p2); + } +#endif } diff --git a/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_swappable.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_swappable.pass.cpp new file mode 100644 index 000000000000..2465987355bc --- /dev/null +++ b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_swappable.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// type_traits + +// is_swappable + +#include +#include +#include "test_macros.h" + +namespace MyNS { + +// Make the test types non-copyable so that generic std::swap is not valid. +struct A { + A(A const&) = delete; + A& operator=(A const&) = delete; +}; + +struct B { + B(B const&) = delete; + B& operator=(B const&) = delete; +}; + +void swap(A&, A&) noexcept {} +void swap(B&, B&) {} + +struct M { + M(M const&) = delete; + M& operator=(M const&) = delete; +}; + +void swap(M&&, M&&) noexcept {} + +struct ThrowingMove { + ThrowingMove(ThrowingMove&&){} + ThrowingMove& operator=(ThrowingMove&&) {} +}; + +} // namespace MyNS + +int main() +{ + using namespace MyNS; + { + // Test that is_swappable applies an lvalue reference to the type. + static_assert(std::is_nothrow_swappable::value, ""); + static_assert(std::is_nothrow_swappable::value, ""); + static_assert(!std::is_nothrow_swappable::value, ""); + static_assert(!std::is_nothrow_swappable::value, ""); + } + { + // Test that it correctly deduces the noexcept of swap. + static_assert(std::is_nothrow_swappable::value, ""); + static_assert(!std::is_nothrow_swappable::value + && std::is_swappable::value, ""); + static_assert(!std::is_nothrow_swappable::value + && std::is_swappable::value); + } + { + // Test that it doesn't drop the qualifiers + static_assert(!std::is_nothrow_swappable::value, ""); + } + { + // test non-referenceable types + static_assert(!std::is_nothrow_swappable::value, ""); + static_assert(!std::is_nothrow_swappable::value, ""); + static_assert(!std::is_nothrow_swappable::value, ""); + } + { + // test for presence of is_nothrow_swappable_v + static_assert(std::is_nothrow_swappable_v); + static_assert(!std::is_nothrow_swappable_v); + } +} diff --git a/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_swappable_with.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_swappable_with.pass.cpp new file mode 100644 index 000000000000..54c0a0403988 --- /dev/null +++ b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_swappable_with.pass.cpp @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// type_traits + +// is_nothrow_swappable_with + +#include +#include +#include "test_macros.h" + +namespace MyNS { + +struct A { + A(A const&) = delete; + A& operator=(A const&) = delete; +}; + +struct B { + B(B const&) = delete; + B& operator=(B const&) = delete; +}; + +struct C {}; +struct D {}; + +void swap(A&, A&) {} + +void swap(A&, B&) noexcept {} +void swap(B&, A&) noexcept {} + +void swap(A&, C&) noexcept {} +void swap(C&, A&) {} + +struct M {}; + +void swap(M&&, M&&) noexcept {} + +} // namespace MyNS + +int main() +{ + using namespace MyNS; + { + // Test that is_swappable_with doesn't apply an lvalue reference + // to the type. Instead it is up to the user. + static_assert(!std::is_nothrow_swappable_with::value, ""); + static_assert(std::is_nothrow_swappable_with::value, ""); + static_assert(std::is_nothrow_swappable_with::value, ""); + static_assert(std::is_swappable_with::value && + !std::is_nothrow_swappable_with::value, ""); + } + { + // test that hetrogenius swap is allowed only if both 'swap(A, B)' and + // 'swap(B, A)' are valid. + static_assert(std::is_nothrow_swappable_with::value, ""); + static_assert(!std::is_nothrow_swappable_with::value && + std::is_swappable_with::value, ""); + static_assert(!std::is_nothrow_swappable_with::value, ""); + } + { + // test we guard against cv void inputs as required. + static_assert(!std::is_nothrow_swappable_with_v); + static_assert(!std::is_nothrow_swappable_with_v); + static_assert(!std::is_nothrow_swappable_with_v); + + } + { + // test for presense of is_nothrow_swappable_with_v + static_assert(std::is_nothrow_swappable_with_v); + static_assert(!std::is_nothrow_swappable_with_v); + } +} diff --git a/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable.pass.cpp new file mode 100644 index 000000000000..43a0a4fe46b1 --- /dev/null +++ b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// type_traits + +// is_swappable + +#include +#include +#include +#include "test_macros.h" + +namespace MyNS { + +// Make the test types non-copyable so that generic std::swap is not valid. +struct A { + A(A const&) = delete; + A& operator=(A const&) = delete; +}; + +struct B { + B(B const&) = delete; + B& operator=(B const&) = delete; +}; + +struct C {}; +struct D {}; + +void swap(A&, A&) {} + +void swap(A&, B&) {} +void swap(B&, A&) {} + +void swap(A&, C&) {} // missing swap(C, A) +void swap(D&, C&) {} + +struct M { + M(M const&) = delete; + M& operator=(M const&) = delete; +}; + +void swap(M&&, M&&) {} + +} // namespace MyNS + +int main() +{ + using namespace MyNS; + { + // Test that is_swappable applies an lvalue reference to the type. + static_assert(std::is_swappable::value, ""); + static_assert(std::is_swappable::value, ""); + static_assert(!std::is_swappable::value, ""); + static_assert(!std::is_swappable::value, ""); + } + static_assert(!std::is_swappable::value, ""); + static_assert(std::is_swappable::value, ""); + { + // test non-referencable types + static_assert(!std::is_swappable::value, ""); + static_assert(!std::is_swappable::value, ""); + static_assert(!std::is_swappable::value, ""); + } + { + // test for presense of is_swappable_v + static_assert(std::is_swappable_v); + static_assert(!std::is_swappable_v); + } +} diff --git a/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable_include_order.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable_include_order.pass.cpp new file mode 100644 index 000000000000..8bd5614065df --- /dev/null +++ b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable_include_order.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// type_traits + +// is_swappable + +// IMPORTANT: The include order is part of the test. We want to pick up +// the following definitions in this order: +// 1) is_swappable, is_nothrow_swappable +// 2) iter_swap, swap_ranges +// 3) swap(T (&)[N], T(&)[N] +// This test checks that (1) and (2) see forward declarations +// for (3). +#include +#include +#include + +#include "test_macros.h" + +int main() +{ + // Use a builtin type so we don't get ADL lookup. + typedef double T[42][50]; + { + static_assert(std::__is_swappable::value, ""); +#if TEST_STD_VER > 14 + static_assert(std::is_swappable_v); +#endif + } + { + T t1 = {}; + T t2 = {}; + std::iter_swap(t1, t2); + std::swap_ranges(t1, t1 + 42, t2); + } +} diff --git a/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable_with.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable_with.pass.cpp new file mode 100644 index 000000000000..9d470c156dc2 --- /dev/null +++ b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_swappable_with.pass.cpp @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// type_traits + +// is_swappable_with + +#include +#include +#include "test_macros.h" + +namespace MyNS { + +struct A { + A(A const&) = delete; + A& operator=(A const&) = delete; +}; + +struct B { + B(B const&) = delete; + B& operator=(B const&) = delete; +}; + +struct C {}; +struct D {}; + +void swap(A&, A&) {} + +void swap(A&, B&) {} +void swap(B&, A&) {} + +void swap(A&, C&) {} // missing swap(C, A) +void swap(D&, C&) {} + +struct M {}; + +void swap(M&&, M&&) {} + +} // namespace MyNS + +int main() +{ + using namespace MyNS; + { + // Test that is_swappable_with doesn't apply an lvalue reference + // to the type. Instead it is up to the user. + static_assert(!std::is_swappable_with::value, ""); + static_assert(std::is_swappable_with::value, ""); + static_assert(std::is_swappable_with::value, ""); + static_assert(std::is_swappable_with::value, ""); + } + { + // test that heterogeneous swap is allowed only if both 'swap(A, B)' and + // 'swap(B, A)' are valid. + static_assert(std::is_swappable_with::value, ""); + static_assert(!std::is_swappable_with::value, ""); + static_assert(!std::is_swappable_with::value, ""); + } + { + // test that cv void is guarded against as required. + static_assert(!std::is_swappable_with_v); + static_assert(!std::is_swappable_with_v); + static_assert(!std::is_swappable_with_v); + } + { + // test for presence of is_swappable_with_v + static_assert(std::is_swappable_with_v); + static_assert(!std::is_swappable_with_v); + } +} diff --git a/libcxx/test/std/utilities/utility/utility.swap/swap.pass.cpp b/libcxx/test/std/utilities/utility/utility.swap/swap.pass.cpp index 8606611f6603..c9c9f3b52760 100644 --- a/libcxx/test/std/utilities/utility/utility.swap/swap.pass.cpp +++ b/libcxx/test/std/utilities/utility/utility.swap/swap.pass.cpp @@ -16,38 +16,88 @@ #include #include -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES #include + +#include "test_macros.h" + +#if TEST_STD_VER >= 11 +struct CopyOnly { + CopyOnly() {} + CopyOnly(CopyOnly const&) noexcept {} + CopyOnly& operator=(CopyOnly const&) { return *this; } +}; + +struct MoveOnly { + MoveOnly() {} + MoveOnly(MoveOnly&&) {} + MoveOnly& operator=(MoveOnly&&) noexcept { return *this; } +}; + +struct NoexceptMoveOnly { + NoexceptMoveOnly() {} + NoexceptMoveOnly(NoexceptMoveOnly&&) noexcept {} + NoexceptMoveOnly& operator=(NoexceptMoveOnly&&) noexcept { return *this; } +}; + +struct NotMoveConstructible { + NotMoveConstructible& operator=(NotMoveConstructible&&) { return *this; } +private: + NotMoveConstructible(NotMoveConstructible&&); +}; + +struct NotMoveAssignable { + NotMoveAssignable(NotMoveAssignable&&); +private: + NotMoveAssignable& operator=(NotMoveAssignable&&); +}; + +template +auto can_swap_test(int) -> decltype(std::swap(std::declval(), std::declval())); + +template +auto can_swap_test(...) -> std::false_type; + +template +constexpr bool can_swap() { + return std::is_same(0)), void>::value; +} #endif -void -test() -{ - int i = 1; - int j = 2; - std::swap(i, j); - assert(i == 2); - assert(j == 1); -} - -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES - -void -test1() -{ - std::unique_ptr i(new int(1)); - std::unique_ptr j(new int(2)); - std::swap(i, j); - assert(*i == 2); - assert(*j == 1); -} - -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES - int main() { - test(); -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES - test1(); + + { + int i = 1; + int j = 2; + std::swap(i, j); + assert(i == 2); + assert(j == 1); + } +#if TEST_STD_VER >= 11 + { + + std::unique_ptr i(new int(1)); + std::unique_ptr j(new int(2)); + std::swap(i, j); + assert(*i == 2); + assert(*j == 1); + + } + { + // test that the swap + static_assert(can_swap(), ""); + static_assert(can_swap(), ""); + static_assert(can_swap(), ""); + + static_assert(!can_swap(), ""); + static_assert(!can_swap(), ""); + + CopyOnly c; + MoveOnly m; + NoexceptMoveOnly nm; + static_assert(!noexcept(std::swap(c, c)), ""); + static_assert(!noexcept(std::swap(m, m)), ""); + static_assert(noexcept(std::swap(nm, nm)), ""); + } #endif } diff --git a/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp b/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp index b1209c3c3651..ad39934b20ca 100644 --- a/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp +++ b/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp @@ -16,50 +16,86 @@ #include #include -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES #include + +#include "test_macros.h" + + +#if TEST_STD_VER >= 11 +struct CopyOnly { + CopyOnly() {} + CopyOnly(CopyOnly const&) noexcept {} + CopyOnly& operator=(CopyOnly const&) { return *this; } +}; + + +struct NoexceptMoveOnly { + NoexceptMoveOnly() {} + NoexceptMoveOnly(NoexceptMoveOnly&&) noexcept {} + NoexceptMoveOnly& operator=(NoexceptMoveOnly&&) noexcept { return *this; } +}; + +struct NotMoveConstructible { + NotMoveConstructible() {} + NotMoveConstructible& operator=(NotMoveConstructible&&) { return *this; } +private: + NotMoveConstructible(NotMoveConstructible&&); +}; + +template +auto can_swap_test(int) -> decltype(std::swap(std::declval(), std::declval())); + +template +auto can_swap_test(...) -> std::false_type; + +template +constexpr bool can_swap() { + return std::is_same(0)), void>::value; +} #endif -void -test() -{ - int i[3] = {1, 2, 3}; - int j[3] = {4, 5, 6}; - std::swap(i, j); - assert(i[0] == 4); - assert(i[1] == 5); - assert(i[2] == 6); - assert(j[0] == 1); - assert(j[1] == 2); - assert(j[2] == 3); -} - -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES - -void -test1() -{ - std::unique_ptr i[3]; - for (int k = 0; k < 3; ++k) - i[k].reset(new int(k+1)); - std::unique_ptr j[3]; - for (int k = 0; k < 3; ++k) - j[k].reset(new int(k+4)); - std::swap(i, j); - assert(*i[0] == 4); - assert(*i[1] == 5); - assert(*i[2] == 6); - assert(*j[0] == 1); - assert(*j[1] == 2); - assert(*j[2] == 3); -} - -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES int main() { - test(); -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES - test1(); + { + int i[3] = {1, 2, 3}; + int j[3] = {4, 5, 6}; + std::swap(i, j); + assert(i[0] == 4); + assert(i[1] == 5); + assert(i[2] == 6); + assert(j[0] == 1); + assert(j[1] == 2); + assert(j[2] == 3); + } +#if TEST_STD_VER >= 11 + { + std::unique_ptr i[3]; + for (int k = 0; k < 3; ++k) + i[k].reset(new int(k+1)); + std::unique_ptr j[3]; + for (int k = 0; k < 3; ++k) + j[k].reset(new int(k+4)); + std::swap(i, j); + assert(*i[0] == 4); + assert(*i[1] == 5); + assert(*i[2] == 6); + assert(*j[0] == 1); + assert(*j[1] == 2); + assert(*j[2] == 3); + } + { + using CA = CopyOnly[42]; + using MA = NoexceptMoveOnly[42]; + using NA = NotMoveConstructible[42]; + static_assert(can_swap(), ""); + static_assert(can_swap(), ""); + static_assert(!can_swap(), ""); + + CA ca; + MA ma; + static_assert(!noexcept(std::swap(ca, ca)), ""); + static_assert(noexcept(std::swap(ma, ma)), ""); + } #endif } diff --git a/libcxx/www/cxx1z_status.html b/libcxx/www/cxx1z_status.html index 41a6978465ab..d0edaad4d1bf 100644 --- a/libcxx/www/cxx1z_status.html +++ b/libcxx/www/cxx1z_status.html @@ -85,7 +85,7 @@ P0033R1LWGRe-enabling shared_from_thisJacksonville P0005R4LWGAdopt not_fn from Library Fundamentals 2 for C++17Jacksonville P0152R1LWGconstexpr atomic::is_always_lock_freeJacksonvilleComplete3.9 - P0185R1LWGAdding [nothrow-]swappable traitsJacksonville + P0185R1LWGAdding [nothrow-]swappable traitsJacksonvilleComplete3.9 P0253R1LWGFixing a design mistake in the searchers interfaceJacksonvilleComplete3.9 P0025R0LWGAn algorithm to "clamp" a value between a pair of boundary valuesJacksonvilleComplete3.9 P0154R1LWGconstexpr std::hardware_{constructive,destructive}_interference_sizeJacksonville