472,142 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

PREPROCESSOR: Multiple expansions.

(This is a contrived example)

With the following preprocessor lines:

#define SIZEOF_mytype 4

#define hton4b htonl

#define OUT(TYPE, VAR) \
OUT1(SIZEOF_ ## TYPE, VAR)

#define OUT1(SIZE, VAR) \
hton ## SIZE ## b(VAR)

I type:

OUT(mytype, t)

and want to get:

htonl(t)

but actually get:

htonSIZEOF_mytypeb(t)

I tried various intermediate macros to catenate SIZEOF_ and TYPE to try
get it to convert to 4 but the closest I have gotten is:

hton 4 b(t)

Whats the trick?

Thanks.

Aug 21 '06 #1
4 1562
well, you're pushing the non-barfing limits of macro usage, but, ok,

does it work any better if you take out the spaces around the ##'s?

Aug 21 '06 #2

Ancient_Hacker wrote:
well, you're pushing the non-barfing limits of macro usage, but, ok,

does it work any better if you take out the spaces around the ##'s?
I did as it happens (spaces surrounding glue have no effect as far as I
can see).

Calm prevailed and I went through it slowly and carefully, resulting in
the following which behaves as expected.

#define SIZEOF_mytype 4

#define hton4b htonl

#define CAT(A, B) A##B

#define OUT2(ARG, VAR) \
CAT(hton, ARG)

#define OUT1(SIZE, VAR) \
OUT2(CAT(SIZE,b(VAR)), VAR)

#define OUT(TYPE, VAR) \
OUT1(SIZEOF_##TYPE, VAR)

(Incidentally, the result is the same when using "A ## B" and "SIZEOF_
## TYPE").

Thanks for the reply anyway.

Aug 21 '06 #3
This works:
#define SIZEOF_mytype 4

#define hton4b htonl

#define OUT(TYPE, VAR) OUT1(SIZEOF_##TYPE, VAR)

#define OUT1(SIZE,VAR) expand(hton,SIZE,VAR)

#define expand(x,y,z) x##y##b(z)
OUT(mytype, t)

Aug 21 '06 #4

Ancient_Hacker wrote:
This works:
#define SIZEOF_mytype 4

#define hton4b htonl

#define OUT(TYPE, VAR) OUT1(SIZEOF_##TYPE, VAR)

#define OUT1(SIZE,VAR) expand(hton,SIZE,VAR)

#define expand(x,y,z) x##y##b(z)
OUT(mytype, t)
Thanks.

Aug 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

205 posts views Thread by Jeremy Siek | last post: by
13 posts views Thread by seemanta dutta | last post: by
13 posts views Thread by Chris Croughton | last post: by
2 posts views Thread by Prashant Mahajan | last post: by
6 posts views Thread by Urs Thuermann | last post: by
31 posts views Thread by Sam of California | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.