From: Ville Voutilainen Date: Wed, 21 Sep 2016 16:48:35 +0000 (+0300) Subject: re PR libstdc++/77537 (pair constructors do not properly SFINAE) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f524d5b34aaac95cb4b2ce7126002cd4fa9d5bae;p=gcc.git re PR libstdc++/77537 (pair constructors do not properly SFINAE) PR libstdc++/77537 Implement LWG 2729 for pair. * include/bits/stl_pair.h (_PCC): New. (_ConstructiblePair, _ImplicitlyConvertiblePair): Turn into static member functions of _PCC. (_MoveConstructiblePair, _ImplicitlyMoveConvertiblePair): Likewise. (_PCCP): New. (pair(const _T1&, const _T2&)): Adjust. (_PCCFP): New. (pair(const pair<_U1, _U2>&)): Adjust. (pair(_U1&&, const _T2&)): Likewise. (pair(const _T1&, _U2&&)): Likewise. (pair(_U1&&, _U2&&)): Likewise. (pair(pair<_U1, _U2>&&)): Likewise. (operator=(const pair&)): Make conditionally deleted. (operator=(pair&&)): Make conditionally suppressed. (operator=(const pair<_U1, _U2>&)): Constrain. (operator=(pair<_U1, _U2>&&): Likewise. * include/std/type_traits (__nonesuch): New. * testsuite/20_util/pair/traits.cc: New. From-SVN: r240322 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 3981509f5d9..0c35cd7056b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,26 @@ +2016-09-21 Ville Voutilainen + + PR libstdc++/77537 + Implement LWG 2729 for pair. + * include/bits/stl_pair.h (_PCC): New. + (_ConstructiblePair, _ImplicitlyConvertiblePair): + Turn into static member functions of _PCC. + (_MoveConstructiblePair, _ImplicitlyMoveConvertiblePair): Likewise. + (_PCCP): New. + (pair(const _T1&, const _T2&)): Adjust. + (_PCCFP): New. + (pair(const pair<_U1, _U2>&)): Adjust. + (pair(_U1&&, const _T2&)): Likewise. + (pair(const _T1&, _U2&&)): Likewise. + (pair(_U1&&, _U2&&)): Likewise. + (pair(pair<_U1, _U2>&&)): Likewise. + (operator=(const pair&)): Make conditionally deleted. + (operator=(pair&&)): Make conditionally suppressed. + (operator=(const pair<_U1, _U2>&)): Constrain. + (operator=(pair<_U1, _U2>&&): Likewise. + * include/std/type_traits (__nonesuch): New. + * testsuite/20_util/pair/traits.cc: New. + 2016-09-20 Ville Voutilainen PR libstdc++/77619 diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index 5ff160ac6d1..ef5253823ef 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -88,52 +88,95 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Concept utility functions, reused in conditionally-explicit // constructors. // See PR 70437, don't look at is_constructible or - // is_convertible if the decayed types are the same to + // is_convertible if the types are the same to // avoid querying those properties for incomplete types. - template - constexpr bool _ConstructiblePair() - { - return __and_<__or_::type, - typename decay<_U1>::type>, - is_constructible<_T1, const _U1&>>, - __or_::type, - typename decay<_U2>::type>, - is_constructible<_T2, const _U2&>>>::value; - } - - template - constexpr bool _ImplicitlyConvertiblePair() - { - return __and_<__or_::type, - typename decay<_U1>::type>, - is_convertible>, - __or_::type, - typename decay<_U2>::type>, - is_convertible>>::value; - } - - template - constexpr bool _MoveConstructiblePair() - { - return __and_<__or_::type, - typename decay<_U1>::type>, - is_constructible<_T1, _U1&&>>, - __or_::type, - typename decay<_U2>::type>, - is_constructible<_T2, _U2&&>>>::value; - } - - template - constexpr bool _ImplicitlyMoveConvertiblePair() - { - return __and_<__or_::type, - typename decay<_U1>::type>, - is_convertible<_U1&&, _T1>>, - __or_::type, - typename decay<_U2>::type>, - is_convertible<_U2&&, _T2>>>::value; - } + template + struct _PCC + { + template + static constexpr bool _ConstructiblePair() + { + return __and_, + is_constructible<_T2, const _U2&>>::value; + } + + template + static constexpr bool _ImplicitlyConvertiblePair() + { + return __and_, + is_convertible>::value; + } + + template + static constexpr bool _MoveConstructiblePair() + { + return __and_, + is_constructible<_T2, _U2&&>>::value; + } + template + static constexpr bool _ImplicitlyMoveConvertiblePair() + { + return __and_, + is_convertible<_U2&&, _T2>>::value; + } + + template + static constexpr bool _CopyMovePair() + { + using __do_converts = __and_, + is_convertible<_U2&&, _T2>>; + using __converts = typename conditional<__implicit, + __do_converts, + __not_<__do_converts>>::type; + return __and_, + is_constructible<_T2, _U2&&>, + __converts + >::value; + } + + template + static constexpr bool _MoveCopyPair() + { + using __do_converts = __and_, + is_convertible>; + using __converts = typename conditional<__implicit, + __do_converts, + __not_<__do_converts>>::type; + return __and_, + is_constructible<_T2, const _U2&&>, + __converts + >::value; + } + }; + + template + struct _PCC + { + template + static constexpr bool _ConstructiblePair() + { + return false; + } + + template + static constexpr bool _ImplicitlyConvertiblePair() + { + return false; + } + + template + static constexpr bool _MoveConstructiblePair() + { + return false; + } + + template + static constexpr bool _ImplicitlyMoveConvertiblePair() + { + return false; + } + }; #endif @@ -186,16 +229,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) { } #else + // Shortcut for constraining the templates that don't take pairs. + using _PCCP = _PCC; + template() - && _ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), + enable_if<_PCCP::template + _ConstructiblePair<_U1, _U2>() + && _PCCP::template + _ImplicitlyConvertiblePair<_U1, _U2>(), bool>::type=true> constexpr pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) { } template() - && !_ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), + enable_if<_PCCP::template + _ConstructiblePair<_U1, _U2>() + && !_PCCP::template + _ImplicitlyConvertiblePair<_U1, _U2>(), bool>::type=false> explicit constexpr pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) { } @@ -207,16 +257,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) { } #else + // Shortcut for constraining the templates that take pairs. + template + using _PCCFP = _PCC::value + || !is_same<_T2, _U2>::value, + _T1, _T2>; + template() - && _ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), - bool>::type=true> + enable_if<_PCCFP<_U1, _U2>::template + _ConstructiblePair<_U1, _U2>() + && _PCCFP<_U1, _U2>::template + _ImplicitlyConvertiblePair<_U1, _U2>(), + bool>::type=true> constexpr pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) { } template() - && !_ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), + enable_if<_PCCFP<_U1, _U2>::template + _ConstructiblePair<_U1, _U2>() + && !_PCCFP<_U1, _U2>::template + _ImplicitlyConvertiblePair<_U1, _U2>(), bool>::type=false> explicit constexpr pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) { } @@ -226,75 +286,67 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // DR 811. template() - && _MoveConstructiblePair<_T1, _T2, _U1, _T2>() - && _ImplicitlyConvertiblePair<_T2, _T2, _T2, _T2>() - && _ImplicitlyMoveConvertiblePair<_T1, _T2, - _U1, _T2>(), + enable_if<_PCCP::template + _MoveCopyPair(), bool>::type=true> constexpr pair(_U1&& __x, const _T2& __y) : first(std::forward<_U1>(__x)), second(__y) { } template() - && _MoveConstructiblePair<_T1, _T2, _U1, _T2>() - && (!_ImplicitlyConvertiblePair<_T2, _T2, _T2, _T2>() - || !_ImplicitlyMoveConvertiblePair<_T1, _T2, - _U1, _T2>()), + enable_if<_PCCP::template + _MoveCopyPair(), bool>::type=false> explicit constexpr pair(_U1&& __x, const _T2& __y) : first(std::forward<_U1>(__x)), second(__y) { } template() - && _MoveConstructiblePair<_T1, _T2, _T1, _U2>() - && _ImplicitlyConvertiblePair<_T1, _T1, _T1, _T1>() - && _ImplicitlyMoveConvertiblePair<_T1, _T2, - _T1, _U2>(), + enable_if<_PCCP::template + _CopyMovePair(), bool>::type=true> constexpr pair(const _T1& __x, _U2&& __y) : first(__x), second(std::forward<_U2>(__y)) { } template() - && _MoveConstructiblePair<_T1, _T2, _T1, _U2>() - && (!_ImplicitlyConvertiblePair<_T1, _T1, _T1, _T1>() - || !_ImplicitlyMoveConvertiblePair<_T1, _T2, - _T1, _U2>()), + enable_if<_PCCP::template + _CopyMovePair(), bool>::type=false> explicit pair(const _T1& __x, _U2&& __y) : first(__x), second(std::forward<_U2>(__y)) { } template() - && _ImplicitlyMoveConvertiblePair<_T1, _T2, - _U1, _U2>(), + enable_if<_PCCP::template + _MoveConstructiblePair<_U1, _U2>() + && _PCCP::template + _ImplicitlyMoveConvertiblePair<_U1, _U2>(), bool>::type=true> constexpr pair(_U1&& __x, _U2&& __y) : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } template() - && !_ImplicitlyMoveConvertiblePair<_T1, _T2, - _U1, _U2>(), + enable_if<_PCCP::template + _MoveConstructiblePair<_U1, _U2>() + && !_PCCP::template + _ImplicitlyMoveConvertiblePair<_U1, _U2>(), bool>::type=false> explicit constexpr pair(_U1&& __x, _U2&& __y) : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } template() - && _ImplicitlyMoveConvertiblePair<_T1, _T2, - _U1, _U2>(), + enable_if<_PCCFP<_U1, _U2>::template + _MoveConstructiblePair<_U1, _U2>() + && _PCCFP<_U1, _U2>::template + _ImplicitlyMoveConvertiblePair<_U1, _U2>(), bool>::type=true> constexpr pair(pair<_U1, _U2>&& __p) : first(std::forward<_U1>(__p.first)), second(std::forward<_U2>(__p.second)) { } template() - && !_ImplicitlyMoveConvertiblePair<_T1, _T2, - _U1, _U2>(), + enable_if<_PCCFP<_U1, _U2>::template + _MoveConstructiblePair<_U1, _U2>() + && !_PCCFP<_U1, _U2>::template + _ImplicitlyMoveConvertiblePair<_U1, _U2>(), bool>::type=false> explicit constexpr pair(pair<_U1, _U2>&& __p) : first(std::forward<_U1>(__p.first)), @@ -304,7 +356,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); pair& - operator=(const pair& __p) + operator=(typename conditional< + __and_, + is_copy_assignable<_T2>>::value, + const pair&, const __nonesuch&>::type __p) { first = __p.first; second = __p.second; @@ -312,7 +367,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } pair& - operator=(pair&& __p) + operator=(typename conditional< + __not_<__and_, + is_copy_assignable<_T2>>>::value, + const pair&, const __nonesuch&>::type __p) = delete; + + pair& + operator=(typename conditional< + __and_, + is_move_assignable<_T2>>::value, + pair&&, __nonesuch&&>::type __p) noexcept(__and_, is_nothrow_move_assignable<_T2>>::value) { @@ -322,7 +386,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template - pair& + typename enable_if<__and_, + is_assignable<_T2&, const _U2&>>::value, + pair&>::type operator=(const pair<_U1, _U2>& __p) { first = __p.first; @@ -331,7 +397,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template - pair& + typename enable_if<__and_, + is_assignable<_T2&, _U2&&>>::value, + pair&>::type operator=(pair<_U1, _U2>&& __p) { first = std::forward<_U1>(__p.first); diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 5085196f654..cd0ba99dcb8 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -2850,6 +2850,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __call_is_nothrow, _Fn, _Args...>>::type { }; + struct __nonesuch { + __nonesuch() = delete; + ~__nonesuch() = delete; + __nonesuch(__nonesuch const&) = delete; + void operator=(__nonesuch const&) = delete; + }; + #if __cplusplus > 201402L # define __cpp_lib_is_callable 201603 diff --git a/libstdc++-v3/testsuite/20_util/pair/traits.cc b/libstdc++-v3/testsuite/20_util/pair/traits.cc new file mode 100644 index 00000000000..850f898a414 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pair/traits.cc @@ -0,0 +1,76 @@ +// { dg-do compile { target c++11 } } + +// 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 + +#include +#include + +using namespace std; + +struct Poison +{ + Poison(Poison&&) = delete; +}; + +struct ThrowingCopy +{ + ThrowingCopy(const ThrowingCopy&) {} + ThrowingCopy& operator=(const ThrowingCopy&) {} +}; + +int main() +{ + static_assert(!is_copy_constructible::value, ""); + static_assert(!is_move_constructible::value, ""); + static_assert(!is_copy_assignable::value, ""); + static_assert(!is_move_assignable::value, ""); + static_assert(!is_copy_constructible>::value, + ""); + static_assert(!is_move_constructible>::value, + ""); + static_assert(!is_copy_assignable>::value, ""); + static_assert(!is_move_assignable>::value, ""); + static_assert(!is_constructible&, + std::pair&>::value, ""); + static_assert(!is_assignable&, + std::pair&>::value, ""); + static_assert(!is_constructible&, + std::pair>::value, ""); + static_assert(!is_assignable&, + std::pair>::value, ""); + static_assert(!is_copy_constructible>>::value, + ""); + static_assert(is_move_constructible>>::value, + ""); + static_assert(!is_nothrow_move_constructible>>::value, + ""); + static_assert(!is_copy_assignable>>::value, + ""); + static_assert(is_move_assignable>>::value, + ""); + static_assert(!is_nothrow_move_assignable>>::value, + ""); +}