推导出模板的运算符/函数的返回类型 [英] Deduce return type of operator/function for templates

查看:34
本文介绍了推导出模板的运算符/函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的事情有可能吗?

// 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 结构并将其专门用于每对本机类型?

Or would somebody have to hack up a return_type struct and specialize it for every pair of native types?

推荐答案

听说过 decltype?

C++0x中你可以做到

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

这篇关于推导出模板的运算符/函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆