Namespaces
Variants

C++ attribute: fallthrough (since C++17)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
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
Special member functions
Templates
Miscellaneous
Attributes
(C++23)
(C++11) (until C++26)
(C++14)
fallthrough
(C++17)
(C++20)
(C++17)
(C++11)
(C++20)

Indica que la caída desde la etiqueta de caso anterior es intencional y no debe ser diagnosticada por un compilador que advierte sobre caídas.

Contenidos

Sintaxis

[ [ fallthrough ] ]

Explicación

Solo puede aplicarse a una sentencia nula para crear una sentencia fallthrough ( [ [ fallthrough ] ] ; ).

Una declaración fallthrough solo puede utilizarse en una switch statement, donde la siguiente declaración a ejecutar es una declaración con una etiqueta case o default para esa declaración switch. Si la declaración fallthrough está dentro de un loop, la siguiente declaración (etiquetada) debe ser parte de la misma iteración de ese loop.

Ejemplo

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
    }
}

Informes de defectos

Los siguientes informes de defectos que modifican el comportamiento se aplicaron retroactivamente a los estándares de C++ publicados anteriormente.

DR Se aplica a Comportamiento publicado Comportamiento correcto
CWG 2406 C++17 [ [ fallthrough ] ] podía aparecer en un bucle
anidado dentro de la sentencia switch objetivo
prohibido

Referencias

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

Véase también

Documentación de C para fallthrough