我可以重新定义一个C ++宏然后定义回来? [英] Can I redefine a C++ macro then define it back?

查看:239
本文介绍了我可以重新定义一个C ++宏然后定义回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我同时使用JUCE图书馆和一些在我的code升压头。 JUCE定义T作为宏(呻吟),和Boost经常使用T在它的模板定义。其结果是,如果你以某种方式包括JUCE头前的升压头中的preprocessor扩展在加速code中的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类,包括一些其他类和地方了链中的一个文件包括升压,如果有任何的它需要一个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.

我在固定初始希望就是

#undef T

在任何包括升压。但问题是,如果我不重新定义它,那么其他code迷糊了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指出的那样,你的 ___牛逼___ 招不起作用,因为preprocessor是pretty简单的生物。另一种方法是使用编译指令:

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与它的兼容性支持。技术上讲,它是依赖于实现的,虽然,但我不认为有暂时燮$ P $的pssing定义的标准方法。

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 ++宏然后定义回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆