CppDS.com

C++ 98 11 14 17 20 手册

C++ 属性:maybe_unused (C++17 起)

来自cppreference.com
< cpp‎ | language‎ | attributes

抑制针对未使用实体的警告。

语法

[[maybe_unused]]

解释

此属性可出现在下列实体的声明中:

若编译器针对未使用实体发布警告,则对于任何声明为 maybe_unused 的实体抑制该警告。

示例

[[maybe_unused]] void f([[maybe_unused]] bool thing1,
                        [[maybe_unused]] bool thing2)
{
   [[maybe_unused]] bool b = thing1 && thing2;
   assert(b); // 发行模式中,assert 在编译中被去掉,因而未使用 b
              // 无警告,因为它被声明为 [[maybe_unused]]
} // 未使用参数 thing1 与 thing2,无警告


关闭