C++关键字之fallthrough

简介: C++关键字之fallthrough

在C++17中引入了fallthrough属性。该属性主要用于switch语句中。在C++的switch语句中,如果当前case分支中不加break, 便会执行下一个case分支的代码。

如下所示,由于n的值为1,代码首先执行case 1分支,然后又因为case 1分支中没有加break,所以接着执行case 2分支、case 3分支,一直到default分支


#include <iostream> 
using namespace std; 
// Driver Code 
int main() 
{ 
    int n = 1; 
    // Switch Cases 
    switch (n) { 
    case 1: { 
        cout << "work through one \n"; 
    } 
    case 2: { 
        cout << "work through two \n"; 
    } 
    case 3: { 
        cout << "work through three \n"; 
    } 
    default: { 
        cout << "work through default \n"; 
    } 
    } 
    return 0; 
}


所以输出为


work through one 
work through two 
work through three 
work through default


而很多C++初学者容易犯这样的错误:在本应当在case分支中加入break的时候却忘了加了。于是编译器会针对这种情况输出Warning信息,提醒程序员他可能忘了加break了。


> g++-9 -W 1.cpp
1.cpp:14:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   14 |         cout << "work through one \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~
1.cpp:17:5: note: here
   17 |     case 2: {
      |     ^~~~
1.cpp:18:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   18 |         cout << "work through two \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~
1.cpp:21:5: note: here
   21 |     case 3: {
      |     ^~~~
1.cpp:22:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   22 |         cout << "work through three \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~~~
1.cpp:25:5: note: here
   25 |     default: {
      |     ^~~~~~~



但是有些时候我们为了实现一些特定的逻辑,所以有意不加break, 但是又不想听到编译器的抱怨,该怎么样让编译器"闭嘴"呢?此时C++17中引入的fallthrough便派上用场了


#include <iostream> 
using namespace std; 
// Driver Code 
int main() 
{ 
    int n = 1; 
    // Switch Cases 
    switch (n) { 
    case 1: { 
        cout << "work through one \n"; 
                [[fallthrough]];
    } 
    case 2: { 
        cout << "work through two \n"; 
                [[fallthrough]];
    } 
    case 3: { 
        cout << "work through three \n"; 
                [[fallthrough]];
    } 
    default: { 
        cout << "work through default \n"; 
    } 
    } 
    return 0; 
}


编译结果如下,编译器真的"闭嘴"了


$ g++-9 -W 1.cpp
相关文章
|
4月前
|
存储 安全 编译器
【C++专栏】C++入门 | auto关键字、范围for、指针空值nullptr
【C++专栏】C++入门 | auto关键字、范围for、指针空值nullptr
62 0
|
2月前
|
存储 安全 编译器
【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
|
3月前
|
存储 安全 编译器
【C++航海王:追寻罗杰的编程之路】引用、内联、auto关键字、基于范围的for、指针空值nullptr
【C++航海王:追寻罗杰的编程之路】引用、内联、auto关键字、基于范围的for、指针空值nullptr
52 5
|
2月前
|
存储 编译器 C++
C++从遗忘到入门问题之float、double 和 long double 之间的主要区别是什么
C++从遗忘到入门问题之float、double 和 long double 之间的主要区别是什么
|
3月前
|
存储 编译器 程序员
C++一分钟之-auto关键字与类型推导
【6月更文挑战第21天】`auto`在C++11中重生,简化了类型声明,尤其在处理复杂类型时。它让编译器根据初始化值推导变量类型,减少了冗余和错误。使用`auto`简化了迭代器声明和函数返回类型推导,但也带来挑战:类型推导可能不直观,未初始化的`auto`是错误的,且过度使用影响可读性。使用`auto&`和`auto*`明确引用和指针,`decltype`辅助复杂类型推导,保持适度使用以维持代码清晰。
36 1
|
3月前
|
存储 安全 编译器
C++进阶之路:何为引用、内联函数、auto与指针空值nullptr关键字
C++进阶之路:何为引用、内联函数、auto与指针空值nullptr关键字
29 2
|
4月前
|
C++
C++中使用namespace关键字定义和访问命名空间的技术性探讨
C++中使用namespace关键字定义和访问命名空间的技术性探讨
30 3
|
3月前
|
Unix 编译器 C语言
【C++航海王:追寻罗杰的编程之路】关键字、命名空间、输入输出、缺省、重载汇总
【C++航海王:追寻罗杰的编程之路】关键字、命名空间、输入输出、缺省、重载汇总
20 0
|
3月前
|
存储 安全 编译器
【C++】:函数重载,引用,内联函数,auto关键字,基于范围的for循环,nullptr关键字
【C++】:函数重载,引用,内联函数,auto关键字,基于范围的for循环,nullptr关键字
26 0
|
3月前
|
编译器 C语言 C++
【C++】:C++关键字,命名空间,输入&输出,缺省参数
【C++】:C++关键字,命名空间,输入&输出,缺省参数
31 0