From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id CD63C3861887 for ; Mon, 21 Sep 2020 10:33:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CD63C3861887 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-575-swSr3HEjPFmOVViaG-pfgg-1; Mon, 21 Sep 2020 06:33:29 -0400 X-MC-Unique: swSr3HEjPFmOVViaG-pfgg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 77556186841B; Mon, 21 Sep 2020 10:33:28 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-114-108.ams2.redhat.com [10.36.114.108]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6F470271A1; Mon, 21 Sep 2020 10:33:27 +0000 (UTC) From: Florian Weimer To: Alejandro Colomar Cc: Libc-alpha , gcc@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: Expose 'array_length()' macro in or References: <946e9377-0558-3adf-3eb9-38c507afe2d0@gmail.com> <874knr8qyl.fsf@oldenburg2.str.redhat.com> Date: Mon, 21 Sep 2020 12:33:25 +0200 In-Reply-To: (Alejandro Colomar's message of "Mon, 21 Sep 2020 12:11:43 +0200") Message-ID: <875z875si2.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Sep 2020 10:33:37 -0000 * Alejandro Colomar: > [[ > CC += libc-coord@sourceware.org > CC += gcc@gcc.gnu.org > CC += libstdc++@gcc.gnu.org > ]] > > Hi Florian, > > On 2020-09-21 10:38, Florian Weimer wrote: >> * Alejandro Colomar via Libc-alpha: >> >>> I'd like to propose exposing the macro 'array_length()' as defined in >>> 'include/array_length.h' to the user. >> >> It would need a good C++ port, probably one for C++98 and another one >> for C++14 or later. > > For C++, I use the following definition: > > > #include > #include > #include > > > #define is_array__(a) (std::is_array <__typeof__(a)>::value) Should be decltype. > However, there are a few problems: > > 1) This doesn't work for VLAs (GNU extension). > I couldn't find a way to do it. Maybe I should file a bug in GCC. I do not think VLA support is critical. C++ programmers will be used to limited support in utility functions. > 2) Also, this requires C++11; I don't know how to do it for older C++. > Again, support from the compiler would be great. I think limited C++98 support is possible using a function template, where the array length N is a template parameter. To enable use in constant expressions, you can return a type of char[N], and the macro wrapper should then apply sizeof to the function result. > 3) The macro can't be used in the same places as the C version, > because of the `({})`. > The `0 * sizeof(struct{...})` trick doesn't work in C++ due to: > error: types may not be defined in 'sizeof' expressions For C++11, you can use a constexpr function instead of a macro. array_length should not be a macro in current C++ modes, so that we retain compatibility if a future C++ standard adds array_length (or nitems) on its own. This is not a concern for legacy C++98 mode. >> Maybe also ask on the libc-coord list. > > Ok. Added CCs. libc-coord is not hosted on sourceware: The discussion here veered off into C++ territory anyway. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill