C's Limited Macro Capabilities
T. William Wells
bill at twwells.com
Mon Dec 4 14:38:09 AEST 1989
In article <69517 at psuecl.bitnet> bpm at psuecl.bitnet (Brian Moquin) writes:
: A student in my C class asked me an interesting question: can you have
: conditional compilation directives embedded within '#define' macros?
: The answer is no. The student pointed out that this then
: severely limits the macro capabilities of C. He said that in assembler
: (MASM, I'm sure), he can write macros which contain arguments that
: determine how the macros get expanded. I had trouble coming up with
: a good analogous example in C, but here's one to think about:
: #define cast(flag,x) #if flag=='I' \
: ((int)(x)) \
: #elif flag=='F' \
: ((float)(x)) \
: #endif
: This is not legal C, but if it were I think it would enhance the power
: of macro expansions significantly. Thoughts?
This particular one can be dealt with in ANSI C, I think.
Something like:
#define cast_F(x) ((float)(x))
#define cast_I(x) ((int)(x))
#define cast(flag,x) cast_##flag((x))
---
Bill { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com
More information about the Comp.lang.c
mailing list