Re: Generic "expression template" library?
On Feb 9, 10:14 pm, Noah Roberts <n...@nowhere.com> wrote:
Juha Nieminen wrote:
Daniel Pitts wrote:
Juha Nieminen wrote:
Daniel Pitts wrote:
x = Multiply<double, double>(1,3) + 4;
Out of curiosity, exactly how would that be different from:
x = 1.0 * 3.0 + 4:
which a compiler will also be able to calculate at compile
time (and which is much easier to read)?
Because in the end I won't be dealing with doubles. It may
be Vectors (not std::vector), Matrices, Colors,
WhatEverYouThinkOf, etc...
I still fail to see the need for a templated version of the
basic operators. For example, what's the relevant difference
between:
x = Multiply<Color, Color>(c1, c2);
and:
x = c1 * c2;
?
Consider the problem of multiplying 3 3x3 matrices. A
straight forward method of doing so might be to implement
operator * like so:
matrix_type operator * (matrix_type const& a, matrix_type const& b)
{
matrix_type ret;
for all i,j do:
ret(i,j) = a(i,j) * b(j,i);
return ret;
}
or something like that.
Important thing to note here is that you've a copying operator
here. Any time * appears in an expression there's a
temporary.
The expression template version will instead do something like this:
matrix_multiplication operator * (mt const& a, mt const& b)
{
return matrix_multiplication(a,b);
}
Of course, but you're still using operator *, and not
Multiple<double,double>().
(FWIW: this technique was used even before the language had
templates, using virtual functions. Since the compiler knew the
actual types involved in the expression, it could still inline
the functions.)
matrix_multiplication then has a conversion operator to
convert to matrix_type that does each of the necessary
operations for each element accessed.
Or there is an overload of the assignment operator and a
constructor which takes matrix_multiplication. Of course,
normally, the overloads will be templates, so that it can also
handle matrix_add, matrix_subtract, etc. (Or in earlier days, it
took a const reference to the base class of all of these
classes).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34