TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
?: operator, formally known as the conditional operator, is the only ternary operator in C++. It evaluates a boolean condition and executes exactly one of two subsequent expressions based on whether the condition resolves to true or false.
Evaluation Mechanics
- Contextual Conversion: The
conditionis contextually converted tobool. - Sequence Point: There is a strict sequencing guarantee. The evaluation of the
conditionis sequenced before the evaluation of either result expression. All side effects of the condition are guaranteed to be committed before the selected expression is evaluated. - Short-Circuiting: The operator guarantees short-circuit evaluation. If the condition is
true, onlyexpression_if_trueis evaluated. Iffalse, onlyexpression_if_falseis evaluated. The unselected expression is entirely ignored, meaning any side effects or function calls within it will not occur.
Type Resolution
Because the conditional operator is an expression, it must yield a single, well-defined type at compile time, regardless of runtime evaluation. The compiler determines the result type by analyzingexpression_if_true and expression_if_false:
- If both expressions have the exact same type, the result is of that type.
- If both expressions are of type
void(e.g., calling twovoid-returning functions), the result type of the ternary expression is validlyvoid. - If one of the expressions is a
throwexpression, the result type of the ternary operation is strictly the type of the non-throw expression. - If the types differ, the compiler attempts to find a common type:
- Arithmetic Types: The compiler applies the usual arithmetic conversions to determine the common type (e.g., if one operand is
intand the other isdouble, the result type isdouble). - Class Hierarchies: If one operand is of a
Baseclass and the other is of aDerivedclass, the compiler evaluates reference binding rules to determine if a common base type can be targeted. - Implicit Conversions: If an implicit conversion exists from one type to the other in exactly one direction, the result takes the target type.
- Ambiguity/Mismatch: If implicit conversions exist in both directions between two distinct class types, or if neither type can be implicitly converted to the other, the compiler generates an error.
- Arithmetic Types: The compiler applies the usual arithmetic conversions to determine the common type (e.g., if one operand is
Value Categories (Lvalue, Xvalue, Prvalue)
The C++ conditional operator determines the value category of its result based on the value categories and types of its operands:- Lvalue Result:
- If both
expression_if_trueandexpression_if_falseare lvalues of the exact same type, the result of the entire ternary expression is an lvalue. - If one operand is an lvalue of a
Baseclass and the other is an lvalue of aDerivedclass, theDerivedobject can be implicitly converted to aBase&. The result is an lvalue of theBaseclass.
- If both
- Prvalue Result:
- If the operands are glvalues of the same type but have different value categories (e.g., one lvalue and one xvalue), the result is a prvalue. The compiler forces a copy or move to materialize a new temporary object because a single expression cannot dynamically resolve to different reference types.
- If the expressions have different types requiring conversion to a common type (excluding the
Base/Derivedlvalue reference binding mentioned above), or if at least one of the expressions is a prvalue, the result is a prvalue.
Precedence and Associativity
- Precedence: The conditional operator has very low precedence. It shares the exact same precedence level as assignment operators (
=,+=, etc.) and thethrowoperator. Because it binds loosely, it often requires parentheses when embedded within larger expressions (such as stream insertion<<). - Associativity: It is right-to-left associative. When multiple conditional, assignment, or throw operators are chained, they group from the right.
Master C++ with Deep Grasping Methodology!Learn More





