Question

This is my trick:

template<typename F, typename TArg>
auto get_return_value(F * f = NULL, TArg * arg = NULL)
     -> decltype((*f)(*arg));

Example of using:

template<typename F, typename T>
decltype(get_return_value<F,T>()) applyFtoT(F f, T t)
{
    return f(t);
}

In case F is lambda:

int b = applyFtoT([](int a){return a*2}, 10);
// b == 20

Function get_return_value looks ugly i think... How to simplify it?

Was it helpful?

Solution

It seems like you could eliminate the need for get_return_value by changing the declaration of applyFtoT like so:

template<typename F, typename T>
auto applyFtoT(F f, T t) -> decltype(f(t))
{
   return f(t);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top