[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

passing enums through stdarg

luserXtrog

2/9/2011 8:50:00 AM

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;
3 Answers

Francois Grieu

2/9/2011 9:11:00 AM

0

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

2/9/2011 12:54:00 PM

0

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

luserXtrog

2/9/2011 10:45:00 PM

0

On Feb 9, 6:54 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 2/9/2011 4:10 AM, Francois Grieu wrote:

> > On 09/02/2011 09:50, luser- -droog wrote:

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

Excellent news! Thank you both.