我想创建一个函数“ lazy”,该函数接受带有不确定数目的参数作为参数的函数。我需要什么类型或必须进行强制转换?
然后,我想稍后在函数“评估”中执行该操作。然后,如何将之前传递给“惰性”函数的参数传递给传递的函数指针?
一些代码来说明我的问题:
char *function_I_want_to_call(void *foo, type bar, ...);
// the arguments are unknown to lazy() and evaluate()
typedef struct {
??? func;
va_list args;
} lazy_fcall;
void lazy(lazy_fcall *result, ??? func, ...) {
// which type do I need here?
va_start(result->_args, fund);
result->func = func;
}
void *evaluate(lazy_fcall *to_evaluate) {
return to_evaluate->func(expand_args(to_evaluate->args));
// what do I have to write there to expand the va_list? It's C, not C++11...
}
int main () {
lazy_fcall lazy_store;
lazy(&lazy_store, function_I_want_to_call, "argument_1", "argument_2");
// ...
printf("%s", (char *)evaluate(&lazy_store));
}
还是这样的事情是不可能的?还有其他可能性吗?
您不能将a扩展va_list
为单独的参数。您要调用的函数必须能够接受va_list
as参数。例如参见printf
与vprintf
。
另外,正如caf所指出的,您不能存储va_list
,因为一旦lazy
函数返回,“指向”的参数将无效。尝试使用va_list
会导致不确定的行为和各种各样的怪异现象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句