On 2/9/2011 4:10 AM, Francois Grieu wrote:
> On 09/02/2011 09:50, luser- -droog wrote:
>
>> I've got a function that takes a variable number of enum
>> values using stdarg. Right now I'm explicitly casting
>> the arguments to int in the call so I can peel them back
>> off as ints in the function and feel pretty safe about it
>> without poring over the standard looking for trouble.
>>
>> So the question is: do enums suffer the "usual promotions"
>> so I can expect them to be passed as ints in a vararg
>> function call? I'd like to drop the casts, if possible.
>> They offer no 'content' to the reader.
>
> Yes. ISO/IEC 9899:1999, 6.7.2.2 itme 3 states:
> The identifiers in an enumerator list are declared as
> constants that have type int and may appear wherever
> such are permitted.
This applies to the named values of an enumeration, but not
to an `enum foo' variable. That is, in
enum foo { BAR, BAZ };
enum foo variable;
.... we know that both BAR and BAZ are constants of type int (and
hence not promotable in function calls), but we're not so certain
about the status of `variable'. We know it is "compatible with char,
a signed integer type, or an unsigned integer type. The choice of
type is implementation-defined, [...]" (6.7.2.2p4). So:
printf ("BAR = %d, BAZ = %d\n", BAR, BAZ);
.... is fine, but for `variable' use:
printf ("variable = %d\n", (int)variable);
--
Eric Sosman
lid