发新话题
打印

expression template help

expression template help

我在某本书上看到关於模板表达式的一个例子
[CODE]
template <typename LeftOpd, typename Op, typename RightOpd>
struct LOR
{
  LeftOpd fod;
  RightOpd rod;
  
  LOR(LeftOpd p, RightOpd r): fod(p), rod(r) {}
  
  double operator[](int i)
  {
    return Op::apply(fod, rod);
  }
};


class Vtr
{
  int length;
  double *vr;
  public:
    Vtr(int n, double* d) {length=n; vr=d;}
   
    template <typename LeftOpd, typename Op, typename RightOpd>
    void operator=(LOR<LeftOpd, Op, RightOpd > exprn)
    {
      for(int i=0; i<length; i++) vr = exprn;
    };
   
    double operator[](int i) const {return vr;}
};


struct Multiply
{
  static double apply(double a, double b) {return a*b;}
};

template <typename LeftOpd>
LOR<LeftOpd, Multiply, Vtr >
operator*(LeftOpd a, Vtr b)
{ return LOR<LeftOpd, Multiply, Vtr >(a,b);
};

int main(void)
{double a[]={1,2,3,4,5};
double b[]={1,2,3,4,5};
double c[]={1,2,3,4,5};
double d[]={1,2,3,4,5};
Vtr X(5,a), Y(5, b), Z(5,c), W(5, d);
  
W = X*Y*Z;
}

[/CODE]
我注意到向量Vtr类double型的。我尝试将代码改为template类。但改了很些地方就是编译不通过。请问版主这应该怎样改才对?      

TOP

我不知道这段代码有什么用,这么冗长真的是很累,我看了 5 分钟才在脑袋里建立一张完整的编译后的代码,呵呵,或许是为了炫耀作者的偏特化技巧吧,活活。
修改很简单啊
复制内容到剪贴板
代码:
template <typename LeftOpd, typename Op, typename RightOpd>
struct LOR
{
  LeftOpd fod;
  RightOpd rod;
  LOR(LeftOpd p, RightOpd r): fod(p), rod(r) {}
  double operator[](int i)
  {
    return Op::apply(fod[i], rod[i]);
  }
};

template <typename T>
class Vtr
{
private:
     int length;
     T   * vr;
public:
    Vtr(int n, T * d) { length =n; vr = d; }

template <typename LeftOpd, typename Op, typename RightOpd>
void operator=(LOR<LeftOpd, Op, RightOpd > exprn)
   {
   for(int i=0; i<length; i++) vr[i] = exprn[i];
    };
    T operator[](int i) const {return vr[i];}
};

struct Multiply
{
  static double apply(double a, double b) {return a*b;}
};

typedef Vtr<double> Vtr_Double;

template <typename LeftOpd>
LOR<LeftOpd, Multiply, Vtr_Double>
operator*(LeftOpd a, Vtr_Double b)
{
return LOR<LeftOpd, Multiply, Vtr_Double >(a,b);
};

int main(void)
{
double a[]={1,2,3,4,5};
double b[]={1,2,3,4,5};
double c[]={1,2,3,4,5};
double d[]={1,2,3,4,5};
Vtr_Double X(5,a), Y(5, b), Z(5,c), W(5, d);
W = X*Y*Z;
}
第二种修改
复制内容到剪贴板
代码:
typedef double DefaultType;

template <typename LeftOpd, typename Op, template <typename> class RightOpd, typename Type=DefaultType>
struct LOR
{
  typedef RightOpd<Type> TheRightOpd;
  LeftOpd fod;
  TheRightOpd rod;
  LOR(LeftOpd p, TheRightOpd r): fod(p), rod(r) {}
  double operator[](int i)
  {
    return Op::apply(fod[i], rod[i]);
  }
};

template <typename T>
class Vtr
{
private:
     int length;
     T   * vr;
public:
    Vtr(int n, T * d) { length =n; vr = d; }

template <typename LeftOpd, typename Op, template <typename> class RightOpd>
    void operator=(LOR<LeftOpd, Op, RightOpd> exprn)
    {
    for(int i=0; i<length; i++) vr[i] = exprn[i];
    };
    T operator[](int i) const {return vr[i];}
};


struct Multiply
{
  static double apply(double a, double b) {return a*b;}
};

typedef Vtr<DefaultType> Vtr_Double;

template <typename LeftOpd>
LOR<LeftOpd, Multiply, Vtr>
operator*(LeftOpd a, Vtr_Double b)
{
return LOR<LeftOpd, Multiply, Vtr>(a,b);
};

int main(void)
{
double a[]={1,2,3,4,5};
double b[]={1,2,3,4,5};
double c[]={1,2,3,4,5};
double d[]={1,2,3,4,5};
Vtr_Double X(5,a), Y(5, b), Z(5,c), W(5, d);
W = X*Y*Z;
}
      
-----------------------------------------
http://www.darkspy.org/blog

自大的人把宗教当迷信,无知的人把迷信当宗教

TOP

谢谢版主, 但你误会了我的意思。原代码是针对double这个型别而设计的,例如

[CODE]
struct Multiply
{
  static double apply(double a, double b) {return a*b;}
};
[/CODE]

中是指定了double类型。由此相关的一些代码都是关於double的。我是想将它改一下,使整段代码跟类型无关,即用template来指定类型。如,

[CODE]
template <typename T>
struct Multiply
{
  static T apply(T a, T b) {return a*b;}
};
[/CODE]

但作这样修改后,其它的代码也要修改。可是我怎样改也调不通。      

TOP

至少给个错误啊,你也知道模版这东西代码不全没办法设想环境。      

TOP

我要整理一下再贴上来, 因为我改了很久很久。编译时错误提示很长,真烦      

TOP

道理一样啊
复制内容到剪贴板
代码:
typedef double DefaultType;

template <typename LeftOpd,

                  template <typename> class Op,
                  template <typename> class RightOpd, typename Type=DefaultType>
struct LOR
{
  typedef Op<Type>       TheOp;
  typedef RightOpd<Type> TheRightOpd;
  LeftOpd fod;
  TheRightOpd rod;
  LOR(LeftOpd p, TheRightOpd r): fod(p), rod(r) {}

  Type operator[](int i)
  {
    return TheOp::apply(fod[i], rod[i]);
  }
};

template <typename T>
class Vtr
{
private:
         int length;
     T   * vr;
public:
    Vtr(int n, T * d) { length =n; vr = d; }
    typedef T TheType;
        template <typename LeftOpd, template <typename> class Op, template <typename> class RightOpd>
    void operator=(LOR<LeftOpd, Op, RightOpd, T> exprn)
    {
      for(int i=0; i<length; i++) vr[i] = exprn[i];
    };
    T operator[](int i) const {return vr[i];}
};


template <typename T>
struct Multiply
{
  typedef typename Vtr<T>::TheType TheType;
  static typename Multiply<T>::TheType apply(TheType a, TheType b) {return a*b;}
};

typedef Vtr<DefaultType> Vtr_Double;

template <typename LeftOpd>
LOR<LeftOpd, Multiply, Vtr>
operator*(LeftOpd a, Vtr_Double b)
{
return LOR<LeftOpd, Multiply, Vtr>(a,b);
};

int main(void)
{
double a[]={1,2,3,4,5};
double b[]={1,2,3,4,5};
double c[]={1,2,3,4,5};
double d[]={1,2,3,4,5};
Vtr_Double X(5,a), Y(5, b), Z(5,c), W(5, d);
W = X*Y*Z;
}
      
-----------------------------------------
http://www.darkspy.org/blog

自大的人把宗教当迷信,无知的人把迷信当宗教

TOP

发新话题