名前空間
変種
操作

C属性: maybe_unused (C23以降)

From cppreference.com
< c‎ | language‎ | attributes

未使用のエンティティに対する警告を抑制します。

目次

[編集] 構文

[[ maybe_unused ]]
[[ __maybe_unused__ ]]

[編集] 説明

この属性は、次のエンティティの宣言に適用できます。

  • 構造体/共用体: struct [[maybe_unused]] S;,
  • typedef名: [[maybe_unused]] typedef S* PS;,
  • オブジェクト: [[maybe_unused]] int x;,
  • 構造体/共用体メンバー: union U { [[maybe_unused]] int n; };,
  • 関数: [[maybe_unused]] void f(void);,
  • 列挙型: enum [[maybe_unused]] E {};,
  • 列挙子: enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };

コンパイラが未使用のエンティティに対して警告を発する場合、maybe_unusedと宣言されたエンティティに対するその警告は抑制されます。

[編集]

#include <assert.h>
 
[[maybe_unused]] void f([[maybe_unused]] _Bool cond1, [[maybe_unused]] _Bool cond2)
{
   [[maybe_unused]] _Bool b = cond1 && cond2;
   assert(b); // in release mode, assert is compiled out, and b is unused
              // no warning because it is declared [[maybe_unused]]
} // parameters cond1 and cond2 are not used, no warning
 
int main(void)
{
    f(1, 1);
}

[編集] 関連項目

C++ドキュメント maybe_unused について
English 日本語 中文(简体) 中文(繁體)