Search code examples
cgccstandards

Assigning static const double with a static const unsigned long long int variable in C


I would like to start with the reduced test case:

const unsigned long long int neg = (const unsigned long long) -1;

#line 1 "not-num-const"
void __cgo_f_1_4(void) { static const double __cgo_undefined__4 = (neg); }

To make a long story short, according to the C standard, would that be a compile error,a valid code, or undefined? Or, has the C standard used by default become different?

gcc 7.3 says it's an error while gcc 9.0 experimental as of early June says it is acceptable.

The test code is generated by a third party tool that wants to know about "neg" in its input. The tool generates a simple C code, gives the code to gcc by invoking gcc as just "gcc," and peeps the stdandard error.

As gcc changed the response, at least as of 9.0, the tool shows regression. I am wondering which one is right according to the C standard. The tool is not necessarilly relying on a particular compiler that is gcc. The compiler could be clang, icc, Visual Studio, Sun Studio, or whatsoever that compromises to the C standard.


Solution

  • neg is not an arithmetic constant expression according to the C11 standard. (C++ would treat it as a constant.) There has been talk of aligning C more with C++ in this particular area, but this is not part of a published standard.

    However, implementations are allowed to accept further expressions as constant expressions in initializers, so they are allowed to accept this construct.