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)

이전 case 레이블에서의 fall through가 의도적이며, fallthrough에 대해 경고하는 컴파일러에 의해 진단되어서는 안 됨을 나타냅니다.

목차

구문

[ [ fallthrough ] ]

설명

null 문 에만 적용하여 fallthrough 문 ( [ [ fallthrough ] ] ; )을 생성할 수 있습니다.

fallthrough 문은 오직 switch 문 내에서만 사용될 수 있으며, 다음으로 실행될 문은 해당 switch 문의 case 또는 default 레이블을 가진 문이어야 합니다. fallthrough 문이 루프 내부에 있는 경우, 다음 (레이블이 지정된) 문은 해당 루프의 동일한 반복의 일부여야 합니다.

예제

void f(int n)
{
    void g(), h(), i();
    switch (n)
    {
        case 1:
        case 2:
            g();
            [[fallthrough]];
        case 3: // fallthrough에 대한 경고 없음
            h();
        case 4: // 컴파일러가 fallthrough에 대해 경고할 수 있음
            if (n < 3)
            {
                i();
                [[fallthrough]]; // 올바름
            }
            else
            {
                return;
            }
        case 5:
            while (false)
            {
                [[fallthrough]]; // 잘못된 형식: 다음 문장이 동일한 반복의
                                 //             일부가 아님
            }
        case 6:
            [[fallthrough]]; // 잘못된 형식, 후속 case 또는 default 레이블 없음
    }
}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
CWG 2406 C++17 [ [ fallthrough ] ] 가 대상 switch 문 내부에 중첩된 루프에서
나타날 수 있었음
금지됨

참고문헌

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

참고 항목

C 문서 에 대한 fallthrough