IMP with an unknown number of parameters

Is it possible to create an IMP where the number of parameters matches the selector for the resolved instance method?

I could use the if statement and a finite number of parameters (for example, from 0 to 10), but is it possible to have, for example, IMP_implementationWithBlock with va_args?

+4
source share
1 answer

You cannot create a function at runtime in C; the number of parameters should be known at compile time.

You can use a variable function to pretend that you have a function with any number of arguments (I included this use in a recent project ), but it may not be portable and probably Undefined Behavior.

If you need to move arguments between functions where signatures and arguments are not known before execution, you will almost certainly want to learn libffi.

Mike Ash has some really useful posts about this: http://www.mikeash.com/pyblog/?tag=libffi that I started and learned most of what I know about it.

+2
source

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


All Articles