Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 26 May 2023 16:09:42 -0400
From: Rich Felker <dalias@...c.org>
To: Jₑₙₛ Gustedt <jens.gustedt@...ia.fr>
Cc: NRK <nrk@...root.org>, musl@...ts.openwall.com
Subject: Re: [C23 const 1/2] C23: change bsearch to a macro that
 respects the const contract

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@...root.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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.