[英]CppCheck warning: expression depends on order of evaluation in x = x |= (1 << 3)
The line of code in C is C中的代码行是
x = x |= (1 << 3);
which gives an cppCheck Error: "Expression 'x=x|=1' depends on order of evaluation of side effects" 给出cppCheck错误:“表达式'x = x | = 1'取决于副作用的评估顺序”
whereas the line 而线
x |= (1 << 3);
is ok. 没关系。
I thought 我想
x = x |= (1 << 3);
whould be the same as 应该是一样的
x = x = x | (1 << 3);
which is just 这只是
x = (x = (x | (1 << 3)));
where actually the outer assignment to x has no effect, meaning the outcome is the same as 实际上x的外部赋值没有效果,这意味着结果与之相同
x |= (1 << 3);
So what exactly is CppCheck complaining about here? 那么CppCheck究竟在这里抱怨什么呢?
edit: think it is a duplicate of why j = j++
is or is not the same as j++
which is discussed in the question referred to above. 编辑:认为这是为什么
j = j++
与上面提到的问题中讨论的j++
相同或不同的重复。
This quote from @Cornstalks' link on sequence points explains it very well. @Cornstalks 关于序列点的链接引用了这一点。
Expressions ... which modify the same value twice are abominations which needn't be allowed (or in any case, needn't be well-defined, ie we don't have to figure out a way to say what they do, and compilers don't have to support them).
两次修改相同值的表达式是不需要允许的可憎行为(或者在任何情况下,不需要明确定义,即我们不必找出说出他们做什么的方式,以及编译器不必支持它们)。
The C Standard simply does not mandate anything about these types of expressions, and therefore there is no particular order of evaluation that is guaranteed in all environments. C标准并不强制要求任何关于这些类型的表达式的内容,因此在所有环境中都没有特定的评估顺序。
x = x |= 1
is pretty much equivalent to x = x += 1
in terms of side effects(modifications to x). 就副作用(对x的修改)而言,
x = x |= 1
几乎等于x = x += 1
。 x = x += 1
is equivalent to x = ++x
in C. This expression is a well-known undefined expression . x = x += 1
相当于C中的x = ++x
。该表达式是一个众所周知的未定义表达式 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.