使用std :: vector进行惰性求值

时间:2018-01-07 20:31:43

标签: c++ stl overloading

我试图实现对std :: vector求和的惰性求值。

  template <class L, class OpTag, class R>
struct Expression
{
    Expression(const L &rcL, const R &rcR)
        : m_rcL(rcL), m_rcR(rcR)
    { }
    double  operator [] (unsigned index) const
    {
        return OpTag::apply(m_rcL[index], m_rcR[index]);
    }
    const L &m_rcL;
    const R &m_rcR;
};

struct OpAdd;

template <class L, class R>
Expression <L, OpAdd, R> operator + (
    const L &rcL, const R &rcR)
{
    return Expression <L, OpAdd, R>(rcL, rcR);
}

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

template <class TExpr>
std::vector<double> & std::vector<double>::operator = (const TExpr &rcX)
{
    for (unsigned i = 0; i < this->size(); ++i)
        (*this)[i] = rcX[i];
    //
    return *this;
}

返回下一个错误:

  

错误C2244&#39; std :: vector&gt; :: operator =&#39;:   无法将函数定义与现有函数匹配   声明

正如我所看到的,问题在于重载std :: vectors的assign运算符。如何解决错误?

0 个答案:

没有答案