【发布时间】:2014-04-03 10:47:45
【问题描述】:
这是我的代码。
#define mp_eval(Func) Func::value
template<int L, int R>
struct StaticMinimum
{
static const int value = (L < R) ? L : R;
};
// Error: too few arguments for class template "StaticMinimum"
cout << mp_eval(StaticMinimum<9, 12>) << endl;
mp_eval(StaticMinimum<9, 12>) 不是要被编译器替换为StaticMinimum<9, 12>::value 吗?我想我一定错过了什么。请告诉我发生了什么。
【问题讨论】:
-
你看过预处理的输出吗?看看这个:ideone.com/GuDwei
-
你不能在宏参数中传递逗号,除非你在整个参数周围加上括号。
-
你能用内联函数或模板函数替换宏吗?
-
@MattMcNabb 我已经考虑过了。但我仍然不知道该怎么做。你能告诉我你是怎么处理的吗?
标签: c++ templates macros template-meta-programming