C 屬性:fallthrough (自 C23 起)
來自 cppreference.com
表示從上一個 case 標籤的貫穿是故意的,並且不應被警告貫穿的編譯器診斷。
目錄 |
[編輯] 語法
[[ fallthrough ]] [[ __fallthrough__ ]] |
|||||||||
[編輯] 解釋
只能在屬性宣告中使用,以建立fallthrough 宣告 ([[fallthrough]];)。
fallthrough 宣告只能在 switch 語句中使用,其中下一個塊項(語句、宣告或標籤)是帶有該 switch 語句的 case
或 default
標籤的語句。
表示從上一個 case 標籤的貫穿是故意的,並且不應被警告貫穿的編譯器診斷。
[編輯] 示例
執行此程式碼
#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) {}
[編輯] 另請參閱
C++ 文件中的 fallthrough
|