Deduce return type of operator/function for templates
Is something like this possible?
// We can even assume T and U are native C++ types
template<typename T, typename U>
magically_deduce_return_type_of(T * U) my_mul() { return T * U; }
Or would somebody开发者_开发知识库 have to hack up a return_type struct and specialize it for every pair of native types?
Heard of decltype
?
In C++0x
you can do
template<class T, class U>
auto mul(T x, U y) -> decltype(x*y)
{
return x*y;
}
You can do this in non C++0x
code:
template<typename T, typename U> class Mul
{
T t_;
U u_;
public:
Mul(const T& t, const U& u): t_(t), u_(u) {}
template <class R>
operator R ()
{
return t_ * u_;
}
};
template<typename T, typename U>
Mul<T, U> mul(const T& t, const U& u)
{
return Mul<T, U>(t, u);
}
Usage: char t = 3; short u = 4; int r = mul(t, u);
Here we have two type deductions. We implicitly declare return type by usage, not exactly decltype(T*U)
I'm using Visual Studio 2008, so I had to come up with a non C++0x way. I ended up doing something like this.
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; }
EDIT: Here's the example - I'm actually doing this to multiply vectors. It looks something like this:
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.
http://www2.research.att.com/~bs/C++0xFAQ.html#decltype
pre-C++0x
I don't know exactly what you want to accomplish, so:
template<typename T, typename U>
void my_mul(T t, U u, bool& overflow)
{
my_mul_impl(t*u, overflow);
}
template<typename TmultU>
void my_mul_impl(TmultU mult, bool& overflow)
{
//here you know the type and can do something meta-weird :)
if(mult > type_traits<TmultU>::max_allowed_in_my_cool_program())
overflow = true;
}
There is more
精彩评论