提取带有可变参数的函数 [英] extract function with variable arguments
本文介绍了提取带有可变参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于我有这个功能:
void A::IAmCool(int x, ...)
{
va_list args;
va_start (args, x);
va_end (args);
}
如何将 var-args 从一个函数传递到另一个函数?我正在寻找这样的东西:
How do I pass var-args from one function to another? I'm looking for something like this:
void A::extractedFunction() /* this doesn't work */
{
va_list args;
va_start (args, ?????);
va_end (args);
}
void A::IAmCool(int x, ...)
{
extractedFunction();
}
这甚至可能吗?我试过使函数内联,但这不起作用.
Is this even possible? I have tried making the function inline but that doesn't work.
推荐答案
通常的模式是使用 valist 来实现您的主要主力函数,而可变参数函数仅作为装饰".然后就可以直接从第三方调用站点使用 main 函数了.示例:
The usual pattern is to implement your main workhorse function taking a valist, and the variadic function only as "decoration". Then you can use the main function directly from third-party call sites. Example:
#include <cstdarg>
int vgizmo(int a, std::va_list ap)
{
// main implementation here!
}
int gizmo(int a, ...) // interface function
{
std::va_list ap;
va_start(ap, a);
int r = vgizmo(a, ap);
va_end(ap);
return r;
}
void some_other_stuff(bool q, char const * fmt, ...)
{
std::va_list ap;
va_start(ap, fmt);
// ...
int b = vgizmo(x, ap); // third parties use vgizmo directly
// ...
va_end(ap);
}
这篇关于提取带有可变参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文