直接比较两个枚举时为什么会出错? [英] Why do I get an error when directly comparing two enums?

查看:416
本文介绍了直接比较两个枚举时为什么会出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要移植到新平台的代码,它开始给我一个错误,无法比较两个不同的枚举器列表中的两个枚举器。我很困惑为什么给我一个错误。

I have some code I'm porting to a new platform, and it started giving me an error about comparing two enumerators from two different enumerator-lists. I'm confused why it's giving me an error about this.

C规范(6.7.2.2)的枚举规范部分指出:

The enumeration specificers section of the C spec (6.7.2.2) states:


枚举列表中的标识符被声明为int类型的常量,并且
可能出现在允许的任何地方。 127)带有=的枚举器将其
枚举常量定义为常量表达式的值。如果第一个枚举数为
no =,则其枚举常量的值为0。

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.127) An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0.

所以我应该能够使用枚举成员与int常量相同。在这个小示例程序中:

So I should be able to use enum members the same as int constants. In this small sample program:

enum first {
  a,
  b
};

enum second {
 c,
 d
};

int main(){
    enum first myf = a;
    enum second mys = c;

    if(myf == mys)
        printf("same value\n"); 
    return 0;
}

使用 gcc -Wall -Werror编译时我收到消息:


错误:枚举优先和枚举第二之间的比较[-Werror = enum -compare]

error: comparison between ‘enum first’ and ‘enum second’ [-Werror=enum-compare]

我知道,如果同时键入 myf mys 作为 int s,编译器会很高兴,就像我可以设置几个 int s与 myf mys 的值进行比较;但是为什么我必须做这两个 来摆脱警告?为什么首先出现此警告?

I know that if I typecast both myf and mys as ints the compiler will be happy, just like I could set a couple of ints with the values from myf and mys and do the comparison; but why do I have to do either of these to get rid of the warning? Why does this warning exist in the first place? There must be some danger in doing this that I'm not seeing.

注意:
我已经阅读了有关该enum-compare标志的gcc文档,但是并没有说明太多内容:

NOTE:
I have read the gcc documentation on this enum-compare flag, but it doesn't say much of anything:


-Wenum-compare

警告不同枚举类型的值之间的比较。在C ++中,还将诊断条件表达式中的枚举不匹配,并且默认情况下启用警告。在C中,此警告由-Wall启用。

-Wenum-compare
Warn about a comparison between values of different enumerated types. In C++ enumeral mismatches in conditional expressions are also diagnosed and the warning is enabled by default. In C this warning is enabled by -Wall.


推荐答案

这不是警告,因为存在标准合规性问题,这是其中之一这似乎不正确的警告。如果您想到枚举的典型用法,那么在很多情况下进行这种比较就没有多大意义。例如:

This isn't a warning because of a standards compliance issue, it's one of those "this doesn't seem right" kind of warnings. If you think of the typical uses of enums, doing such a comparison doesn't make much sense in many cases. For example:

enum Day {
  Sunday,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
};

enum Month {
  January,
  February,
  March,
  April,
  May,
  June,
  July,
  August,
  September,
  October,
  November,
  December
};

enum Day day = Wednesday;
enum Month month = April; 

if (day == month) { ... }

此计算结果为true,但总的来说比较起来没有多大意义。如您所述,如果您知道您的意思,那么类型转换将说服编译器。

This evaluates to true, but in general the comparison wouldn't make much sense. If you know you meant it, the typecast will convince the compiler, as you noted.

这篇关于直接比较两个枚举时为什么会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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