]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix laziness of __and/or/not_
authorPatrick Palka <ppalka@redhat.com>
Fri, 2 Sep 2022 15:19:51 +0000 (11:19 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 2 Sep 2022 15:19:51 +0000 (11:19 -0400)
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_<A, __not_<B>>;

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_.

libstdc++-v3/include/std/type_traits
libstdc++-v3/testsuite/20_util/logical_traits/requirements/short_circuit.cc

index 8b11f31741b7fe9925ef67af8fe2931ae8dc5579..be9f2955539540b82d0489482579f253ef52efb8 100644 (file)
@@ -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<typename... _Bn>
-    using __or_ = decltype(__detail::__or_fn<_Bn...>(0));
+    struct __or_
+    : decltype(__detail::__or_fn<_Bn...>(0))
+    { };
 
   template<typename... _Bn>
-    using __and_ = decltype(__detail::__and_fn<_Bn...>(0));
+    struct __and_
+    : decltype(__detail::__and_fn<_Bn...>(0))
+    { };
 
   template<typename _Pp>
-    using __not_ = __bool_constant<!bool(_Pp::value)>;
+    struct __not_
+    : __bool_constant<!bool(_Pp::value)>
+    { };
   /// @endcond
 
 #if __cplusplus >= 201703L
index 86996b27fa5d6f958dc6f051387da21592f71550..ff90f8a47c318c9af7d04f4bbd9bb9bf35de7992 100644 (file)
@@ -14,6 +14,10 @@ static_assert(!std::conjunction_v<std::false_type, invalid>);
 static_assert(!std::conjunction_v<std::false_type, invalid, invalid>);
 static_assert(!std::conjunction_v<std::true_type, std::false_type, invalid>);
 static_assert(!std::conjunction_v<std::true_type, std::false_type, invalid, invalid>);
+static_assert(!std::conjunction_v<std::false_type,
+                                 std::conjunction<invalid>,
+                                 std::disjunction<invalid>,
+                                 std::negation<invalid>>);
 
 // [meta.logical]/8: For a specialization disjunction<B_1, ..., B_n>, 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<std::true_type, invalid>);
 static_assert(std::disjunction_v<std::true_type, invalid, invalid>);
 static_assert(std::disjunction_v<std::false_type, std::true_type, invalid>);
 static_assert(std::disjunction_v<std::false_type, std::true_type, invalid, invalid>);
+static_assert(std::disjunction_v<std::true_type,
+                                std::conjunction<invalid>,
+                                std::disjunction<invalid>,
+                                std::negation<invalid>>);
+
+#if __GLIBCXX__
+// Also test the corresponding internal traits __and_, __or_ and __not_.
+static_assert(!std::__and_v<std::false_type, invalid>);
+static_assert(!std::__and_v<std::false_type, invalid, invalid>);
+static_assert(!std::__and_v<std::true_type, std::false_type, invalid>);
+static_assert(!std::__and_v<std::true_type, std::false_type, invalid, invalid>);
+static_assert(!std::__and_v<std::false_type,
+                           std::__and_<invalid>,
+                           std::__or_<invalid>,
+                           std::__not_<invalid>>);
+
+static_assert(std::__or_v<std::true_type, invalid>);
+static_assert(std::__or_v<std::true_type, invalid, invalid>);
+static_assert(std::__or_v<std::false_type, std::true_type, invalid>);
+static_assert(std::__or_v<std::false_type, std::true_type, invalid, invalid>);
+static_assert(std::__or_v<std::true_type,
+                         std::__and_<invalid>,
+                         std::__or_<invalid>,
+                         std::__not_<invalid>>);
+#endif