C++ attribute: fallthrough (since C++17)
From cppreference.net
<
cpp
|
language
|
attributes
C++
C++ language
| 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 | ||||||||||||||||
Declarations
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Attributes
|
(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)
|
表明从前一个 case 标签的直落行为是故意的,不应被会对直落行为发出警告的编译器诊断出来。
目录 |
语法
[
[
fallthrough
]
]
|
|||||||||
说明
仅可应用于 空语句 以创建 直落语句 ( [ [ fallthrough ] ] ; )。
fallthrough 语句仅可用于 switch 语句中,且下一条待执行语句必须为该 switch 语句中带有 case 或 default 标签的语句。若 fallthrough 语句位于循环内部,则下一条(带标签的)语句必须属于该循环的同一轮迭代。
示例
运行此代码
void f(int n) { void g(), h(), i(); switch (n) { case 1: case 2: g(); [[fallthrough]]; case 3: // 不会对fallthrough发出警告 h(); case 4: // 编译器可能对fallthrough发出警告 if (n < 3) { i(); [[fallthrough]]; // 正确 } else { return; } case 5: while (false) { [[fallthrough]]; // 非良构:下一条语句不属于 // 同一循环迭代 } case 6: [[fallthrough]]; // 非良构,没有后续的case或default标签 } }
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| CWG 2406 | C++17 | [ [ fallthrough ] ] 可能出现在目标switch语句内嵌套的循环中 | 禁止使用 |
参考文献
- C++23 标准 (ISO/IEC 14882:2024):
-
- 9.12.6 Fallthrough 属性 [dcl.attr.fallthrough]
- C++20 标准 (ISO/IEC 14882:2020):
-
- 9.12.5 Fallthrough 属性 [dcl.attr.fallthrough]
- C++17 标准 (ISO/IEC 14882:2017):
-
- 10.6.5 Fallthrough 属性 [dcl.attr.fallthrough]
参见
|
C 文档
关于
fallthrough
|