Macro to count number of arguments

后端 未结 4 1697
甜味超标
甜味超标 2020-11-30 06:16

I have a variadic function from a third-party C library:

int func(int argc, ...);

argc indicates the number of passed optional

4条回答
  •  盖世英雄少女心
    2020-11-30 06:45

    I came up with the following workaround for PP_NARG:

    #define PP_NARG(...)     (sizeof(#__VA_ARGS__) - 1 ?       \
        PP_NARG_(__VA_ARGS__, PP_RSEQ_N()) : 0)
    

    It stringifies __VA_ARGS__, so if it's empty its length equals 1 (because #__VA_ARGS__ == '\0').
    It works with -std=c99 -pedantic.

    I still have problems with wrapping the variadic function though. When __VA_ARGS__ is empty, my_func expands to func(0, ) which triggers a compilation error.

提交回复
热议问题