I am trying to find the return type of a non-member function. At first, I thought std::result_of doing this work, but it seems that the latter only works for called objects. In particular, std::result_of<decltype(f)>::type does not work . I finally managed to find a code that works
#include <iostream> #include <typeinfo> void f(int); // we deduce the returning type of this function template<typename Ret, typename... Args> struct Helper { using type = Ret; }; template<typename Ret, typename... Args> Helper<Ret, Args...> invoke(Ret(*fp)(Args...)) { return {}; } template<typename Ret, typename... Args> using Return_Type = typename Helper<Ret, Args...>::type; int main() { std::cout << typeid(decltype(invoke(f))::type).name() << std::endl; // fine }
I am using an additional function here, the invoke pattern, which takes a pointer to the function that I want to infer the return type and returns a helper structure from which I read the actual return type.
The code seems a bit confusing as it includes a call function (although the actual evaluation is not performed). Is there any other alternative simpler / clearer / shorter way to do this?
source share