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

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

 



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;
    // But this DOES
    if (!(a1 = a2)) return 1;
    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 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