Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to warn when assigning or performing arithmetic with different enum types in GCC?

While I'm aware this is valid C not to differentiate between enum types.

GCC does have -Wenum-compare (which I'm using) and works as expected.

I tried using -Wconversion but this doesn't make any difference.

How can assignments and arithmetic operators (+/-/&/|... etc) also generate warnings? (assignment, or and... etc)

{
    enum Foo f = SOME_VALUE;
    enum Bar b = SOME_OTHER_VALUE;

    if (f != b) {
        /* this warns! */
    }

    f = b;  /* <-- how to warn about this? */
    f |= b;  /* .. and this? */

}

Notes:

  • Switching to C++ is not an option (as suggested in other answers).
  • This question is closely related, however its not a duplicate because it's about passing arguments instead of arithmetic.
like image 958
ideasman42 Avatar asked Jul 26 '17 07:07

ideasman42


1 Answers

According to this answer, only clang supports the desired behavior.

There are some approaches to emulate strongly typed enums in C, but they seem to have various limitations.

like image 174
ivaigult Avatar answered Oct 19 '22 13:10

ivaigult