In a constexpr function, I am unable to define a non-literal variable within the branch of an if statement conditioned by C++20's std::is_constant_evaluated()? Clang and GCC both indicate it is not permitted, but in the example below, other constructs which cannot be evaluated at compile-time are permitted. Is there a specific restriction on the use of non-literals?
#include <type_traits>
struct Foo {
~Foo() {}
};
void non_constexpr() {}
constexpr bool bar()
{
if (std::is_constant_evaluated()) {
} else {
non_constexpr();
double d;
reinterpret_cast<int*>(&d);
Foo f; // error: variable ‘f’ of non-literal type ‘Foo’ in ‘constexpr’ function
}
return true;
}
constexpr bool x = bar();