I’m doing some numerical simulations where it is nice to overload operations on vectors (similar to valarrays). For example, I can write
template <typename T>
vector<T> operator*(const vector<T>& A, const vector<T>& B){
//blah blah
}
But what if I want to generalize this template so as to act on two different types of vectors and (potentially) return a third type? I.e. I want to write
template <typename T, template U, template V>
vector<V> operator*(const vector<T>& A, const vector<U>& B){
//blah blah
}
Now, the above does indeed work if I use the operator in a situation “A*B” where A and B are distinct types and return a another distinct type. However, if A and B are the same type, it does not work. Certainly I could define different templates for each combination (i.e. T only, or T and U only, or T, U, and V) but that seems ugly. Is there a way I can use a single template expression of the T,U, and V variety given above and make it work even if “A”, “B”, and “A*B” are all the same types (or have only 2 different types?)
To be honest, this doesn’t make sense. Your template shouldn’t work at all because V cannot be deduced and it is the third template parameter. If you had written:
This would “work” but only if you explicitly specified V, something like
Surely you want to return a
vector<V>whereVis the type of the expressionT()*U(). This is possible to do in C++11, but not trivially in C++03( I mean, you could do some type-traiting at best). Here’s how it’s done in C++11:HTH