Improving _Generic in C2y
thephd.dev
thephd.dev
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...
Fore pure type level programming, it is possible to write lazily evaluated std::conditional. What's your usecase?
Still, since we have __VA_OPT__ and typeof, standard C has gotten to the point of being almost usable ...
https://lwn.net/SubscriberLink/983965/1240798fdfe236df/
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.
#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.(FWIW I haven't found a compelling reason yet to use _Generic in my own C code)
If I can use _Generic, I can get a cleaner interface by directly calling the right function, and skipping the macro mess.
https://github.com/llvm/llvm-project/commit/12728e144994efe8...
https://clang.llvm.org/docs/LanguageExtensions.html#c11-gene...
Move fast!