HN Reader
New
Top
Best
Ask
Show
Job
Parameterized types in C using the new tag compatibility rule
154
95
7 days ago
by ingve
Not personally interested in this hack, but
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3037.pdf
means struct foo {} defined multiple times with the same fields in the same TU now refers to the same thing instead of to UB and that is a good bugfix.
7 days ago
by JonChesterfield
The recent #def #enddef proposal[1] would eliminate the need for backslashes to define readable macros, making this pattern much more pleasant, finger crossed for its inclusion in C2Y!
[1]
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3531.txt
7 days ago
by fuhsnn
Neat similarity to Zig's approach to generic types. The generic type is defined as a type constructor, a function that returns a type. Every instantiation of that generic type is an invocation of that function. So the generic growable list type is `fn ArrayList(comptype T: type) type` and a function that takes two lists of i32 and returns a third is `fn foo(a: ArrayList(i32), b: ArrayList(i32)) ArrayList(i32)`
7 days ago
by Arnavion
If you're reaching for that hack, just use C++? You don't have to go all in on C++-isms, you can always write C-style C++ and only use the features you need.
7 days ago
by IAmLiterallyAB
Here is my experimental library for generic types with some godbolt links to try:
https://github.com/uecker/noplate
7 days ago
by uecker
Sometimes I look at the way C macros are used to simulate generics and wonder to myself... Why don't y'all just put templates into the standard? If the way you're writing C code is by badly imitating C++, then just imitate C++! There's no shame in it!
7 days ago
by wsve
Slighty off-topic, why is he using ptrdiff_t (instead of size_t) for the cap & len types?
7 days ago
by rwmj
It seems as though this makes it impossible to do the new-type paradigm in C23 ? If Goose and Beaver differ only in their name, C now thinks they're the same type so too bad we can tell a Beaver to fly even though we deliberately required a Goose ?
7 days ago
by tialaramex
I think this is an interesting change, even though I (as someone who has loved C for 30+ years and use it daily in a professional capacity) don't immediately see a lot of use-cases I'm sure they can be found as the author demonstrates. Cool, and a good post!
7 days ago
by unwind
Are we getting a non-broken `_Generic` yet? Because that's the thing that made me give up with disgust the last project I tried to write in C. Manually having to do `extern template` a few times is nothing in comparison.
7 days ago
by o11c
i fear this will make slopy code compile more often OK.
7 days ago
by Surac