Expressions in depth
Creation of the expression
Because of the expression template mechanism, every glas-expression
is represented by an object that refers to all necessary data to
evaluate (part of) the expression. For being generic, these objects
are instances of a template class. To avoid having to list all
template arguments of the template class when constructing the
object, free functions are available that rely on template argument
deduction. Thus instead of writing following expression (which most
probably will not fit on your screen but that is not necessarily a
bad thing because it exactly proves the point we want to make here
;-)
scalar_vector_mult_expression< vector_norm_2_expression< dense_vector< double > >, dense_vector< double > >( vector_norm_2_expression< dense_vector< double > >(v), dense_vector< double >(w) )
we can write
scalar_vector_mult( vector_norm_2(v), w )
These free functions are only an aid to construct the
expression-object using template argument deduction. Their
implementation is therefore straightforward:
scalar_vector_mult_expression<S,V> scalar_vector_mult(S const& s, V const& v )
{ return scalar_vector_mult_expression<S,V>( s, v ) ; }
Similarly, vector_norm_2(v) creates the UnaryExpression
vector_norm_2_expression<V> vector_norm_2( V const& v )
{ return vector_norm_2_expression<V>( v ) ; }
The expression-object
The creation of the scalar_vector_mult_expression
does not compute the result of this expression, but only stores the
data that are needed to compute it. For instance
scalar_vector_mult_expression
might be implemented
like
template <class S, class V>
struct scalar_vector_mult_expression
{
scalar_vector_mult_expression(S const& s, V const& v )
: s_( s )
, v_( v )
{}
S const& s_ ;
V const& v_ ;
};
while vector_norm_2_expression might be
implemented like
template <class V>
struct vector_norm_2_expression
{
norm_1_expression( V const& v )
: v_( v )
{}
V cons& argument() const
{ return v_ ; }
V const& v_ ;
};
Further reading