以下代码编译良好。
#define CMD_MACRO(pp, cmd) \
{ \
if (pp)\
{ cmd; } \
}
template<class T> void operate_on(T &data, char c) {
data=data+1;
};
int main() {
int book=4;
char c;
CMD_MACRO(book, {
operate_on<int>(book, c);
});
};
请注意,我的代码中的实际宏更复杂,我给出了一个简化版本,这可能没有多大逻辑意义
现在,如果我在函数中添加另一个模板参数,它会给出编译错误(代码注释中解释了问题):
template<class T, bool b> void operate_on(T &data, char c) {
data=data+1;
};
int main() {
int book=4;
char c;
CMD_MACRO(book, {
operate_on<int, false>(book, c); /* here the "," between int and
false is being treated
as separating arguments to CMD_MACRO,
instead being part of 'cmd'. Thats strange
because the comma separating book and c is
treated fine as part of 'cmd'. */
});
};
test.cpp:18:6: error: macro "CMD_MACRO" passed 3 arguments, but takes just 2
test.cpp: In function 'int main()':
test.cpp:16: error: 'CMD_MACRO' was not declared in this scope
如何解决问题(我需要将额外的模板参数添加到现有代码中,并且收到这样的错误)。