Forward an invocation of a variadic function in C

前端 未结 12 2437
悲哀的现实
悲哀的现实 2020-11-22 06:41

In C, is it possible to forward the invocation of a variadic function? As in,

int my_printf(char *fmt, ...) {
    fprintf(stderr, \"Calling printf with fmt %         


        
12条回答
  •  逝去的感伤
    2020-11-22 07:09

    C99 supports macros with variadic arguments; depending on your compiler, you might be able to declare a macro that does what you want:

    #define my_printf(format, ...) \
        do { \
            fprintf(stderr, "Calling printf with fmt %s\n", format); \
            some_other_variadac_function(format, ##__VA_ARGS__); \
        } while(0)
    

    In general, though, the best solution is to use the va_list form of the function you're trying to wrap, should one exist.

提交回复
热议问题