From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1005) id EB7643840C35; Fri, 26 Jun 2020 17:59:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB7643840C35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1593194366; bh=zzBzwAJq3iFqi3coGHHPbEq7/oV8kZis4ZHAc+fCNX0=; h=From:To:Subject:Date:From; b=tzEJwT40BWwU62DGVolZo+++w6+MYMZg1iDVeTPoNQRs1CCcpj+CxR9j6RVwy3vfU UL4CZeNphiKPMd7zctCPdqqzdRRiVm0xuh6r8vH64BiVngXYrzqR7TCOUVFdgNtlCP ZVB5IZl3w0Pz8odndbuh+Et7l4bCyiDEL7VKqJxQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Michael Meissner To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/meissner/heads/work005)] libstdc++: Implement P1972R2 changes to std::variant (PR 95832) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/users/meissner/heads/work005 X-Git-Oldrev: a2c5150e401a9c084f88b2d5576b29ef709dbadb X-Git-Newrev: c98fc4eb3afeda6ad8220d0d79bc1247a92c7c65 Message-Id: <20200626175926.EB7643840C35@sourceware.org> Date: Fri, 26 Jun 2020 17:59:26 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Jun 2020 17:59:27 -0000 https://gcc.gnu.org/g:c98fc4eb3afeda6ad8220d0d79bc1247a92c7c65 commit c98fc4eb3afeda6ad8220d0d79bc1247a92c7c65 Author: Jonathan Wakely Date: Tue Jun 23 10:24:49 2020 +0100 libstdc++: Implement P1972R2 changes to std::variant (PR 95832) G++ implements P1972R2 since r11-1597-0ca22d027ecc and so we no longer need the P0608R3 special case to prevent narrowing conversions to bool. Since non-GNU compilers don't necessarily implment P1972R2 yet, this may cause a regression for those compilers. There is no feature-test macro we can use to detect it though, so we'll have to live with it. libstdc++-v3/ChangeLog: PR libstdc++/95832 * include/std/variant (__detail::__variant::_Build_FUN): Remove partial specialization to prevent narrowing conversions to bool. * testsuite/20_util/variant/compile.cc: Test non-narrowing conversions to bool. * testsuite/20_util/variant/run.cc: Likewise. Diff: --- libstdc++-v3/include/std/variant | 21 ++++----------- libstdc++-v3/testsuite/20_util/variant/compile.cc | 8 ++++++ libstdc++-v3/testsuite/20_util/variant/run.cc | 31 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 258a5fb18bd..6eeb3c80ec2 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -164,7 +164,7 @@ namespace __detail { namespace __variant { - // Returns the first appearence of _Tp in _Types. + // Returns the first appearance of _Tp in _Types. // Returns sizeof...(_Types) if _Tp is not in _Types. template struct __index_of : std::integral_constant {}; @@ -733,10 +733,8 @@ namespace __variant // Helper used to check for valid conversions that don't involve narrowing. template struct _Arr { _Ti _M_x[1]; }; - // Build an imaginary function FUN(Ti) for each alternative type Ti - template, bool>, - typename = void> + // "Build an imaginary function FUN(Ti) for each alternative type Ti" + template struct _Build_FUN { // This function means 'using _Build_FUN::_S_fun;' is valid, @@ -744,24 +742,15 @@ namespace __variant void _S_fun(); }; - // ... for which Ti x[] = {std::forward(t)}; is well-formed, + // "... for which Ti x[] = {std::forward(t)}; is well-formed." template - struct _Build_FUN<_Ind, _Tp, _Ti, false, + struct _Build_FUN<_Ind, _Tp, _Ti, void_t{{std::declval<_Tp>()}})>> { // This is the FUN function for type _Ti, with index _Ind static integral_constant _S_fun(_Ti); }; - // ... and if Ti is cv bool, remove_cvref_t is bool. - template - struct _Build_FUN<_Ind, _Tp, _Ti, true, - enable_if_t, bool>>> - { - // This is the FUN function for when _Ti is cv bool, with index _Ind - static integral_constant _S_fun(_Ti); - }; - template>> struct _Build_FUNs; diff --git a/libstdc++-v3/testsuite/20_util/variant/compile.cc b/libstdc++-v3/testsuite/20_util/variant/compile.cc index a53071c8867..b2b60d1cf10 100644 --- a/libstdc++-v3/testsuite/20_util/variant/compile.cc +++ b/libstdc++-v3/testsuite/20_util/variant/compile.cc @@ -155,6 +155,14 @@ void arbitrary_ctor() static_assert(!is_constructible_v, unsigned>); static_assert(!is_constructible_v, int>); static_assert(!is_constructible_v, void*>); + + // P1957R2 Converting from T* to bool should be considered narrowing + struct ConvertibleToBool + { + operator bool() const { return true; } + }; + static_assert(is_constructible_v, ConvertibleToBool>); + static_assert(is_constructible_v, ConvertibleToBool>); } struct none { none() = delete; }; diff --git a/libstdc++-v3/testsuite/20_util/variant/run.cc b/libstdc++-v3/testsuite/20_util/variant/run.cc index 3e2228a4666..0ac5de25289 100644 --- a/libstdc++-v3/testsuite/20_util/variant/run.cc +++ b/libstdc++-v3/testsuite/20_util/variant/run.cc @@ -139,6 +139,20 @@ void arbitrary_ctor() variant v3 = 0; VERIFY(v3.index() == 1); } + + { + // P1957R2 Converting from T* to bool should be considered narrowing + struct ConvertibleToBool + { + operator bool() const { return true; } + }; + variant v1 = ConvertibleToBool(); + VERIFY(std::get<0>(v1) == true); + variant v2 = ConvertibleToBool(); + VERIFY(std::get<0>(v2) == true); + variant v3 = ConvertibleToBool(); + VERIFY(std::get<1>(v3) == true); + } } struct ThrowingMoveCtorThrowsCopyCtor @@ -226,6 +240,23 @@ void arbitrary_assign() v3 = 0; VERIFY(v3.index() == 1); } + + { + // P1957R2 Converting from T* to bool should be considered narrowing + struct ConvertibleToBool + { + operator bool() const { return true; } + }; + variant v1; + v1 = ConvertibleToBool(); + VERIFY(std::get<0>(v1) == true); + variant v2; + v2 = ConvertibleToBool(); + VERIFY(std::get<0>(v2) == true); + variant v3; + v3 = ConvertibleToBool(); + VERIFY(std::get<1>(v3) == true); + } } void dtor()