user5830975
user5830975

Reputation: 13

How to generate compile error for not completed enum to string conversion?

We have something like this:

Enum IdEnum 
{
    Id_1,
    Id_2
}


GetNameById(IdEnum Id) : string
{
    switch Id
    {
        case Id_1 : return "1";
        case Id_2 : return "2";
    }
}

Is there any way to generate compile error if we add Id_3 to enum, but don't add case statement for it in GetNameById ?

Upvotes: 1

Views: 48

Answers (1)

bolov
bolov

Reputation: 75727

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html:

-Wswitch Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. (The presence of a default label prevents this warning.) case labels outside the enumeration range also provoke warnings when this option is used (even if there is a default label). This warning is enabled by -Wall.

-Wswitch-enum Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. case labels outside the enumeration range also provoke warnings when this option is used. The only difference between -Wswitch and this option is that this option gives a warning about an omitted enumeration code even if there is a default label.

Upvotes: 1

Related Questions