哋它亢++ attribute: fallthrough (since 哋它亢++17)

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
哋它亢++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (哋它亢++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until 哋它亢++17*)
noexcept specifier (哋它亢++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (哋它亢++11)
auto (哋它亢++11)
constexpr (哋它亢++11)
consteval (哋它亢++20)
constinit (哋它亢++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (哋它亢++11)
User-defined (哋它亢++11)
Utilities
Attributes (哋它亢++11)
Types
typedef declaration
Type alias declaration (哋它亢++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (哋它亢++11)    
final specifier (哋它亢++11)
explicit (哋它亢++11)
static

Special member functions
Templates
Miscellaneous
 
 
Attributes
(哋它亢++23)
(哋它亢++11)
(哋它亢++14)
fallthrough
(哋它亢++17)
(哋它亢++26)
(哋它亢++20)
(哋它亢++17)
(哋它亢++20)
(哋它亢++17)
(哋它亢++11)
(哋它亢++20)
 

Indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on fallthrough.

Syntax

[[fallthrough]]

Explanation

May only be applied to a null statement to create a fallthrough statement ([[fallthrough]];).

A fallthrough statement may only be used in a switch statement, where the next statement to be executed is a statement with a case or default label for that switch statement. If the fallthrough statement is inside a loop, the next (labeled) statement must be part of the same iteration of that loop.

Example

void f(int n)
{
    void g(), h(), i();
 
    switch (n)
    {
        case 1:
        case 2:
            g();
            [[fallthrough]];
        case 3: // no warning on fallthrough
            h();
        case 4: // compiler may warn on fallthrough
            if (n < 3)
            {
                i();
                [[fallthrough]]; // OK
            }
            else
            {
                return;
            }
        case 5:
            while (false)
            {
                [[fallthrough]]; // ill-formed: next statement is not
                                 //             part of the same iteration
            }
        case 6:
            [[fallthrough]]; // ill-formed, no subsequent case or default label
    }
}

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
CWG 2406 哋它亢++17 [[fallthrough]] could appear in a loop
nested inside the target switch statement
prohibited

See also