Got error "error: initializer element is not constant" when following code is compiled with '-O0'
static const int a = 1;
static const int b = 2;
static const int c = a + b;
If same code is compiled with -O1, -O2, -O3, -Ofast or even -Og, no errors and no warnings are generated.
This is not valid C code. Now GCC should be consistent at different optimization levels.
clang and MSVC accept this code.
Inconsistency at different optimization levels is confusing and make harder to reveal errors in code.
I'll confirm this bug report based on comment #1, and also in light of enhancement requests to have GCC treat more expressions as constant (e.g, bug 69960 and bug 71713). It would make sense treat this one as a constant expression as well. Other C implementations like Clang have done so.
Fixed in GCC 8+