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

Help with defining a macro?

P: n/a
I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

Is it possible to write such a macro?

If so, could you please provide an example?
Nov 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <6G64h.9243$Wd5.1258@trnddc05>,
Max TenEyck Woodbury <ma*@mtew.isa-geek.netwrote:
>I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.
>Is it possible to write such a macro?
>If so, could you please provide an example?
Not possible in C89: in C89, macros only have no arguments
("object like macro") or a fixed-length list of arguments
("function like macro").

Not possible in C99: in C99, you cannot declare a function-like
macro that might be handed no arguments at all. function-like
macros in C99 must be declared with at least one fixed argument.

I have not looked at C99 handling of macros with variable argument
lists, so I do not know if you can somehow iterate over the arguments
or otherwise indicate that you want something to be done to each of
them in turn.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Nov 7 '06 #2

P: n/a

Max TenEyck Woodbury wrote:
I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

Is it possible to write such a macro?

If so, could you please provide an example?
It is not possible to write such a macro in C89 or C99, AFAIK.

This sounds like a potential case of preprocessor abuse; what is the
purpose of all these macros? Could the same purpose be served using a
set of functions (which can be written to take variable-sized argument
lists)?

Nov 7 '06 #3

P: n/a
Walter Roberson wrote:
In article <6G64h.9243$Wd5.1258@trnddc05>,
Max TenEyck Woodbury <ma*@mtew.isa-geek.netwrote:
>>I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

>>Is it possible to write such a macro?

>>If so, could you please provide an example?


Not possible in C89: in C89, macros only have no arguments
("object like macro") or a fixed-length list of arguments
("function like macro").

Not possible in C99: in C99, you cannot declare a function-like
macro that might be handed no arguments at all. function-like
macros in C99 must be declared with at least one fixed argument.
I have reason to believe that your last sentence is incorrect.
While that is true of functions, it is not true of macros. In
particular the following works:

#define STR(...) # __VA_ARGS__

which allows the argument list to contain commas.
>
I have not looked at C99 handling of macros with variable argument
lists, so I do not know if you can somehow iterate over the arguments
or otherwise indicate that you want something to be done to each of
them in turn.
Well that is the question I asked...
Nov 8 '06 #4

P: n/a
John Bode wrote:
Max TenEyck Woodbury wrote:
>>I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.

Is it possible to write such a macro?

If so, could you please provide an example?


It is not possible to write such a macro in C89 or C99, AFAIK.
If it is possible, and I am not sure it is, it almost certainly
has to use the ## operator to get around expansion suppression
on the rescan.
>
This sounds like a potential case of preprocessor abuse; what is the
purpose of all these macros? Could the same purpose be served using a
set of functions (which can be written to take variable-sized argument
lists)?
This is needed to build a table with fairly complex entries at compile
time. I have some (slightly ugly) macros that build elements of an
array initializer, but I would like to make it a bit simpler to the
users by hiding more of the implementation details.

So no, it can not be done with functions.
Nov 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.