Counting args in variadic macros

M

Michael Mair

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
 
R

Richard Heathfield

(e-mail address removed) 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).
 
W

websnarf

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.)
 
M

Michael Mair

Richard said:
(e-mail address removed) said:
^^^^^^

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
 
J

jontwang

Michael said:
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...d/thread/765aa557d15ef5ba/9bdf0b75d0162d19you

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

websnarf

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

Robert Gamble

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
 
J

jjf

Robert said:
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.std.c/browse_frm/thread/77ee8c8f92e4a3fb

Initial message is Nice expanded version in news:[email protected]
 
M

Michael Mair

Robert said:
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
 
T

Tom St Denis

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
 
K

Keith Thompson

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

Michael Mair

Tom said:
^^^^^^

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
 

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

Similar Threads

Variadic debug macros 5
C99's variadic macros. 7
variadic function 27
variadic macros 5
recursive variadic macros 9
Variadic macros 3
Variadic templates std::tuple 2
No Rest Args in Variadic Macro Ok? 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top