可能重复:
为什么使用goto不好?
GOTO仍被视为有害吗?
我通过xkcd进行了调整,看到了这个(几年前还读了一些关于它们的负面文章):

这究竟是什么问题?为什么goto甚至可以在C++中使用呢?
我为什么不使用它们?
以下C++示例无法使用gcc或clang进行编译,但仅使用ICC生成警告,并且对MSVC一无所知:
int main(int argc, char *argv[])
{
if (argc < 2)
goto clean_up;
#if 1 // FAIL
int i = 0;
#elif 0 // workaround - OK
{
int i = 0;
}
#else // workaround - OK
int i;
i = 0;
#endif
clean_up:
return 0;
}
Run Code Online (Sandbox Code Playgroud)
克++:
init.cpp:13: error: jump to label ‘clean_up’
init.cpp:4: error: from here
init.cpp:7: error: crosses initialization of ‘int i’
Run Code Online (Sandbox Code Playgroud)
铛++:
init.cpp:4:9: error: cannot jump from this goto statement to its label
goto clean_up; …Run Code Online (Sandbox Code Playgroud)