为什么 GCC 即使在我使用 [[fallthrough]] 时也会警告我 fallthrough?
Why is GCC warning me about a fallthrough even when I use [[fallthrough]]?
在下面的代码中,我使用 C++1z 中的标准 [[fallthrough]]
属性来记录需要 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";
~~~~~~~~~~^~~~~~~~
为什么?
属性后缺少分号:
case 0:
std::cout << "a\n";
[[fallthrough]];
// ^
case 1:
[[fallthrough]]
属性应用于空语句(参见 P0188R1). The current Clang trunk 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 团队。
在下面的代码中,我使用 C++1z 中的标准 [[fallthrough]]
属性来记录需要 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";
~~~~~~~~~~^~~~~~~~
为什么?
属性后缺少分号:
case 0:
std::cout << "a\n";
[[fallthrough]];
// ^
case 1:
[[fallthrough]]
属性应用于空语句(参见 P0188R1). The current Clang trunk 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 团队。