views:

75

answers:

1

Hello world!

I'm trying to write a v8 module in C++; there, the functions receive a variable number of arguments in an array. I want to take that array and call a function like gettext and printf that receives a formatted string and it's necessary args. The thing is, how can one take an array and send the elements as arguments to one of those functions?

In python, I'd do something like this:

def the_function(s, who, hmany): print s%(who, hmany)

the_args = ["Hello, %s from the %d of us", "world", 3]
the_function(*the_args)

How can that be accomplished in C++? (I'm using v8 and node.js, so maybe there's a function or class somewhere in those namespaces that I'm not aware of)

+2  A: 

Here's one way:

void foo(const char *firstArg, ...) {
    va_list argList;
    va_start(argList, firstArg);

    vprintf(firstArg, argList);

    va_end(argList);
}

Assuming that you're trying to do a printf. Basically, va_list is the key, and you can use it to either examine the arguments, or pass them to other functions that take va_list.

EboMike