How could I (temporarily) un-typedef something?

2020-08-16 16:36发布

问题:

I'm using a 3rd party library that typedefs byte to char.

(This is evil as char could be signed or unsigned depending on the compiler's choice).

Unfortunately that typedef has spilled over into the codebase that I maintain and I'm keen to remove them: using uint8_t directly instead.

Is there a way I can somehow undo this typedef once I get to my code (i.e. direcly after #include <3rdpartylib>)?

I will be in a position to remove the "solution" from my codebase once I've removed all the bytes.

回答1:

The easiest way is bracketing the includes of the 3rd-party-library thus:

#define byte somethingnotnamedbyte
#include <3rdpartylib>
#undef byte

That avoids the need to edit <3rdpartylib>, and avoids polluting your code with macros or that bad typedef.

Precondition: The preprocessing-token byte is only used as that typedef-name in <3rdpartylib>, never for anything else.



回答2:

Its not possible to do this. Undef only works on macros.



标签: c++