From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 053A4388CC1C for ; Mon, 21 Sep 2020 22:04:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 053A4388CC1C Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-106-jyM-uoboOkW81Wg1-FEgPQ-1; Mon, 21 Sep 2020 18:04:45 -0400 X-MC-Unique: jyM-uoboOkW81Wg1-FEgPQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 940A91868410; Mon, 21 Sep 2020 22:04:44 +0000 (UTC) Received: from localhost (unknown [10.33.36.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id DA66110013BD; Mon, 21 Sep 2020 22:04:43 +0000 (UTC) Date: Mon, 21 Sep 2020 23:04:43 +0100 From: Jonathan Wakely To: Alejandro Colomar Cc: Florian Weimer , gcc@gcc.gnu.org, libstdc++@gcc.gnu.org, Libc-alpha , libc-coord@lists.openwall.com Subject: Re: Expose 'array_length()' macro in Message-ID: <20200921220443.GP6061@redhat.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> MIME-Version: 1.0 In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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 22:04:49 -0000 On 21/09/20 23:52 +0200, Alejandro Colomar via Libstdc++ wrote: >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 That should be not . ># define array_length(arr) (std:size(arr)) C++ programmers will not accept a macro for this. ># else You're missing "template" here. ># if __cplusplus >= 201103L >constexpr ># endif >inline std::size_t >array_length(const T(&array)[N]) Remove the name of the unused parameter. ># 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)*/; >} > > >