Consider this code:
typedef union { float v; unsigned u; } T;
constexpr T x = { .u = 0 };
constexpr float f(void)
{
return x.v;
}
Is this code valid?
Invocations:
$ g++ t506a.cpp -c -std=c++20 -pedantic -Wall -Wextra
<nothing>
$ clang++ t506a.cpp -c -std=c++20 -pedantic -Wall -Wextra
t506a.cpp:3:17: error: constexpr function never produces a constant expression
[-Winvalid-constexpr]
constexpr float f(void)
^
t506a.cpp:5:9: note: read of member 'v' of union with active member 'u' is not allowed in a
constant expression
return x.v;
^
1 error generated.
Which compiler is correct?