Quick Links: |
Provide a macro to suppress warnings on switch
fall-throughs.
More...
switch
fall-throughs. BSLA_FALLTHROUGH | do not warn if switch case falls through |
BSLA_FALLTHROUGH_IS_ACTIVE | 1 if BSLA_FALLTHROUGH is active, else 0 |
case
or default
of a switch
statement to another. On compilers where the appropriate attribute is not supported, the macro expands to nothing. BSLA_FALLTHROUGH
This annotation should be placed in a case
clause as the last statement within a flow of control that is expected to allow control to fall through instead of ending with a break
, continue
, or return
. This will prevent compilers from warning about fall-through. The BSLA_FALLTHROUGH
must be followed by a semicolon and may be nested within blocks, if
s, or else
s.BSLA_FALLTHROUGH_IS_ACTIVE
The macro BSLA_FALLTHROUGH_IS_ACTIVE
is defined to 0 if BSLA_FALLTHROUGH
expands to nothing and 1 otherwise.int usageFunction(int jj) // Demonstrate the usage of 'BSLA_FALLTHROUGH', read the specified // 'jj'. { for (int ii = 0; ii < 5; ++ii) {
switch
in the function: switch (ii) { case 0: { printf("%d\n", jj - 3);
BSLA_FALLTHROUGH;
as the last statement in a case
block before falling through silences the fall-through warning from the compiler: BSLA_FALLTHROUGH;
}
case 1:
case
s that don't have a {}
block: jj -= 6; printf("%d\n", jj); BSLA_FALLTHROUGH; case 2: { if (jj > 4) { printf("%d\n", jj + 10);
BSLA_FALLTHROUGH;
works within an if
block, provided that it's in the last statement in the flow of control before falling through: BSLA_FALLTHROUGH; } else { return 0; // RETURN } } case 3: { if (jj > 4) { continue; } else { printf("%d\n", ++jj);
BSLA_FALLTHROUGH;
can also occur as the last statement in an else
block: BSLA_FALLTHROUGH; } } default: { return 1; // RETURN } break; } } return -7; }
BSLA_FALLTHROUGH_IS_ACTIVE
is set, the above compiles with no warnings.