IMP with unknown number of parameters

偶尔善良 提交于 2019-12-06 04:57:00

问题


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

I could use an 'if' statement and a finite number of parameters (say between 0 and 10), but is it possible to have eg IMP_implementationWithBlock with va_args ?


回答1:


You can't create a function at runtime in C; the number of parameters has to be known at compile time.

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

If you need to move arguments between functions where the signatures and arguments are not known until runtime, you almost certainly want to look into libffi.

Mike Ash has a few really useful posts about it: http://www.mikeash.com/pyblog/?tag=libffi that's where I got started and learned most of what I know about it.



来源:https://stackoverflow.com/questions/18295758/imp-with-unknown-number-of-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!