Macro substitution: curly braces behaviour?

T

TJ

I'd like to know how the preprocessor treats curly braces, as compared
to how it treats parentheses.

What happens if I pass this:

{ a_function( arg1, arg2, arg3 ) }

as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:

"{ a_function( arg1"
"arg2"
"arg3 ) }"

I tried it with gcc 4.0 and it seems that the curly braces group the
stream of tokens into one argument, just like parentheses do. However
I'd like to know if this is proper C89 behaviour, or just GCC.

Thanks.

TJ
 
G

Guest

TJ said:
I'd like to know how the preprocessor treats curly braces, as compared
to how it treats parentheses.

It doesn't. It just copies them without any regard to their position,
and does not interpret them in any way.
What happens if I pass this:

{ a_function( arg1, arg2, arg3 ) }

as an argument to a function macro? Does it get passed in whole, or
split into smaller parts like so:

It is a single argument, because the commas are parenthesised.
 
C

Christopher Layne

Harald said:
It is a single argument, because the commas are parenthesised.

When are they not parenthesized within the context of a function or
function-like macro call?

Anyways, I don't see his behavior:

$ cat cpp.c
#define func(a, b, c) some_func(a, b, c);

{ func(1, 2, 3) }
func({ other_func(1, 2, 3) })

$ cpp -ansi cpp.c
# 1 "cpp.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "cpp.c"


{ some_func(1, 2, 3); }
cpp.c:4:29: error: macro "func" requires 3 arguments, but only 1 given
func
 
G

Guest

Christopher said:
When are they not parenthesized within the context of a function or
function-like macro call?

macro(a, b)

The first argument is a. The second argument is b. For both arguments,
there are no
parentheses.

macro({ a_function( arg1, arg2, arg3 ) })

The first and only argument is { a_function( arg1, arg2, arg3 ) }. The
parentheses and nested commas are part of the argument.
Anyways, I don't see his behavior:

$ cat cpp.c
#define func(a, b, c) some_func(a, b, c);

{ func(1, 2, 3) }
func({ other_func(1, 2, 3) })

$ cpp -ansi cpp.c
# 1 "cpp.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "cpp.c"


{ some_func(1, 2, 3); }
cpp.c:4:29: error: macro "func" requires 3 arguments, but only 1 given
func

That's the behaviour the OP claimed to see.
 
C

Christopher Layne

Harald said:
macro(a, b)

The first argument is a. The second argument is b. For both arguments,
there are no
parentheses.

macro({ a_function( arg1, arg2, arg3 ) })

The first and only argument is { a_function( arg1, arg2, arg3 ) }. The
parentheses and nested commas are part of the argument.

Right. I think we're on the same page in the first place. :) I thought you
were saying that it was a single argument since the commas were parenthesized
in general.
 
T

TJ

It is a single argument, because the commas are parenthesised.

Ah ok, bad example.
... It just copies them without any regard to their position,
and does not interpret them in any way.

Yes that explains it. I also tested with a better example and it ended
up just like you said. Thanks.

TJ
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top