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:29, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:
>
> 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.

The change in behaviour happened with https://gcc.gnu.org/r13-23 which
was intended precisely to warn about cases like your A::operator=
because that previously got ignored by the warning.
But I still don't see why it only happens with C++20.



[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