문제

What does C standard talk about mixing enumerations in the case constants of a switch-case statement? I ensure that there are no duplicate values of the mixed enum types.

switch (value) /* value is int type */
{
case enum1_val: /* enum1_val is of enum type enum1 */
    break;
case enum2_val: /* enum2_val is of enum type enum2 */
    break;
}

I get the code compiled clean with the -ansi -Wall flags, but Klockwork reports some problem in this code.

도움이 되었습니까?

해결책

It's allowed in C. What you get is a warning by Klocwork because it thinks mixing different enum types is not a good idea. (I personally agree on this):

Quoting from Klocwork:

Inconsistent case labels

The INCONSISTENT.LABEL checker finds situations in which more than one enum type is used as a switch expression or as a label in a switch statement.

Vulnerability and risk

Using labels with different enum types in a switch statement can cause problems because enum members with the same value can have different meanings. The design intent fails, and unexpected results can occur.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top