News for hackers. Stuff that matters.
Re:Improving _Generic in C2y by fanf2 on Sunday August 04, 2024 @03:48PM (#41155848)
The article describes two problems with _Generic.

There is a third: the discarded branches must typecheck. (Unlike C++ templates, where substitution failure is not an error.) Simon Tatham described the issue in more detail:

https://www.chiark.greenend.org.uk/~sgtatham/quasiblog/c11-g...

Re:Improving _Generic in C2y by fweimer on Sunday August 04, 2024 @04:25PM (#41156091)
There's also the macro expansion/amplification issue (explained at the end of the page you reference). _Generic seems a dead end, it doesn't even work in practice for <tgmath.h>.
Re:Improving _Generic in C2y by kzrdude on Sunday August 04, 2024 @08:30PM (#41157261)
That kind of problem of macro expansion was well explored in a recent Lwn article.. Kilobytes long expansions for some simple operations.

https://lwn.net/SubscriberLink/983965/1240798fdfe236df/

This merits changes in C too but I don't know what if it's not templates.

Re:Improving _Generic in C2y by skissane on Sunday August 04, 2024 @10:21PM (#41157653)
> This merits changes in C too but I don't know what if it's not templates.

C’s preprocessor is really weak-compared e.g to M4, or the PL/I preprocessor. Yet people manage to do all kinds of amazing tricks with it. The price of those tricks, is a big blowout in preprocessing time. If they added some of the missing features (e.g. conditional macro expansion, iteration), people would be able to achieve the same things with far better performance.

Obviously that couldn’t do everything templates could do - even with a much more powerful macro system, macros would still be ignorant of types - and if they became type-aware, they’d essentially be templates by another name. Still, more powerful macros could deliver a lot of benefits (in compilation time and ease of understanding) for real world code bases, even if C templates never happen.

Re:Improving _Generic in C2y by o11c on Sunday August 04, 2024 @04:38PM (#41156170)
There's also the fact that the bodies must be expressions, so it's only useful with GNU statement expressions.

Still, since we have __VA_OPT__ and typeof, standard C has gotten to the point of being almost usable ...

Re:Improving _Generic in C2y by fuhsnn on Sunday August 04, 2024 @05:11PM (#41156375)
In its current form without at least requiring balanced enclosing brackets, dropping the semantic check will make _Generic a new macro expansion system worthy of a dedicated round of obfuscated C contest.
Re:Improving _Generic in C2y by Conscat on Sunday August 04, 2024 @07:22PM (#41156974)
I tried using `_Generic` inside C++ templates as a lighter alternative to `std::conditional_t`, but I was disappointed to find it didn't have short-circuiting semantics, which is my biggest pain point with `std::conditional_t`. If it wasn't necessary to type-check, that would make variadic programming much easier for me. The other problem is that only Clang accepts it in C++ mode.
Re:Improving _Generic in C2y by gpderetta on Monday August 05, 2024 @02:34PM (#41164009)
If constexpr makes it moot nowadays, but before that there were generic lambda tricks.

Fore pure type level programming, it is possible to write lazily evaluated std::conditional. What's your usecase?

Re:Improving _Generic in C2y by Joker_vD on Sunday August 04, 2024 @11:28PM (#41157939)
That's... not really a problem when you dispatch on pointer types? Like, to fix the example from the article you quote:

     #define string_length_fixed(x) _Generic(x,              \
         const char *            : strlen((const char*)(x)), \
         struct MyStringBuffer * : ((struct MyStringBuffer *)(x))->length)
The discarded branches don't get evaluated so it's perfectly fine.
Re:Improving _Generic in C2y by ori_b on Monday August 05, 2024 @03:44AM (#41158961)
There's a far worse problem: that you effectively need to use it from preprocessor macros, which effectively prevents it from being used from being used maintainable.
Re:Improving _Generic in C2y by flohofwoe on Monday August 05, 2024 @06:07AM (#41159663)
The "interface" would directly expose the specialized and uniquely named functions. _Generic is just an inline helper to automatically select the right specialized function by a type. If C ABI compatibility is required, that's about the only way to do it (for better or worse).

(FWIW I haven't found a compelling reason yet to use _Generic in my own C code)

Re:Improving _Generic in C2y by ori_b on Monday August 05, 2024 @06:29AM (#41159789)
I don't think there are very many compelling reasons to use it. It's a dead end.

If I can use _Generic, I can get a cleaner interface by directly calling the right function, and skipping the macro mess.

Re:Improving _Generic in C2y by colonwqbang on Sunday August 04, 2024 @05:10PM (#41156363)
Nice, does this mean we can have const-generic accesors now? I.e. &s->somefield and get a const pointer if s was a const pointer, but a mutable one otherwise? I guess we still need to define two functions though, but that can probably be handled by macros. This is something that really hinders use of const in current C.
Re:Improving _Generic in C2y by brcmthrowaway on Sunday August 04, 2024 @06:19PM (#41156705)
Wow, I can't believe they wasted a decade on 2 releases

Move fast!

Re:Improving _Generic in C2y by kats on Sunday August 04, 2024 @06:57PM (#41156881)
Don't move fast. This is the C programming language. The foundation of all software. Each new feature will interact with everything else, because C is connected to almost everything else. Making C larger is actually a really bad thing.
Re:Improving _Generic in C2y by flohofwoe on Monday August 05, 2024 @06:12AM (#41159682)
Look how that worked out for C++ ;)
Re:Improving _Generic in C2y by Conscat on Sunday August 04, 2024 @07:44PM (#41157079)
Clang, as a non-standard feature, allows you to put a type-id into `_Generic` rather than an expression.
Re:Improving _Generic in C2y by Conscat on Sunday August 04, 2024 @09:15PM (#41157442)
Re:Improving _Generic in C2y by ekeane on Sunday August 04, 2024 @10:24PM (#41157664)
Presumably because the article points that out.
Re:Improving _Generic in C2y by downvotetruth on Sunday August 04, 2024 @11:18PM (#41157886)
Just be sure to have no regrets.
Re:Improving _Generic in C2y by plorkyeran on Sunday August 04, 2024 @10:25PM (#41157671)
You're being downvoted for very clearly not reading the article as that extension and the fact that it got standardized is what the article is about.