Replace preprocessor macro with typedef of the same name

I want to replace the macro with the corresponding type with the same name. I have

#define FooType char* 

in a third-party library, and this breaks some of my code (more precisely: some code that I have to use, and which I cannot change myself). I want to replace it with a typedef with the same name, and then #undef with a macro. I tried something like this:

 #define TMP_MACRO FooType #undef FooType typedef TMP_MACRO FooType; #undef TMP_MACRO 

But the preprocessor extends this value to:

 typedef FooType FooType; 

(at least that's what g++ -E told me). Therefore, the TMP_MACRO macro TMP_MACRO not expand immediately. Since "FooType" does not exist, it does not compile.

How can I replace the FooType macro FooType the correct type and subsequently define the macro? Or is it impossible?

+4
source share
2 answers

A typedef declaration is usually on the same line, but line numbers mean nothing to the compiler.

 typedef FooType #undef FooType FooType; 
+9
source

Why not just

 #undef FooType typedef char* FooType 

The result you get is correct, since you are instructing the preprocessor to replace TMP_MACRO with FooType (the fact that you undef FooType subsequently means nothing in this regard).

0
source

All Articles