[PATCH v2] Implement C23 const-preserving standard library macros

Joseph Myers josmyers@redhat.com
Wed Nov 19 17:53:15 GMT 2025


On Wed, 19 Nov 2025, Paul Eggert wrote:

> On 2025-11-19 09:37, Collin Funk wrote:
> > Wouldn't this break a ton of existing code since HAYSTACK is evaluated
> > multiple times [1]?
> 
> Shouldn't be a problem for correct programs. _Generic doesn't evaluate its
> first argument; it merely uses the argument's type.
> 
> There is a problem for incorrect programs: too many diagnostics. For example,
> for strchr (1, 'x') you'll see something like the following. Without help from
> the compiler, though, I don't see how to fix this quality-of-implementation
> issue.

I don't think programs that are so incorrect they'd have had an error 
anyway, such as those not passing a pointer at all, are a big concern; 
it's clear from those error messages where the problem is (and for the 
case the feature is concerned with - where the code isn't const-safe - you 
only get the single diagnostic when the pointer-to-const return value is 
implicitly converted to pointer-to-unqualified).  But one of the questions 
I explicitly posed was indeed how this might be supported better through a 
language extension (which does still need to involve macro expansion 
rather than purely being a form of marking a function as overloaded, 
because when macro expansion is suppressed the functions explicitly retain 
their previous types).  I was imagining something like __builtin_tgmath 
with a goal of avoiding multiple expansions of the arguments (so avoiding 
the size of the expansion blowing up with nested calls to such macros), 
but any such feature would also serve to reduce the number of diagnostics.

-- 
Joseph S. Myers
josmyers@redhat.com



More information about the Libc-alpha mailing list