Daily bit(e) of C++ | conditional operator
Daily bit(e) of C++ ♻️3, The conditional (ternary) operator.
The conditional operator, also known as the ternary operator, is essentially an if statement in the form of an expression.
The operator is sequenced. The left side, with all its side effects, is evaluated first, followed by one of the right-side operands (depending on the boolean value of the first operand).
An important aspect of the conditional operator is that, as an expression, it must have a well-defined type. If the two right-side operands do not align on a type, conversions may be applied to arrive at a compatible common type.
Before C++14 permitted compound expressions in constexpr functions, the ternary operator was the only way to inject logic into a constant-evaluated expression.