4
votes

I want to replace a macro with a proper typedef 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 I am forced to use and which I can't change by myself). I want to replace it by a typedef of the same name and then #undef the macro. I tried something like that:

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

But the preprocessor expands this to:

typedef FooType FooType;

(at least that is what g++ -E told me). So the macro TMP_MACRO is not expanded immediatelly. As 'FooType' is not there, it does not compile.

How can I replace the macro FooType by a proper type and undefine the macro afterwards? Or is this impossible?

2
Start by filing a bug report with the third party and have the sinner chastised. - pmr
Thought about this too - this is an impressive example why macros are evil :D - Sh4pe

2 Answers

9
votes

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

typedef FooType
#undef FooType
FooType;
0
votes

Why not simply

#undef FooType
typedef char* FooType

The output you're getting is correct, since you instruct the preprocessor to replace TMP_MACRO with FooType (the fact that you undef FooType afterwards counts for nothing in regards to this).