为什么海湾合作委员会在我使用[[ Fallthrough ]]的时候还警告我有漏洞?

在下面的代码中,我使用 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";
~~~~~~~~~~^~~~~~~~

为什么?

14524 次浏览

You are missing a semicolon after the attribute:

case 0:
std::cout << "a\n";
[[fallthrough]];
//             ^
case 1:

The [[fallthrough]] attribute is to be applied to an empty statement (see 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]]
^
;

Update: Cody Gray reported this issue to the GCC team.