That code is basically how expression templates work, it's just that expression templates
are more generalized whereas the above code essentially ONLY handles one specific
expression (M*V+W). Basically what it does is it delays the computation until the end,
and then it performs both operations at once, to avoid the temporary that would result
from M*V.
So here's an idea that stems from that. Since you don't want to keep a temporary,
because of it's size, could you perhaps instead define your own iterator classes? For
example, an iterator that iterates from one index containing 42 to another?
Constructing the iterator would be easy, but the increment operators would be expensive
since they would be linear searches. But it avoids the temporary copy.
Hi jsmith,
Yes, it seems a better (or rather best) way compared to using statics, for instance. But I couldn't get hold of literature of expression templates. I searched in Stroustrup "C++ programming language" as well as Lafore "Object oriented programming in C++". Could you guide me where I can get some info on the subject and also illustrate your idea a bit further with examples?
I was actually referring to expression templates as a better solution, but iterators as a easy solution. Yes, I just found some other literature on expression templates and it certainly isn't a piece of cake... especially to a non-expert user of templates... I have never used a template as, template <typename T, typename T1, typename T2> for example! Your link seems more resourceful than the one I could get a hold of.
But in the meanwhile, I would like to know the iterator (hack) around this issue with an example if its possible...