I was reading some articles and in many of them mentioned about expression templates can avoid using temporary objects. But none of them mentioned how this is done. As far as I know, due to the design architecture operations are done using temporary object. For example, if a, b and c are two matrix and if we do a = b+c
then the result of b+c
is kept in a temporary object like temp = b+c
and then result is copied back to a like a = temp
.
But if we use expression templates then this addition(+) operation returns reference of b and c and then the main calculation happen when assignment operator(=) is evaluated. This is the simple general concept of template expression. But I don't understand how it get rid of temporary objects. It would be nice if someone could give just the general idea how this temporary is avoided.