C++ preprocessor __VA_ARGS__ number of arguments

后端 未结 12 2121
一向
一向 2020-11-22 08:03

Simple question for which I could not find answer on the net. In variadic argument macros, how to find the number of arguments? I am okay with boost preprocessor, if it has

12条回答
  •  攒了一身酷
    2020-11-22 08:51

    I usually use this macro to find a number of params:

    #define NUMARGS(...)  (sizeof((int[]){__VA_ARGS__})/sizeof(int))
    

    Full example:

    #include 
    #include 
    #include 
    
    #define NUMARGS(...)  (sizeof((int[]){__VA_ARGS__})/sizeof(int))
    #define SUM(...)  (sum(NUMARGS(__VA_ARGS__), __VA_ARGS__))
    
    void sum(int numargs, ...);
    
    int main(int argc, char *argv[]) {
    
        SUM(1);
        SUM(1, 2);
        SUM(1, 2, 3);
        SUM(1, 2, 3, 4);
    
        return 1;
    }
    
    void sum(int numargs, ...) {
        int     total = 0;
        va_list ap;
    
        printf("sum() called with %d params:", numargs);
        va_start(ap, numargs);
        while (numargs--)
            total += va_arg(ap, int);
        va_end(ap);
    
        printf(" %d\n", total);
    
        return;
    }
    

    It is completely valid C99 code. It has one drawback, though - you cannot invoke the macro SUM() without params, but GCC has a solution to it - see here.

    So in case of GCC you need to define macros like this:

    #define       NUMARGS(...)  (sizeof((int[]){0, ##__VA_ARGS__})/sizeof(int)-1)
    #define       SUM(...)  sum(NUMARGS(__VA_ARGS__), ##__VA_ARGS__)
    

    and it will work even with empty parameter list

提交回复
热议问题