转发在C可变参数函数的调用 [英] Forward an invocation of a variadic function in C
问题描述
在C,是有可能转发可变参数函数的调用?如,
In C, is it possible to forward the invocation of a variadic function? As in,
int my_printf(char *fmt, ...) {
fprintf(stderr, "Calling printf with fmt %s", fmt);
return SOMEHOW_INVOKE_LIBC_PRINTF;
}
转发的方式调用上面显然不是在这种情况下,严格要求(因为你可以用其他方式登录调用,或使用vfprintf),但codeBase的我工作的要求包装做一些实际的工作,并没有(也不可能有加)的辅助功能类似于vfprintf。
Forwarding the invocation in the manner above obviously isn't strictly necessary in this case (since you could log invocations in other ways, or use vfprintf), but the codebase I'm working on requires the wrapper to do some actual work, and doesn't have (and can't have added) a helper function akin to vfprintf.
[更新:似乎有基于迄今已提供的答案一些混乱。为了那句问题的另一种方式。一般来说,你可以用一些任意可变参数函数的,而无需修改函数的定义]
[Update: there seems to be some confusion based on the answers that have been supplied so far. To phrase the question another way: in general, can you wrap some arbitrary variadic function without modifying that function's definition.]
推荐答案
如果您还没有类似于 vfprintf
函数接受一个 va_list的
而不是可变数量的参数,你不能做到这一点。请参见 的http:// C -faq.com/varargs/handoff.html 。
If you don't have a function analogous to vfprintf
that takes a va_list
instead of a variable number of arguments, you can't do it. See http://c-faq.com/varargs/handoff.html.
这篇关于转发在C可变参数函数的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!