Namespaces
Variants

C++ attribute: maybe_unused (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)
(C++20)
maybe_unused
(C++17)
(C++17)
(C++11)
(C++20)

사용되지 않는 엔터티에 대한 경고를 억제합니다.

목차

구문

[ [ maybe_unused ] ]

설명

이 속성은 다음 엔터티의 선언에 나타날 수 있습니다:

(C++26부터)

[ [ maybe_unused ] ] 로 선언된 엔티티의 경우, 해당 엔티티나 그 구조적 바인딩이 사용되지 않을 때 컴파일러가 발생시키는 미사용 엔티티에 대한 경고가 억제됩니다.

[ [ maybe_unused ] ] 로 선언된 레이블은 사용되지 않을 경우, 컴파일러가 발생시키는 사용되지 않은 레이블에 대한 경고가 억제됩니다.

(C++26부터)

예제

#include <cassert>
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
                        [[maybe_unused]] bool thing2)
{
    [[maybe_unused]] lbl: // 레이블 "lbl"이 사용되지 않아도 경고 없음
    [[maybe_unused]] bool b = not false and not true;
    assert(b); // 릴리스 모드에서 assert는 컴파일되지 않고 "b"는 사용되지 않음
               // [[maybe_unused]]로 선언되어 있어 경고가 발생하지 않음
} // 매개변수 "thing1"과 "thing2"가 사용되지 않아도 경고 없음
int main() {}

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
CWG 2360 C++17 구조적 바인딩에 [ [ maybe_unused ] ] 적용 불가능 허용됨

참조문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 9.12.8 Maybe unused 속성 [dcl.attr.unused]
  • C++20 표준 (ISO/IEC 14882:2020):
  • 9.12.7 Maybe unused 속성 [dcl.attr.unused]
  • C++17 표준(ISO/IEC 14882:2017):
  • 10.6.6 Maybe unused 속성 [dcl.attr.unused]

참고 항목

C 문서 참조: maybe_unused