在C ++中实现延迟赋值时出错

时间:2016-09-17 19:05:12

标签: c++ lazy-evaluation crtp

我有一些奇怪的问题,实现了惰性分配,并按https://eigen.tuxfamily.org/dox/TopicInsideEigenExample.html中的说明添加。

代码是

template<typename Derived> class Base;
template<typename Derived, typename OtherDerived> class SumOp;

template<typename Derived, typename OtherDerived>
class SumOp: public Base<SumOp<Derived, OtherDerived>>{
public:
    Derived & lhs;
    OtherDerived & rhs;

    SumOp(Derived & lhs_, OtherDerived & rhs_):lhs(lhs_), rhs(rhs_){}
    double  packet(size_t index, Base<OtherDerived>& src){
        return lhs.packet(index)+ rhs.packet(index);
    }
};
template<typename Derived, typename OtherDerived>
struct Assign{
    static Derived& run(Derived & dst, OtherDerived & src){
        size_t length = dst.size();
        for (size_t index =0; index < length; index++){
            dst.copyPacket(index, src);
        }
        return dst;
    }
};


template<typename Derived>
class Base{
public:
    Base(){}
    Derived& derived(){
        return *static_cast<Derived*>(this);
    }

    const Derived& derived() const{
        return *static_cast<const Derived*>(this);
    }

    template<typename OtherDerived>
            SumOp<Derived,OtherDerived> operator+(Base<OtherDerived> & other){
                return SumOp<Derived, OtherDerived>(this->derived(), other.derived());
    }

    template<typename OtherDerived>
       Derived & operator=(Base<OtherDerived>& other){
        return Assign<Derived, OtherDerived>::run(derived(), other.derived());
    }
    template<typename OtherDerived>
            void copyPacket(size_t index, Base<OtherDerived> & other){
        derived().writePacket(index, other.derived().packet(index));
    }
};


class Vector: public Base<Vector> {
public:
    double * data;
    size_t nRow;

    Vector(size_t nRow_):nRow(nRow_){
        data = (double *)malloc(sizeof(double)*nRow);
    }
    ~Vector(){
         free(data);
    }
    template<typename OtherDerived>
            Vector& operator=( Base<OtherDerived>& other){
        return Base<Vector>::operator=(other);
    }
    size_t size(){
        return nRow;
    }
    void writePacket(size_t index, double src){
        data[index] = src;
    }
    double packet(size_t index){
        return data[index];
    }
};

简而言之,问题出在我在Vector类中调用operator=时,编译器抱怨我已将左值Base<SumOp<Vector, Vector>>传递给运算符而不是SumOp<Vector, Vector>&。后者在调用operator+时创建,返回SumOp<Vector, Vector>

编译器错误是

no known conversion for argument 1 from ‘SumOp<Vector, Vector>’ to ‘Base<SumOp<Vector, Vector> >&’

我不确定为什么在Eigen中的实现是可以的以及如何解决这个问题。

感谢您的帮助。

修改

当我调用以下函数时会出现问题

void test_vector(){
    Vector a(10), b(10), c(10);
    for (int i=0;i<10;i++){
        a.data[i]=1.0;
        b.data[i]=1.0;
    }
    //This is very it goes wrong
    c = a+b;


    for (int i =0 ; i<10;i++){
       std::cout << c.data[i]  << std::endl;
    }

};

使用g ++ - 6时,错误读取

invalid initialization of non-const reference of type ‘Base<SumOp<Vector, Vector> >&’ from an rvalue of type ‘Base<SumOp<Vector, Vector> >’
     c = a+b;
         ~^~
In file included from /home/ran/Desktop/experiment/PointerMatrix/vector.cpp:6:0:
/home/ran/Desktop/experiment/PointerMatrix/vector.h:76:21: note:   initializing argument 1 of ‘Vector& Vector::operator=(Base<OtherDerived>&) [with OtherDerived = SumOp<Vector, Vector>]’
             Vector& operator=( Base<OtherDerived>& other){

1 个答案:

答案 0 :(得分:3)

问题是因为operator函数的参数不是const引用。 operator+返回一个临时SumOp对象,该对象不能作为非const引用传递。 (有些编译器可能会支持此作为扩展。)

将参数更改为const &类型应解决此问题。