From: Alejandro Colomar <colomar.6.4.3@gmail.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: Libc-alpha <libc-alpha@sourceware.org>,
gcc@gcc.gnu.org, libstdc++@gcc.gnu.org,
libc-coord@lists.openwall.com
Subject: Re: Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h>
Date: Mon, 21 Sep 2020 14:47:51 +0200 [thread overview]
Message-ID: <f27f172e-5bc7-3493-35f9-a094bf6c95b2@gmail.com> (raw)
In-Reply-To: <875z875si2.fsf@oldenburg2.str.redhat.com>
[[ CC += libc-coord at lists.openwall.com ]]
On 2020-09-21 12:33, Florian Weimer wrote:
> * Alejandro Colomar:
>
>> [[
>> CC += libc-coord at sourceware.org
>> CC += gcc at gcc.gnu.org
>> CC += libstdc++ at 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 <cassert>
>> #include <sys/cdefs.h>
>> #include <type_traits>
>>
>>
>> #define is_array__(a) (std::is_array <__typeof__(a)>::value)
>
> Should be decltype.
Thanks.
>
>> 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.
Sorry, I don't know much C++, and I don't know how to do this.
>
>> 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.
See above.
>
>>> Maybe also ask on the libc-coord list.
>>
>> Ok. Added CCs.
>
> libc-coord is not hosted on sourceware:
>
> <https://www.openwall.com/lists/libc-coord/2020/01/30/1>
>
> The discussion here veered off into C++ territory anyway.
I added the correct list now.
>
> Thanks,
> Florian
>
Thanks,
Alex.
next prev parent reply other threads:[~2020-09-21 12:47 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-20 23:00 Alejandro Colomar
2020-09-21 8:38 ` Florian Weimer
2020-09-21 10:11 ` Alejandro Colomar
2020-09-21 10:33 ` Florian Weimer
2020-09-21 12:47 ` Alejandro Colomar [this message]
2020-09-21 15:47 ` [libc-coord] " enh
2020-09-22 16:25 ` Rich Felker
2020-09-22 16:44 ` Jonathan Wakely
2020-09-22 16:53 ` Ville Voutilainen
2020-09-21 14:01 ` Jonathan Wakely
2020-09-21 21:52 ` Expose 'array_length()' macro in <sys/param.h> Alejandro Colomar
2020-09-21 22:04 ` Jonathan Wakely
2020-09-21 22:13 ` Ville Voutilainen
2020-09-22 9:10 ` Alejandro Colomar
2020-09-22 9:40 ` Jonathan Wakely
2020-09-22 10:01 ` Florian Weimer
2020-09-22 10:35 ` Alejandro Colomar
2020-09-22 11:40 ` Florian Weimer
2020-09-22 14:58 ` [RFC] <sys/param.h>: Add nitems() and snitems() macros Alejandro Colomar
2020-09-25 13:20 ` [PATCH v2] " Alejandro Colomar
2020-09-25 14:10 ` Alejandro Colomar
2020-09-25 14:48 ` Jonathan Wakely
2020-09-25 16:30 ` Alejandro Colomar
2020-09-25 17:39 ` Jonathan Wakely
2020-09-25 17:42 ` Jonathan Wakely
2020-09-25 17:46 ` Alejandro Colomar
2020-09-25 19:37 ` [PATCH v3] <sys/param.h>: Add nitems() Alejandro Colomar
2020-09-22 9:16 ` [libc-coord] Re: Expose 'array_length()' macro in <sys/param.h> Florian Weimer
2020-09-30 15:58 ` Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h> Joseph Myers
2020-09-30 20:39 ` Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f27f172e-5bc7-3493-35f9-a094bf6c95b2@gmail.com \
--to=colomar.6.4.3@gmail.com \
--cc=fweimer@redhat.com \
--cc=gcc@gcc.gnu.org \
--cc=libc-alpha@sourceware.org \
--cc=libc-coord@lists.openwall.com \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).