In this answer I basically needed static int n = -1; inside a function. I wanted to avoid magic numbers all over the place so I used this instead:
double f(int i)
{
static const int sentinel = -1;
static int n = sentinel;
if (n == sentinel)
// ...
}
It was however pointed to me that this is not conformant to standard because sentinel is not a (compile time) constant.
This makes sense to me since I know constant integers were made usable in compile time expressions (e.g. size of arrays) in C++. However gcc, clang and icc >v16 compile this code without any warning. Only icc <=v16 and MSVC give this warning/error (see on godbolt).
What does the C Standard say? Does this change between various versions of the standard (c90, c99, c11)? If this is not conformant, can we get a warning on gcc and clang? If it is conformant why do old icc and MSVC give errors?