Igor Tandetnik wrote:
Tommy <bad@reallybad.com> wrote:
But the only true variadic method is to go direct with va_list
traversal using a count or terminator method.
For example:
MY_MACRO(
count1, fmt1, ...,
count2, fmt2, ...,
count3, fmt3, ...,
.
.
countN, fmtN, ...,
NULL);
Could you show how you would actually implement such a macro? So
that, for example,
MY_MACRO(
2, "%d %f\n", 1, 2.0,
2, "%s %s\n", "hello", "world",
NULL);
prints
1 2.0
hello world
I can't with the macro because as I noted in my initial post:
I remember once coming across a similar need. If I recall,
the "design issues" was two folds:
- Not knowing exactly the true count of the arguments and types,
- Requiring a count as a previous or a terminator of some sort.
But if you go direct, you can solve it very easily:
void foo(int argc, ...)
{
va_list argptr;
va_start(argptr, argc);
const char *p = NULL;
while (argc && (p = va_arg(argptr,const char *))) {
vprintf_s( p, argptr );
while (argc--) va_arg(argptr, void *); // SEE NOTE1
argc = va_arg(argptr, int);
}
va_end(argptr);
}
#define MY_MACRO foo
MY_MACRO(
2, "%d %f\n", 1, 2.0,
2, "%s %s\n", "hello", "world",
NULL);
me after printing the first line.
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
overhead. -- RFC 1925