https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79648
Bug ID: 79648 Summary: error on x in constant expression when __builtin_constant_p(x) == true Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- While looking into bug 79482 and bug 65656 I noticed that although G++ successfully compiles function f() below it gives an error on g(). The only difference between the two functions is that in g() the local variable is const-qualified. GCC folds the (i - i) into zero and propagates the constant result into the argument of __builtin_constant_p() but then doesn't (cannot) do the same thing in the initializer for b. In C++ mode this error is independent of optimization. In C mode, it happens only with optimization and not without. $ cat t.c && gcc -S -Wall -Wunused -Wpedantic t.c int f (int i) { int x = i - i; enum { b = __builtin_constant_p (x) ? x : 0 }; return b; } int g (int i) { const int x = i - i; enum { b = __builtin_constant_p (x) ? x : 0 }; return b; } t.c: In function ‘int g(int)’: t.c:14:45: error: the value of ‘x’ is not usable in a constant expression enum { b = __builtin_constant_p (x) ? x : 0 }; ^ t.c:12:13: note: ‘x’ was not initialized with a constant expression const int x = i - i; ^ t.c:14:45: error: the value of ‘x’ is not usable in a constant expression enum { b = __builtin_constant_p (x) ? x : 0 }; ^ t.c:12:13: note: ‘x’ was not initialized with a constant expression const int x = i - i; ^ t.c:14:45: error: enumerator value for ‘b’ is not an integer constant enum { b = __builtin_constant_p (x) ? x : 0 }; ^