Conditional expressions
A conditional expression is a compound expression that contains a condition that is implicitly converted to type bool in C++(operand1), an expression to be evaluated if the condition evaluates to true (operand2), and an expression to be evaluated if the condition has the value false (operand3).
The conditional expression contains one two-part operator. The ? symbol follows the condition, and the : symbol appears between the two action expressions. All expressions that occur between the ? and : are treated as one expression.
- An arithmetic type
- A compatible pointer, structure, or union type
- void
Two objects are compatible when they have the same type but not necessarily the same type qualifiers (volatile or const). Pointer objects are compatible if they have the same type or are pointers to void.
- If the value is true, the second operand is evaluated.
- If the value is false, the third operand is evaluated.
If the second and third expressions evaluate to arithmetic types, the usual arithmetic conversions are performed on the values. The types of the second and third operands determine the type of the result.
a ? b : c ? d : e ? f : g
a ? b : (c ? d : (e ? f : g))