views:

143

answers:

2

I'm trying to create a variable length function (obviously, heh) in C++, and what I have right now works, but only for the first argument. If someone could please let me know how to get this working with all the arguments that are passed, I would really appreciate it.

Code:

void udStaticObject::accept( udObjectVisitor *visitor, ... )
{
    va_list marker;
    udObjectVisitor *i = visitor;
    va_start( marker, visitor );
    while( 1 )
    {
        i->visit_staticObject( this );
//the if here will always go to the break immediately, allowing only 
//one argument to be used
        if( ( i = va_arg( marker, udObjectVisitor* ) ) )
            break;
    }
    va_end( marker );
}

Based on my past posts, and any help posts I make in general, there is probably some information that I did not provide that you will need to know to help. I apologize in advance if I forgot anything, and please let me know what you need to know so I can provide the information.

+4  A: 

If you use a variadic function, you need some way to tell the function how many arguments were passed. For example, printf() and friends take a formatting string that contains format specifiers for each of the passed arguments, and they count the number of format specifiers to determine how many arguments were passed.

When passing a list of pointers, you can accomplish this "more simply" by passing a null pointer as the last argument. That way, you simply read arguments until you reach the null pointer.

However, you should seriously consider not using a variadic function for this. You can accomplish the same behavior by taking a vector of pointers as a parameter and iterating over the contents of that vector. There are a number of reasons why this is superior to using a variadic function:

  • Variadic functions have absolutely no type safety. You lose any and all type information about the arguments when you pass them to a variadic function, so for example, a caller of your function could pass a numeric value instead of a pointer and you'd never be able to tell inside of your function.

  • With the variadic solution, the caller must correctly indicate the number of arguments. If the caller omits the null pointer at the end (or otherwise misinforms your function of how many arguments there are) and you try to read more arguments than were passed, you end up with undefined behavior. Right now you might say "well, that's not hard to forget," but inevitably, someone will forget or screw it up and debugging this sort of issue is a beating.

  • The solution taking a vector and iterating over its contents is far simpler to implement, easier to debug, and much more idiomatic in C++.

Wherever there is an option between using a variadic function and not using a variadic function, you should prefer not to use a variadic function (I'll admit, I have never written a variadic function in any of the C++ code I have written, though I've written a few in C).

James McNellis
+1  A: 

Maybe the test is backwards? Try this:

    if( ( i = va_arg( marker, udObjectVisitor* ) ) != NULL )
        break;
Derek Ledbetter
yeah it looks that way to me too....
Matt Joiner