How could I (temporarily) un-typedef something?

2020-08-16 16:47发布

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.

标签: c++
2条回答
仙女界的扛把子
2楼-- · 2020-08-16 17:01

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.

查看更多
放我归山
3楼-- · 2020-08-16 17:15

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

查看更多
登录 后发表回答