Why does “&& true” added to a constraint make a function template a better overload?
26
Consider the following two overloads of a function template foo
:
template <typename T>
void foo(T) requires std::integral<T> {
std::cout << "foo requires integral\n";
}
template <typename T>
int foo(T) requires std::integral<T> && true {
std::cout << "foo requires integral and true\n";
return 0;
}
Note the difference between the two constraints: the second has an extra && true
.
Intuitively speaking, true
is redundant in a conjunction (since X && true
is just X
). However, it looks like this makes a semantic difference, as foo(42)
would call the second overload.
Why is this the case? Specifically, why is the second function template a better overload?
c++ language-lawyer c++20 c++-concepts
&& !false
make it even better? – Adrian Mole Apr 8 at 14:40