0

Is it possible to make a function with an unknown number of parameters without knowing ANY of them?

ie.
void Test(...)

I know you could do
void Test(va_alist)
with varargs.h, doesn't make sense that this functionality has been removed does it?

Thanks!

Dave

4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

>Is it possible to make a function with an unknown number
>of parameters without knowing ANY of them?
No, C++ requires at least one non-variable parameter so that the stdarg macros know where to hook the beginning of the parameter list.

>doesn't make sense that this functionality has been removed does it?
The standard language caters to everyone, not just convenient implementations where it's easy to do what you're asking.

0

You've got that backwards, bub.

What you are saying is that you want your feature even though it wouldn't work or it would break on someone else's computer. Such a functionality doesn't cater to everyone. Just you.

Since this is C++, whatever it is you are trying to do can better be done in other ways:
default arguments, overloaded functions, heck, even the named argument idiom.

0

Actually I think you could implement something like you want if you pass NULL as the first argument and check for that value in the parameter list

void foo(const char* fmt, ...)
{
    if( fmt != NULL)
    {
         // blabla
    }
    else
    {
         // do something else
    }
}
0

Yes, but you would still have to have some way of figuring out what the remaining arguments are.

If I were to say printf( "%s", 12 ); it would fail, because it has no way to verify that the second argument is, in fact, a char*. Since it isn't, the program crashes.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.