将 va_list 转换为变量参数? [英] converting va_list to variable argument?
问题描述
我有一个函数 log_message
它接受可变参数.
I have a function log_message
it takes variable arguments.
log_message(int level, char *fmt, ...)
现在在调用这个(log_message
) 函数之前,我必须添加新函数(_log_message
),新函数将调用log_message
.
now before calling this(log_message
) function i have to add new function(_log_message
), and new function will call log_message
.
_log_message(int level, char *fmt, ...)
新功能也一样.当_log_message
调用log_message
时,它会将变量输入转换为va_list
.现在我有了va_list
,我不想改变原来的,有什么办法可以改回变量输入,这样我就可以调用原来的(log_message
).
new function is also same. when _log_message
will call log_message
it will convert variable input to va_list
. Now i have va_list
, i don't wanna change the original one, is there any way to change back to variable input, so i will able to call the original one(log_message
).
推荐答案
不,没有办法将 va_list
转换回参数列表.
No, there is no way to turn a va_list
back into a list of arguments.
通常的方法是定义一个以 va_list
作为参数的基本函数.例如,标准C库定义了printf
和vprintf
;第一个是可变参数函数,第二个具有完全相同的功能,但使用 va_list
代替.同样,它定义了fprintf
和vfprintf
.用vfprintf
来定义printf
、vprintf
和fprintf
很简单:
The usual approach is to define a base function which takes a va_list
as an argument. For example, the standard C library defines printf
and vprintf
; the first is a varargs function and the second has exactly the same functionality but takes a va_list
instead. Similarly, it defines fprintf
and vfprintf
. It's trivial to define printf
, vprintf
and fprintf
in terms of vfprintf
:
int fprintf(FILE* stream, const char* format, ...) {
va_list ap;
va_start(ap, format);
int n = vfprintf(stream, format, ap);
va_end(ap);
return n;
}
int vprintf(const char* format, va_list ap) {
return vfprintf(stdout, format, ap);
}
int printf(const char* format, ...) {
va_list ap;
va_start(ap, format);
int n = vprintf(format, ap);
va_end(ap);
return n;
}
(类似于各种 exec*
函数,它们有 va_list
和 varargs
两种.)
(Similarly for the various exec*
functions, which come in both va_list
and varargs
varieties.)
我建议您采用类似的策略.
I'd suggest you adopt a similar strategy.
这篇关于将 va_list 转换为变量参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!