Kadir Erdem Demir Kadir Erdem Demir - 1 year ago 84
C++ Question

Deducing the function return type from its parameter's return type

I have the code below

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();

Even U exists in my function pointer's return type which is template parameter F(I am using std::mem_fn to create it later F might be std::function as well ) currently I am needing to explicitly pass U's type to compiler.

How can I have my old Vs2010 compiler to deduce U's type ?

Answer Source

Works in 2010:

template<typename F>
auto GetListAndSearchName (F listGetter, const std::string& stringName) 
  -> decltype(listGetter(myList[0])[0])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download