Evgenii Legotckoi
Oct. 23, 2018, 4:11 p.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.

  1. int x = 100;
  2.  
  3. bool check_x()
  4. {
  5. switch (x)
  6. {
  7. case 0:
  8. return true;
  9. case 50:
  10. x = 0:
  11. break;
  12. case 100:
  13. return false;
  14. default:
  15. return false;
  16. }
  17. }

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

  1. int x = 100;
  2.  
  3. bool check_x()
  4. {
  5. switch (x)
  6. {
  7. case 0:
  8. case 50:
  9. case 100:
  10. return true;
  11. default:
  12. return false;
  13. }
  14. }

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.

  1. int x = 100;
  2.  
  3. bool check_x()
  4. {
  5. switch (x)
  6. {
  7. case 0:
  8. [[fallthrough]];
  9. case 50:
  10. [[fallthrough]];
  11. case 100:
  12. return true;
  13. default:
  14. return false;
  15. }
  16. }

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

  1. int x = 100;
  2.  
  3. bool check_x()
  4. {
  5. switch (x)
  6. {
  7. case 0:
  8. [[fallthrough]];
  9. case 50:
  10. [[fallthrough]];
  11. case 100:
  12. return true;
  13. case 105:
  14. [[fallthrough]];
  15. }
  16. return false;
  17. }

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

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html