Counting the number of arguments in __VA_ARGS__

vijay nag vijunag@gmail.com
Thu Dec 12 13:21:00 GMT 2013


On Thu, Dec 12, 2013 at 6:38 PM, Florian Weimer <fweimer@redhat.com> wrote:
> Is there a way to count the number of variadic macro arguments in C?
>
> I found a way to check if the list is empty: examine
> sizeof(STR((__VA_ARGS__))) where STR is a macro that stringifies its
> argument.  If it is 3, __VA_ARGS__ is empty.  I wonder if there is a less
> hackish approach.
>
> The macro arguments are expressions, but not of a uniform type, so an
> array-based approach does not work.
>
> --
> Florian Weimer / Red Hat Product Security Team

#define PP_NARG( ...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,[..],_61,_62,_63,N,...) N
#define PP_RSEQ_N() 63,62,61,60,[..],9,8,7,6,5,4,3,2,1,0

PP_NARG(A)  1
PP_NARG(A, B) 2
........



More information about the Gcc-help mailing list