* Counting the number of arguments in __VA_ARGS__
@ 2013-12-12 13:08 Florian Weimer
2013-12-12 13:21 ` vijay nag
0 siblings, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2013-12-12 13:08 UTC (permalink / raw)
To: gcc-help
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Counting the number of arguments in __VA_ARGS__
2013-12-12 13:08 Counting the number of arguments in __VA_ARGS__ Florian Weimer
@ 2013-12-12 13:21 ` vijay nag
2013-12-12 13:32 ` Florian Weimer
0 siblings, 1 reply; 4+ messages in thread
From: vijay nag @ 2013-12-12 13:21 UTC (permalink / raw)
To: Florian Weimer; +Cc: gcc-help
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
........
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Counting the number of arguments in __VA_ARGS__
2013-12-12 13:21 ` vijay nag
@ 2013-12-12 13:32 ` Florian Weimer
2013-12-18 16:10 ` Alexander Monakov
0 siblings, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2013-12-12 13:32 UTC (permalink / raw)
To: vijay nag; +Cc: gcc-help
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Counting the number of arguments in __VA_ARGS__
2013-12-12 13:32 ` Florian Weimer
@ 2013-12-18 16:10 ` Alexander Monakov
0 siblings, 0 replies; 4+ messages in thread
From: Alexander Monakov @ 2013-12-18 16:10 UTC (permalink / raw)
To: Florian Weimer; +Cc: vijay nag, gcc-help
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-12-18 16:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-12 13:08 Counting the number of arguments in __VA_ARGS__ Florian Weimer
2013-12-12 13:21 ` vijay nag
2013-12-12 13:32 ` Florian Weimer
2013-12-18 16:10 ` Alexander Monakov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).