Daniel Jour
Daniel Jour

Reputation: 16156

Deduce function type of overloaded function based on argument types

Suppose there is an overloaded function:

void overloaded(int) {}
void overloaded(void) {}

Because I don't want to (or can) write down the full function signature (like void(int) or void(void)) I need to get this signature using only the function name (overloaded) and its argument type(s) (int or void).

I tried several approaches using decltype and friends, but unfortunately to no success.

So, in short, I need something like this:

cout << typeid(
  get_overload(overloaded,(int)0))::type /* where the magic happens */
  .name() << endl;

Upvotes: 0

Views: 311

Answers (1)

iavr
iavr

Reputation: 7647

If you're allowed to use the name overloaded inside the type function, this will do:

template<typename... A>
using sig = decltype(overloaded(std::declval<A>()...))(A...);

sig<int> gives void(int), and sig<> gives void().

This is just a wrapper of tclamb's solution in a template alias.

Upvotes: 2

Related Questions