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].
x
after the jump. – Gros/permissive-
flag to MSVC and it will complain as well. I don't know though whether MSVC's behavior without that flag is well-defined (I would assume so, otherwise why would they allow it?). – Fantinlatour