Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > passing enums through stdarg

Reply
Thread Tools

passing enums through stdarg

 
 
luser- -droog
Guest
Posts: n/a
 
      02-09-2011
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;
 
Reply With Quote
 
 
 
 
Francois Grieu
Guest
Posts: n/a
 
      02-09-2011
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-09-2011
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
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      02-09-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums without identifier, enums and typedef =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= C Programming 10 01-20-2007 01:20 AM
template with stdarg: enum problem Klaus Schneider C++ 8 10-12-2005 04:14 PM
Confusion with stdarg Mac A. Cody C Programming 9 01-04-2005 02:11 PM
Using stdarg with unknown types Clint Olsen C Programming 6 10-29-2003 06:24 AM
Equivalent of stdarg.h but on the calling side ? Francesco Bochicchio C Programming 5 07-04-2003 09:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57