C++ goto(而不是继续)语法奇怪
我有以下代码:
do
{
doStuffP1();
if (test)
{ goto skip_increment;
}
dostuffP2();
skip_increment:
// 1; // Only works if I remove the comment at line start.
} while (loop);
无法编译(VC++ 2010)并出现此错误:
file_system_helpers.cpp(109) : error C2143: syntax error : missing ';' before '}'
如果我将其更改为:
skip_increment:
1;
它会编译(并工作)。
这真的是 C++ 语法的限制吗?
I have the following code:
do
{
doStuffP1();
if (test)
{ goto skip_increment;
}
dostuffP2();
skip_increment:
// 1; // Only works if I remove the comment at line start.
} while (loop);
Which doesn't compile (VC++ 2010) with this error:
file_system_helpers.cpp(109) : error C2143: syntax error : missing ';' before '}'
If I change it to:
skip_increment:
1;
It compiles (and works).
Is this really a limitation of C++ syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设“1;”您的第一个代码片段中应该缺少吗?
在这里查看这个语法: http://www.lysator.liu .se/c/ANSI-C-grammar-y.html
这仅将标签定义为“标记语句”。也就是说,块体可以在其内容序列中的任何位置包含
label:
,但标签后面的语句不是可选的。所以这会使skip_increment: }
无效。(好吧,您使用的是 C++ 而不是 C;但我怀疑在定义 C++ 语言时是否有人非常关心是否考虑到 goto 的额外使用。)
I assume the "1;" was supposed to be missing from your first code snippet?
Look at this grammar here: http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
This defines labels only as a "labeled-statement". That is, a block body can contain
label: <statement>
anywhere in its sequence of contents, but the statement after the label is not optional. So this would makeskip_increment: }
invalid.(And, OK, you're using C++ and not C; but I doubt if making allowances for extra uses of goto was something anyone cared much about while defining the C++ language.)