Re: Counting the number of arguments in __VA_ARGS__

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Dec 12, 2013 at 6:38 PM, Florian Weimer <fweimer@xxxxxxxxxx> 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
........




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux