Glas :[glas] When to evaluate expressions |
From: Neal Becker (ndbecker2_at_[hidden])
Date: 2007-01-03 10:56:34
I've been thinking about this subject, and wondered if any of you have some
ideas on this. (Disclaimer - I'm no expert).
With expression templates, evaluation of expressions is deferred. This is a
bit like lazy evaluation in languages such as Haskell.
Evaluation usually? occurs on assignment to a concrete container:
vector<T> out = in1 * in2;
But can be carried in deferred form:
typeof (in1 * in2) out = in1 * in2;
So the current practice (which is what I'm identifying above) is relying on
the programmer to decide when evaluation occurs.
My question is, is this the right appoach? Perhaps, instead, by default the
compiler should decide?
This is a lot like other forms of optimization. Ultimately, hand-tuned
assembly can out-perform compiled code - but only when the programmer has a
lot of knowledge and a lot of spare time. Otherwise, the compiler can
generate better code.
Does anyone agree with me that it is desirable for a compiler to decide on the
point of evaluation of expressions (rather than the programmer)? If so, is
there any reasonable way to implement this in c++?