Hi David, > Date: 2026-11-11 10:25:35+0000 > From: David Laight > > On Tue, 10 Nov 2026 16:33:40 +0100 > Alejandro Colomar wrote: > > > This macro serves to cast const away without risking unwanted > > conversions due to mistakes in the cast. > > > > It's similar to C++'s const_cast(), but it is more restrictive: it can > > only cast away 'const', and not arbitrary qualifiers. > > > > Signed-off-by: Alejandro Colomar > > --- > > include/linux/stddef.h | 12 ++++++++++++ > > 1 file changed, 12 insertions(+) > > > > diff --git a/include/linux/stddef.h b/include/linux/stddef.h > > index e1851c50c89b..d79a81406b78 100644 > > --- a/include/linux/stddef.h > > +++ b/include/linux/stddef.h > > @@ -194,4 +194,16 @@ enum { > > #define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \ > > __TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS) > > > > +/** > > + * const_cast() - Drop const in the left-most derivation of a pointer > > + * > > + * @T: Type for the cast. > > + * @p: Pointer to be casted. > > + * > > + * If @p is of type const @T, then it is casted to @T. > > + * Otherwise, @p is returned without any casts. > > + * This avoids mistakes that change anything other than 'const' in a cast. > > + */ > > +#define const_cast(T, p) _Generic(p, const T: (T) (p), default: (p)) > > Should it be de_const_cast(), or just de_const(). If we want to be explicit about it being for dropping, perhaps drop_const() or cast_drop_const() feels more readable to me than de_const(). I had been calling this const_cast() for following C++'s name. > It is a shame you need to specify the type, In some cases we don't want to drop const; it's a "maybe drop const", actually. Perhaps for this reason, cast_drop_const() might be misleading, in that it tells you that it dropped const, but sometimes it didn't. On the other hand, if you thougt you had dropped const and haven't, you'll get build errors, so it's not a big problem. The reason why we may want to not drop const is that since we do: const_cast(CQChar_of(s) *, strprefix(s, pfx)) 'CQChar_of() *' expands to either 'const char *' or 'char *', depending on the qualification of 's'. In case it expands to 'const char *', we want to retain const, and not drop it. The most accurate name would be cast_at_most_drop_const_but_maybe_not(). :) > but why not: > _Generic(p, T: p, const T: (T)(p)) > so that you get a compile time error from the _Generic() if the type > is wrong. Thanks! I like that being more robust. Have a lovely day! Alex --