walnutmon said:
That makes sense, what I was actually getting at is this though... I
wasn't holding off, I simply didn't realize it at the time... At
compilation time does C++ optimize such an instruction into the more
simple version that I explicitly stated in the second example?
I what you have in your code is explicitly
int x = a * b * c * d * e;
and there's no pre-calculated value for 'b * c * d * e' then the
compiler cannot optimize it into what you started in your second example
simply because it will not achieve anything. There's absolutely no
difference in doing the above, or doing
int f = b * c * d * e;
int x = a * f;
Now, if somewhere earlier you calculated 'f' yourself
int f = b * c * d * e;
and then, later, you calculate 'x' as
int x = a * b * c * d * e;
the compiler could be smart enough to optimize the second into
int x = a * f;
assuming that it it can establish the fact that 'b', 'c', 'd', and 'e'
didn't change between these points.
Also if you do your
int x = a * b * c * d * e;
repeatedly for different values of 'a', but the same values of 'b', 'c',
'd', and 'e', the compiler could be smart enough to pre-calculate 'b * c
* d * e' and use it to calculate 'x' instead of re-calculating the whole
thing every time.
But all this relies on the details you haven't provided. Outside of
those more specific contexts, once again, there's absolutely no
difference between your first and the second variant and, therefore, it
would be strange to expect the compiler to do this as an "optimization".