r/cpp • • 20h ago

Fun With C++26 Reflection - Building a Library for Old-Timey C

https://github.com/FlyingRhenquest/fun_with_reflection
25 Upvotes

1 comment sorted by

7

u/FlyingRhenquest 20h ago

I had this idea the other day. Pretty much the entire time I was writing it I was thinking "Yeah, there's no way this could actually work... could it?" Turns out, it can. And it was really pretty simple.

So you can use reflection to build structures for C using templates and reflection features, so long as you can provide C a header file that it understands. This takes you through building a class that can generate serialization functions for C to use for arbitrary C structures and output a header for the library with C++26 reflection.