Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you perform macro expansion within #ifdef?

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?

Update:

Here is some semi-compilable code to demonstrate the problem further: (to avoid squishing it into the comments below)

#define PREFIX hello

#define DO_COMBINE(p, x)  p ## _ ## x
#define COMBINE(p, x)     DO_COMBINE(p, x)
#define MY_VAR(x)         COMBINE(PREFIX, x)

// MY_VAR(test) should evaluate to hello_test

#define hello_test "blah blah"

// This doesn't work
#ifdef MY_VAR(test)
  printf("%s\n", MY_VAR(test));
#endif
like image 411
Malvineous Avatar asked Apr 24 '10 06:04

Malvineous


People also ask

How macro expansion is done?

Macros are expanded by the preprocessor, which is a separate program that runs before the compiler proper. Macros perform a simple text substitution. This program prints 1 , because PLUS()PLUS() expands to ++ which is the increment operator.

What is a macro expansion?

A macro consists of name, set of formal parameters and body of code. The use of macro name with set of actual parameters is replaced by some code generated by its body. This is called macro expansion.

What is macro explain macro expansion by taking suitable example?

Macro represents a group of commonly used statements in the source programming language. Macro Processor replaces each macro instruction with the corresponding group of source language statements. This is known as the expansion of macros.

What is semantic expansion in macro?

2. Semantic expansion: Semantic expansion implies generation of instructions tailored to the requirements of a specific usage. Semantic expansion is characterized by the fact that different uses of a macro can lead to codes which differ in the number, sequence and opcodes of instructions.


1 Answers

Is there more to your program than this question describes? The directive

#define MY_VAR(x) prefix_##x

defines exactly one preprocessor identifier. The call

blah ^&* blah MY_VAR(hello) bleh <>? bleh

simply goes in one end of the preprocessor and comes out the other without defining anything.

Without some other magic happening, you can't expect the preprocessor to remember what arguments have been passed into what macros over the course of the preceding source code.

You can do something like

#define prefix_test 1
#if MY_VAR(test) == 1
#undef prefix_test // optional, "be clean"
...
#endif
#undef prefix_test

to query whether the prefix currently has the particular value prefix_. (Undefined identifiers are replaced with zero.)

like image 179
Potatoswatter Avatar answered Oct 10 '22 12:10

Potatoswatter