From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id E8E303858C54; Fri, 2 Sep 2022 15:20:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8E303858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662132018; bh=n9/0yY38KLY+LHEdx/RShCT29MLoVSqS+P9mpiAN+QA=; h=From:To:Subject:Date:From; b=v5Ru7U6nCXi1QOrdC0fejmHy8irnp4vwG5MtON+pilSwa4JFt4lpR+eYdNclV0l5S FgqG1ECMyCL4yvU4/t2axLS/j19h/036c/K7BxKbLvu9NIodIdfU8igl1ubiLmHQK4 I/6sgDOVjlUSnV1hEmSCwLuSZqaGypgKFmkc8nRI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-2379] libstdc++: Fix laziness of __and/or/not_ X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 67b6d1be0623de1a8aa32fe249bfa0129c55b11a X-Git-Newrev: 51c42b38e43b5283b116882529d232719b099bfc Message-Id: <20220902152018.E8E303858C54@sourceware.org> Date: Fri, 2 Sep 2022 15:20:18 +0000 (GMT) List-Id: https://gcc.gnu.org/g:51c42b38e43b5283b116882529d232719b099bfc commit r13-2379-g51c42b38e43b5283b116882529d232719b099bfc Author: Patrick Palka Date: Fri Sep 2 11:19:51 2022 -0400 libstdc++: Fix laziness of __and/or/not_ r13-2230-g390f94eee1ae69 redefined the internal logical operator traits __and_, __or_ and __not_ as alias templates that directly resolve to true_type or false_type. But it turns out using an alias template here causes the traits to be less lazy than before because we now compute the logical result immediately upon _specialization_ of the trait, and not later upon _completion_ of the specialization. So for example, in using type = __and_>; we now compute the conjunction and thus instantiate A even though we're in a context that doesn't require completion of the __and_. What's worse is that we also compute the inner negation and thus instantiate B (for the same reason), independent of the __and_ and the value of A! Thus the traits are now less lazy and composable than before. Fortunately, the fix is cheap and straightforward: redefine these traits as class templates instead of as alias templates so that computation of the logical result is triggered by completion, not by specialization. libstdc++-v3/ChangeLog: * include/std/type_traits (__or_, __and_, __not_): Redefine as a class template instead of as an alias template. * testsuite/20_util/logical_traits/requirements/short_circuit.cc: Add more tests for conjunction and disjunction. Add corresponding tests for __and_ and __or_. Diff: --- libstdc++-v3/include/std/type_traits | 12 ++++++--- .../logical_traits/requirements/short_circuit.cc | 29 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 8b11f31741b..be9f2955539 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -168,13 +168,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // to either true_type or false_type which allows for a more efficient // implementation that avoids recursive class template instantiation. template - using __or_ = decltype(__detail::__or_fn<_Bn...>(0)); + struct __or_ + : decltype(__detail::__or_fn<_Bn...>(0)) + { }; template - using __and_ = decltype(__detail::__and_fn<_Bn...>(0)); + struct __and_ + : decltype(__detail::__and_fn<_Bn...>(0)) + { }; template - using __not_ = __bool_constant; + struct __not_ + : __bool_constant + { }; /// @endcond #if __cplusplus >= 201703L diff --git a/libstdc++-v3/testsuite/20_util/logical_traits/requirements/short_circuit.cc b/libstdc++-v3/testsuite/20_util/logical_traits/requirements/short_circuit.cc index 86996b27fa5..ff90f8a47c3 100644 --- a/libstdc++-v3/testsuite/20_util/logical_traits/requirements/short_circuit.cc +++ b/libstdc++-v3/testsuite/20_util/logical_traits/requirements/short_circuit.cc @@ -14,6 +14,10 @@ static_assert(!std::conjunction_v); static_assert(!std::conjunction_v); static_assert(!std::conjunction_v); static_assert(!std::conjunction_v); +static_assert(!std::conjunction_v, + std::disjunction, + std::negation>); // [meta.logical]/8: For a specialization disjunction, if // there is a template type argument B_i for which bool(B_i::value) is true, @@ -24,3 +28,28 @@ static_assert(std::disjunction_v); static_assert(std::disjunction_v); static_assert(std::disjunction_v); static_assert(std::disjunction_v); +static_assert(std::disjunction_v, + std::disjunction, + std::negation>); + +#if __GLIBCXX__ +// Also test the corresponding internal traits __and_, __or_ and __not_. +static_assert(!std::__and_v); +static_assert(!std::__and_v); +static_assert(!std::__and_v); +static_assert(!std::__and_v); +static_assert(!std::__and_v, + std::__or_, + std::__not_>); + +static_assert(std::__or_v); +static_assert(std::__or_v); +static_assert(std::__or_v); +static_assert(std::__or_v); +static_assert(std::__or_v, + std::__or_, + std::__not_>); +#endif