From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32097 invoked by alias); 12 Dec 2013 13:21:38 -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 32087 invoked by uid 89); 12 Dec 2013 13:21:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-oa0-f41.google.com Received: from mail-oa0-f41.google.com (HELO mail-oa0-f41.google.com) (209.85.219.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 12 Dec 2013 13:21:36 +0000 Received: by mail-oa0-f41.google.com with SMTP id j17so392769oag.14 for ; Thu, 12 Dec 2013 05:21:35 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.60.98.69 with SMTP id eg5mr5512190oeb.42.1386854494926; Thu, 12 Dec 2013 05:21:34 -0800 (PST) Received: by 10.182.105.69 with HTTP; Thu, 12 Dec 2013 05:21:34 -0800 (PST) In-Reply-To: <52A9B548.4010707@redhat.com> References: <52A9B548.4010707@redhat.com> Date: Thu, 12 Dec 2013 13:21:00 -0000 Message-ID: Subject: Re: Counting the number of arguments in __VA_ARGS__ From: vijay nag To: Florian Weimer Cc: "gcc-help@gcc.gnu.org" Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes X-SW-Source: 2013-12/txt/msg00086.txt.bz2 On Thu, Dec 12, 2013 at 6:38 PM, Florian Weimer 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 ........