Evgenii Legotckoi
Evgenii LegotckoiOct. 23, 2018, 6:11 a.m.

C++ 17 - [[fallthrough]] attribute

Start at work to use a compiler that supports the standard C++17.

Actively we begin to use various features from this standard..

I was pleased with the [[fallthrough]] attribute for switch case constructions.


The essence of this attribute is that it indicates that the break statement was intentionally omitted in the switch case construction and that this is not a programmer's mistake.

The fact is that the switch case construction allows the end-to-end execution of the program code through all the choices of case, if the break statement has not been added.

Here is the classic version of this design.

int x = 100;

bool check_x()
{
    switch (x)  
    {
        case 0:
            return true;
        case 50:
            x = 0:
            break;
        case 100:
            return false;
        default:
            return false;
    }
}

But the variant with the end-to-end execution of the code when the break statement is omitted.

int x = 100;

bool check_x()
{
    switch (x)  
    {
        case 0:
        case 50:
        case 100:
            return true;
        default:
            return false;
    }
}

In this case, the same code will be executed for all values 0, 50, 100. But at the same time, the compiler may issue a prerequisite, and other programmers who may support your program code in the future may consider this an error.

To indicate that the omission of the break statement was made intentionally, the fallthrough attribute was entered,. And then this program code will look like this.

int x = 100;

bool check_x()
{
    switch (x)  
    {
        case 0:
            [[fallthrough]];
        case 50:
            [[fallthrough]];
        case 100:
            return true;
        default:
            return false;
    }
}

You can also inform the compiler that the default operator was also intentionally omitted.

int x = 100;

bool check_x()
{
    switch (x)  
    {
        case 0:
            [[fallthrough]];
        case 50:
            [[fallthrough]];
        case 100:
            return true;
        case 105:
            [[fallthrough]];
    }
    return false;
}

In general, a very useful thing, which removes discrepancies and the human factor.

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10

C++ - Тест 003. Условия и циклы

  • Result:42points,
  • Rating points-8
Last comments
i
innorwallNov. 9, 2024, 3:03 a.m.
Qt/C++ - Lesson 009. QTimer – How to work with timer? She also saw an increase in her energy levels and improvement in her shortness of breath better business bureau online pharmacy priligy Transfection induced VEGFR2 promoter activity i…
i
innorwallNov. 9, 2024, 2:36 a.m.
Step 1: Setting Up Your Environment for Python Programming This fixed combination drug may be used as initial therapy or substituted for previously titrated doses of the individual components priligy side effects Expanded clinical phenotype o…
i
innorwallNov. 9, 2024, 2:11 a.m.
PyQt5 - Lesson 003. QSystemTrayIcon - How to minimize application to tray where can i buy priligy The medium was changed every three days
i
innorwallNov. 8, 2024, 11:43 p.m.
Release of C++/Qt and QML application deployment utility CQtDeployer v1.4.0 (Binary Box) can i buy priligy in usa Reprod Biomed Online 10 3 310 319
i
innorwallNov. 8, 2024, 11:29 p.m.
EVILEG - 4 Years Online buy priligy Pelleted food this way of administration is less stressful for the mice, and a few vendors offer a commercially available chow
Now discuss on the forum
i
innorwallNov. 9, 2024, 2:44 a.m.
добавить qlineseries в функции We ve got the first batch ready for clinical trials, said Ovadje priligy usa LICENSED SHOP Nolvadex
9
9AnonimOct. 25, 2024, 4:10 p.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…
F
FynjyJuly 22, 2024, 11:15 a.m.
при создании qml проекта Kits есть но недоступны для выбора Поставил Qt Creator 11.0.2. Qt 6.4.3 При создании проекта Qml не могу выбрать Kits, они все недоступны, хотя настроены и при создании обычного Qt Widget приложения их можно выбрать. В чем может …

Follow us in social networks