Closed
Description
Sorry about the title I'm not really sure how to describe this.
Testcase:
class Inner {
public:
constexpr bool is_constant_evaluated() const {
if(__builtin_is_constant_evaluated()) {
return true;
} else {
volatile int x = 0;
return (x == 1);
}
}
constexpr Inner() {}
};
class Outer {
public:
static const constexpr auto A = Inner();
// If constexpr, no warning
// If constinit, warns
static const constinit bool A_is_constant_evaluated = A.is_constant_evaluated();
};
static_assert(Outer::A_is_constant_evaluated == true);
Compiling this with -Wpedantic -std=c++20
, clang 17.0.6 complains
eval.cpp:9:21: warning: in-class initializer for static data member is not a constant expression; folding it to a constant is a GNU extension [-Wgnu-folding-constant]
9 | return (x == 1);
GCC and MSVC both accept this without any warnings.
This warning makes no sense because first this code is in a else
block where the if
is guarded by __builtin_is_constant_evaluated
. (std::is_constant_evaluated
and if constexpr
seem to have the same result.) Secondly it doesn't make sense since if Clang was invoking the code with the else
block then A_is_constant_evaluated
would be false. But the static_assert
succeeds.