<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54803>54803</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
std::conjunction and std::disjunction implementations don't implement less obvious standard requirements
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jacobsa
</td>
</tr>
</table>
<pre>
Here are some conformance tests for behavior specified for `std::conjunction` and `std::disjunction` in [[meta.logical]](https://timsong-cpp.github.io/cppwp/n4659/meta.logical#lib:disjunction) by N4659 (C++17). In particular they exercise the following:
* The requirement that the type traits inherit from an input type.
* The requirement that the type traits be usable with integral constants for which `bool(Bi::value)` is valid even if `value` is not a `bool`.
* The clauses that say `value` need not be present for later inputs in the event of short-circuiting.
```c++
// Weird false/true types that aren't actually bool constants (but should still
// be legal according to [meta.logical] because `bool(T::value)` is valid), are
// distinct from std::false_type and std::true_type, and are distinct from other
// instantiations of the same template.
//
// These let us check finicky details mandated by the standard like
// "std::conjunction should evaluate to a type that inherits from the first
// false-y input".
template <int>
struct MyFalse : std::integral_constant<int, 0> {};
template <int>
struct MyTrue : std::integral_constant<int, -1> {};
/////////////////////
// std::conjunction
/////////////////////
// Base case: always true.
static_assert(std::is_base_of_v<std::true_type, std::conjunction<>>);
// One predicate: inherits from that predicate, regardless of value.
static_assert(std::is_base_of_v<MyFalse<0>, std::conjunction<MyFalse<0>>>);
static_assert(std::is_base_of_v<MyTrue<0>, std::conjunction<MyTrue<0>>>);
// Multiple predicates, with at least one false: inherits from that one.
static_assert(std::is_base_of_v<
MyFalse<1>, std::conjunction<MyTrue<0>, MyFalse<1>, MyTrue<2>>>);
static_assert(std::is_base_of_v<
MyFalse<1>, std::conjunction<MyTrue<0>, MyFalse<1>, MyFalse<2>>>);
// Short circuiting: in the case above, additional predicates need not even
// define a value member.
struct Empty {};
static_assert(std::is_base_of_v<
MyFalse<1>, std::conjunction<MyTrue<0>, MyFalse<1>, Empty>>);
// All predicates true: inherits from the last.
static_assert(std::is_base_of_v<
MyTrue<2>, std::conjunction<MyTrue<0>, MyTrue<1>, MyTrue<2>>>);
/////////////////////
// std::disjunction
/////////////////////
// Base case: always false.
static_assert(std::is_base_of_v<std::false_type, std::disjunction<>>);
// One predicate: inherits from that predicate, regardless of value.
static_assert(std::is_base_of_v<MyFalse<0>, std::disjunction<MyFalse<0>>>);
static_assert(std::is_base_of_v<MyTrue<0>, std::disjunction<MyTrue<0>>>);
// Multiple predicates, with at least one true: inherits from that one.
static_assert(std::is_base_of_v<
MyTrue<1>, std::disjunction<MyFalse<0>, MyTrue<1>, MyFalse<2>>>);
static_assert(std::is_base_of_v<
MyTrue<1>, std::disjunction<MyFalse<0>, MyTrue<1>, MyTrue<2>>>);
// Short circuiting: in the case above, additional predicates need not even
// define a value member.
static_assert(std::is_base_of_v<
MyTrue<1>, std::disjunction<MyFalse<0>, MyTrue<1>, Empty>>);
// All predicates false: inherits from the last.
static_assert(
std::is_base_of_v<MyFalse<2>,
std::disjunction<MyFalse<0>, MyFalse<1>, MyFalse<2>>>);
```
The implementations in libc++ fail basically all of these tests, apparently because they only ever evaluate to `std::false_type` or `std::true_type` rather than something that inherits from an input as the standard requires. Even after fixing that I believe there are also problems with short-circuiting in both cases, and with failing to treat `-1` as `true` in at least the case of `std::conjunction`.
I tested this using the version as of [`8a2a9665`](https://github.com/llvm/llvm-project/blob/8a2a966520023cd7d424e89b730c054f1f06f496/libcxx/include/type_traits), in C++17 mode using Google's internal build system.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVWEtz2zYQ_jXSBWMOTVGSddDBj7jNIe2hmelRA4KghAQkWAC0rX_fb0FKImXZsTJOmnpoisRjsfvti7uZybfL36WVjOPfmVIyYarC2JJXQjIvnXcMryyTG_6g8OBqKVShZB6GR7PY-Xw0ucaFjV-aSnhlKgwzXuWD6Vy5_rSq2Gh6g6uUnkfarJXgej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