开发者

C++ warning: suggest parentheses around arithmetic in operand of |

I have a code like

A = B|C|D|E;

Throwing the warning "suggest parentheses around arithmetic in operand of |"

Expecting that expression needs high priority paranthesis for operators, tried the following ways:

A=(B|C)|(D|E);

one more as :

A=(((B|C)|D)|E);

Still the same warning persists.

Please help me in resolving this.

Thanks, Sujatha

开发者_高级运维

B, C,D are enums and E is an integer.


You have some arithmetic operator in your expression that isn't really simply B, or that isn't really simply C, etc. The compiler is suggesting that you parenthesize whichever expression so that readers will see that you wrote what you meant. If you don't parenthesize, everyone has to remember exactly what the priorities are, and they have to figure out if you remembered when you wrote it.

Try this: (B)|(C)|(D)|(E).


This is a weird warning. You only really need to pay attention to precedence when you're using different operators and those operators have different precedences. For instance, in arithmetic multiplication has higher precedence than addition.

But in this case you're using only one operator multiple times. Bitwise or is associative and commutative ((A | B) | C == A | (B | C) and A | B == B | A) so there's really no reason for the warning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜