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

Recommended Answers

All 6 Replies

>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.

well apparently not EVERYone because it doesn't let me do what I want...

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.

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
    }
}

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.

I was thinking if the first argument is NULL then there are no other arguments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.