2010-09-30 74 views
8

是這樣的可能嗎?推導模板的返回類型的操作符/函數

// 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; } 

或者有人不得不破解一個return_type結構並專門爲每一對本機類型?

回答

12

聽說decltype

C++0x你可以做

template<class T, class U> 
auto mul(T x, U y) -> decltype(x*y) 
{ 
    return x*y; 
} 
+0

是這樣的可能沒有的C++ 0x? – Chris 2010-09-30 14:03:16

+1

@Chris:不行!此功能在MSVC++ [2010]和g ++(版本4.3及更高版本)中可用。 – 2010-09-30 14:13:21

+0

沒有,這是中了「自動」和「decltype」項目已添加是爲了克服這一缺陷導致的問題與像通用編程領域,不必猜配合物類型,如容器的迭代器等非標準操作的原因之一在某些編譯器中存在「typeof」來支持這一點,但現在已經爲我們的快樂而標準化了。 – David 2010-09-30 14:14:18

0

前的C++ 0x

我不知道到底要完成什麼,所以:

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; 
} 

還有more

2

我正在使用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. 
+0

你可以提供一個例子,你將如何使用my_mul()? – Alsk 2010-10-01 09:01:05

+0

您可以讓編譯器通過引用函數:'mult(result_double3,float3,double3)'''來推斷傳遞變量的結果類型以存儲結果。不方便的語法,但會節省手頭黑客時間。 – Alsk 2010-10-04 10:21:21

8

可以在非C++0x代碼做到這一點:

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); 
} 

用法: 焦炭T = 3; 短u = 4; int r = mul(t,u);

這裏我們有兩種類型扣除。我們含蓄地使用聲明返回類型,並不完全decltype(T * U)

相關問題