属性: maybe_unused (C++17以上)
提供: cppreference.com
未使用のエンティティに対する警告を抑制します。
構文
[[maybe_unused]]
|
|||||||||
説明
この属性は以下のエンティティの宣言で現れることができます。
- クラス、構造体、共用体:
struct [[maybe_unused]] S; - typedef (エイリアス宣言によって宣言されたものを含む):
[[maybe_unused]] typedef S* PS;,using PS [[maybe_unused]] = S*; - 変数 (静的データメンバを含む):
[[maybe_unused]] int x; - 非静的データメンバ:
union U { [[maybe_unused]] int n; }; - 関数:
[[maybe_unused]] void f(); - 列挙:
enum [[maybe_unused]] E {}; - 列挙子:
enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };
コンパイラが未使用のエンティティに警告を発する場合、 maybe_unused 宣言されたエンティティに対しては警告が抑制されます。
例
Run this code
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
[[maybe_unused]] bool thing2)
{
[[maybe_unused]] bool b = thing1 && thing2;
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