How do you perform macro expansion within #ifdef?
Posted
by Malvineous
on Stack Overflow
See other posts from Stack Overflow
or by Malvineous
Published on 2010-04-24T06:47:13Z
Indexed on
2010/04/24
6:53 UTC
Read the original article
Hit count: 319
Hi all,
I have some fairly generic code which uses preprocessor macros to add a certain prefix onto other macros. This is a much simplified example of what happens:
#define MY_VAR(x) prefix_##x
"prefix_" is actually defined elsewhere, so it will be different each time the file is included. It works well, but now I have some code I would like to skip if one of the tokens doesn't exist, but this doesn't work:
#if defined MY_VAR(hello)
What I want it to expand to is this:
#ifdef prefix_hello
But I can't figure out how. I need to use the MY_VAR() macro to do the expansion, so I can't just hardcode the name. (It's actually for some testing code, the same code gets included with a different prefix each time to test a bunch of classes, and I want to skip a couple of tests for a handful of the classes.)
Is this possible with the C++ preprocessor?
© Stack Overflow or respective owner