r/cpp • u/Personal-History8048 • May 14 '26
Your C++ struct is the schema: a proto3 serializer in C++26 reflection
I built a header-only proto3 wire-format library with one constraint: no .proto files, no codegen, no descriptor runtime. The user writes a plain C++ struct, and that struct is the schema:
#include "proto3.hpp"struct SearchRequest {std::string query; // field 1std::int32_t page_number; // field 2std::int32_t results_per_page; // field 3};SearchRequest req{"hello", 1, 10};std::string bytes = proto3::serialize(req); // -> proto3 wire bytesSearchRequest back = proto3::deserialize<SearchRequest>(bytes);
blog: Your C++ struct is the schema: a proto3 serializer in C++26 reflection
github: struct_proto26
41
u/SevenIsMy May 14 '26
So how does other languages adopt the format, without the proto file? It this only good for C++ to C++ with access to the same header?
6
u/yeochin May 14 '26
Neat. For many applications this will be much more convenient. However, for high-performance, high-throughput applications you may need to figure out some alternative arrangement. The existing protocol buffer implementation benefits from "views" into the binary blob as opposed to copying into a data-structure. This drives cache-locality and massive performance gains despite all the branching.
If you can solve this with a struct-like C++ reflection then you'll have everything truly needed to replace the codegen.
4
u/mort96 May 14 '26
This smells AI. Is it AI?
3
u/germandiago May 14 '26
I wish this was done for capnproto. Maybe I should. I work with capnproto and its RPC and the serialization/deserialization is a considerable part of the code.
What would be best is some kind of named tuples coming from the generated capnproto schema where I can tell the serializer which fields to synthethize or pick for a struct via some DSL. That would keep things fast and maintainable.
24
u/riztazz May 14 '26
Reflection is gonna simplify so much of my code
Hurry up msvc team (˘・_・˘)
2
u/datnt84 May 14 '26
Cool. I am doing such things for years with Qt MOC.
-2
u/feverzsj May 14 '26
What about setter and getter?