实现在C ++表达式模板中评估表达式的能力

时间:2012-05-09 21:23:51

标签: c++ templates template-meta-programming

我必须评估x power n + 1格式的表达式。我使用以下格式重载+, - ,*,/

template<class L,class R>
Expr<L,R,plus<double> > operator+(const L l,const R r){
return Expr<L,R,plus<double> >(l,r);
};

我如何为权力做同样的事情?

我的程序可以计算x + 1 + 2x类型的表达式或任何没有幂的表达式。我希望能够评估一个像(2xpower2)+2左右的幂,其中x的值是变量。

1 个答案:

答案 0 :(得分:0)

为x ^ 0定义一个返回1的模板特化。

为返回x * pow<x, y-1>的一般情况(i.t. x ^ y)定义模板