Temporary Variables: Runtime rvalue Detection - The Ternary Conditional Operator
(Page 2 of 4 )
conditional expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
[the C++ Standard]
This operator allows you to compact an "if-then-else" statement into one single statement. Instead of writing:
if ( value==true ) { desc=”true”; } else { desc=”false”; }
You can use the ternary operator:
desc = value ? “true” : “false”;
This operator takes an expression to evaluate, then an expression to evaluate whether the condition is true and an expression to evaluate whether the condition is false. The first expression is implicitly converted to bool and if it is true, the result of the conditional expression is the second expression, otherwise that of the third expression.
Next: The RVALUE_TEST macro >>
More C++ Articles
More By J. Nakamura