I'm not familiar with the details of how the addition and multiplication operators are defined in C++, and I also don't know what information is or is not available to the compiler to optimize out inefficient orders of operation.
Let's say we have a matrix datatype with an implementation of element-wise multiplication by a scalar (i.e scaling the matrix by a scalar). If we have the following code snippet:
Matrix a;
float b = /* value set at runtime */;
float c = /* value set at runtime */;
a = a * b * c;
If we naively evaluated this expression left-to-right, we would first scale a
by b
and then scale it by c
rather than scaling a
by the product of b
and c
. Is the compiler capable of selecting the latter
(more efficient) behavior instead of the former?