Why do I get an error when directly comparing two enums?

后端 未结 4 1834
闹比i
闹比i 2021-02-13 20:09

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 g

4条回答
  •  粉色の甜心
    2021-02-13 20:49

    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) { ... }
    

    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.

提交回复
热议问题