# A Trick for Type-Safe Generic Data Structures in C

Original title: Type Safe Generic Data Structures in C

> Hacker News story 49943479 · language: en · submitted 2026-10-03
> Web version: https://zeli.app/story/49943479
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 150 · by [AlexeyBrin](<https://news.ycombinator.com/user?id=AlexeyBrin>) · 💬 132
[original](<https://danielchasehooper.com/posts/typechecked-generic-c-data-structures/>) · [HN discussion](<https://news.ycombinator.com/item?id=49943479>)

## Summary

Daniel Chase Hooper shows how to make generic data structures in C type-safe using a union with a payload member. The compiler catches type mismatches when you pass the wrong type to a macro like list_prepend, and the payload member takes no memory. He also covers inline storage with flexible array members and using __typeof__ for type-safe returns.

## Key quote

> The way I found to do this is to use a union with a payload member that has a parameterized type.

## HN discussion (132 comments)

### [Panzerschrek](<https://news.ycombinator.com/item?id=49965816>)

> 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++?

### [lor_louis](<https://news.ycombinator.com/item?id=49964581>)

> 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.

### [tniemi](<https://news.ycombinator.com/item?id=49964830>)

> 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...

### [dang](<https://news.ycombinator.com/item?id=49971220>)

> 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)

### [add2](<https://news.ycombinator.com/item?id=49971675>)

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

### [randomNumber7](<https://news.ycombinator.com/item?id=49966318>)

> 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?

### [mkehrt](<https://news.ycombinator.com/item?id=49967949>)

> 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.

### [veexx103](<https://news.ycombinator.com/item?id=49964833>)

> Is it feasible to create a new language by adding features to C
>
> just as was done with C++?

---

## Cite

- Page: https://zeli.app/story/49943479
- Date: 2026-10-03
- Source: danielchasehooper.com (https://danielchasehooper.com/posts/typechecked-generic-c-data-structures/)
- Hacker News: https://news.ycombinator.com/item?id=49943479

## Other languages

- zh: https://zeli.app/zh/story/49943479 · markdown: https://zeli.app/zh/story/49943479.md
- ja: https://zeli.app/ja/story/49943479 · markdown: https://zeli.app/ja/story/49943479.md
- ko: https://zeli.app/ko/story/49943479 · markdown: https://zeli.app/ko/story/49943479.md
- de: https://zeli.app/de/story/49943479 · markdown: https://zeli.app/de/story/49943479.md
- es: https://zeli.app/es/story/49943479 · markdown: https://zeli.app/es/story/49943479.md
- ru: https://zeli.app/ru/story/49943479 · markdown: https://zeli.app/ru/story/49943479.md
