When declaring
int x[x];
the global x
is used to define the size of array. The x
in the []
is the global variable since the declaration of the local variable is not yet complete.
In the second case,
int x = x;
is undefined behavior since the x
on the RHS is the same as the x
on the LHS since the declaration of x
is complete by the time x
on the RHS is encountered.
These are described in the C++11 Standard:
3.3.2 Point of declaration
1 The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any), except as noted below. [ Example:
int x = 12;
{ int x = x; }
Here the second x is initialized with its own (indeterminate) value. — end example ]
2 Note: a name from an outer scope remains visible up to the point of declaration of the name that hides it.[ Example:
const int i = 2;
{ int i[i]; }
declares a block-scope array of two integers. — end example ] — end note ]