· 01:40
Hello and welcome to CodeCast. Today we’re diving into how you can use C++26 Reflection to build a struct-of-arrays vector, inspired by Andrew Kelley’s talk on Practical Data-Oriented Design. Kelley shows how memory usage soars when you switch “from an array of structs” to “a struct of arrays,” and here the blog walks through implementing SoaVector in under a hundred lines of code.
First we define a simple Point with char x and int y. Instead of one array of Point, we generate separate x and y arrays at compile time using std::meta::define_aggregate and nonstatic_data_members_of. We store pointers, size, and capacity just once, then write push_back and grow functions that piece-wise allocate, copy, and clean up each field.
Next we add two overloads of operator[]—one returning a Point by value, the other returning a proxy, PointRef, with char& and int& members. Thanks to annotations, these proxies even format as Point when you print them.
Finally, the post compares this with Zig’s elegant meta functions, noting that in Zig “types are first class” and you can write functions that take types and return types, getting concise code generation. It’s a powerful demonstration of what C++ can do with reflection, and it shows we’re just scratching the surface of code-gen possibilities. Thanks for listening!
Link to Article
Listen to jawbreaker.io using one of many popular podcasting apps or directories.