c preprocessor puzzle

M

marv andersen

Hi,

What should the following produce after preprocessing
#define y(x) x
#define x(a) y(a
x(1) x(2)))

I think we should get '1 2', but cpp on linux seems to think this
expands to an invalid use of y? My reasoning is that we should get
the following:
y(1 x(2)))
y(1 y(2))
y(1 2)
1 2

.... but maybe I'm missing something.

Any help much appreciated!

Marv
 
B

Ben Bacarisse

marv andersen said:
What should the following produce after preprocessing
#define y(x) x
#define x(a) y(a
x(1) x(2)))

I think we should get '1 2', but cpp on linux seems to think this
expands to an invalid use of y? My reasoning is that we should get
the following:
y(1 x(2)))
y(1 y(2))
y(1 2)
1 2

... but maybe I'm missing something.

cpp is correct. The key wording from the standard is this (from
6.10.3.1 para 1):

"Before being substituted, each argument’s preprocessing tokens are
completely macro replaced as if they formed the rest of the
preprocessing file; no other preprocessing tokens are available."

Is that enough? Because this looks like coursework, I am inclined to
leave it at that, but if you are still puzzled, say so, and I'll include
a fuller explanation.
 
M

marv andersen

cpp is correct.  The key wording from the standard is this (from
6.10.3.1 para 1):

  "Before being substituted, each argument’s preprocessing tokens are
  completely macro replaced as if they formed the rest of the
  preprocessing file; no other preprocessing tokens are available."

Is that enough?  Because this looks like coursework, I am inclined to
leave it at that, but if you are still puzzled, say so, and I'll include
a fuller explanation.

Thanks - I see my mistake, the extra )s aren't visible when the
argument '1 x(2)' is being expanded, as we're 'argument expanding',
not 'rescanning' at this point, so we get as far as '1 y(2' but then
find we're missing the closing ). [Not coursework BTW, just trying to
understand cpp a bit better.]
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top