When using expression templates, how do I create specializations? From the Wikipedia example, I can make a Vector sum template class like so:
template <typename E1, typename E2>
class VecSum : public VecExpression<VecSum<E1, E2> > {
E1 const& _u;
E2 const& _v;
public:
VecSum(E1 const& u, E2 const& v) : _u(u), _v(v) {
assert(u.size() == v.size());
}
double operator[](size_t i) const { return _u[i] + _v[i]; }
size_t size() const { return _v.size(); }
};
template <typename E1, typename E2>
VecSum<E1,E2> const
operator+(E1 const& u, E2 const& v) {
return VecSum<E1, E2>(u, v);
}
According to Wikipedia, if I have a Vector
class that extends VecExpression<Vector>
and a constructor from the VecExpression
class that uses the []
operator and a loop, this will allow loop merging so statements like the following only use a single loop:
Vector a = ...;
Vector b = ...;
Vector c = ...;
Vector d = a+b+c;
I get why this works, but I'm not sure how to extend it to scalars. I want to be able to add a scalar (int, float, or double) to the entire Vector, but I'm not sure how to do this. My best guess is to create specializations for the VecSum class like:
template<typename E2> VecSum<int, E2>{ /*stuff goes here*/ }
template<typename E1> VecSum<E1, int>{ /*stuff goes here*/ }
template<typename E2> VecSum<float, E2>{ /*stuff goes here*/ }
template<typename E1> VecSum<E1, float>{ /*stuff goes here*/ }
template<typename E2> VecSum<double, E2>{ /*stuff goes here*/ }
template<typename E1> VecSum<E1, double>{ /*stuff goes here*/ }
But this seems like its a lot more work than is necessary, is there another solution?