Допустим, у меня есть код на С++:
if (error)
goto exit;
...
// size_t i = 0; //error
size_t i;
i = 0;
...
exit:
...
Я понимаю, что мы не должны использовать goto
, но все же почему
size_t i;
i = 0;
компилируется, тогда как size_t i = 0;
нет?
Почему такое поведение обеспечивается стандартом (, упомянутым @SingerOfTheFall )?
It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer.