Conditional operator cannot work with mixed data types, so:
bool cond = true;
cout << (cond?1:2) << endl;
cout << (cond?"msg1":"msg2") << endl;
cout << (cond?1:"msg") << endl;
On the last line, I get this error message:
error: incompatible operand types ('int' and 'const char *')
Is there a way to mix different types in such a statement, using a single line of code? I need to put it inside a preprocessor macro.
Compiler: clang 3.5
For the last statement you can determine the common type like std::string.
For example
std::cout << ( cond ? std::to_string( 1 ) : "msg" ) << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With