The ternary conditional operator ?:
requires its second and third operand to have "agreeable" types (speaking informally). But this requirement has one exception (pun intended): either the second or third operand can be a throw expression (which has type void
), regardless of the type of the other operand.
In other words, one can write the following pefrectly valid C++ expressions using the ?:
operator
i = a > b ? a : throw something();
BTW, the fact that throw expression is actually an expression (of type void
) and not a statement is another little-known feature of C++ language. This means, among other things, that the following code is perfectly valid
void foo()
{
return throw something();
}
although there's not much point in doing it this way (maybe in some generic template code this might come handy).