名前空間
変種
操作

C++ 属性: maybe_unused (C++17 以降)

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
C++言語
全般
フロー制御
条件実行文
if
繰り返し文 (ループ)
for
範囲for (C++11)
ジャンプ文
関数
関数宣言
ラムダ式
inline指定子
動的例外仕様 (C++17まで*)
noexcept指定子 (C++11)
例外
名前空間
指定子
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点数
文字 - 文字列 - nullptr (C++11)
ユーザー定義 (C++11)
ユーティリティ
属性 (C++11)
typedef宣言
型エイリアス宣言 (C++11)
キャスト
メモリ確保
クラス
クラス固有の関数プロパティ
explicit (C++11)
static

特殊メンバ関数
テンプレート
その他
 
 
属性
(C++23)
(C++11)(C++26 まで)
(C++14)
(C++20)
maybe_unused
(C++17)
(C++17)
(C++11)
(C++20)
 

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

目次

[編集] 構文

[[maybe_unused]]

[編集] 説明

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

(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 ドキュメント
English 日本語 中文(简体) 中文(繁體)