English 中文(简体)
Is it possible to write a varargs function that sends it argument list to another varargs function? [duplicate]
原标题:

This question already has answers here:

Closed 13 years ago.

Possible Duplicate:
C Programming: Forward variable argument list.

What I d like to do is send data to a logging library (that I can t modfify) in a printf kind of way.

So I d like a function something like this:

void log_DEBUG(const char* fmt, ...) {
   char buff[SOME_PROPER_LENGTH];
   sprintf(buff, fmt, <varargs>);
   log(DEBUG, buff);
}

Can I pass varargs to another vararg function in some manner?

最佳回答

You can t forward the variable argument list, since there s no way to express what s underneath the ... as a parameter(s) to another function.

However you can build a va_list from the ... parameters and send that to a function which will format it up properly. This is what vsprintf is for. Example:

void log_DEBUG(const char* fmt, ...) {
   char buff[SOME_PROPER_LENGTH];
   va_list args;
   va_start(args, fmt);
   vsprintf(buff, fmt, args);
   va_end(args);
   log(DEBUG, buff);
}
问题回答

You can send it to another function that takes a va_list as an argument. There is no other way, short of resorting to hand crafted asm, or doing some kind of horrifying guessing game to figure out the number of parameters.

This would work:

void log_DEBUG(const char* fmt, ...)
{
  va_list va;
  va_start(va,fmt);
  char buff[blah];
  vsprintf(buff,fmt,va);
  log(DEBUG,buff);
  va_end(va);
}

Basically, whenever you write a function that takes ..., you should write another version that takes a va_list - its the polite thing to do if you want to enable this type of chaining call.

This is why you have the vprintf family of functions.

For your specific requirement, you can use vsprintf. My C/C++ is too rusty to recall if there s a straightforward way to do it when the other function isn t designed for it (without getting into ugly stack manipulation), but I tend to think not.

Not in standard C++.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签