Macro invocation

R

Ron

In macro invocation, what kind of tokens can I pass as arguments?

Another question: can I pass more than one token for single arguments? eg.

#define MYMACRO(arg1, arg2) arg1 arg2

MYMACRO("abc" L'd' .12, "def")

Is it valid? But if I can pass anything I could even pass a comma since
it's a preprocessing token, ok? So i could write:

MYMACRO("abc" , L'd' .12, "def")

That is ambiguous since it's not clear if the first comma closes the first
argument or if it's just another token to pass. Please, give me an answer.

Greetings.

ps: i'm supposing that c and c++ preprocessors follow almost the same rules,
but not sure it's right.
 
T

those who know me have no need of my name

in comp.lang.c i read:
Another question: can I pass more than one token for single arguments? eg.

short answer: no.
#define MYMACRO(arg1, arg2) arg1 arg2

MYMACRO("abc" L'd' .12, "def")

the first argument consists of three tokens of incompatible type, so there
is no way to stitch them together, hence are not acceptable syntactically.

there are two arguments, because the , is the argument separation token:

[#11] The sequence of preprocessing tokens bounded by the
outside-most matching parentheses forms the list of arguments for
the function-like macro. The individual arguments within the list
are separated by comma preprocessing tokens, but comma preprocessing
tokens between matching inner parentheses do not separate arguments.
If there are sequences of preprocessing tokens within the list of
arguments that would otherwise act as preprocessing directives, the
behavior is undefined.
Is it valid? But if I can pass anything I could even pass a comma since
it's a preprocessing token, ok? So i could write:

to pass a comma as an argument it would need to be within an additional set
of parentheses. (which still won't work because the tokens present cannot
be stitched together.)
ps: i'm supposing that c and c++ preprocessors follow almost the same rules,
but not sure it's right.

your post is to comp.lang.c and so you shall get a c language response,
irrespective of whether it applies to c++.
 
D

Dan Pop

In said:
in comp.lang.c i read:


short answer: no.

It's also the wrong answer. Multitoken arguments are just fine as single
macro arguments:

size_t offset = offsetof(struct tm, tm_sec);

Dan
 
T

those who know me have no need of my name

in comp.lang.c i read:
It's also the wrong answer. Multitoken arguments are just fine as single
macro arguments

you are quite correct. i can clearly see how i managed to write what i
wrote, yet i have no idea how that much brain damage found it's way onto
the net.

sorry Ron, Dan is quite correct -- you can indeed provide a macro argument
with multiple tokens. e.g., Dan's example of:
size_t offset = offsetof(struct tm, tm_sec);

works swimmingly, in that when the first argument is expanded the resulting
`struct tm' can be usefully used in quite a number of contexts, including
the specific one cited. arbitrary stuff such as you used as an example:

will expand fine, but later processing will likely have a syntax error,
there being no context (i can imagine) where a string, character and
floating point literal can be used without intervening operators and/or
enclosing 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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top