将需要逗号的模板传递给单参数宏 [英] Passing a template which requires a comma to a single-argument macro
问题描述
我有一些代码基本上可以压缩为
I have some code that essentially condenses down to
#define FOO(a)
FOO(std::map<int, int>);
但是它会发出编译错误(宏 FOO <的实际参数太多了
)。
But it emits a compile error (too many actual parameters for macro FOO
).
很明显,预处理程序认为我已经提供了 std :: map< int
和 int>
作为参数。
Obviously the preprocessor is thinking that I've supplied std::map<int
and int>
as arguments.
有办法解决吗?预处理器不会以这种方式用引号引起来的字符串。
Is there a way round this? The preprocessor will not treat a quoted string with a comma in this way.
推荐答案
这在理想情况下应该是注释,但是这样不支持注释中的代码,因此,您可以执行
This should perhaps ideally be a comment, but SO doesn't support code in comments, so, you can do
#include <map>
#define T_ARGS( ... ) < __VA_ARGS__ >
#define FOO( a ) a x;
auto main() -> int
{
FOO( std::map T_ARGS( int, int ) );
(void) x;
}
或者您可以定义一个解析为逗号的宏,也可以只使用关于特定于特定用例的任何方案(例如,分别传递模板名称)。
or you can define a macro that resolves to comma, or you can use just about any scheme that's specific to some particular use case (e.g., passing template name separately).
这篇关于将需要逗号的模板传递给单参数宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!