473,407 Members | 2,598 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,407 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 4152
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
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.