Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a meaningful function signature from anything callable

Consider such a beast:

template<typename Func>
void register_function(Func func) {
  // type-erase Func and pass it on to some other function
}

Assume that this can be passed anything callable.

I know how to get at the function's signature if Func is a plain function type. Given that func could be a plain function, a std::function<F>, or a function object (a std::bind() expression), how can I get at the function's arguments?

Note:

  • in this case, the functions only ever have either zero, one, or two arguments
  • if it's a function object, it's the result of std::bind()
  • the signature is needed in order to get at the argument's types, which need to be usable in the type-erased thing passed on

  • this is strictly C++03 (embedded platform), so no variable template arguments etc.

like image 388
sbi Avatar asked Aug 19 '14 12:08

sbi


1 Answers

Impossible. A function object can have overloaded or templated operator(). Thus the idea of it having "a signature" simply doesn't apply, because it can have an unbounded number of signatures.

If you restrict it to only having one signature, then you can take the address of operator() and then get the arguments from the member function pointer type using regular template specialization.

like image 112
Puppy Avatar answered Sep 28 '22 04:09

Puppy