以下代碼編譯正常。C++宏問題(逗號解釋)
#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
如何解決這個問題(我需要額外的模板參數添加到現有的代碼和我得到這樣的錯誤)。
一個較新的問題有一些優秀的答案:http://stackoverflow.com/questions/13842468/comma-in-c-c-macro/13842784#13842784 – jjrv 2014-07-17 05:36:52