einpoklum
einpoklum

Reputation: 132212

std::result_of syntax kerfuffle

I have the following (CUDA) function:

__device__ auto foo(my_class& x, my_function_ptr_type y ) {
    return [gen](my_class& x) { return x.set_y(y); };
}

And I want typedef its return value's type. I've fiddling with the std::result_of syntax, and can't get it quite right. This won't work:

using foo_return_type = std::result_of<decltype(foo(my_class{}, my_function_ptr_type{nullptr}))>::type;

Nor this:

using foo_return_type = std::result_of<decltype(foo(my_class, my_function_ptr_type))>::type;

Nor this:

using foo_return_type = std::result_of<foo>::type;

What should I have as the template-argument to std::result_of?

Notes:

Upvotes: 0

Views: 79

Answers (1)

songyuanyao
songyuanyao

Reputation: 173014

You need a function pointer type and the parameter types, then combine them in the format of F(ArgTypes...). e.g.

using foo_return_type = std::result_of<decltype(&foo)(my_class&, my_function_ptr_type)>::type;

LIVE


You can also make your own type trait, if you don't stick to std::result_of. e.g.

template <typename F>
struct return_type_of_function {};
template <typename R, typename... Args>
struct return_type_of_function<R(Args...)> {
    using type = R;
};

then

using foo_return_type = return_type_of_function<decltype(foo)>::type;

LIVE

Upvotes: 1

Related Questions