template curiosity
Just for curiosity:
Is there a way to avoid this rarely annoying case?
1 2 3 4 5 6 7 8
|
template<typename T>
inline VectorT<T> operator*(T s, const VectorT<T> &v) { return v * s; }
int main()
{
VectorT<double> v;
v = 2 * v; // FAIL! must be: v = 2.0 * v; or else it search for const VectorT<int>
.......
|
I think something like this but I realize that no substitution can happen.
1 2
|
template<typename T>
inline VectorT<T> operator*(VectorT<T>::value_type s, const VectorT<T> &v) { return v * s; }
|
I think also this but it creates namespace pollution, right?
1 2
|
template<typename C>
inline C operator*(typename C::value_type s, C &v) { return v * s; }
|
Perhaps the simplest way is to use two type parameters for the template:
1 2 3
|
template < typename T, typename U >
inline VectorT<T> operator*( const U& mult, const VectorT<T>& seq )
{ return seq * mult ; }
|
Topic archived. No new replies allowed.