Is taking the address of a local variable a constant expression in C++11?
The following C++11 program:
int x = 42;
void f()
{
int y = 43;
static_assert(&x < &y, "foo");
}
int main()
{
f();
}
Doesn't compile with gcc 4.7 as it complains:
error: '&y' is not a constant expression
This would agree with my intuition. The address of y potentially changes
with each invocation of f, so of course it cannot be calculated during
translation.
However none of the bullet points in 5.19 [expr.const] seem to preclude it
from being a constant expression.
The only two contenders I see are:
an lvalue-to-rvalue conversion...
but unless I am mistaken (?) there are no lvalue-to-rvalue conversions in
the program.
And
an id-expression that refers to a variable [snip] unless:
it is initialized with a constant expression
which y is - it is initialized with the constant expression 43.
So is this an error in the standard, or am I missing something?
No comments:
Post a Comment