I recently ran into a situation where I wrote the following code:
for(int i = 0; i < (size - 1); i++)
// do whatever
// Assume 'size' will be constant during the duration of the for loop
int foo = 1;
if(foo + foo + foo + foo + foo + foo)
// Assume val is an int that can take on any value
The C++ language specification permits the compiler to make any optimization that results in no observable changes to the expected results.
If the compiler can determine that
size is constant and will not change during execution, it can certainly make that particular optimization.
Alternatively, if the compiler can also determine that
i is not used in the loop (and its value is not used afterwards), that it is used only as a counter, it might very well rewrite the loop to:
for(int i = 1; i < size; i++)
because that might produce smaller code. Even if this
i is used in some fashion, the compiler can still make this change and then adjust all other usage of
i so that the observable results are still the same.
To summarize: anything goes. The compiler may or may not make any optimization change as long as the observable results are the same.