[[maybe_unused]]在枚举器上 [英] [[maybe_unused]] on enumerator
问题描述
查看 [[maybe_unused]]
的规范,它指出:
Looking at the specification of the [[maybe_unused]]
, it states:
出现在类,typedef,变量,非静态数据成员,函数,枚举或枚举器的声明中.如果编译器对未使用的实体发出警告,则对于声明为maybe_unused的任何实体,该警告将被抑制.
Appears in the declaration of a class, a typedef, a variable, a nonstatic data member, a function, an enumeration, or an enumerator. If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused.
正如上面提到的枚举器,我有点希望它有一个用例.我唯一想出的是-Wswitch
警告,我在Clang,GCC和MSVC上尝试过.
As this mentions enumerator, I kinda expect it to have a use-case. As the only thing I could come up with is the -Wswitch
warning, I tried it with Clang, GCC and MSVC.
enum A
{
B,
C [[maybe_unused]]
};
void f(A a)
{
switch (a)
{
case B: break;
}
}
所有3个编译器都给我以下警告的一种变化:
All 3 compilers give me a variation of following warning:
<source>:9:13: warning: enumeration value 'C' not handled in switch [-Wswitch]
switch (a)
^
这是使用此属性的有效用例吗,是否还有其他用例在此位置添加属性,或者这仅仅是无用的添加?
Is this a valid use case for using this attribute, are there other use cases for adding the attribute at this location or is this just a useless addition?
推荐答案
A bug was logged for Clang and marked as resolved: https://bugs.llvm.org/show_bug.cgi?id=36231
这似乎可以确认枚举值允许在没有警告的情况下在开关中丢失,以防被标记为[[maybe_unused]]
This seems to confirm that the enum value is allowed to be missing in a switch without warning in case it is tagged with [[maybe_unused]]
这篇关于[[maybe_unused]]在枚举器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!