考虑下面的switch语句:
switch( value )
{
case 1:
return 1;
default:
value++;
// fall-through
case 2:
return value * 2;
}
此代码编译,但它是有效的(=定义的行为)C90/C99?我从未见过默认情况不是最后一个情况的代码。
编辑:
正如Jon Cage和KillianDS所写的:这真的是丑陋而令人困惑的代码,我很清楚这一点。我只对通用语法(有定义吗?)和预期的输出感兴趣。
C99标准没有明确说明这一点,但综合所有事实来看,它是完全有效的。
case和default标签等同于goto标签。参见6.8.1标记语句。特别有趣的是6.8.1.4,它启用了前面提到的达夫装置:
任何语句前都可以加上
声明标识符为的前缀
标签名称。标签本身
不改变流控制,其中
继续畅通无阻地穿过它们。
编辑:开关内的代码没有什么特别的;它是一个正常的if语句代码块,带有额外的跳转标签。这解释了跌倒行为,以及为什么休息是必要的。
6.8.4.2.7甚至给出了一个例子:
switch (expr)
{
int i = 4;
f(i);
case 0:
i=17;
/*falls through into default code */
default:
printf("%d\n", i);
}
在人工程序片段中
存在标识符为I的对象
具有自动存储期限
(在块内)但从来没有
初始化,因此如果
控制表达式有一个非零
值,对printf函数的调用
将访问一个不确定值。
类似地,对函数f的调用
无法联系上。
case常量在switch语句中必须是唯一的:
6.8.4.2.3各大小写标号的表达式为整数常数
表达和没有两种的情况
常数表达式是一样的
开关语句也应相同
转换后的值。可能有
一个交换机最多只能有一个默认标签
声明。
所有的情况都被评估,然后它跳转到默认标签,如果给定:
6.8.4.2.5 The integer promotions are performed on the controlling
expression. The constant expression in
each case label is converted to the
promoted type of the controlling
expression. If a converted value
matches that of the promoted
controlling expression, control jumps
to the statement following the matched
case label. Otherwise, if there is a
default label, control jumps to the
labeled statement. If no converted
case constant expression matches and
there is no default label, no part of
the switch body is executed.
另一个例子:如果“default”是一个意外的情况,并且您希望记录错误,但也要做一些合理的事情,那么这可能很有用。示例来自我自己的一些代码:
switch (style)
{
default:
MSPUB_DEBUG_MSG(("Couldn't match dash style, using solid line.\n"));
case SOLID:
return Dash(0, RECT_DOT);
case DASH_SYS:
{
Dash ret(shapeLineWidth, dotStyle);
ret.m_dots.push_back(Dot(1, 3 * shapeLineWidth));
return ret;
}
// more cases follow
}
在某些情况下,它是有效的,非常有用的。
考虑下面的代码:
switch(poll(fds, 1, 1000000)){
default:
// here goes the normal case : some events occured
break;
case 0:
// here goes the timeout case
break;
case -1:
// some error occurred, you have to check errno
}
重点是上面的代码比级联的if更可读,更有效。你可以把default放在最后,但这是毫无意义的,因为它会把你的注意力集中在错误情况上,而不是正常情况上(这里是默认情况)。
实际上,这不是一个很好的例子,在民意调查中你知道最多可能发生多少事件。我真正的观点是,在某些情况下,有一组定义好的输入值,其中有“例外”和正常情况。将异常或正常情况放在前面更好,这是一个选择的问题。
在软件领域,我想到了另一种非常常见的情况:带有一些终端值的递归。如果可以使用开关表示它,默认值将是包含递归调用的常用值和终端值的不同元素(个别情况)。通常不需要关注终端值。
Another reason is that the order of the cases may change the compiled code behavior, and that matters for performances. Most compilers will generate compiled assembly code in the same order as the code appears in the switch. That makes the first case very different from the others: all cases except the first one will involve a jump and that will empty processor pipelines. You may understand it like branch predictor defaulting to running the first appearing case in the switch. If a case if much more common that the others then you have very good reasons to put it as the first case.
阅读评论,这就是为什么最初的海报在阅读英特尔编译器分支循环重组关于代码优化后提出这个问题的具体原因。
然后,它将成为代码可读性和代码性能之间的仲裁。也许最好是写个评论,向将来的读者解释为什么先出现一个案例。