Comma inside macro

V

Vincent Rivière

Hello.

I experience a problem when calling a macro with an expression containing a
comma.

$ cat bug.c
#define M(x, y) x,y
#define A(x) x
#define B(x) A(x)

B(M(a,b))

$ gcc -E bug.c
....
bug.c:5:9: error: macro "A" passed 2 arguments, but takes just 1

GCC (all versions) fails with that error
HP-UX cc fails with similar error
Visual C++ (all versions) accepts that code and produces the obvious result:
a,b

Which compiler is right ?
How can I make GCC act as Visual C++ ?
 
V

Vincent Rivière

raof01 said:
You need parentheses:
#define M(x, y) ((x), (y))

Of course, in the real world, parentheses would be mandatory, but I
removed them in order to keep the example as simple as possible.

I added them and... it works ! No more preprocessor error !

I my real world problem, the comma went from a template argument list,
such as:
#define M(x, y) f<x,y>() // fails

I've just tried to add additional parentheses around the macro
definition, and it works !
#define M(x, y) (f<x,y>()) // works

I always thunk that extra parentheses were here only for avoiding
priority problems in the expanded expression. I was wrong !

They you for your answer !
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top