Daniel Murphy
Daniel Murphy

Reputation: 186

Templated function return type from return type of a lambda

I'm trying to work out the required return type for a function that returns an unordered map keyed on the value type of the templated iterator Iter and the result of a lambda(F) when called with the value of the iterator. I'm compiling with GCC 4.9.1 in C++1y mode.

This is as far as I've got, but this could be heading in entirely the wrong direction, as I have very little templating experience.

template<typename Iter, typename Func>
unordered_map<Iter::value_type, decltype(F(*Start)) Map(Iter Start, Iter End, Func F)

Any suggestions appreciated.

Upvotes: 0

Views: 71

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

template< class Iter, class Func>
auto Map( Iter Start, Iter End, Func F )
    -> unordered_map<typename Iter::value_type, decltype(F(*Start))>

More generally you can leverage std::function to deduce the result type of a known function type,

template< class Func >
struct ResultOf
{
    typedef typename std::function<
        typename std::remove_pointer<Func>::type
        >::result_type T;
};

Upvotes: 4

Related Questions