This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Counting the number of arguments in __VA_ARGS__


On 12/12/2013 02:21 PM, vijay nag wrote:
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

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.

--
Florian Weimer / Red Hat Product Security Team


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]