我试图用宏来创建一些静态变量。将模板宏定义为变量
我的问题是,我该如何定义一个具有2个参数的宏,第一个是模板,第二个是静态变量。模板应该有多于一种类型。
例如:
#define macro(x, y, z, v) x::y z = v;
int main(int argc, char *argv[]) {
// this works
macro(std::queue<int>, value_type, test, 4)
// this also works
std::pair<int, int>::first_type x = 3;
// this is produsing some compiler errors
macro(std::pair<int, int>, first_type, test2, 4)
return 0;
}
,是它甚至有可能做到这一点?
这里是错误:
main.cpp(47) : warning C4002: too many actual parameters for macro 'macro'
main.cpp(47) : error C2589: 'int' : illegal token on right side of '::'
main.cpp(47) : error C2589: 'int' : illegal token on right side of '::'
main.cpp(47) : error C2143: syntax error : missing ',' before '::'
main.cpp(50) : error C2143: syntax error : missing ';' before '}'
main.cpp(51) : error C2143: syntax error : missing ';' before '}'
由约阿希姆Pileborg
#define macro(x, y, z, v, ...) x<__VA_ARGS__>::y z = v;
...
// now it works
macro(std::pair, first_type, test2, 4, int, int)
THX约阿希姆
你可以粘贴你得到的编译器错误吗? – 2013-03-11 15:06:20