Re: Counting the number of arguments in __VA_ARGS__

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

 



On Thu, 12 Dec 2013, Florian Weimer wrote:

> > #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
> 
> Hmm.  I think this returns the 64th argument if the argument list is longer
> than 63.  I don't want to silently produce wrong results if some arbitrary
> limit is exceeded.

You can establish an upper bound on the number of arguments using the
following device:

#define PP_STRIP_1(_, ...) __VA_ARGS__
#define PP_STRIP_2(_, ...) PP_STRIP_1(__VA_ARGS__)
#define PP_STRIP_3(_, ...) PP_STRIP_2(__VA_ARGS__)

PP_STRIP_3(a, b, c, d, e)  ->  d, e
PP_STRIP_3(a)  ->  (empty)

So you can check that arg list count does not exceed 63 by examining
sizeof(STR((PP_STRIP_63(__VA_ARGS__))))

One downside to this is that the above simple device relies on a GNU extension
to the C preprocessor and produces a warning with -pedantic. 

Alexander




[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