https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110881

            Bug ID: 110881
           Summary: Feature request: an attribute for enum members that
                    would skip the -Wswitch-enum warning
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: valentyn.pavliuchenko at gmail dot com
  Target Milestone: ---

Sometimes enum members are added only for static_assert() checks, but not for
actual use. The problem is that it triggers the -Wswitch-enum warning if these
members are not explicitly specified in a switch() statement.

The request is to add an attribute like [[unused]] for such enum members that
would not cause a -Wswitch-enum warning when the member is omitted in a
switch().

Example case that demonstrates the problem:

// build with -Wall
#include <iostream>

enum MyEnum
{
    eMyEnum_One,
    eMyEnum_Two,
    eMyEnum_Three,
    eMyEnum_Four,

    eMyEnum_Count, // not a real enum value, but rather a marker for
static_assert() statements like in the bar() function below
};

void doEven();

void bar(MyEnum inValue)
{
    static_assert(eMyEnum_Count == 4, "review the code below"); // would
trigger if new members are added to the enum
    if (inValue == eMyEnum_Two || inValue == eMyEnum_Four)
        doEven();
}

void foo(MyEnum inValue)
{
    switch (inValue) // warning: enumeration value 'eMyEnum_Count' not handled
in switch [-Wswitch]
    {
        case eMyEnum_One: std::cout << "1\n";
        case eMyEnum_Two: std::cout << "2\n";
        case eMyEnum_Three: std::cout << "3\n";
        case eMyEnum_Four: std::cout << "4\n";
    }
}

Possible code after a fix:


enum MyEnum
{
    eMyEnum_One,
    eMyEnum_Two,
    eMyEnum_Three,
    eMyEnum_Four,

    [[unused]]eMyEnum_Count,
};

Reply via email to