Why does Clang fail to compile the following code, with the message that the expression is not constexpr, and why does GCC not? Which compiler is correct? https://godbolt.org/z/nUhszh (Obviously, this is just an example. I do, in fact, need to be able to invoke a constexpr function-object in a constexpr context.)
#include <type_traits>
template <typename Predicate>
constexpr int f(Predicate&& pred) {
if constexpr (pred(true)) {
return 1;
}
else {
return 0;
}
}
int main() {
f([](auto m) {
return std::is_same_v<decltype(m), bool>;
});
}
Output of clang 8.0.0 with -std=c++17 -stdlib=libc++ -O1 -march=skylake
:
<source>:5:19: error: constexpr if condition is not a constant expression
if constexpr (pred(true)) {
^
<source>:14:5: note: in instantiation of function template specialization 'f<(lambda at <source>:14:7)>' requested here
f([](auto m) {
^
1 error generated.
Compiler returned: 1