Consider this code:
void foo()
{
goto bar;
int x = 0;
bar: ;
}
GCC and Clang reject it, because the jump to bar:
bypasses variable initialization. MSVC doesn't complain at all (except using x
after bar:
causes a warning).
We can do a similar thing with a switch
:
void foo()
{
switch (0)
{
int x = 0;
case 0: ;
}
}
Now all three compilers emit errors.
Are those snippets ill-formed? Or do they cause UB?
I used to think that both were ill-formed, but I can't find the revelant parts of the standard. [stmt.goto] doesn't say anything about this, and neither does [stmt.select].
It's ill-formed when the initialization is non-vacuous.
The initializer makes the initialization non-vacuous. To contrast, this
would be well-formed. Though the usual caveats about using
x
with an indeterminate value would apply.From goto statement: