AutosarC++17_03-A5.16.1ΒΆ

The ternary conditional operator shall not be used as a sub-expression

Required inputs: IR

Rationale

Using the result of the ternary conditional operator as an operand or nesting conditional operators makes the code less readable and more difficult to maintain.

Example
// $Id: A5-16-1.cpp 271927 2017-03-24 12:01:35Z piotr.tanski $
#include <cstdint>
constexpr bool fn1(std::int32_t x) {
return (x > 0); // Compliant
}
std::int32_t fn2(std::int32_t x) {
std::int32_t i = (x >= 0 ? x : 0); // Compliant

std::int32_t j =
x + (i == 0 ? (x >= 0 ? x : -x) : i); //   Non-compliant - nested
                                    //   conditional operator
                                    //   and used as a
                                    //   sub-expression
return (i > 0
      ? (j > 0 ? i + j : i)
      : (j > 0 ? j : 0)); // Non-compliant - nested conditional operator
}
See Also
  • HIC++ v4.0 [8]: 5.8.1 Do not use the conditional operator (?:) as a sub- expression.

Possible Messages

Key

Text

Severity

Disabled

use_of_nested_conditional_operator

Use of nested conditional operator.

None

False

Options