C++ 属性: maybe_unused (C++17 以降)
From cppreference.com
< cpp | language | attributes
未使用のエンティティに関する警告を抑制します。
目次 |
[編集] 構文
[[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]] auto [a, b] = std::make_pair(42, 0.23);
|
(C++26以降) |
[[maybe_unused]] と宣言されたエンティティについて、そのエンティティまたはその構造化束縛が未使用の場合、コンパイラによって発行される未使用エンティティに関する警告は抑制されます。
|
[[maybe_unused]] と宣言されたラベルについて、それらが未使用の場合、コンパイラによって発行される未使用ラベルに関する警告は抑制されます。 |
(C++26以降) |
[編集] 例
このコードを実行
#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 = not false and not true; 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() {}
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| CWG 2360 | C++17 | 構造化束縛に [[maybe_unused]] を適用できない | 許可 |
[編集] 参考文献
- C++23標準 (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]
[編集] 関連項目
| maybe_unused のための C ドキュメント
|