简体   繁体   English

开关柜中的混合枚举

[英]Mixing enumeration in switch-case

What does C standard talk about mixing enumerations in the case constants of a switch - case statement? 什么C标准谈的情况下常数混合枚举switch - case说法? I ensure that there are no duplicate values of the mixed enum types. 我确保混合enum类型没有重复的值。

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. 我使用-ansi -Wall标志将代码编译干净,但是Klockwork在此代码中报告了一些问题。

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. 它在C中是允许的。您得到的是Klocwork的警告,因为它认为混合不同的enum类型不是一个好主意。 (I personally agree on this): (我个人对此表示同意):

Quoting from Klocwork : 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. INCONSISTENT.LABEL检查器可查找将多个枚举类型用作switch表达式或switch语句中的标签的情况。

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. 在switch语句中使用具有不同枚举类型的标签可能会引起问题,因为具有相同值的枚举成员可能具有不同的含义。 The design intent fails, and unexpected results can occur. 设计意图失败,并且可能发生意外结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM