[[maybe_unused]] in the enumerator

Looking at the specification [[maybe_unused]] , she states:

Appears in the class declaration, typedef, variable, non-static data member, function, enumeration or enumerator. If the compiler issues warnings about unused objects, this warning is suppressed for any declared Maybe_unused entity.

As the enumerator says, I expect him to have a precedent. As the only thing I could come up with is a warning -Wswitch , I tried it with Clang, GCC and MSVC.

 enum A { B, C [[maybe_unused]] }; void f(A a) { switch (a) { case B: break; } } 

All 3 compilers give me the following warnings:

 <source>:9:13: warning: enumeration value 'C' not handled in switch [-Wswitch] switch (a) ^ 

Living code

Is this a valid use case for using this attribute, are there any other use cases for adding the attribute in this place, or is it just a useless addition?

+10
c ++ language-lawyer attributes c ++ 17
source share
2 answers

The error was registered for Clang and marked as fixed: https://bugs.llvm.org/show_bug.cgi?id=36231

This seems to confirm that the enumeration value may not be present in the switch without warning if it is marked [[Maybe_unused]]

0
source share

The purpose of this attribute is to declare that the object can never be used, and if that happens, the implementation should not bother you with a warning that somewhere, for example, you may use the wrong object.

Operators

switch is a completely different matter: not processing the enumerator is problematic, even if the enumerator is never used in this TU; this indicates a logical gap in your program. What if this function has an external connection, and someone else calls it with this enumerator?

In short, even in TU, in which we marked the enumerator as potentially unused (which seems pointless because they are often in the namespace area), covering it in your program logic is still highly recommended (and Clang is absolutely right in giving you this advice).

+5
source share

All Articles