Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |language‎ |attributes
       
       
      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
       
      Declarations
       
      Attributes
      (C++23)
      (C++11)(until C++26)
      (C++14)
      (C++20)
      maybe_unused
      (C++17)
      (C++17)
      (C++11)
      (C++20)
       

      Suppresses warnings on unused entities.

      Contents

      [edit]Syntax

      [[maybe_unused]]

      [edit]Explanation

      This attribute can appear in the declaration of the following entities:

      (since C++26)

      For entities declared[[maybe_unused]], if the entities or their structured bindings are unused, the warning on unused entities issued by the compiler is suppressed.

      For labels declared[[maybe_unused]], if they are unused, the warning on unused labels issued by the compiler is suppressed.

      (since C++26)

      [edit]Example

      Run this code
      #include <cassert> [[maybe_unused]]void f([[maybe_unused]]bool thing1,[[maybe_unused]]bool thing2){[[maybe_unused]] lbl:// the label “lbl” is not used, no warning[[maybe_unused]]bool b= notfalse and nottrue;assert(b);// in release mode, assert is compiled out, and “b” is unused// no warning because it is declared [[maybe_unused]]}// parameters “thing1” and “thing2” are not used, no warning int main(){}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2360C++17could not apply[[maybe_unused]] to structured bindingsallowed

      [edit]References

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

      [edit]See also

      C documentation formaybe_unused
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/attributes/maybe_unused&oldid=181406"

      [8]ページ先頭

      ©2009-2025 Movatter.jp