Pass a function with an undefined number of arguments and call it using variational arguments

I would like to create a lazy function that takes a function with an undefined number of arguments as a parameter. What type do I need or what throws should be made?

Then I want to do this thing later in the Rate function. How to pass the arguments passed before to the "lazy" function to the passed function pointer?

Some code to illustrate my problem:

char *function_I_want_to_call(void *foo, type bar, ...);
// the arguments are unknown to lazy() and evaluate()

typedef struct {
    ??? func;
    va_list args;
} lazy_fcall;

void lazy(lazy_fcall *result, ??? func, ...) {
// which type do I need here?
    va_start(result->_args, fund);
    result->func = func;
}

void *evaluate(lazy_fcall *to_evaluate) {
    return to_evaluate->func(expand_args(to_evaluate->args));
    // what do I have to write there to expand the va_list? It C, not C++11...
}

int main () {
    lazy_fcall lazy_store;
    lazy(&lazy_store, function_I_want_to_call, "argument_1", "argument_2");
    // ...
    printf("%s", (char *)evaluate(&lazy_store));
}

Or is something like this simply impossible? What other options are there?

+4
source share
1 answer

va_list . , , va_list. . printf vprintf.


, caf, va_list, , "" , lazy. va_list undefined .

+3

All Articles