Why does the GCC warn me of failure even when I use [[fallthrough]]?

In the following code snippet, I use the standard [[fallthrough]] attribute from C ++ 1z to document what passing is required:

 #include <iostream> int main() { switch (0) { case 0: std::cout << "a\n"; [[fallthrough]] case 1: std::cout << "b\n"; break; } } 

With GCC 7.1, the code compiles without errors. However, the compiler still warns me of the failure:

 warning: this statement may fall through [-Wimplicit-fallthrough=] std::cout << "a\n"; ~~~~~~~~~~^~~~~~~~ 

Why?

+67
c ++ c ++ 1z switch-statement
Jul 11 '17 at 6:16
source share
1 answer

You are missing a semicolon after the attribute:

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

The [[fallthrough]] attribute must be applied to an empty instruction (see P0188R1 ). The current Clang trunk gives a useful 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.

+88
Jul 11 '17 at 6:16
source share



All Articles