0

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;
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • Really the only difference between this and http://stackoverflow.com/questions/21738775/c11-how-to-write-a-wrapper-function-to-make-stdfunction-objects/21740143#21740143 is that the other wants to deduce and then store in a `std::function`, so the answers there (mostly) apply to this answer – Mooing Duck Mar 18 '14 at 22:04
  • 1
    But like the other answer, I'm not sure _why_ you think you need this. Why can't you write the full function signature? – Mooing Duck Mar 18 '14 at 22:06

1 Answers1

2

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.

iavr
  • 7,547
  • 1
  • 18
  • 53
  • 2
    Notice that this will work even in non-exact matches (I don't know whether this is intended - the OP said "argument types", but then gave the parameter types...). For example, if `A` is ``, your call still succeeds but `sig` is `void(long)` which is not the exact type. – Johannes Schaub - litb Mar 18 '14 at 22:15
  • Right I guess this is what your answer does. – iavr Mar 18 '14 at 22:17