在下面的代码中,我使用C ++ 1z中的标准[[fallthrough]]
属性来记录需要的内容:
#include <iostream>
int main() {
switch (0) {
case 0:
std::cout << "a\n";
[[fallthrough]]
case 1:
std::cout << "b\n";
break;
}
}
使用GCC 7.1,代码编译时没有错误。但是,编译器仍然警告我一个突破:
warning: this statement may fall through [-Wimplicit-fallthrough=]
std::cout << "a\n";
~~~~~~~~~~^~~~~~~~
为什么?
答案 0 :(得分:103)
您在属性后缺少分号:
case 0:
std::cout << "a\n";
[[fallthrough]];
// ^
case 1:
[[fallthrough]]
属性将应用于空语句(请参阅P0188R1)。当前的Clang主干gives a helpful error in this case:
error: fallthrough attribute is only allowed on empty statements
[[fallthrough]]
^
note: did you forget ';'?
[[fallthrough]]
^
;
更新:Cody Gray reported此问题发给GCC团队。