The line of code in C is
x = x |= (1 << 3);
which gives an cppCheck Error: "Expression 'x=x|=1' depends on order of evaluation of side effects"
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 |= (1 << 3);
So what exactly is CppCheck complaining about here?
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.
This quote from @Cornstalks' link on sequence points explains it very well.
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.
x = x |= 1
is pretty much equivalent to x = x += 1
in terms of side effects(modifications to x). x = x += 1
is equivalent to x = ++x
in C. This expression is a well-known undefined expression .
Read more about it [here]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.