1

Let's say I have something, following the example on cppreference.com, but specified differently:

typedef double call_t(char, int&);

I need such a form of invocation of result_of that would give back the return type of the function signature defined as the above call_t. That is something that I could use as:

template <class Signature>
std::result_of<SOME_MAGIC_AROUND(Signature)>::type call(Signature* f)
{
    return (*f)();
}

double d = call(fn); // where `fn` is declared as having `call_t` type

Doing result_of<call_t>::type doesn't work. I've tried also various combinations around it, but came to nothing.

Ethouris
  • 1,791
  • 13
  • 18

1 Answers1

3

You have to specify the parameters too. e.g.

template <class Signature>
typename std::result_of<Signature*(char, int&)>::type call(Signature* f)
//                      ^^^^^^^^^^^^^^^^^^^^^^
{
    return (*f)(...);
}

LIVE


std::result_of has been deprecated since C++17, you can use std::invoke_result instead.

template <class Signature>
typename std::invoke_result<Signature*, char, int&>::type call(Signature* f)
//                          ^^^^^^^^^^^^^^^^^^^^^^
{
    return (*f)(...);
}

EDIT

Signature is a template parameter, but expected to be a typedef for a function. I need its "return type" so that I can use as a return type for the call forwarder - regardless of what parameters a user might have specified for Signature.

You can make a type trait which gets the return type from the function type. 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 use it as

template <class Signature>
typename return_type_of_function<Signature>::type call(Signature* f)
{
    return (*f)(...);
}

LIVE

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Ok, the problem I haven't mentioned though is that this should be a universal callback wrapper (the code is shared between C++03 code and C++11 code, hence and extra feature for the latter). `Signature` is a template parameter, but expected to be a typedef for a function. I need its "return type" so that I can use as a return type for the call forwarder - regardless of what parameters a user might have specified for `Signature`. – Ethouris Dec 17 '19 at 17:06
  • @Ethouris Answer revised. – songyuanyao Dec 18 '19 at 01:20
  • So... I've hit this code again after I lost it due to its uselessness and no solution, and yes, I ended up making my own "return type extractor", it was much easier than finding anything in the standard. Pity. – Ethouris Mar 21 '20 at 19:12