我在我的代码中同时使用了 JUCE 库和一些 Boost 头文件.Juce 将T"定义为宏(groan),而 Boost 在其模板定义中经常使用T".结果是,如果您以某种方式在 Boost 头文件之前包含 JUCE 头文件,预处理器会在 Boost 代码中扩展 JUCE 宏,然后编译器就会无可救药地丢失.
I am using both the JUCE Library and a number of Boost headers in my code. Juce defines "T" as a macro (groan), and Boost often uses "T" in it's template definitions. The result is that if you somehow include the JUCE headers before the Boost headers the preprocessor expands the JUCE macro in the Boost code, and then the compiler gets hopelessly lost.
在大多数情况下,保持我的包含顺序正确并不难,但是当您有一个包含其他一些类的 JUCE 类并且在链的某处某个文件包含 Boost 时,它会变得棘手,如果其中任何一个文件之前,它需要 JUCE 包含您遇到麻烦.
Keeping my includes in the right order isn't hard most of the time, but it can get tricky when you have a JUCE class that includes some other classes and somewhere up the chain one file includes Boost, and if any of the files before it needed a JUCE include you're in trouble.
我最初希望解决这个问题
My initial hope at fixing this was to
#undef T
在 Boost 的任何包含之前.但问题是,如果我不重新定义它,那么其他代码就会因为没有声明T"而感到困惑.
before any includes for Boost. But the problem is, if I don't re-define it, then other code gets confused that "T" is not declared.
然后我想也许我可以像这样做一些循环#define技巧:
I then thought that maybe I could do some circular #define trickery like so:
// some includes up here
#define ___T___ T
#undef T
// include boost headers here
#define T ___T___
#undef ___T___
丑陋,但我认为它可能有用.
Ugly, but I thought it may work.
遗憾的是没有.我在使用T"作为宏的地方出现错误
Sadly no. I get errors in places using "T" as a macro that
'___T___' was not declared in this scope.
有没有办法让这两个库可靠地协同工作?
Is there a way to make these two libraries work reliably together?
正如 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_macro
和 push_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.
这篇关于我可以重新定义一个 C++ 宏然后再定义它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!