[[maybe_unused]] on enumerator

后端 未结 2 1979
谎友^
谎友^ 2021-02-14 09:00

Looking at the specification of the [[maybe_unused]], it states:

Appears in the declaration of a class, a typedef­, a variable, a non­static data member,

相关标签:
2条回答
  • 2021-02-14 09:11

    The intention of this attribute is to declare that an entity might never be made use of, and if that's the case, the implementation shouldn't bother warning you that somewhere, for example, you might be using a wrong entity.

    switch statements are a completely different matter altogether: not handling an enumerator is problematic even if the enumerator is never used in that TU; it indicates a logical gap in your program. What if that function has external linkage, and someone else invokes it with that enumerator?

    In short, even in TUs in which we marked an enumerator as potentially unused (which feels pointless, because at namespace scope they often are), covering it in your program logic is still very well-advised (and Clang is quite right in giving you that advice).

    0 讨论(0)
  • 2021-02-14 09:25

    A bug was logged for Clang and marked as resolved: https://bugs.llvm.org/show_bug.cgi?id=36231

    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]]

    0 讨论(0)
提交回复
热议问题