Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Macro preprocessing

Reply
Thread Tools

Macro preprocessing

 
 
dhun
Guest
Posts: n/a
 
      11-07-2009
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?
 
Reply With Quote
 
 
 
 
Andrey Vul
Guest
Posts: n/a
 
      11-07-2009
On Nov 7, 12:59*pm, dhun <(E-Mail Removed)> wrote:
> 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?


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro preprocessing C51 x armcc entik C Programming 1 03-23-2012 09:21 AM
Preprocessing directive in the middle of macro arguments Francois Grieu C Programming 6 01-18-2008 11:16 AM
Recursing macro preprocessing? Henrik Goldman C++ 4 10-22-2006 05:25 AM
Preprocessing directives Ron C++ 3 11-12-2003 04:45 AM
Indentation of preprocessing directives Dave C++ 2 11-03-2003 03:34 AM



Advertisments