Initializing the shadow copy variable
Is there anything in the standard that defines the initialization of a variable from the variable it shadows?
For example:
int i = 7;
{
int i = i;
}
Visual Studio 2013 allows this without warning and works as expected. The internal variable i
is 7. Clang and GCC, however, give me a warning about initializing the initializing variable from itself, will be uninitialized.
source to share
The standard has something to say:
3.3.2 Declaration point [basic.scope.pdecl]
1 Declaration point for a name immediately after its full declaration (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 (undefined) value. -end example]
This is exactly your case. The program demonstrates undefined behavior by accessing an uninitialized object.
My copy of VS2013 reports error C4700: uninitialized local variable 'i' used
for this code. Not sure why your copy is behaving differently.
source to share