0
这是我很难用英语解释正是我的意思,但以下非编译的代码可能说明我所追求的:推导返回类型为模板参数方法的类型
template<class T>
auto fn(T t) -> decltype(T::method_call())
{
return t.method_call();
}
基本上我希望函数返回T的方法返回的内容。这是什么语法?
这是我很难用英语解释正是我的意思,但以下非编译的代码可能说明我所追求的:推导返回类型为模板参数方法的类型
template<class T>
auto fn(T t) -> decltype(T::method_call())
{
return t.method_call();
}
基本上我希望函数返回T的方法返回的内容。这是什么语法?
在C++ 14,可以使用推断返回类型简单地说:
template <typename T>
decltype(auto) fn(T t) { return t.method_call(); }
您还可以使用尾随返回类型指定同样的事情:
template <typename T>
auto fn(T t) -> decltype(t.method_call()) { return t.method_call(); }
而且没有尾返回类型 'template decltype(std :: declval ().method_call())fn(T);'。 –
Jarod42