Privacy policyContactsAbout siteOpinionsGitHubDonate
© EVILEG 2015-2018
Recommend hosting
TIMEWEB

C++ 17 - [[fallthrough]] attribute

cpp, C++17, fallthrough

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.






Comments

Only authorized users can post comments.
Please, Log in or Sign up
v
Jan. 17, 2019, 11:51 a.m.
vitalir12

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

  • Result:20points,
  • Rating points-10
v
Jan. 17, 2019, 11:49 a.m.
vitalir12

C++ - Test 002. Constants

  • Result:50points,
  • Rating points-4
v
Jan. 17, 2019, 11:13 a.m.
vitalir12

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

  • Result:28points,
  • Rating points-10
Last comments
I
Jan. 16, 2019, 8:06 a.m.
IscanderChe

Заработало. Забыл model->select(); вписать.
I
Jan. 16, 2019, 8:02 a.m.
IscanderChe

Всё равно пусто, хотя строка с данными в базу добавляется.
Jan. 16, 2019, 7:51 a.m.
Евгений Легоцкой

потому, что нужно сохранять информацию для всех остальных ролей и столбцов через вызов переопределённого метода. Да к тому же вы ещё и зациклили вызов метода data. QVariant MySqlTableModel:...
I
Jan. 16, 2019, 7:43 a.m.
IscanderChe

Сделал вот так. В tableView ничего нет, кроме заголовка. QVariant MySqlTableModel::data(const QModelIndex &index, int role) const{ if (role == Qt::DisplayRole) { QTime ...
Now discuss on the forum
Jan. 18, 2019, 11:26 a.m.
nayk1982

Для Desktop делал так: void pause(int ms){ QTimer timer; timer.setInterval( qBound(1, ms, 3600000) ); timer.setSingleShot(true); QEventLoop loop; QObject::connect(&...
Jan. 17, 2019, 12:01 p.m.
Алексей Внуков

у меня просто есть отдельное поле с чекбоксамими какие колонки нужно отображать CheckBox { id: checkBox text: qsTr("some text") checked: true onC...
Jan. 15, 2019, 4:53 p.m.
Михаиллл

Спасибо, заработало.Но выдало обычный текст без форатирования HTML.Придется искать дальше
Jan. 15, 2019, 12:52 p.m.
BlinCT

Я же вам выше написал CLion умеет работать с ремоут машинами. И Qt так же собирает.
Join us in social networks

For registered users on the site there is a minimum amount of advertising