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

后端 未结 4 1811
闹比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 21:08

    It's warning you because you have the warning flag on. The flag description doesn't explain why it exists, but it's probably safe to assume that it exists to prevent accidental comparisons between different enumeration types, because this usually is an error.

    Furthermore, you are correct that you can use enumeration values the same as you can int constants. And if you said if (myf == c) then it most likely wouldn't have thrown a warning (I say most likely because I haven't experimented, and GCC honestly can do whatever it wants with that warning, but technically c is just an integral constant and does not carry the type enum second). But instead you're explicitly comparing two values of different enumeration types.

提交回复
热议问题