且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以重新定义一个 C++ 宏然后再定义它吗?

更新时间:2023-09-11 21:53:34

正如 greyfade 所指出的,你的 ___T___ 技巧不起作用,因为预处理器是一个非常简单的生物.另一种方法是使用 pragma 指令:

As greyfade pointed out, your ___T___ trick doesn't work because the preprocessor is a pretty simple creature. An alternative approach is to use pragma directives:

 // juice includes here
 #pragma push_macro("T")
 #undef T
 // include boost headers here
 #pragma pop_macro("T")

这应该适用于 MSVC++,并且 GCC 添加了对 pop_macropush_macro 的支持以与其兼容.虽然从技术上讲,它是依赖于实现的,但我认为没有一种标准的方法可以暂时取消定义.

That should work in MSVC++ and GCC has added support for pop_macro and push_macro for compatibility with it. Technically it is implementation-dependent though, but I don't think there's a standard way of temporarily suppressing the definition.