Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/attributes/fallthrough"

From cppreference.com
< cpp‎ | language‎ | attributes
(cwg 2406 I *think* "the same execution of the substatement of the innermost enclosing iteration statement" means "same iteration")
m (References: Consistent order: newest to oldest)
 
(8 intermediate revisions by 6 users not shown)
Line 7: Line 7:
 
{{sdsc begin}}
 
{{sdsc begin}}
 
{{sdsc|1=
 
{{sdsc|1=
{{ttb|<nowiki>[[fallthrough]]</nowiki>}}
+
{{ttb|{{c/core|[[fallthrough]]}}}}
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
 
===Explanation===
 
===Explanation===
May only be applied to a [[cpp/language/statements#Expression_statements|null statement]] to create a ''fallthrough statement'' ({{c|[[fallthrough]];}}).
+
May only be applied to a [[cpp/language/statements#Expression statements|null statement]] to create a ''fallthrough statement'' ({{c|[[fallthrough]];}}).
  
 
A fallthrough statement may only be used in a [[cpp/language/switch|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.
 
A fallthrough statement may only be used in a [[cpp/language/switch|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.
 
Indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on fallthrough.
 
  
 
===Example===
 
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
void f(int n) {
+
void f(int n)
  void g(), h(), i();
+
{
  switch (n) {
+
    void g(), h(), i();
     case 1:
+
   
    case 2:
+
    switch (n)
      g();
+
     {
    [[fallthrough]];
+
        case 1:
    case 3: // no warning on fallthrough
+
        case 2:
      h();
+
            g();
    case 4: // compiler may warn on fallthrough
+
            [[fallthrough]];
      if(n < 3) {
+
        case 3: // no warning on fallthrough
          i();
+
            h();
          [[fallthrough]]; // OK
+
        case 4: // compiler may warn on fallthrough
      }
+
            if (n < 3)
      else {
+
            {
          return;
+
                i();
      }
+
                [[fallthrough]]; // OK
    case 5:
+
            }
      while (false) {
+
            else
        [[fallthrough]]; // ill-formed: next statement is not part of the same iteration
+
            {
      }
+
                return;
    case 6:
+
            }
      [[fallthrough]]; // ill-formed, no subsequent case or default label
+
        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
 +
    }
 
}
 
}
 
}}
 
}}
  
{{langlinks|ja|zh}}
+
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=cwg|dr=2406|std=C++17|before={{c|[[fallthrough]]}} could appear in a loop<br>nested inside the target switch statement|after=prohibited}}
 +
{{dr list end}}
 +
 
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section=9.12.6|title=Fallthrough attribute|id=dcl.attr.fallthrough}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=9.12.5|title=Fallthrough attribute|id=dcl.attr.fallthrough}}
 +
{{ref std end}}
 +
{{ref std c++17}}
 +
{{ref std|section=10.6.5|title=Fallthrough attribute|id=dcl.attr.fallthrough}}
 +
{{ref std end}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/language/attributes/fallthrough}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|es|ja|zh}}

Latest revision as of 11:43, 12 August 2024

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

Special member functions
Templates
Miscellaneous
 
 
Attributes
(C++23)
(C++14)
fallthrough
(C++17)
(C++20)
(C++17)
(C++11)
(C++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.

Contents

[edit] Syntax

[[fallthrough]]

[edit] 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.

[edit] 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
    }
}

[edit] Defect reports

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

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

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 9.12.6 Fallthrough attribute [dcl.attr.fallthrough]
  • C++20 standard (ISO/IEC 14882:2020):
  • 9.12.5 Fallthrough attribute [dcl.attr.fallthrough]
  • C++17 standard (ISO/IEC 14882:2017):
  • 10.6.5 Fallthrough attribute [dcl.attr.fallthrough]

[edit] See also

C documentation for fallthrough