Why in conditional operator(?:
), second and third operands must have the same type?
My code like this:
#include
the ?:
expression will produce a value(called rvalue), which is to be assigned to a variable(which is called lvalue), as J.N. metioned, C++ is a statically typed language, lvalue must be known at compile time.
int num = a>b ? a : "eh, string?";
The code above won't compile, cause the variable num
can only hold an int, string is not allowed.