Get lambda template parameter return value, how to simplify code?

This is my trick:

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

Usage example:

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

In the case where F is lambda:

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

The get_return_value function looks ugly, I think ... How to simplify it?

+3
source share
1 answer

It looks like you could eliminate the need get_return_valueby modifying the declaration applyFtoTas follows:

template<typename F, typename T>
auto applyFtoT(F f, T t) -> decltype(f(t))
{
   return f(t);
}
+4
source

Source: https://habr.com/ru/post/1524556/


All Articles