在下面的代码中,我使用标准 [[fallthrough]]
来自 C++1z 的属性以记录需要失败:
#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";
~~~~~~~~~~^~~~~~~~
为什么?
最佳答案
属性后面少了一个分号:
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]]
^
;
更新:科迪·格雷 reported这个问题交给 GCC 团队。
关于c++ - 为什么即使我使用 [[fallthrough]],GCC 也会警告我失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45026747/