为什么在C/C ++中交织switch/for/if语句是有效的? [英] Why it is valid to intertwine switch/for/if statements in C/C++?
问题描述
我正在阅读 boost/asio/coroutine.hpp
,无法理解BOOST_ASIO_CORO_REENTER和BOOST_ASIO_CORO_YIELD的实现.
I'm reading boost/asio/coroutine.hpp
and cannot understand the implementation of BOOST_ASIO_CORO_REENTER and BOOST_ASIO_CORO_YIELD. The expanded form of
reenter (this) {
yield ..
yield ..
}
似乎是交织在一起的switch/if/for语句.我想知道为什么这是有效的C代码?我写了类似的东西(如下所示),发现它可以使用gcc进行编译.
seems to be intertwined switch/if/for statements. I'm wondering how come this is valid C code? I wrote something similar (shown below) and found it compiles using gcc.
int main() {
int a = 1;
switch (a)
case 0: if (1) a = 2;
else case 1: for (;;) {
case 3:
break;
}
return 0;
}
推荐答案
从语法上讲,开关的主体只是一个语句(通常,但不一定是复合语句)
Syntactically, the body of a switch is just a statement (usually, but not necessarily a compound statement)
6.8 :
statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
其中可能带有标签 6.8.1 :
labeled-statement:
identifier : statement
case constant-expression : statement
default : statement
示例:
switch(1) one: case 1: dothis();
如果它是复合语句,则每个子语句都可以递归标记.示例:
If it is a compound statement, then each substatement recursively may also be labeled. Example:
switch(x) {
if(1) one: case 1: dothis();
else case 0: orthis(); /*fallthru*/
three: case 3: three();
}
语法将 case
/ default
-标签和常规标签视为相同,只有语义检查才能验证 case
/ default
-标签在 switch
内.
The syntax treats case
/default
-labels and regular labels the same, only the semantic check verifies that case
/default
-labels be inside a switch
.
在实施方面,所有内容都将编译为(扁平)汇编.
Implementation-wise, everything compiles into (flat) assembly.
例如
if(test) YesBranch; else ElseBranch;
展平为(伪程序集)
IF_NOT_THEN_GOTO(test, PAST_YES_BRANCH)
YesBranch
goto PAST_NO_BRANCH;
NoBranch
PAST_NO_BRANCH:;
而且没有理由为什么这样的平面代码中的任何内容都无法标记.
and there's no reason why anything in such flat code couldn't be labeled.
case
/ default
标签也与常规标签一样,除了(通常)在计算的跳转中使用它们之外.
case
/default
labels are also just like regular labels except they're also used in (most usually) a computed jump.
这篇关于为什么在C/C ++中交织switch/for/if语句是有效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!