Consider following statement:
C a, b; //C contains c1, c2 and c3 all integers
if(a.c1==b.c1 && a.c2 == b.c2) {
a.c3=b.c3;
}
Will this statement be optimized to the following:
if(a.c1 == b.c1) {
if(a.c2 == b.c2) {
a.c3=b.c3;
}
}
AFAIK, C++ compilers does not perform this kind of operation since it can have side effects. But these are built-in types.
- Is there anything related in the standard?
- If it is compiler specific is main stream compilers (MS, GNU, Intel) are doing it or not?
Yes. The following code snippet:
will be “optimized” to this (or something equivalent):
This is required not because of optimization but because the C++ standard requires short-circuit evaluation. So reasonably standards-conforming C++ compilers should be able to short-circuit that.
There isn’t a single place in the C++ standard that explicitly states that some boolean operators are short-circuited. It is implied from the rules:
The rules are similar for the
||operator:And the conditional
?operator: