Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing enums through stdarg

8 views
Skip to first unread message

luser- -droog

unread,
Feb 9, 2011, 3:50:12 AM2/9/11
to
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[i] = 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;

Francois Grieu

unread,
Feb 9, 2011, 4:10:30 AM2/9/11
to
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.


Francois Grieu

Eric Sosman

unread,
Feb 9, 2011, 7:54:21 AM2/9/11
to

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
eso...@ieee-dot-org.invalid

luser- -droog

unread,
Feb 9, 2011, 5:45:18 PM2/9/11
to

Excellent news! Thank you both.

0 new messages