Why is this an issue?
While the ternary operator is pleasingly compact, its use can make code more difficult to read. It should therefore be avoided in favor of the more
verbose if
/else
structure.
Noncompliant code example
printf("%s", (i > 10 ? "yes" : "no"));
Compliant solution
if (i > 10) {
printf("yes");
} else {
printf("no");
}
Exceptions
For C++11 mode only, the issue is not raised for ternary operators used inside constexpr
functions. In C++11 such functions are
limited to just a return statement, so the use of a ternary operator is required in them. This restriction is lifted in later standards, and thus
issues are raised.