从函数参数的返回类型推断函数返回类型
Deducing the function return type from its parameter's return type
下面有代码
template<typename U, typename F >
U GetListAndSearchName( F listGetter, const std::string& stringName )
{
std::vector<UserType> newList;
for ( size_t i = 0; i < myList.size(); i++)
{
const std::vector<U>& list = listGetter(myList[i]);
for ( size_t i = 0; i < list.size(); i++ )
{
if ( list[i]->GetName() == stringName )
return list[i];
}
}
return U();
}
即使U存在于我的函数指针的返回类型中,即模板参数F(我使用std::mem_fn稍后创建它,F也可能是std::function),目前我需要显式地将U的类型传递给编译器。
我怎么能有我的旧Vs2010编译器来推断U的类型?
2010年作品:
template<typename F>
auto GetListAndSearchName (F listGetter, const std::string& stringName)
-> decltype(listGetter(myList[0])[0])
您需要使用decltype和尾随返回类型。它们都是c++ 11的特性,但是根据MSDN的说法,Visual Studio 2010应该会支持它们。你还需要一个类型trait来从vector中提取value_type。
template<typename T>
struct value_type { typedef T::value_type type; };
template<typename F>
auto GetListAndSearchName( F listGetter, const std::string& stringName )
-> typename value_type<decltype(listGetter(myList[0]))>::type
相关文章:
- 如何获取std::result_of函数的返回类型
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 函数作为模板参数,是否对返回类型强制约束
- C++中函数的向量返回类型引发错误
- 检查函数返回类型是否与STL容器类型值相同
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 在 c++ 中将函数返回类型指定为模板参数
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- C++ 这里有一个返回 (24) 的布尔返回类型函数
- 使用SFINAE来检测void返回类型函数的存在
- 模板返回类型函数如何在C++中工作
- 如何在返回类型函数模板的专用化中使用派生类型?( "couldn't infer template argument" )
- Bon appetit :从 int 返回类型函数在 main() 中打印字符串
- 在引用或指针返回类型函数上输入
- 在后面的返回类型函数语法中,auto关键字背后是否有意图
- 我可以在c++中重写字符串返回类型函数吗?