2 <clause number="14.12" title="Conditional operator">
3 <paragraph>The ?: operator is called the conditional operator. It is at times also called the ternary operator. <grammar_production><name><non_terminal where="14.12">conditional-expression</non_terminal></name> : <rhs><non_terminal where="14.11">conditional-or-expression</non_terminal></rhs><rhs><non_terminal where="14.11">conditional-or-expression</non_terminal><terminal>?</terminal><non_terminal where="14.14">expression</non_terminal><terminal>:</terminal><non_terminal where="14.14">expression</non_terminal></rhs></grammar_production></paragraph>
4 <paragraph>A conditional expression of the form b ? x : y first evaluates the condition b. Then, if b is true, x is evaluated and becomes the result of the operation. Otherwise, y is evaluated and becomes the result of the operation. A conditional expression never evaluates both x and y. </paragraph>
5 <paragraph>The conditional operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e). </paragraph>
6 <paragraph>The first operand of the ?: operator must be an expression of a type that can be implicitly converted to <keyword>bool</keyword>, or an expression of a type that implements operator true. If neither of these requirements is satisfied, a compile-time error occurs. </paragraph>
7 <paragraph>The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then, <list><list_item> If X and Y are the same type, then this is the type of the conditional expression. </list_item><list_item> Otherwise, if an implicit conversion (<hyperlink>13.1</hyperlink>) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression. </list_item><list_item> Otherwise, if an implicit conversion (<hyperlink>13.1</hyperlink>) exists from Y to X, but not from X to Y, then X is the type of the conditional expression. </list_item><list_item> Otherwise, no expression type can be determined, and a compile-time error occurs. </list_item></list></paragraph>
8 <paragraph>The run-time processing of a conditional expression of the form b ? x : y consists of the following steps: <list><list_item> First, b is evaluated, and the <keyword>bool</keyword> value of b is determined: </list_item><list><list_item> If an implicit conversion from the type of b to <keyword>bool</keyword> exists, then this implicit conversion is performed to produce a <keyword>bool</keyword> value. </list_item><list_item> Otherwise, the operator true defined by the type of b is invoked to produce a <keyword>bool</keyword> value. </list_item></list><list_item> If the <keyword>bool</keyword> value produced by the step above is true, then x is evaluated and converted to the type of the conditional expression, and this becomes the result of the conditional expression. </list_item><list_item> Otherwise, y is evaluated and converted to the type of the conditional expression, and this becomes the result of the conditional expression. </list_item></list></paragraph>