J
jontwang
How do you get a count of the number of arguments passed into a
variadic macro?
variadic macro?
How do you get a count of the number of arguments passed into a
variadic macro?
How do you get a count of the number of arguments passed into a
variadic macro?
How do you get a count of the number of arguments passed into a
variadic macro?
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...
Michael said:You don't in general.
As with variadic functions, you can pass the argument number
first...
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 [...]
How do you get a count of the number of arguments passed into a
variadic macro?
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.
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.
How do you get a count of the number of arguments passed into a
variadic macro?
Thank you! That's precisely what I needed.
Tom said:^^^^^^
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.
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.