fallthrough
在switch语句中,不同case间如果有statement(如函数调用,赋值等操作),但没有break语句,很有可能是coder的一个bug。在C++17中,部分compiler会提示用户,给出相应的warning信息。但如果是coder故意而为,又不希望出现该warning信息,可以使用该关键词提示compiler。具体用法请见下面的例子:
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num;
switch(num) {
case 1:
cout << "input 1" << endl;
[[fallthroug]]; // ok, compiler gives no warn
case 2:
cout << "input 2" << endl; // compiler might give warning message
default:
cout << "value not recognize" << endl;
}
return 0;
}