printf("%i", _Generic(REGISTER_AX, enum SegmentRegister: 0, enum GpRegister: 1, default: 2));
In the above code, the desired result is 1
, because REGISTER_AX
is a member of enum GpRegister
.
Even though the compiler gladly accepts enum
s as associated types, the code will print out 2
, because the enum value is evaluated as an int
.
My question is: Is there a way to produce the desired result, and will an expression in a generic selection ever associate with an enum
type?
Enumeration types are no good test for _Generic
, and this for two different reasons.
First, as already mentioned in a comment, the constants of such a type are themselves of type int
and not of the enumeration type.
Second, values that you would declare with that type, say as a variable or with a cast, would be compatible with one specific integer type. Which type that is is implementation (=compiler) dependent and can even vary from one enumeration type to another.
So basically it is not easy to distinguish enumeration types from other integer types. Things become even more difficult if you dig a bit deeper, being compatible types is not a transitive relation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With