Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C11 - enums in Generic Selections

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 enums 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?

like image 683
glank Avatar asked Mar 20 '15 00:03

glank


1 Answers

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.

like image 163
Jens Gustedt Avatar answered Oct 13 '22 10:10

Jens Gustedt