3 ms·
It is a non op; it's a matter of scope and is resolved entirely statically. One shadows the variable by a variable of the same name. The key is that the new va
by Blikkentrekker 4y ago
It is a non op; it's a matter of scope and is resolved entirely statically.
One shadows the variable by a variable of the same name. The key is that the new variable is scoped only to the inside of the loop, whereas the original variable is scoped to the outside of it, thus assigning to the original variable in a next iteration of the loop updates that variable again.
The major case where this is an issue is if one somehow took a reference to the variable, that reference thus now points to the new value rather than the old.
Taking a reference to the new, more closely scoped variable that is local to the iteration of the loop does not have this problem.