警告:案例不是以枚举类型评估的? [英] Warning: case not evaluated in enumerated type?

查看:74
本文介绍了警告:案例不是以枚举类型评估的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到了新的编译器Clang LLVM 4.0及其漂亮的版本。只是在这种情况下,它向我显示了一些旧的旧版代码的丑陋警告:

I recently upgraded to the new compiler Clang LLVM 4.0 and its nice. Is just in this case it is showing me an ugly warning for some old legacy code:

switch (var) {
    case kConstant: case 3: case 4: case 8: case 35: //WARNING HERE :(
    // do my thing here
    break;
    case kOtherConstant:
    // do another thing here
    break;
    default:
    break;
}

var 可能是枚举
中定义的值之一,例如:

var could be one of the values defined in the enum something like this:

typedef enum SomeConstants {
    kConstant,
    kOtherConstant,
};

,如您所见,没有定义4、8、35(这就是编译器进行合并的原因),但实际上它们会发生(这是我正在使用的此封闭源代码库的晦涩部分)。

and as you see 2, 4, 8, 35 are not defined (that is why the compiler is complining), but in reality they happen (this is one the obscure parts of this closed source library I am using).

有没有办法我可以某种方式修改我的开关代码,所以我不知道无臂但烦人的警告?现在,我使用以下命令使其静音:

Is there a way I can somehow modify my switch code so I don't get the harmless but annoying warning? Right now I am silencing it using:

switch (var) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
    case kConstant: case 3: case 4: case 8: case 35:
#pragma GCC diagnostic pop
    ...

我想知道是否有更优雅的解决方法。

I wonder if there is a more elegant way of solving this.

推荐答案

您可以将 switch()语句的表达式转换为 int 的表达式,这样它就不会/不能执行该检查。

You can cast the expression of the switch() statement to int so it doesn't/can't perform that check.

毕竟,它实际上是用来保存 int 值的,而不是列出的枚举器之一。

After all, it's actually being used to hold an int value, not one of the listed enumerators.

这篇关于警告:案例不是以枚举类型评估的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆