Consider the following snippet of code:
int main(){
constexpr int x = -1;
if(x >= 0){
constexpr int y = 1<<x;
}
}
GCC 7 (and probably other versions of GCC) refuses to compile this and says:
error: right operand of shift expression '(1 << -1)' is negative [-fpermissive]
I can guess where this may have come from: the constexpr
declaration on y
makes GCC evaluate y
at compile time, where it might be negative. Removing the constexpr
fixes the error.
However, is this undefined behavior by the standard? The condition is always false, so the value of y
will never be used.
In my actual code, x
is a template parameter, which may or may not be negative.