C++ attribute: fallthrough (since C++17)
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| Storage duration specifiers | ||||||||||||||||
| Initialization | ||||||||||||||||
| Expressions | ||||||||||||||||
| Alternative representations | ||||||||||||||||
| Literals | ||||||||||||||||
| Boolean - Integer - Floating-point | ||||||||||||||||
| Character - String - nullptr (C++11) | ||||||||||||||||
| User-defined (C++11) | ||||||||||||||||
| Utilities | ||||||||||||||||
| Attributes (C++11) | ||||||||||||||||
| Types | ||||||||||||||||
typedef
declaration
|
||||||||||||||||
| Type alias declaration (C++11) | ||||||||||||||||
| Casts | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++23)
|
||||
|
(C++11)
(until C++26)
|
||||
|
(C++14)
|
||||
|
fallthrough
(C++17)
|
||||
|
(C++26)
|
||||
|
(C++20)
|
||||
|
(C++17)
|
||||
|
(C++17)
|
||||
|
(C++11)
|
||||
|
(C++20)
|
||||
|
(TM TS)
|
||||
|
(C++20)
|
Gibt an, dass das Fallthrough vom vorherigen Case-Label beabsichtigt ist und nicht von einem Compiler diagnostiziert werden sollte, der vor Fallthrough warnt.
Inhaltsverzeichnis |
Syntax
[
[
fallthrough
]
]
|
|||||||||
Erklärung
Darf nur auf eine null statement angewendet werden, um eine fallthrough statement zu erstellen ( [ [ fallthrough ] ] ; ).
Eine Fallthrough-Anweisung darf nur in einer switch -Anweisung verwendet werden, wobei die nächste auszuführende Anweisung eine Anweisung mit einem case- oder default-Label für diese switch-Anweisung ist. Wenn sich die fallthrough-Anweisung innerhalb einer Schleife befindet, muss die nächste (gelabelte) Anweisung Teil derselben Iteration dieser Schleife sein.
Beispiel
void f(int n) { void g(), h(), i(); switch (n) { case 1: case 2: g(); [[fallthrough]]; case 3: // keine Warnung bei Fallthrough h(); case 4: // Compiler kann bei Fallthrough warnen if (n < 3) { i(); [[fallthrough]]; // OK } else { return; } case 5: while (false) { [[fallthrough]]; // ungültig: nächste Anweisung ist nicht // Teil derselben Iteration } case 6: [[fallthrough]]; // ungültig, kein nachfolgender Case- oder Default-Label } }
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrektes Verhalten |
|---|---|---|---|
| CWG 2406 | C++17 |
[
[
fallthrough
]
]
konnte in einer Schleife erscheinen
die innerhalb der Ziel-switch-Anweisung verschachtelt ist |
verboten |
Referenzen
- C++23-Standard (ISO/IEC 14882:2024):
-
- 9.12.6 Fallthrough-Attribut [dcl.attr.fallthrough]
- C++20-Standard (ISO/IEC 14882:2020):
-
- 9.12.5 Fallthrough-Attribut [dcl.attr.fallthrough]
- C++17-Standard (ISO/IEC 14882:2017):
-
- 10.6.5 Fallthrough-Attribut [dcl.attr.fallthrough]
Siehe auch
|
C-Dokumentation
für
fallthrough
|