From: Ville Voutilainen Date: Thu, 1 Dec 2016 16:23:21 +0000 (+0200) Subject: Implement LWG 2766, X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a2863bde755d39626ee25e3b7a8875e0d93d7217;p=gcc.git Implement LWG 2766, Swapping non-swappable types and LWG 2749, swappable traits for variants. * include/bits/move.h (swap(_Tp&, _Tp&)): Constrain with __is_tuple_like. * include/bits/stl_pair.h (swap(pair<_T1, _T2>&, pair<_T1, _T2>&)): Add a deleted overload. * include/bits/unique_ptr.h (swap(unique_ptr<_Tp, _Dp>&, unique_ptr<_Tp, _Dp>&)): Likewise. * include/std/array (swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&)): Likewise. * include/std/optional (swap(optional<_Tp>&, optional<_Tp>&)): Likewise. * include/std/tuple (__is_tuple_like_impl, __is_tuple_like): Move to type_traits. (swap(tuple<_Elements...>&, tuple<_Elements...>&)): Add a deleted overload. * include/std/type_traits (__is_tuple_like_impl, __is_tuple_like): New. (swap(_Tp&, _Tp&)): Constrain with __is_tuple_like. * include/std/utility (__is_tuple_like_impl): Move to type_traits. * include/std/variant (swap(variant<_Types...>&, variant<_Types...>&)): Add a deleted overload. * testsuite/20_util/optional/swap/2.cc: Add tests for disabled swaps. * testsuite/20_util/pair/swap_cxx17.cc: New. * testsuite/20_util/tuple/swap_cxx17.cc: Likewise. * testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc: Likewise. * testsuite/20_util/variant/compile.cc: Add tests for disabled swaps. * testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc: New. * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust. * testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc: Likewise. From-SVN: r243120 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index cc0b3ae0c6a..21404f16bfa 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,43 @@ +2016-12-01 Ville Voutilainen + + Implement LWG 2766, + Swapping non-swappable types and LWG 2749, + swappable traits for variants. + * include/bits/move.h (swap(_Tp&, _Tp&)): Constrain + with __is_tuple_like. + * include/bits/stl_pair.h (swap(pair<_T1, _T2>&, pair<_T1, _T2>&)): + Add a deleted overload. + * include/bits/unique_ptr.h + (swap(unique_ptr<_Tp, _Dp>&, unique_ptr<_Tp, _Dp>&)): Likewise. + * include/std/array + (swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&)): Likewise. + * include/std/optional + (swap(optional<_Tp>&, optional<_Tp>&)): Likewise. + * include/std/tuple (__is_tuple_like_impl, __is_tuple_like): + Move to type_traits. + (swap(tuple<_Elements...>&, tuple<_Elements...>&)): Add a deleted + overload. + * include/std/type_traits (__is_tuple_like_impl, __is_tuple_like): + New. + (swap(_Tp&, _Tp&)): Constrain with __is_tuple_like. + * include/std/utility (__is_tuple_like_impl): Move to type_traits. + * include/std/variant + (swap(variant<_Types...>&, variant<_Types...>&)): + Add a deleted overload. + * testsuite/20_util/optional/swap/2.cc: Add tests for disabled + swaps. + * testsuite/20_util/pair/swap_cxx17.cc: New. + * testsuite/20_util/tuple/swap_cxx17.cc: Likewise. + * testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc: + Likewise. + * testsuite/20_util/variant/compile.cc: Add tests for disabled + swaps. + * testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc: + New. + * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust. + * testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc: + Likewise. + 2016-12-01 Ville Voutilainen The convertible_to traits need to use a variadic catch-all for the diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h index d0aefe76cf0..0bd11d65fa5 100644 --- a/libstdc++-v3/include/bits/move.h +++ b/libstdc++-v3/include/bits/move.h @@ -181,7 +181,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline #if __cplusplus >= 201103L - typename enable_if<__and_, + typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>, + is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value>::type swap(_Tp& __a, _Tp& __b) noexcept(__and_, diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index ef5253823ef..981dbeb6875 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -478,6 +478,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) noexcept(noexcept(__x.swap(__y))) { __x.swap(__y); } + +#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 + template + inline + typename enable_if, + __is_swappable<_T2>>::value>::type + swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete; +#endif #endif // __cplusplus >= 201103L /** diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index f9ec60f769b..03f9bfc7d1f 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -650,6 +650,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION unique_ptr<_Tp, _Dp>& __y) noexcept { __x.swap(__y); } +#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 + template + inline + typename enable_if::value>::type + swap(unique_ptr<_Tp, _Dp>&, + unique_ptr<_Tp, _Dp>&) = delete; +#endif + template inline bool diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 3ab0355cf08..fa7bac6cfbe 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -288,6 +288,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER noexcept(noexcept(__one.swap(__two))) { __one.swap(__two); } +#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 + template + inline + typename enable_if< + !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type + swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete; +#endif + template constexpr _Tp& get(array<_Tp, _Nm>& __arr) noexcept diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index ea673cc6e8b..191d64ba902 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -929,6 +929,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(noexcept(__lhs.swap(__rhs))) { __lhs.swap(__rhs); } + template + inline enable_if_t && is_swappable_v<_Tp>)> + swap(optional<_Tp>&, optional<_Tp>&) = delete; + template constexpr optional> make_optional(_Tp&& __t) diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 63cacd4935c..fb2fd177aee 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -1442,17 +1442,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION forward_as_tuple(_Elements&&... __args) noexcept { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); } - template - struct __is_tuple_like_impl> : true_type - { }; - - // Internal type trait that allows us to sfinae-protect tuple_cat. - template - struct __is_tuple_like - : public __is_tuple_like_impl::type>::type>::type - { }; - template struct __make_tuple_impl; @@ -1597,6 +1586,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(noexcept(__x.swap(__y))) { __x.swap(__y); } +#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 + template + inline + typename enable_if...>::value>::type + swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete; +#endif + // A class (and instance) which can be used in 'tie' when an element // of a tuple is not required struct _Swallow_assign diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index e5f2bbaf745..f164f7158b1 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -2593,9 +2593,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct __is_nothrow_swappable; + template + class tuple; + + template + struct __is_tuple_like_impl : false_type + { }; + + template + struct __is_tuple_like_impl> : true_type + { }; + + // Internal type trait that allows us to sfinae-protect tuple_cat. + template + struct __is_tuple_like + : public __is_tuple_like_impl::type>::type>::type + { }; + template inline - typename enable_if<__and_, + typename enable_if<__and_<__not_<__is_tuple_like<_Tp>>, + is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value>::type swap(_Tp&, _Tp&) noexcept(__and_, diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index 3982156d695..8e02f0e5e7a 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -140,10 +140,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using tuple_element_t = typename tuple_element<__i, _Tp>::type; #endif - template - struct __is_tuple_like_impl : false_type - { }; - // Various functions which give std::pair a tuple-like interface. /// Partial specialization for std::pair diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 34ad3fdefb0..89ca9798609 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -889,10 +889,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return false; } template - inline auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) - noexcept(noexcept(__lhs.swap(__rhs))) -> decltype(__lhs.swap(__rhs)) + inline enable_if_t<__and_..., + is_swappable<_Types>...>::value> + swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs) + noexcept(noexcept(__lhs.swap(__rhs))) { __lhs.swap(__rhs); } + template + inline enable_if_t..., + is_swappable<_Types>...>::value> + swap(variant<_Types...>&, variant<_Types...>&) = delete; + class bad_variant_access : public exception { public: diff --git a/libstdc++-v3/testsuite/20_util/optional/swap/2.cc b/libstdc++-v3/testsuite/20_util/optional/swap/2.cc index 5793488a375..cb9291aada7 100644 --- a/libstdc++-v3/testsuite/20_util/optional/swap/2.cc +++ b/libstdc++-v3/testsuite/20_util/optional/swap/2.cc @@ -33,11 +33,11 @@ void swap(B&, B&) noexcept(false); static_assert( std::is_swappable_v> ); static_assert( !std::is_nothrow_swappable_v> ); -// Not swappable, but optional is swappable via the generic std::swap. +// Not swappable, and optional not swappable via the generic std::swap. struct C { }; void swap(C&, C&) = delete; -static_assert( std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); // Not swappable, and optional not swappable via the generic std::swap. struct D { D(D&&) = delete; }; diff --git a/libstdc++-v3/testsuite/20_util/pair/swap_cxx17.cc b/libstdc++-v3/testsuite/20_util/pair/swap_cxx17.cc new file mode 100644 index 00000000000..6b09f42e7f7 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pair/swap_cxx17.cc @@ -0,0 +1,35 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +#include + +// Not swappable, and pair not swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); + +// Not swappable, and pair not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); diff --git a/libstdc++-v3/testsuite/20_util/tuple/swap_cxx17.cc b/libstdc++-v3/testsuite/20_util/tuple/swap_cxx17.cc new file mode 100644 index 00000000000..d2a75ce1103 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/tuple/swap_cxx17.cc @@ -0,0 +1,43 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +// NOTE: This makes use of the fact that we know how moveable +// is implemented on tuple. If the implementation changed +// this test may begin to fail. + +#include + +// Not swappable, and tuple not swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); + +// Not swappable, and tuple not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc new file mode 100644 index 00000000000..bf106ec8466 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/specialized_algorithms/swap_cxx17.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +// Not swappable, and unique_ptr not swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( !std::is_swappable_v> ); + +// Not swappable, and unique_ptr not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v> ); + diff --git a/libstdc++-v3/testsuite/20_util/variant/compile.cc b/libstdc++-v3/testsuite/20_util/variant/compile.cc index e3330bef928..8250a9522f8 100644 --- a/libstdc++-v3/testsuite/20_util/variant/compile.cc +++ b/libstdc++-v3/testsuite/20_util/variant/compile.cc @@ -219,6 +219,21 @@ void test_relational() } } +// Not swappable, and variant not swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); + +// Not swappable, and variant not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); +static_assert( !std::is_swappable_v> ); + void test_swap() { variant a, b; diff --git a/libstdc++-v3/testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc b/libstdc++-v3/testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc new file mode 100644 index 00000000000..2e93c4d9bd9 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/array/specialized_algorithms/swap_cxx17.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +// Not swappable, and pair not swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( !std::is_swappable_v> ); + +// Not swappable, and pair not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v> ); + diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc index f99bbf6b26b..568ec850a39 100644 --- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc @@ -27,6 +27,6 @@ int n1 = std::get<1>(a); int n2 = std::get<1>(std::move(a)); int n3 = std::get<1>(ca); -// { dg-error "static assertion failed" "" { target *-*-* } 295 } -// { dg-error "static assertion failed" "" { target *-*-* } 304 } +// { dg-error "static assertion failed" "" { target *-*-* } 303 } // { dg-error "static assertion failed" "" { target *-*-* } 312 } +// { dg-error "static assertion failed" "" { target *-*-* } 320 } diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc index 1941f3c1893..32cb10b7dc2 100644 --- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc @@ -22,4 +22,4 @@ typedef std::tuple_element<1, std::array>::type type; -// { dg-error "static assertion failed" "" { target *-*-* } 343 } +// { dg-error "static assertion failed" "" { target *-*-* } 351 }