Im folgenden Code verwende ich den Standard [[fallthrough]]
Attribut aus C++ 1z, um zu dokumentieren, dass ein Fallthrough gewünscht wird:
#include <iostream>
int main() {
switch (0) {
case 0:
std::cout << "a\n";
[[fallthrough]]
case 1:
std::cout << "b\n";
break;
}
}
Mit GCC 7.1 wird der Code fehlerfrei kompiliert. Der Compiler warnt mich jedoch immer noch vor einem Fallthrough:
warning: this statement may fall through [-Wimplicit-fallthrough=]
std::cout << "a\n";
~~~~~~~~~~^~~~~~~~
Warum?
Nach dem Attribut fehlt ein Semikolon:
case 0:
std::cout << "a\n";
[[fallthrough]];
// ^
case 1:
Das Attribut [[fallthrough]]
Ist auf eine leere Anweisung anzuwenden (siehe P0188R1 ). Der aktuelle Clang-Trunk gibt in diesem Fall einen hilfreichen Fehler aus :
error: fallthrough attribute is only allowed on empty statements
[[fallthrough]]
^
note: did you forget ';'?
[[fallthrough]]
^
;
Update: Cody Gray gemeldet dieses Problem an das GCC-Team.