代码示例:
int main(int argc, char **argv)
{
switch(argc)
{
case 0:
argc = 5;
__attribute__((fallthrough));
case 1:
break;
}
}
Using gcc 6.3.0 ,只有-std=c11
,这段代码给出警告:
<source>: In function 'main':
7 : <source>:7:3: warning: empty declaration
__attribute__((fallthrough));
^~~~~~~~~~~~~
在不引发警告的情况下使用它的正确方法是什么?
最佳答案
如前所述,__attribute__ ((fallthrough))
是在 GCC 7 中引入的。
要保持向后兼容性并清除 Clang 和 GCC 的失败警告,您可以使用/* fall through */
marker comment .
应用于您的代码示例:
int main(int argc, char **argv)
{
switch(argc)
{
case 0:
argc = 5;
/* fall through */
case 1:
break;
}
return 0;
}
关于c - 如何在 gcc 中正确使用 __attribute__((fallthrough)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45349079/