With Clang, this code compiles fine, with no errors nor warnings:
static const int ZERO = 0;
int main() {
switch (0) {
case ZERO: break;
}
}
But with GCC, I get an error:
<source>: In function 'main':
<source>:5:9: error: case label does not reduce to an integer constant
5 | case ZERO: break;
| ^~~~
Why is that?
Note: In this case I can just use a #define
, but I'm wondering why there's a difference between Clang and GCC's behavior here.
I'm using Apple Clang 11.0.3 (seems to be installed via Xcode) and GCC 10.2.0, but I was able to reproduce this problem on Godbolt with Clang trunk and GCC trunk. Both locally and in Godbolt I was using x86-64.