这样的事情可能吗?
// 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 结构并将其专门用于每对原生类型?
最佳答案
听说过 decltype
?
在 C++0x
中你可以这样做
template<class T, class U>
auto mul(T x, U y) -> decltype(x*y)
{
return x*y;
}
关于c++ - 推断模板的运算符/函数的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3831126/