[PATCH v2] Implement C23 const-preserving standard library macros
Collin Funk
collin.funk1@gmail.com
Wed Nov 19 17:37:39 GMT 2025
Joseph Myers <josmyers@redhat.com> writes:
> Another question would be whether some GCC extension should be added
> to support this feature better with macros that only expand each
> argument once.
>
> Tested for x86_64.
>
[...]
> +#if __HAVE_GENERIC_SELECTION
> +/* If PTR is a pointer to const, return CALL cast to type CTYPE,
> + otherwise return CALL. Pointers to types with non-const qualifiers
> + are not valid. This should not be defined for C++, as macros are
> + not an appropriate way of implementing such qualifier-generic
> + operations for C++. */
> +# define __glibc_const_generic(PTR, CTYPE, CALL) \
> + _Generic (0 ? (PTR) : (void *) 1, \
> + const void *: (CTYPE) (CALL), \
> + default: CALL)
> +#endif
[...]
> extern char *strstr (const char *__haystack, const char *__needle)
> __THROW __attribute_pure__ __nonnull ((1, 2));
> +# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
> +# define strstr(HAYSTACK, NEEDLE) \
> + __glibc_const_generic (HAYSTACK, const char *, \
> + strstr (HAYSTACK, NEEDLE))
Wouldn't this break a ton of existing code since HAYSTACK is evaluated
multiple times [1]? There are likely examples for the others.
I haven't thought about it for long, but I don't see a way to implement
them without doing that.
Collin
[1] https://codesearch.debian.net/search?q=strchr%28%2B%2B&literal=1
More information about the Libc-alpha
mailing list