public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexander Monakov <amonakov@ispras.ru>
To: Florian Weimer <fweimer@redhat.com>
Cc: vijay nag <vijunag@gmail.com>,
	    "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: Counting the number of arguments in __VA_ARGS__
Date: Wed, 18 Dec 2013 16:10:00 -0000	[thread overview]
Message-ID: <alpine.LNX.2.00.1312182001290.30407@monopod.intra.ispras.ru> (raw)
In-Reply-To: <52A9BADA.6040201@redhat.com>

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

      reply	other threads:[~2013-12-18 16:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-12 13:08 Florian Weimer
2013-12-12 13:21 ` vijay nag
2013-12-12 13:32   ` Florian Weimer
2013-12-18 16:10     ` Alexander Monakov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.00.1312182001290.30407@monopod.intra.ispras.ru \
    --to=amonakov@ispras.ru \
    --cc=fweimer@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=vijunag@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).