c++ - 推断模板的运算符/函数的返回类型

标签 c++ templates

这样的事情可能吗?

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

相关文章:

c++ - 合并两个排序的链表

c++ - 接口(interface)的延迟实现

c++ - 使用模板在 C++ 中映射函数

c++ - 我可以为不同的类型集定义模板吗?

java - eclipse try-with-resource 模板?

c++ - 构造函数中的部分模板推导

c++ - 拦截并将 C 调用转发给第三方库

c++ - __cdecl、__stdcall 和 __fastcall 的调用方式完全相同?

javascript - Marionette.js 模板打印不完整

c++ - 编译时模板实例化检查