[Bug c++/77434] warn about suspicious precedence of ternary operator (?:)
joseph at codesourcery dot com
gcc-bugzilla@gcc.gnu.org
Wed Aug 31 20:35:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77434
--- Comment #2 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
On Wed, 31 Aug 2016, joseph at codesourcery dot com wrote:
> > Code such as the following are suspicious:
> >
> > int foo(int a, int b)
> > {
> > return (a > 0 && a <= (b == 1) ? 1 : 2);
>
> Actually I don't think this is suspicious at all; it's just an ordinary
> conditional expression and this precedence rule doesn't tend to confuse
> people normally. Certainly it's less dubious than various existing cases
> warned about for precedence.
Or, if it's suspicious, it's only because of the boolean expression (b ==
1) in the RHS of <=, where the alternative parse makes more sense. The
following is not suspicious and it would seem silly to warn for it:
return (a > 0 && b <= 3 ? 1 : 2);
(because the suggested alternative parse would involve b <= 3 ? 1 : 2 as
RHS of &&, which is unnatural as a conditional expression in boolean
context where the halves of the expression aren't boolean).
More information about the Gcc-bugs
mailing list