Simplest Way to Determine Return Type of Function

Simplest way to determine return type of function

You can leverage std::function here which will give you an alias for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:

using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;

We can make this a little more generic like

template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;

which then lets you use it like

int foo(int a, int b, int c, int d) {
return 1;
}

auto bar = [](){ return 1; };

struct baz_
{
double operator()(){ return 0; }
} baz;

using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;

Obtaining the return type of a function

EDIT

As of TypeScript 2.8 this is officially possible with ReturnType<T>.

type T10 = ReturnType<() => string>;  // string
type T11 = ReturnType<(s: string) => void>; // void
type T12 = ReturnType<(<T>() => T)>; // {}
type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]

See this pull request to Microsoft/TypeScript for details.

TypeScript is awesome!


Old-school hack

Ryan's answer doesn't work anymore, unfortunately. But I have modified it with a hack which I am unreasonably happy about. Behold:

const fnReturnType = (false as true) && fn();

It works by casting false to the literal value of true, so that the type system thinks the return value is the type of the function, but when you actually run the code, it short circuits on false.

How to determine the return type of a function in template

Since Fx is supposed to be a function type, not a function pointer type, so the specialization should be declared as:

template< class R, class... A>
struct return_type<R(A...)>
{
using type = R;
};

Other issues:

  1. Change using ReturnType = return_type<Fx>::type; to using ReturnType = typename return_type<Fx>::type;.

  2. Move the declaration of ReturnType (and definition of return_type) before using it as the return type of operator().

  3. Change return ((*fx)(args), ...); to return (*fx)(args...); in the operator(); i.e. all the arguments are supposed to be passed to fx instead of calling fx multiple times with each argument.

LIVE

BTW: Return type deduction (since C++14) is worthy of consideration too. E.g.

template < class... A >
auto operator()(A... args)
{
return (*fx)(args...);
}

LIVE

How to get type from the return of a function?

First of all this syntax is wrong: const objPipeLine<T>.

If you want to infer function argument, you should do const objPipeLine=<T>()=>{}.

Just put generic right after equal sign.

Consider this example:

type FirstFunc<T> = () => T;
type Pipeline<T> = ((obj: T) => T)[]

const objPipeLine = <Return,>(firstFunc: FirstFunc<Return>, pipeline: Pipeline<Return>) => {}

const result = objPipeLine(
() => ({ age: 42 }),
[(obj) => obj]
)

Playground

I have created Return generic and let TS infer it. If you hover your mouse on obj in [(obj) => obj] you will see that it is inferred as {age: number}.

If you are interested in typing pipeline function you can check my article, this answer and this answer

how to get a return type of a member function pointer

You can use partial template specialization to determine the return type of mptr:

template <typename T>
struct ReturnType;

template <typename Object, typename Return, typename... Args>
struct ReturnType<Return (Object::*)(Args...)>
{
using Type = Return;
};

void my_func(auto mptr) {
typename ReturnType<decltype(mptr)>::Type obj;
}

Live Demo



Related Topics



Leave a reply



Submit