linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: Alejandro Colomar <colomar.6.4.3@gmail.com>,
	Ville Voutilainen <ville.voutilainen@gmail.com>,
	gcc@gcc.gnu.org, libstdc++ <libstdc++@gcc.gnu.org>,
	Libc-alpha <libc-alpha@sourceware.org>,
	LKML <linux-kernel@vger.kernel.org>,
	libc-coord@lists.openwall.com
Subject: Re: Expose 'array_length()' macro in <sys/param.h>
Date: Tue, 22 Sep 2020 12:01:02 +0200	[thread overview]
Message-ID: <87k0wmyvtt.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <20200922094020.GS6061@redhat.com> (Jonathan Wakely's message of "Tue, 22 Sep 2020 10:40:20 +0100")

* Jonathan Wakely:

> I don't see much point in using std::size here. If you're going to
> provide the alternative implementation for when std::size isn't
> defined, why not just use it always?
>
> template<typename _Tp, std::size_t _Num>
> #if __cplusplus >= 201103L
> constexpr
> #endif
> inline std::size_t
> __array_length(const _Tp(&)[_Num]) __THROW
> {
>   return _Num;
> }
>
> This only requires <cstddef>, not <iterator>.

I agree that this is an advantage.  But the version without constexpr is
not sufficient because __array_length does not produce a constant
expression.

I've seen something like this used instead:

  template<typename _Tp, std::size_t _Num>
  char (&___array_length(const _Tp(&)[_Num]))[_Num];
  #define __array_length(v) (sizeof(___array_length(v)))

If the function type is too cute, a helper struct could be used instead.

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


  reply	other threads:[~2020-09-22 10:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <946e9377-0558-3adf-3eb9-38c507afe2d0@gmail.com>
     [not found] ` <874knr8qyl.fsf@oldenburg2.str.redhat.com>
     [not found]   ` <dbcf5c85-c468-72f8-0f83-92ec2a6a2991@gmail.com>
     [not found]     ` <875z875si2.fsf@oldenburg2.str.redhat.com>
     [not found]       ` <20200921140100.GA449323@redhat.com>
     [not found]         ` <e734429a-d543-7e75-48e9-a8297a94b035@gmail.com>
     [not found]           ` <20200921220443.GP6061@redhat.com>
     [not found]             ` <CAFk2RUbEtvgFb_FZmcM9L4-g1kG_E7S2p9gveM0Z5Fe=zEDm9w@mail.gmail.com>
2020-09-22  9:10               ` Expose 'array_length()' macro in <sys/param.h> Alejandro Colomar
2020-09-22  9:40                 ` Jonathan Wakely
2020-09-22 10:01                   ` Florian Weimer [this message]
2020-09-22 10:35                     ` Alejandro Colomar
     [not found] ` <alpine.DEB.2.21.2009301557590.5720@digraph.polyomino.org.uk>
2020-09-30 20:39   ` Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h> 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=87k0wmyvtt.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=colomar.6.4.3@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=libc-coord@lists.openwall.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ville.voutilainen@gmail.com \
    /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 NNTP newsgroup(s).