【发布时间】:2016-11-18 15:30:45
【问题描述】:
我有下面的代码
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 的类型?
【问题讨论】:
-
你不能。返回类型本身不能推导出来。
-
我已经加入了一个特定的标准标签。我不确定这在 C++03 中是否容易解决。
-
你不能在 c++03 中,你可以在 C++14 中使用
decltype(auto) -
寻找function_traits。
-
@101010 如果你有时间,也许你可以把它写成答案。即使对我没有用,它也可能是别人的答案。
标签: c++ visual-studio-2010 templates c++03