https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128160
Bug ID: 128160
Summary: "initializer element is not constant" for a cast to or
from a fixed-point type of a negative constant, e.g.
(_Accum) -0.5r
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: yuki.tanaka at defaultcase dot de
Target Milestone: ---
_Accum x = (_Accum) -0.5r;
<source>:1:12: error: initializer element is not constant
1 | _Accum x = (_Accum) -0.5r;
| ^
Compiler returned: 1
(_Accum) -0.5r is an arithmetic constant expression: TR 18037 makes the
fixed-point types arithmetic types and adds fixed-point constants to the
allowed operands in C11 6.6p8, and the cast converts an arithmetic type to an
arithmetic type. So it is valid in a static initializer.
(_Accum) 0.5r, -0.5r without a cast, (_Fract) -0.5r (same type) and (_Accum) -1
are accepted. The same error is given for static const _Sat _Fract x = (_Sat
_Fract) -0.5r;, for block-scope static objects, and for (_Fract) -0.5k, (int)
-2.5k, (float) -0.5r and (_Accum) -1.5. Automatic variables are not affected.
mips and avr GCC give the same error.
Compiler Explorer:
https://godbolt.org/z/hPnxYvzWr