logoalt Hacker News

Type Safe Generic Data Structures in C (2025)

127 points • by AlexeyBrin • last Saturday at 11:57 AM • 98 comments • view on HN

Comments

Panzerschrek • today at 3:05 PM

I don't understand people reinventing macro-based hacks in C to achieve what was achieved in other languages many years ago. Why not using C++, for example? It's available almost everywhere, introducing its usage in an existing C codebase is pretty simple. Sure, C++ has its own downsides, but is it better to create mess with macros in C rather then using exiting language facilities and standard library containers provided by C++?

➕ show 8 replies
add2 • today at 10:25 PM

While C++ template is using value types, C generic pointer is not.

lor_louis • today at 1:26 PM

I wrote a summarizing article on type safe container types a while back, but with some C23 specific changes and a few tweaks to work better for complex types.

https://louissven.xyz/article/how_I_do_container_types_in_C....

Feel free to flag/delete if this isn't the place.

➕ show 1 reply
dang • today at 9:40 PM

Related. Others?

I write type-safe generic data structures in C - https://news.ycombinator.com/item?id=44425461 - June 2025 (182 comments)

Type-safe generic data structures in C - https://news.ycombinator.com/item?id=26735655 - April 2021 (52 comments)

tniemi • today at 1:45 PM

That `(1 ? (item) : (list)->payload)` was a neat trick. It gets optimized away, but the type comparison happens before that.

Still feels a bit like a party trick, but if it works...

➕ show 2 replies
veexx103 • today at 1:45 PM

Is it feasible to create a new language by adding features to C

just as was done with C++?

➕ show 7 replies
mkehrt • today at 5:42 PM

I absolutely cannot remember the details, but we did typesafe, macro based generic data structures in C in an undergrad class I took ~20 years ago (CMU Operating Systems). The idea has definitely been around, though I don't know if the implantation was the same.

randomNumber7 • today at 3:41 PM

As far as I know many C programmers think the addition of variable length arrays in C99 was a mistake. What would be the downside of this approach?

➕ show 3 replies
rramadass • today at 1:11 PM

Also see Templates in C by David Priver - https://www.davidpriver.com/ctemplates.html

He also has other interesting C techniques, namely;

Adding reflection to C - https://news.ycombinator.com/item?id=49964525

_Generic for Type Reification in C - https://www.davidpriver.com/creification.html

See also his C2y interpreter with REPL named "DrC" for the upcoming C29 standard (https://en.wikipedia.org/wiki/C29_(C_standard_revision)) - https://github.com/drpriver/drc

PS: I really like his style of writing and presentation; concise and precise without unnecessary fluff and page beautifying.

➕ show 2 replies
ranger_danger • today at 7:49 PM

I just want C with classes, like C++ started out as.

What I'd really like is a standards-conformant (C++98/03) compiler that actually enforces the standard properly (or maybe it becomes a new standard), and ONLY allows that version to be used, AND only the classes.

No templates, no exceptions, no standard library (just use a plain libc).

carlos256 • today at 2:10 PM

Jesus, I hate those macro hacks so much. Most of the time you only need a single container, so writing an ad-hoc implementation is cleaner than that.

➕ show 1 reply
tankiya • today at 6:31 PM

[flagged]

nice_byte • today at 2:26 PM

I've tried doing this for a few years and it just sucks ass. Just use c++ and templates if you need proper generic data structures. C is a defective language.

➕ show 3 replies