For more exciting content, please pay attention to wechat public number: Back-end technology Cabin

The fallthrough property was introduced in C++17. This property is used primarily in switch statements. In the C++ switch statement, if there is no break in the current case branch, the code in the next case branch is executed.

As shown below, since the value of n is 1, the code first executes case 1 branch, then case 2 branch, case 3 branch, and then default branch because there is no break in case 1 branch

#include <iostream> 
using namespace std; 
  
// Driver Code 
int main(a) 
{ 
    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; 
} 
Copy the code

So the output is

work through one 
work through two 
work through three 
work through default 
Copy the code

Many C++ beginners make the mistake of forgetting to add a break in the case branch when they should have. The compiler then prints a Warning to warn the programmer that he may have forgotten to 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: {
      |     ^~~~~~~
Copy the code

But sometimes we don’t want to hear the compiler complain because we want to implement certain logic, so we don’t want the compiler to shut up. This is where the fallthrough introduced in C++17 comes in handy

#include <iostream> 
using namespace std; 
  
// Driver Code 
int main(a) 
{ 
    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; 
} 
Copy the code

As a result, the compiler literally shut up

$ g++-9 -W 1.cpp
Copy the code

Recommended reading

  • Boltdb Learning Notes 2 — data structures
  • Boltdb learning notes – storage management
  • Boltdb Learning Notes ○ – Overview
  • Read clickHouse cluster monitoring
  • STL source code analysis — Vector
  • Principles of the ZooKeeper Client
  • Redis implements distributed locking
  • Restrict the C/C++ keyword
  • Python garbled nine questions

For more exciting content, please scan the code to follow the wechat public number: back-end technology cabin. If you think this article is helpful to you, please share, forward, read more.