From: Jonathan Wakely <jwakely@redhat.com>
To: Alejandro Colomar <colomar.6.4.3@gmail.com>
Cc: Florian Weimer <fweimer@redhat.com>,
gcc@gcc.gnu.org, libstdc++@gcc.gnu.org,
Libc-alpha <libc-alpha@sourceware.org>,
libc-coord@lists.openwall.com
Subject: Re: Expose 'array_length()' macro in <sys/param.h>
Date: Mon, 21 Sep 2020 23:04:43 +0100 [thread overview]
Message-ID: <20200921220443.GP6061@redhat.com> (raw)
In-Reply-To: <e734429a-d543-7e75-48e9-a8297a94b035@gmail.com>
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 <cstddef>
>
># if __cplusplus >= 201703L
># include <array>
That should be <iterator> not <array>.
># define array_length(arr) (std:size(arr))
C++ programmers will not accept a macro for this.
># else
You're missing "template<typename T, std::size_t N>" 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 <stddef.h>
>
># 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)*/;
>}
>
>
>
next prev parent reply other threads:[~2020-09-21 22:04 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-20 23:00 Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h> 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
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 [this message]
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=20200921220443.GP6061@redhat.com \
--to=jwakely@redhat.com \
--cc=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).