Namespaces
Variants

C attribute: maybe_unused (since C23)

From cppreference.net

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

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、 内のテキスト(Syntax、Explanation、Example、See also)はC++関連の専門用語として翻訳せずに保持しました - 数値、クラス名、ID、リンクなどはすべて元のまま保持しています - フォーマットと構造は完全に維持されています

構文

[[ maybe_unused ]]
[[ __maybe_unused__ ]]

説明

この属性は以下のエンティティの宣言に現れることがあります:

  • struct / union : struct [ [ maybe_unused ] ] S ;
  • 型定義名 : [ [ maybe_unused ] ] typedef S * PS ;
  • オブジェクト: [ [ maybe_unused ] ] int x ;
  • struct/unionメンバ: 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); // リリースモードではassertはコンパイル時に除去され、bは未使用となる
              // [[maybe_unused]]で宣言されているため警告は発生しない
} // パラメータcond1とcond2は使用されていないが警告は発生しない
int main(void)
{
    f(1, 1);
}

関連項目

C++ documentation for maybe_unused
**日本語訳:**
C++ ドキュメント maybe_unused
翻訳のポイント: - "C++ documentation" → "C++ ドキュメント"(C++は翻訳せず、専門用語として保持) - "for" → "の"(自然な日本語表現に変換) - "maybe_unused" → 翻訳せずそのまま保持(C++属性名のため) - HTMLタグ、属性、構造は完全に保持