I am trying to do matrix addition using expression templates and for this task I have base class: Exp
template<typename subtype>
class Exp{
public:
inline const subtype& self(void) const{
return *static_cast<const subtype*>(this);
}
};
a derived class: matrix
template<typename T,unsigned rows_,unsigned cols_ >
class matrix : public Exp<matrix<T,rows_,cols_>>{
//some members
};
and another derived class: addExp
template<typename T, typename Op1 , typename Op2>
class addExp: public Exp< addExp<T,Op1,Op2> >{
const Op1& op1;
const Op2& op2;
public:
addExp(const Op1& a, const Op2& b): op1(a), op2(b){}
T& operator()(const std::size_t i,const std::size_t j) const{
return op1(i,j) + op2(i,j);
}
};
I am now trying to do operator overloading on addExp for adding matrices.
template<typename T,typename Lhs, typename Rhs>
inline addExp<T,Lhs, Rhs>
operator+(const Exp<Lhs> &lhs, const Exp<Rhs> &rhs) {
return addExp<T,Lhs, Rhs>(lhs.self(), rhs.self());
}
later in my code I try to put two matrix objects(which should have Exp as base class) as function parameters here but I get an error:
prog.cpp: In function ‘int main()’:
prog.cpp:76:25: error: no match for ‘operator+’ (operand types are ‘matrix<int, 3u, 3u>’ and ‘matrix<int, 3u, 3u>’)
matrix<int,3,3> m3 = m1+m2;
~~^~~
prog.cpp:69:1: note: candidate: template<class T, class Lhs, class Rhs> addExp<T, Lhs, Rhs> operator+(const Exp<Lhs>&, const Exp<Rhs>&)
operator+(const Exp<Lhs> &lhs, const Exp<Rhs> &rhs) {
^~~~~~~~
prog.cpp:69:1: note: template argument deduction/substitution failed:
prog.cpp:76:26: note: couldn't deduce template parameter ‘T’
matrix<int,3,3> m3 = m1+m2;
where did I go wrong here and how do I fix this?
Aucun commentaire:
Enregistrer un commentaire