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:
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.
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.
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>.
> 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.
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 in sane interfaces.
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)
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.
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.
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...