I have this problem in C++: can I typedef a bitfield whose values come from an enum?
Code will be more explainatory:
typedef {
AUDIO = 0x01,
VIDEO = 0x02,
SUBTITLE = 0x04,
DATA = 0x08,
GUARD,
ALL = 0xFF
} my_enum_e;
// I'd like to replace 'unsigned int' by 'my_enum_e' or similar
int myFunction( unsigned int mask )
{
// code
}
// called like this:
myFunction( AUDIO|VIDEO|DATA );
In the prototype of the function, I'd like to use my_enum_e
as an input value type, so that when exploring the code, you can immediately know which values you're supposed to put in there.
Now, changing the prototype to
int myFunction( my_enum_e mask );
makes the compiler whine about a cast error. I cant fix it by casting the function calls like this:
int myFunction( my_enum_e mask )
{
// code
}
myFunction( (my_enum_e)(VIDEO|AUDIO|DATA) );
But I find this quite horrible, and I'm not even sure it is legal (could it truncate the value??).
Do you have a solution?