This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

enum type mixed with another type

I'm getting a warning "enumeration type mixed with another type" with the following line of code.

typedef enum { FALSE = 0, TRUE  = !FALSE } bool;

bool bResult;
bool bAAA;
bool bBBB;

bResult = bAAA && bBBB;  // <-- warning

bResult = (bAAA == TRUE) && (bBBB == TRUE); // <-- same results

Any suggestions?

Parents
  • "The && operator ... returns 1 if both its operands compare unequal to zero, 0 otherwise."

    That's the point - it takes arguments of type int, and returns a result of type int

    The code you showed is using your "bool" enum type; not int - hence the compiler warns you,

    "enumeration type mixed with another type"

    The "other type" in this case is, of course, int.

    You missed section A4.2 in K&R:

    "Enumerations behave like integers, but it is common for a compiler to issue a warning when an object of a particular enumeration type is assigned something other than one of its constants, or an expression of its type"

    The way to get around such warnings is always to use an appropriate explicit cast.

    "code should now be safer with this convension"

    Not really.
    I don't think it makes any difference at all to the safety here, but it does have the disadvantage that the compiler can no longer produce symbolic debug information for the enum type.

    For safety, I think the explicit cast is probably the way to go - as it makes your intention fully clear.

Reply
  • "The && operator ... returns 1 if both its operands compare unequal to zero, 0 otherwise."

    That's the point - it takes arguments of type int, and returns a result of type int

    The code you showed is using your "bool" enum type; not int - hence the compiler warns you,

    "enumeration type mixed with another type"

    The "other type" in this case is, of course, int.

    You missed section A4.2 in K&R:

    "Enumerations behave like integers, but it is common for a compiler to issue a warning when an object of a particular enumeration type is assigned something other than one of its constants, or an expression of its type"

    The way to get around such warnings is always to use an appropriate explicit cast.

    "code should now be safer with this convension"

    Not really.
    I don't think it makes any difference at all to the safety here, but it does have the disadvantage that the compiler can no longer produce symbolic debug information for the enum type.

    For safety, I think the explicit cast is probably the way to go - as it makes your intention fully clear.

Children
No data