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

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

 



>
> >     // 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.


Thanks for the quick reply and explanation. That makes sense, and I didn't
think about that.

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.


OK. In that case the warning makes sense. I thought there was a bit more
semantic analysis going on, but that would probably be rather hard to
implement.

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


Thanks for the hint. I was for now going for if (auto assign = (a1 = a2);
!assign) which works, but is quite a bit of additional noise compared to
your solution.

Given your explanations and the proposed fix, I am actually happy with just
implementing that. But I am also curious why it doesn't happen with c++17.

Best,
Thomas

On Tue, 26 Nov 2024 at 13:45, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:

> 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