473,218 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Counting args in variadic macros

How do you get a count of the number of arguments passed into a
variadic macro?

Jul 11 '06 #1
14 4131
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
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
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
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
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
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
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
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
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
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
Thank you! That's precisely what I needed.

Jul 12 '06 #12

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jois.de.vivre | last post by:
Hi, I'm writing a C++ program that uses C style macros for debugging purposes. I have code that looks something like this: #define DEBUG(str, ...) Show(__FILE__, __LINE__, str, ##...
3
by: Thomas Carter | last post by:
I understand that C99 supports variadic macros. However, is it not the case that a variadic macro defined as #define SAMPLE_MACRO(...) Bloody-blah must take at least one argument? I would be...
15
by: Urs Thuermann | last post by:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this: int i; char *s; DBG("simple message\n"); DBG("message with an int...
8
by: Christof Warlich | last post by:
Hi, is there any way to access individual elements in the body of a variadic macro? I only found __VA_ARGS__, which always expands to the complete list. Thanks for any help, Christof
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.