C attribute: maybe_unused (since C23)
From cppreference.net
<
c
|
language
|
attributes
未使用エンティティに関する警告を抑制します。
目次 |
構文
[[
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
|