To address your question about "i
is used before it's declared, right?"
Not in C++. [basic.scope.pdecl]
says
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 ]