const size_t buffer_size = 5; int buffer[buffer_size];
compiles fine in c, but not for the same reason. C99/C11 has dynamic arrays
const size_t buffer_size = 5; int buffer[buffer_size];
compiles fine in c, but not for the same reason. C99/C11 has dynamic arrays
Which I don't think is standard -- but it's not using VLAs. I wondered if it just has constant expression semantics for const variables.
Weirdly, adding a _Static_assert to test this theory proves it for c99 but not c11 :/
https://godbolt.org/z/q-bb-n c99 with clang https://godbolt.org/z/ad14Ah c11 with clang https://godbolt.org/z/xJSDQa c11 with gcc (which is the only one giving the output I'd expect)
That would be my guess also, for applicable const variables. File scope const variables are quite constexpr-y in C anyway, since C requires all file scope variable initializers to be constant expressions (C++ only requires that for constexpr variables).
Toying around with clang in Godbolt, it seems that there are some quirks regarding this. The following is accepted:
static const int n = 0;
int buf[n]; // invalid zero-length array is accepted
// probably another non-standard extension
But the following is not, despite n having the same zero value: static const int n;
int buf[n]; // complains about a file scope VLATry "clang -std=c11 -pedantic-errors".