具有未知数量参数的 IMP [英] IMP with unknown number of parameters

查看:81
本文介绍了具有未知数量参数的 IMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个参数数量与正在解析的实例方法的选择器相匹配的 IMP?

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

我可以使用if"语句和有限数量的参数(比如在 0 到 10 之间),但是是否可以使用带有 va_args 的 IMP_implementationWithBlock ?

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 ?

推荐答案

你不能在 C 中在运行时创建函数;在编译时必须知道参数的数量.

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.

如果您需要在签名和参数直到运行时才知道的函数之间移动参数,您几乎肯定要查看 libffi.

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 有一些关于它的非常有用的帖子:http://www.mikeash.com/pyblog/?tag=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.

这篇关于具有未知数量参数的 IMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆