即使在我使用]的情况下,为什么GCC也会警告我有一个漏洞?

在下面的一段代码中,我使用了C ++ 1z中的标准[[fallthrough]]属性来logging下一个需要的转换:

 #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"; ~~~~~~~~~~^~~~~~~~ 

为什么?

在属性后面缺less分号:

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

[[fallthrough]]属性将应用于空语句(请参阅P0188R1 )。 在这种情况下 ,当前的俚语中继给出了一个有用的错误 :

 error: fallthrough attribute is only allowed on empty statements [[fallthrough]] ^ note: did you forget ';'? [[fallthrough]] ^ ; 

更新:Cody Gray向GCC团队报告了这个问题。