By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Counting args in variadic macros

P: n/a
How do you get a count of the number of arguments passed into a
variadic macro?

Jul 11 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
jo******@gmail.com schrieb:
How do you get a count of the number of arguments passed into a
variadic macro?
You don't in general.
As with variadic functions, you can pass the argument number
first...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 11 '06 #2

P: n/a
jo******@gmail.com said:
How do you get a count of the number of arguments passed into a
variadic macro?
You'll need to establish a protocol, a contract between caller and callee,
so that the called function knows when it has processed all its arguments.
This might be controlled by one of the fixed arguments. The printf
function, for example, uses the % signs in its format string to learn how
many arguments it has been sent. Or you might use a sentinel; for example,
you might have a bunch of ints terminated by a 0 (or a 999, or whatever).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #3

P: n/a
jo******@gmail.com wrote:
How do you get a count of the number of arguments passed into a
variadic macro?
You have to decide on a criteria for termination of traversal then just
implicitely compute it from there (which may involve traversal.) For
example, for printf(), the format string implicitely describes exactly
what the types and number of parameters are. There is no language
specified way of knowing this count outside of that kind of
function-specific convention. (Though it is likely that there is a
platform specific way of gaining access to this information.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 11 '06 #4

P: n/a
Richard Heathfield schrieb:
jo******@gmail.com said:
>>How do you get a count of the number of arguments passed into a
variadic macro?
^^^^^^
>
You'll need to establish a protocol, a contract between caller and callee,
so that the called function knows when it has processed all its arguments.
This might be controlled by one of the fixed arguments. The printf
function, for example, uses the % signs in its format string to learn how
many arguments it has been sent. Or you might use a sentinel; for example,
you might have a bunch of ints terminated by a 0 (or a 999, or whatever).
Read again the OP's question...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 11 '06 #5

P: n/a
Michael Mair said:
Richard Heathfield schrieb:
>jo******@gmail.com said:
>>>How do you get a count of the number of arguments passed into a
variadic macro?
^^^^^^
>>
You'll need to establish a protocol, a contract between caller and
callee, so that the called function knows when it has processed all its
arguments. This might be controlled by one of the fixed arguments. The
printf function, for example, uses the % signs in its format string to
learn how many arguments it has been sent. Or you might use a sentinel;
for example, you might have a bunch of ints terminated by a 0 (or a 999,
or whatever).

Read again the OP's question...
This Is Not My Day.

Goodnight! :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #6

P: n/a
Michael Mair wrote:
jo******@gmail.com schrieb:
How do you get a count of the number of arguments passed into a
variadic macro?

You don't in general.
As with variadic functions, you can pass the argument number
first...
Interesting... I wonder why this thread at comp.std.c++ suggests that
it's doable:
http://groups.google.com/group/comp....b75d0162d19you

Although I haven't been able to find any code demonstrating the
technique.

I suspect that there is some way to do it, though, since the
preprocessor has to count the number of args anyways to perform the
expansion.

Jul 11 '06 #7

P: n/a
we******@gmail.com wrote:
jo******@gmail.com wrote:
How do you get a count of the number of arguments passed into a
variadic macro?

You have to decide on a criteria for termination of traversal then just
implicitely compute it from there (which may involve traversal.) For
example, for printf(), the format string [...]
D'oh! I must be smoking the same drugs as Richard.

Anyhow, variadic macros are not supported by most deployed compilers
out there, so the question is mostly answered by "you don't, because
that's illegal".

In C99 I recall someone created a twisted set of macros that allowed
you to figure it out, up to some finite constant that has to be defined
at compile time.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 11 '06 #8

P: n/a
jo******@gmail.com wrote:
How do you get a count of the number of arguments passed into a
variadic macro?
Unfortunately the Standard doesn't specify a mechanism to do this but
Laurent Deniau posted an ingenious and (AFAICT) portable way to do this
in comp.std.c a little while ago. It's actually relatively
straight-forward and clean, the only limitation is that it can only
portably handle 63 arguments (I would sincerely hope that wouldn't be a
problem). The post was entitled "__VA_NARG__" and was posted on Jan 16
2006. The followup by Roland contains a formatted version that is
ready to cut and paste. You can find the post on Google Groups.

As an interesting side note, this makes it possible to write
pseudo-overloaded functions based on the number of arguments that are
properly expanded in the preprocessor. For example:

sum(2, 3);
sum(5, 6, 7);

could expand to:

sum2(2, 3);
sum3(5, 6, 7);

where sum2 is a version of a function that handles 2 arguments, sum3 is
a version that handles 3 arguments, etc. I have never had the need to
do this but it is interesting nonetheless.

Robert Gamble

Jul 11 '06 #9

P: n/a
jjf

Robert Gamble wrote:
jo******@gmail.com wrote:
How do you get a count of the number of arguments passed into a
variadic macro?

Unfortunately the Standard doesn't specify a mechanism to do this but
Laurent Deniau posted an ingenious and (AFAICT) portable way to do this
in comp.std.c a little while ago. It's actually relatively
straight-forward and clean, the only limitation is that it can only
portably handle 63 arguments (I would sincerely hope that wouldn't be a
problem). The post was entitled "__VA_NARG__" and was posted on Jan 16
2006. The followup by Roland contains a formatted version that is
ready to cut and paste. You can find the post on Google Groups.
The thread's at
http://groups.google.com/group/comp....ee8c8f92e4a3fb

Initial message is news:dq**********@sunnews.cern.ch
Nice expanded version in news:dq**********@rzsun03.rrz.uni-hamburg.de

Jul 12 '06 #10

P: n/a
Robert Gamble schrieb:
jo******@gmail.com wrote:
>>How do you get a count of the number of arguments passed into a
variadic macro?

Unfortunately the Standard doesn't specify a mechanism to do this but
Laurent Deniau posted an ingenious and (AFAICT) portable way to do this
in comp.std.c a little while ago. It's actually relatively
straight-forward and clean, the only limitation is that it can only
portably handle 63 arguments (I would sincerely hope that wouldn't be a
problem). The post was entitled "__VA_NARG__" and was posted on Jan 16
2006. The followup by Roland contains a formatted version that is
ready to cut and paste. You can find the post on Google Groups.
doubleplus cool! Goes into my collection...

Thank you
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 12 '06 #11

P: n/a
Thank you! That's precisely what I needed.

Jul 12 '06 #12

P: n/a

jo******@gmail.com wrote:
How do you get a count of the number of arguments passed into a
variadic macro?
Some neato answers but the usual way is to have an escape symbol. E.g.
if you are passing pointers you terminate the list with a NULL.

e.g.

myfunc(&a, &b, NULL);

That's a bit more portable [and clean] then using the 64 argument limit
(which iirc is not mandatory is it?).

If you are targetting a given ABI look into if there is a register for
the # of params. Sometimes there is and you can easily extract it.
Not portable but fast.

Tom

Jul 12 '06 #13

P: n/a
jo******@gmail.com writes:
Thank you! That's precisely what I needed.
You're welcome, I'm sure, but I have no idea what you're talking
about. Pleaes provide some context when you post a followup. Until
recently, Google Groups made this gratuitously difficult, but they've
fixed that bug; much of <http://cfaj.freeshell.org/google/is still
relevant, though.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 12 '06 #14

P: n/a
Tom St Denis schrieb:
jo******@gmail.com wrote:
>>How do you get a count of the number of arguments passed into a
variadic macro?
^^^^^^
>
Some neato answers
And you read the replies about the OP dealing with macros and
not functions.
but the usual way is to have an escape symbol. E.g.
if you are passing pointers you terminate the list with a NULL.
Not necessarily useful for variadic macros.
e.g.

myfunc(&a, &b, NULL);

That's a bit more portable [and clean] then using the 64 argument limit
(which iirc is not mandatory is it?).

If you are targetting a given ABI look into if there is a register for
the # of params. Sometimes there is and you can easily extract it.
Not portable but fast.
For macros?
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 12 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.