条件运算符不能处理混合数据类型,因此:
bool cond = true;
cout << (cond?1:2) << endl;
cout << (cond?"msg1":"msg2") << endl;
cout << (cond?1:"msg") << endl;
在最后一行,我收到此错误消息:
error: incompatible operand types ('int' and 'const char *')
有没有办法使用一行代码在这样的语句中混合不同的类型?我需要将它放在预处理器宏中。
编译器:clang 3.5
最佳答案
对于最后一条语句,您可以确定常见类型,如 std::string
。
例如
std::cout << ( cond ? std::to_string( 1 ) : "msg" ) << std::endl;
关于c++ - 具有混合数据类型的条件运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29182638/