From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 3134B384607A for ; Mon, 21 Sep 2020 21:52:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3134B384607A Received: by mail-wm1-x333.google.com with SMTP id b79so1034828wmb.4 for ; Mon, 21 Sep 2020 14:52:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=gWOTn/SOO/HtlBi4XKSJmdOfT9q68d3YCydxTplrsZQ=; b=nrPvTnE52SrDMh9IKfiW+7Dcxfp1Nu46waN0t21RXAFhlcqZpYfV4iEwa6vvReQ2mk YWFWsTixQEdxXV1GNwd5g6Id1QXEWErWTwo5lIZxXD63P4WbBbsK6i1Yw0+KrLUS0EfZ FuvS3KLcu0Hoa1FRBvmcrhPlBVNW+In1eWnwaTrEr7n9aghXscLez+osxYrJHRfbPcCg /wocLmta2XtJ4kZQgMURPKuRkgE+fc0rjdDVfdxY6XRZ9IID47JDuvXADctjKB8ykUwM QYD3vpL3Gjt6/JP2e5ur771VYDbsHiPTsz1eM7POFCYlp3ht1YA4CEJ7GvBrrOFhxF08 DqZw== X-Gm-Message-State: AOAM530pGno669N195Fv9Q62npVAzDKVKGyqoS+inwjFFonbbSuL4Rcg OvnFaDeEq0FlMhco3N0vp4PeepgsyMDshQ== X-Google-Smtp-Source: ABdhPJyDIPfmQAXhNSCTD+81/k57qZW7e7bUtSvzD3fHB3Bmvx/3SCeoAXaurDSkzGtYERZXlbTvGg== X-Received: by 2002:a1c:f619:: with SMTP id w25mr1265008wmc.62.1600725135243; Mon, 21 Sep 2020 14:52:15 -0700 (PDT) Received: from [192.168.1.143] ([170.253.60.68]) by smtp.gmail.com with ESMTPSA id t15sm22283297wrp.20.2020.09.21.14.52.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Sep 2020 14:52:14 -0700 (PDT) Subject: Re: Expose 'array_length()' macro in To: Jonathan Wakely , Florian Weimer Cc: gcc@gcc.gnu.org, libstdc++@gcc.gnu.org, Libc-alpha , libc-coord@lists.openwall.com References: <946e9377-0558-3adf-3eb9-38c507afe2d0@gmail.com> <874knr8qyl.fsf@oldenburg2.str.redhat.com> <875z875si2.fsf@oldenburg2.str.redhat.com> <20200921140100.GA449323@redhat.com> From: Alejandro Colomar Message-ID: Date: Mon, 21 Sep 2020 23:52:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200921140100.GA449323@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Sep 2020 21:52:17 -0000 I have developed this draft code, the C++ part being based on what you wrote. I am a C programmer, and my C++ is very basic, and I tend to write C-compatible code when I need C++, so I can't really write the C++ part. I tested the code with all C versions (--std= {c89, c99, c11, c18, c2x}), and it worked for all of them (correctly returning 18 in all of them), and if I uncomment the part of the pointer, it has a nice error message. I used `-Wall -Wextra -Werror -pedantic -Wno-vla -Wno-sizeof-pointer-div`. However, the C++ part needs some work to be able to compile. Would you mind finishing it? Thanks, Alex ------------------------------------------------------------------ #if defined(__cplusplus) # include # if __cplusplus >= 201703L # include # define array_length(arr) (std:size(arr)) # else # if __cplusplus >= 201103L constexpr # endif inline std::size_t array_length(const T(&array)[N]) # if __cplusplus >= 201103L noexcept # endif { return N; } # endif # if __cplusplus >= 202002L # define array_slength(arr) (std:ssize(arr)) # else # if __cplusplus >= 201103L constexpr # endif inline std::ptrdiff_t array_slength(const T(&array)[N]) # if __cplusplus >= 201103L noexcept # endif { return N; } # endif #else /* !defined(__cplusplus) */ #include # define __is_same_type(a, b) \ __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) # define __is_array(arr) (!__is_same_type((arr), &(arr)[0])) # if __STDC_VERSION__ >= 201112L # define __must_be(e, msg) ( \ 0 * (int)sizeof( \ struct { \ _Static_assert((e), msg); \ char ISO_C_forbids_a_struct_with_no_members__; \ } \ ) \ ) # else # define __must_be(e, msg) ( \ 0 * (int)sizeof( \ struct { \ int : (-!(e)); \ char ISO_C_forbids_a_struct_with_no_members__; \ } \ ) \ ) # endif # define __must_be_array(arr) __must_be(__is_array(arr), "Must be an array!") # define __array_length(arr) (sizeof(arr) / sizeof((arr)[0])) # define array_length(arr) (__array_length(arr) + __must_be_array(arr)) # define array_slength(arr) ((ptrdiff_t)array_length(arr)) #endif int main(void) { int a[5]; const int x = 6; int b[x]; #if __cplusplus >= 201103L constexpr #endif int y = 7; int c[y]; int *p; (void)p; return array_slength(a) + array_slength(b) + array_length(c) /*+ array_length(p)*/; }