passing enums through stdarg

L

luser- -droog

Hello.
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.

eg.
object consoper (state *st, char *name,
void (*fp)(), int out, int n, ... /* enum typepat pat0, ... ,
patN-1 */) {
object nm;
size opcode;
int i;
....
va_list args;

/* load pattern from variable arguments */
va_start(args,n);
for (i = 0; i < n; i++) {
optab[opcode].sig[ sp ].t = va_arg(args, int);
}
va_end(args);

....
op = consoper(st, "pop", Apop, 0, 1, (int)anytype); INSTALL;
op = consoper(st, "exch", AAexch, 2, 2, (int)anytype,
(int)anytype); INSTALL;
op = consoper(st, "dup", Adup, 2, 1, (int)anytype); INSTALL;
op = consoper(st, "copy", Icopy, 0, 1, (int)integertype); INSTALL;
op = consoper(st, "index", Iindex, 1, 1, (int)integertype);
INSTALL;
 
F

Francois Grieu

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.


Francois Grieu
 
E

Eric Sosman

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);
 
L

luser- -droog

On 2/9/2011 4:10 AM, Francois Grieu wrote:
On 09/02/2011 09:50, luser- -droog wrote:
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);

Excellent news! Thank you both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top