fallthrough
suggest changeIf a case does not leave the switch, execution continues into the next case. A break statement prevents that. Unintended fallthrough can introduce bugs, so several compilers and static analyzers warn about it.
C++17 added the [[fallthrough]] attribute to mark an intentional fallthrough. A compiler can then warn when a case that contains a statement continues without break or [[fallthrough]]. Empty cases, such as 2011 and 2014 below, are the usual way to share one block and typically do not warn.
switch(input) {
case 2011:
case 2014:
case 2017:
std::cout << "Using modern C++" << std::endl;
[[fallthrough]]; // > No warning
case 1998:
case 2003:
standard = input;
}
See the proposal for more detailed examples on how [[fallthrough]] can be used.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents