C++ Reference ยท Operators
The C++ conditional operator ?:
Reference for the C++ conditional operator ?: (ternary): syntax, result type and value category, lvalue results, nesting and when to prefer if/else.
The conditional operator condition ? a : b evaluates condition, then evaluates and yields only one of the two branches. It is C++'s only ternary operator and the expression counterpart of if/else: use it where a value is needed, such as an initializer, a function argument or a return statement.
Both branches take part in determining the type of the result, so they must have types that can be converted to a common type. If both are lvalues of the same type, the result is an lvalue and can be assigned to.
int larger = (a > b) ? a : b; const char* word = (n == 1) ? "item" : "items";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
condition ? expression_if_true : expression_if_false
| Name | Description |
|---|---|
condition |
Evaluated first and contextually converted to bool. |
expression_if_true |
Evaluated only if the condition is true. |
expression_if_false |
Evaluated only if the condition is false. |
Returns: the value of the chosen branch. If the branches have the same type and value category (both lvalues, or both prvalues), the result has that type and category. Otherwise the usual arithmetic conversions or implicit conversions find a common type, and the result is a prvalue.
Errors / undefined behavior: the program is ill-formed if no common type exists (cond ? 1 : "text"). A branch that is a throw expression does not take part in the type decision. Returning a reference to a local variable chosen through ?: is undefined behavior, as for any dangling reference.
Usage notes
- Associativity: right to left, so
a ? b : c ? d : eisa ? b : (c ? d : e). Format long chains one branch per line, or useif/elseor aswitch. - Precedence: very low, just above assignment. Wrap it in parentheses inside bigger expressions, especially in
<<chains:std::cout << x ? "a" : "b"parses as(std::cout << x) ? .... - Const initialization:
const T x = cond ? a : b;is the idiom that lets you keep a variableconst. Anifstatement would need a mutable variable or a lambda. - Mixed types:
true ? 1 : 2.5is adoubleeven when theintbranch is chosen. Mixing signed and unsigned operands converts to unsigned. - Class types: both branches can be class types if one converts implicitly to the other, but not both ways. Surprising copies can happen, so prefer branches of the same type.
- In
constexprcode: the unchosen branch is not evaluated, so it may contain something that would otherwise fail in constant evaluation (a division by zero, a throw). - Not a statement: you cannot put
return,breakor declarations in a branch. Athrowexpression is allowed. - Elvis operator: C++ has no
a ?: b. Writea ? a : b, which evaluatesatwice, so keep side effects out of it.