This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Counting the number of arguments in __VA_ARGS__
- From: vijay nag <vijunag at gmail dot com>
- To: Florian Weimer <fweimer at redhat dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 12 Dec 2013 18:51:34 +0530
- Subject: Re: Counting the number of arguments in __VA_ARGS__
- Authentication-results: sourceware.org; auth=none
- References: <52A9B548 dot 4010707 at redhat dot com>
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
........