expression template help

From: Rex_chaos (rex_chaos_at_21cn.com)
Date: 10/10/03


Date: 9 Oct 2003 21:43:29 -0700

Hi all,
  As some book tells, I try the following example of expression
template.

template < typename LeftOpd, typename Op, typename RightOpd >
struct LOP
{
  LeftOpd lod;
  RightOpd rod;
  
  LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
  
  ADT operator[](const unsigned int i)
  {
    return Op::apply(lod[i], rod[i]);
  }
};

struct Plus
{
  static double apply( double a, double b) { return a+b; }
};

template <typename LeftOpd>
LOP< LeftOpd, Plus, Vec > operator+(LeftOpd a, Vec b)
{
  return LOP<LeftOpd, Plus, Vec>(a, b);
};

It is a *very* simple example. I am trying to modify it. I have
written my vector class (a template class, says MyVec<T>). So here
Plus and opeator+ should also be modified to a template structure and
template function. I try the following code(I am sorry, I really have
no idea how to do it)

template < typename LeftOpd, typename Op<typename ADT>, typename
RightOpd >
struct LOP
{
  LeftOpd lod;
  RightOpd rod;
  
  LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}
  
  ADT operator[](const unsigned int i)
  {
    return Op<ADT>::apply(lod[i], rod[i]);
  }
};

template <typename ADT>
struct Plus
{
  static ADT apply( ADT a, ADT b) { return a+b; }
};

template <typename LeftOpd, typename ADT>
LOP< LeftOpd, Plus<ADT>, MyVec<ADT> > operator+(LeftOpd a, MyVec<ADT>
b)
{
  return LOP<LeftOpd, Plus<ADT>, MyVec<ADT> >(a, b);
};

The code don't work. I am looking for your help.

BTW, I have also defined my Matrix class. I would like to generalize
the expression template. For instance, I hope the following code works
with the help of expression template

MyMat<double> M(10,10);
MyVec<double> V(10); // a column vector

(M*V + 2.3*M)*M

Any idea?



Relevant Pages