mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: "Jₑₙₛ Gustedt" <jens.gustedt@inria.fr>
Cc: NRK <nrk@disroot.org>, musl@lists.openwall.com
Subject: Re: [musl] [C23 const 1/2] C23: change bsearch to a macro that respects the const contract
Date: Fri, 26 May 2023 16:09:42 -0400	[thread overview]
Message-ID: <20230526200941.GL4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <20230526212951.148fc424@inria.fr>

On Fri, May 26, 2023 at 09:29:51PM +0200, Jₑₙₛ Gustedt wrote:
> 
> on Fri, 26 May 2023 23:20:37 +0600 you (NRK <nrk@disroot.org>) wrote:
> 
> > On Fri, May 26, 2023 at 01:29:31PM +0200, Nat! wrote:
> > > I think it's sort of obvious, that these macros increase code
> > > brittleness due to now multiple execution of macro arguments vs. a
> > > single execution in a function call.  
> > 
> > It would be heavily surprising if the controlling expression of
> > _Generic was evaluated. Similar to `sizeof`, it only needs to know
> > the type of the expression and thus doesn't require evaluation (only
> > exception being VLAs in a sizeof).
> 
> Yes, exactly, that's the idea. And VLA (and similar) are not allowed
> for the controling expression of a generic selection, so this problem
> does not arise, there.

Yes. And anyway, having them is a requirement of the standard, not
some optional enhancement we're making.

> > | The controlling-expression and the expressions of the selections
> > that | are not chosen are never evaluated.
> > 
> > However, there is one thing that I don't quite understand about this
> > patch:
> > 
> > > +		void const*: (void const*)bsearch((K), (void
> > > const*)(B), (N), (S), (C)), \  
> > 
> > What's with the `(void const*)(B)` cast? It's already determined to be
> > `void const *` via _Generic.
> 
> Even if not evaluated, all branches have to be syntactically and
> semantically correct. If you don't use these kind of casts, you may get
> a lot of warnings.

Could you elaborate on how such a warning could arise? bsearch (the
function) takes const void *, and any object pointer type (assuming no
other qualification like volatile on the pointed-to type) converts
implicitly to const void *. So I don't think it's possible.

> This "feature" makes the use of these beast a bit subtle, sometimes.
> 
> (Originally, generic selection had been designed to chose between
> different function pointers similar as in <tgmath.h> to implement
> something like overloading.)

In that case it may have been needed, if the alt function took void *
rather than const void *. However, thankfully that path wasn't taken,
so I think this is just a remnant of an untaken path in the
standardization process and should be dropped.

I'll go ahead and follow up here rather than branching this thread
elsewhere too: public headers are supposed to avoid creative content
like comments. Once you remove the unneeded casts, comments, etc.
these _Generics become much more compact and don't need the many-line,
aligned-\ macro thing we don't generally do in musl.

Rich

  reply	other threads:[~2023-05-26 20:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-25 14:44 [musl] [C23 const 0/2] some interfaces become type-generic Jens Gustedt
2023-05-25 14:44 ` [musl] [C23 const 1/2] C23: change bsearch to a macro that respects the const contract Jens Gustedt
2023-05-26 11:29   ` Nat!
2023-05-26 17:20     ` NRK
2023-05-26 19:29       ` Jₑₙₛ Gustedt
2023-05-26 20:09         ` Rich Felker [this message]
2023-05-27  7:51           ` Jₑₙₛ Gustedt
2023-05-25 14:45 ` [musl] [C23 const 2/2] C23: change string.h and wchar.h interfaces to macros " Jens Gustedt
     [not found] <cover.1685541439.git.Jens.Gustedt@inria.fr>
2023-05-31 14:00 ` [musl] [C23 const 1/2] C23: change bsearch to a macro " Jens Gustedt

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=20230526200941.GL4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=jens.gustedt@inria.fr \
    --cc=musl@lists.openwall.com \
    --cc=nrk@disroot.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).