When does C ++ 11 give operator precedence warnings?

When writing the code, I have known for a long time that && has a higher precedence than ||; however compiling using the C ++ 11 standard gave me a warning that now I have to use parentheses when using both in the same statement.

Now I got a warning that the concatenation -> and + must also have parentheses. Now my statements are looking very ugly with 5 or more brackets around them.

1) Is there a resource that says which operator combinations now require parentheses?

2) Is there a way to disable only operator priority warnings but keep other warnings?

Compiler gcc

with flags-O2 -Wall -g

There were warnings when I added the flag -std=c++11

Expression example:

(((string[0] << CHAR_BIT) + string[1] << CHAR_BIT) + string[2] << CHAR_BIT) + string[3];

      

+3


source to share


2 answers


See the gcc manual :

-Wparentheses

Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a true value is expected, or when statements are nested, whose precedence people are often confused about.

Also be warned if a comparison like x <= y <= z appears; this is equivalent to (x <= y? 1: 0) <= z, which is a different interpretation from this normal mathematical notation.

Also warns about the dangerous use of the GNU extension for ?: with the middle operand omitted. When the condition is in a ?: boolean expression, the missing value is always 1. Often programmers expect it to be the value evaluated inside the conditional instead.



(I added emphasis)

To disable this behavior, specify -Wno-parentheses

- gcc/g++

.

+1


source


When does C ++ 11 give operator precedence warnings?

The only time a standard requires a diagnostic message (note that the standard does not distinguish between warnings and errors that stop compilation) is when a program violates the standard. Unless the compiler excludes the "no diagnostics required" statement.

All other warnings are optional to the compiler and not required by the standard.




1) Is there a resource that says which operator combinations now require parentheses?

No, since parentheses are not required. The warning is just a suggestion from the compiler. The program is well formed.

There were warnings when I added the -std = C ++ 11 flag

For what it's worth, my GCC warns, regardless of the standard argument.




2) Is there a way to disable only operator priority warnings but keep other warnings?

The warning itself states which warning option enabled it (here is the warning from my GCC):

warning: suggest parentheses around '+' inside '< [- Wparentheses]

To turn off, you can use the appropriate option to turn it off: -Wno-WHATEVER

.




Now my statements look very ugly, with 5 or more parentheses floating around them.

I recommend instead extracting the repeating structure and reusing the standard algorithm:

std::accumulate(string, string + 4, 0, [](auto sum, auto value) {
    return (sum << CHAR_BIT) + value;
});

      

Much less parentheses :) Note that in C ++ 11 (up to C ++ 14) you cannot use a auto

lambda as an argument type. I don't know what types you are using.

+3


source







All Articles