Я использую Visual Studio 2008, поэтому мне пришлось придумать способ, отличный от C ++ 0x. Я закончил тем, что делал что-то подобное.
template<typename T> struct type_precedence { static const int value = -1; };
template< > struct type_precedence<long double> { static const int value = 0; };
template< > struct type_precedence<double> { static const int value = 1; };
template< > struct type_precedence<float> { static const int value = 2; };
template< > struct type_precedence<unsigned long long> { static const int value = 3; };
template< > struct type_precedence<long long> { static const int value = 4; };
template< > struct type_precedence<unsigned long> { static const int value = 5; };
template< > struct type_precedence<long> { static const int value = 6; };
template< > struct type_precedence<unsigned int> { static const int value = 7; };
template< > struct type_precedence<int> { static const int value = 8; };
template< > struct type_precedence<unsigned short> { static const int value = 9; };
template< > struct type_precedence<short> { static const int value = 10; };
template< > struct type_precedence<unsigned char> { static const int value = 11; };
template< > struct type_precedence<char> { static const int value = 12; };
template< > struct type_precedence<bool> { static const int value = 13; };
/////////////////////////////////////////////////////////////////////////////////////////
template<typename T, typename U, bool t_precedent = ((type_precedence<T>::value) <= (type_precedence<U>::value))>
struct precedent_type {
typedef T t;
};
template<typename T, typename U>
struct precedent_type<T,U,false> {
typedef U t;
};
/////////////////////////////////////////////////////////////////////////////////////////
template<typename T, typename U>
typename precedent_type<T,U>::t my_mul() { return T * U; }
РЕДАКТИРОВАТЬ: Вот пример - я делаю это для умножения векторов. Это выглядит примерно так:
template<int N, typename T, typename U>
vec<N,typename precedent_type<T,U>::t> operator *(const vec<N,T>& v1,const vec<N,U>& v2) {
...
}
...
double3 = float3 * double3;
float4 = float4 * int4;
etc.