开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
vc 2005
问题(Question):
定义 variadic macros 时
, 与 tamplate的 , 无法区分
#define MACRO_C(msg, ...) \
{\
ostringstream oss; \
oss << "MACRO_B: " << __VA_ARGS__ << " " << msg; \
cout << oss.str() << endl; \
}
在这行时 会把tamplate的逗号当作是上面 msg与 ...的区隔
MACRO_C("NOT " << FUN_C<int, float>(14) << " MACRO_C: " << 15 , "ERROR?");
想请教一下如何写才能避免此情况呢
感谢
程式码(Code):(请善用置底文网页, 记得排版)
#include <sstream>
#include <iostream>
using namespace std;
#define MACRO_A(x) \
{\
ostringstream oss; \
oss << x; \
cout << oss.str() << endl; \
}
#define MACRO_B(...) \
{\
ostringstream oss; \
oss << "MACRO_B: " << __VA_ARGS__; \
cout << oss.str() << endl; \
}
#define MACRO_C(msg, ...) \
{\
ostringstream oss; \
oss << "MACRO_B: " << __VA_ARGS__ << " " << msg; \
cout << oss.str() << endl; \
}
string FUN_B(int x)
{
ostringstream oss;
oss << x;
return oss.str();
}
template<class X, class Y>
string FUN_C(X x)
{
ostringstream oss;
oss << x;
return oss.str();
}
int _tmain(int argc, _TCHAR* argv[])
{
MACRO_A("MACRO_A: " << 1 << " MACRO_A: " << 2);
MACRO_A("MACRO_A: " << FUN_B(3) << " MACRO_A: " << 4);
string c = FUN_C<int, float>(5);
MACRO_A("MACRO_A: " << c << " MACRO_A: " << FUN_B(6));
// MACRO_A("MACRO_A: " << FUN_C<int, float>(7) << " MACRO_A: " << FUN_B(8));
MACRO_B(FUN_C<int, float>(9));
MACRO_B("I AM " << FUN_C<int, float>(10) << " MACRO_B: " << 11);
MACRO_C("OK?", "MACRO_C: " << 12 << " MACRO_C: " << 13);
// MACRO_C("NOT " << FUN_C<int, float>(14) << " MACRO_C: " << 15 , "ERROR?");
return 0;
}
补充说明(Supplement):
上面黄色两行都会compile error
第二个ERROR属于我叙述问题的情况