From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15241 invoked by alias); 18 Dec 2013 16:10:56 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 15226 invoked by uid 89); 18 Dec 2013 16:10:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: smtp.ispras.ru Received: from smtp.ispras.ru (HELO smtp.ispras.ru) (83.149.199.79) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Dec 2013 16:10:54 +0000 Received: from [10.10.3.121] (unknown [83.149.199.91]) by smtp.ispras.ru (Postfix) with ESMTP id AF9B2224A7; Wed, 18 Dec 2013 20:10:51 +0400 (MSK) Date: Wed, 18 Dec 2013 16:10:00 -0000 From: Alexander Monakov To: Florian Weimer cc: vijay nag , "gcc-help@gcc.gnu.org" Subject: Re: Counting the number of arguments in __VA_ARGS__ In-Reply-To: <52A9BADA.6040201@redhat.com> Message-ID: References: <52A9B548.4010707@redhat.com> <52A9BADA.6040201@redhat.com> User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2013-12/txt/msg00127.txt.bz2 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