Re: Inconsistent -Wparentheses warnigs for gcc>=13 with -std=c++20?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 26 Nov 2024 at 12:26, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:
>
> On Tue, 26 Nov 2024 at 08:36, Thomas Madlener via Gcc-help
> <gcc-help@xxxxxxxxxxx> wrote:
> >
> > Dear Developers,
> >
> > I have recently fallen over some new -Wparentheses warnings when switching
> > to gcc13 and c++20. Switching compilers and c++ standards at the same time
> > is bound to uncover a few of these. However, in this case, I think I found
> > some inconsistent behavior and I am wondering whether this warrants a bug
> > report, or if I am just missing something.
> >
> > Consider the following code:
> > struct A {
> >     int i;
> >     bool operator=(const A&) {
> >         if (i == 2) return true;
> >         return false;
> >     }
> > };
> >
> > int main() {
> >     A a1{1};
> >     A a2{2};
> >     // This does NOT trigger a warning
> >     if ((a1 = a2)) return 2;
>
> The redundant extra parentheses here are how you tell GCC that you
> really did mean to use an assignment, not equality comparison.
>
> >     // But this DOES
> >     if (!(a1 = a2)) return 1;
>
> In this case, if the use of == was an accident and you had intended to
> write !(a1 == a2) then the code would look the same (except for the
> missing '=' character). The "extra" parentheses here would have to be
> present because of the ! operator, so unlike the first case above,
> they are not redundant.

Using if ((!(a1 = a2))) suppresses the warning for this case.

I'm not sure why it only warns for C++20 though, I'll look into that.


>
> >     return 0;
> > }
> >
> > Compiling this with gcc13 and -Wall -Wextra -Wpedantic -std=c++20 generates
> > no warning for the first if statement in main, while it does so for the
> > second. To my understanding those are effectively the same context, but
> > negating the boolean makes gcc emit a warning, while the non negated check
> > does not. Is this (apparent discrepancy) expected behavior? And if so, what
> > am I missing?
> >
> > And then out of interest? Why do I get a -Wparentheses warning here in the
> > first place? I thought that having operator= return a bool should make the
> > condition evaluate the return value of that rather than the assigned value.
> > However, I am probably missing something here and would appreciate any
> > insights.
>
> The warning is intended to help when people write `if (a = b)` instead
> of `if (a == b)` which is a very easy mistake to type, but completely
> changes the meaning of the code.
> The types involved (i.e. the fact that A::operator= is being called)
> are not relevant at all, as far as I know. The warning operates purely
> on the syntax and warns about the use of = where maybe == was
> intended.
>
>
> >
> > The code from above on compiler explorer, with
> > - gcc12 -std=c++20 (no warning)
> > - gcc13 -std=c++17 (no warning)
> > - gcc13 -std=c++20 (warning)
> > https://godbolt.org/z/sdjh7bfzn
> >
> > Best regards,
> > Thomas



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux