Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace preprocessor macro by typedef of the same name

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?

like image 305
Sh4pe Avatar asked Jan 17 '23 00:01

Sh4pe


1 Answers

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

typedef FooType
#undef FooType
FooType;
like image 107
aschepler Avatar answered Jan 29 '23 03:01

aschepler