看起来,如果将具有多个参数的模板实例化作为参数传递给宏,则C++预处理器将失败。C++预处理器不知道模板参数?
查看下面的示例。
#include <stdio.h>
#define FOO(v) printf("%d\n",v::val())
template<int N>
struct bar {
static int val() { return N; }
};
template<int N, int M>
struct baz {
static int val() { return N+M; }
};
int main() {
printf("%d\n",bar<1>::val());
printf("%d\n",baz<1,2>::val());
FOO(bar<10>); // OK
FOO(baz<20,30>); // error: too many arguments provided to function-like macro invocation
FOO((baz<20,30>)); // error: '::val' has not been declared
}
Tested铿锵++和g ++
它应该被看作是一个错误吗?
你可以设计宏提取括号参数里面,但这个例子当然不需要宏。 – chris
一个相关的问题:C++预处理器是否知道C++? AFAICT的C++预处理器并没有太大的改变,因为它只是C预处理器...;) –
@chris,谢谢,从您的消息的措辞我找到了类似问题的答案http://stackoverflow.com/questions/13842468/comma-in-cc-macro/13842784#13842784 – hutorny