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