C 属性: fallthrough (自 C23 起)
来自 cppreference.cn
< c | language | attributes
指示从前一个 case 标签的 fall through 是故意的,并且不应被警告 fallthrough 的编译器诊断。
目录 |
[编辑] 语法
[[ fallthrough ]] [[ __fallthrough__ ]] |
|||||||||
[编辑] 说明
只能在属性声明中使用,以创建 fallthrough 声明 ([[fallthrough]];)。
fallthrough 声明只能在 switch 语句中使用,其中遇到的下一个块项(语句、声明或标签)是带有该 switch 语句的 case 或 default 标签的语句。
指示从前一个 case 标签的 fall through 是故意的,并且不应被警告 fallthrough 的编译器诊断。
[编辑] 示例
运行此代码
#include <stdbool.h> void g(void) {} void h(void) {} void i(void) {} void f(int n) { switch (n) { case 1: case 2: g(); [[fallthrough]]; case 3: // no warning on fallthrough h(); case 4: // compiler may warn on fallthrough if(n < 3) { i(); [[fallthrough]]; // OK } else { return; } case 5: while (false) { [[fallthrough]]; // ill-formed: no subsequent case or default label } case 6: [[fallthrough]]; // ill-formed: no subsequent case or default label } } int main(void) {}