Use C++17 compiler prompts to improve code quality

Alas, the author of some old codes really hates all kinds of bad habits, such as not checking whether the return value of the function is success or failure, just call it in one go; in the Switch statement, case is written but not break, etc.

Some authors even directly turn off all warning messages of the compiler, intending to "clean the compilation period", which is simply unreasonable.

Let's introduce the compiler hints and keyword usage under the C++17 standard.

Despotism does not look at the return value of the function

#include <iostream> 
using namespace std; 

bool check(int x)
{
    return x<10;
}
  
// Driver Code 
int main() 
{
    for(int i=0;i<3;i++)
        check(i);

}

The check in this place does not look at whether it is reasonable at all, it is just a cycle, which is very bad. However, we will not find this problem if we turn on all warnings, because the return value warning has not yet been included in the warning.

In C++17, the keyword `nodiscard` was introduced to mark the situation where the return value of the function is not processed.

#include <iostream> 
using namespace std; 

[[nodiscard]] bool check(int x)
{
    return x<10;
}
  
// Driver Code 
int main() 
{
    for(int i=0;i<3;i++)
        check(i);

}

At this time, compile again and start to clean up this situation:

$ g++ a.cc -W
a.cc: In function ‘int main()’:
a.cc:13:14: warning: ignoring return value of ‘bool check(int)’, declared with attribute nodiscard [-Wunused-result]
   13 |         check(i);
      |         ~~~~~^~~
a.cc:4:20: note: declared here
    4 | [[nodiscard]] bool check(int x)
      |                    ^~~~~

The interesting thing about this keyword is that the person who provides the service requires all people who call the service to process the return value of the service, rather than the person who calls the service decides whether to handle it or not. This is also in line with the design principle of the service.

Dedicated to write case but not break

This warning message is not a C++17 patent. Generally speaking, we want our code to be rigorous, and we must turn on all warnings, such as the following code

#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; 
} 

A bunch of breaks are not written, and various logic errors have already been booked.

How to enable all compiler warnings: `g++ a.cc -W`

$ g++ a.cc -W
a.cc: In function ‘int main()’:
a.cc:12:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   12 |         cout << "work through one \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~
a.cc:14:5: note: here
   14 |     case 2: {
      |     ^~~~
a.cc:15:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   15 |         cout << "work through two \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~
a.cc:17:5: note: here
   17 |     case 3: {
      |     ^~~~
a.cc:18:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   18 |         cout << "work through three \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~~~
a.cc:20:5: note: here
   20 |     default: {
      |     ^~~~~~~

Then, if some breaks are not written on purpose here, you have to spend energy to check them one by one, which is a headache. C++17 introduces the keyword `fallthrogh`, indicating that this kind of break is not written on purpose, and it will Reduce the number of warnings.

#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"; 
    } 
    default: { 
        cout << "work through default \n"; 
    } 
    } 
    return 0; 
} 

At this point, the warning will be reduced

$ g++ a.cc -W
a.cc: In function ‘int main()’:
a.cc:20:17: warning: this statement may fall through [-Wimplicit-fallthrough=]
   20 |         cout << "work through three \n";
      |                 ^~~~~~~~~~~~~~~~~~~~~~~
a.cc:22:5: note: here
   22 |     default: {
      |     ^~~~~~~

Guess you like

Origin blog.csdn.net/qq_33882435/article/details/127789141