How to get a meaningful function signature from any called

Consider this beast:

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

Suppose this can be transmitted by any called.

I know how to get a function signature if Func is a simple type of function. Given that Func can be a simple function, a std::function<F> or a function object ( std::bind() expression), how can I get function arguments?

Note:

  • in this case, functions always have only zero, one or two arguments
  • If it is a function object, this is the result of std::bind()
  • a signature is required in order to get the types of arguments that should be used in a type-erasable thing passed to

  • it is strictly C ++ 03 (embedded platform), therefore the arguments of the variable template, etc.

+5
source share
2 answers

impossible. A function object may have an overloaded or boilerplate operator() . Thus, the idea of โ€‹โ€‹having a โ€œsignatureโ€ simply does not apply, since it can have an unlimited number of signatures.

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

+10
source

If you know at runtime the signature of some called (simple) function, you can use (on Linux OS) libffi to name it.

If you donโ€™t even know at the time of signing the function to call, this is not possible, because in general ABI conventions will dictate different ways of passing arguments to the function according to their type.

For example, x86-64 ABI (for most 64-bit x86-64-based systems) requires floating point and integral values โ€‹โ€‹to be transmitted in different sets of registers.

See, for example, the x86 wikipage calling conventions.

0
source

Source: https://habr.com/ru/post/1200671/


All Articles