Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does '[[fallthrough]]' need the square brackets '[[]]'?

Tags:

c++

c++17

The switch statement has commands to manage its flow, which are break and [[fallthrough]].

break forces the flow to jump out of the switch and [[fallthrough]] keeps executing commands ignoring the case validation (actually that is the default behavior of switch). For example:

switch (x) {
       case 0:
              ...statements...
              break;             //will jump to (...after switch statements...)
       case 1:
              ...statements...
              [[fallthrough]];   //don't jump
       case 2:
              ...statements...   //will be executed if x == 1 or x == 2
}
...after switch statements...

Why was [[fallthrough]] defined with square brackets if break wasn't?

*The command [[fallthrough]] was introduced in C++17 as indicated at site cppreference.

like image 483
TheArchitect Avatar asked Mar 14 '20 10:03

TheArchitect


2 Answers

This is an attribute. These attributes already existed in compilers like GCC to give extensions to the language. It could be used like __attribute__((<attribute_name>)). They decided this was not just hardly readable, but also problematic in other aspects, so they introduced this [[<attribute>]] syntax for clarity and better safety in c++11.

So it is important to note that this double square bracket is not just for fallthrough, but for lot of other attribute too like noreturn and carries_dependency. See this for more info.

Update: Just an interesting note: In LLVM source code there is a macro for this which look like this:

/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
#define LLVM_FALLTHROUGH [[fallthrough]]
#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
#elif __has_attribute(fallthrough)
#define LLVM_FALLTHROUGH __attribute__((fallthrough))
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
#define LLVM_FALLTHROUGH [[clang::fallthrough]]
#else
#define LLVM_FALLTHROUGH
#endif

Well it uses the [[fallthrough]] in it also, but it could be also observed how __attribute__((fallthrough)) is used if its an older c++ compiler which support this.

like image 125
Eraklon Avatar answered Oct 01 '22 04:10

Eraklon


[[fallthrough]] is syntactic noise. If you remove it the code means exactly the same thing. Its purpose is to tell aggressive compilers that you don't want to get a warning that you might not be smart enough to understand the code you've written.

Those things in double square brackets are attributes; the underlying requirement for an attribute is that adding or removing it doesn't change the meaning of the program.

The reason that it's different from break is that break actually means something: it says that the code has reached the end of the processing for that particular case, so execution should continue after the end of the switch statement.

An attribute is not a keyword. A keyword is always treated as a special identifier by the compiler (except when it's inside a quoted string; "switch" is just text). Making fallthrough a keyword would mean that code like bool fallthrough = false; would become ill-formed.

like image 23
Pete Becker Avatar answered Oct 01 '22 04:10

Pete Becker