Help with defining a macro?

  • Thread starter Max TenEyck Woodbury
  • Start date
M

Max TenEyck Woodbury

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

Walter Roberson

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

John Bode

Max said:
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)?
 
M

Max TenEyck Woodbury

Walter said:
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...
 
M

Max TenEyck Woodbury

John said:
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.
 

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

Defining macro on the command-line vs in the source 3
Help me, going live with a quiz 0
Simple Program 0
macro chain 4
MACRO help 23
Help with array 4
I know VBA (macros) - nothing else! 0
macro expansion 2

Members online

Forum statistics

Threads
473,755
Messages
2,569,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top