Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Macro preprocessing

0 views
Skip to first unread message

dhun

unread,
Nov 7, 2009, 12:59:24 PM11/7/09
to
Hi All,
I have a following kind of situation.

#define i(p) p
#define r(x) i(in)##_##x

int main(){
#define in a
int a,a_b;
a = r(b);
#undef in
}

where I need to construct a symbol ab, at the place where code is
executed.

A g++ -E test.c, however insert a space between a and b.

What could be possible solution for this?

Andrey Vul

unread,
Nov 7, 2009, 4:26:55 PM11/7/09
to

Premature ## . Create helper macros to redirect the preprocessor until
only the expanded token is pasted.
In this case:
#define s__(x,y) x##y
#define s(x,y) s__(x,y)
#define r(x) s(i(in),s(_,x))

Two things happen here:
1. r is defined as the result of a redirected paste macro.
2. s creates redirection so that results of expansion instead of the
raw tokens are pasted.

The paste operator (##) has a higher precedence than expansion. so you
need to create a paster and a wrapper to allow for expansion before
pasting.
With my modifications, gcc does not warn about invalid tokens.
This warning needs to be taken very seriously though, it shows that
your code is missing something.

0 new messages