I think the essence of your question is why the second identifier is recognized as the identification of the same object as the first, in int i = i; or int &p = p;
This is defined in [basic.scope.pdecl] / 1 of the C ++ 14 standard:
The declaration point for the name immediately after its full declarator and before its initializer (if any), except as noted below. [Example:
unsigned char x = 12; { unsigned char x = x; }
Here, the second x initialized with its own (undefined) value. -end example]
The semantics of these operators covers other threads:
Note. The given example differs in semantics from int i = i; , because not UB evaluates the uninitialized unsigned char , but UB evaluates the uninitialized int .
As noted in a related topic, g ++ and clang may give warnings when they detect this.
Regarding the rationale for the scope rule: I donβt know for sure, but the scope rule existed in C, so maybe it just made its way in C ++, and now it would be strange to change it.
If we said that the declared variable does not enter the scope for its initializer, then int i = i; can make the second i find i from the outer region, which will also be confusing.
source share