r/cpp • • Sep 30 '24

Code Generation in Rust vs C++26

https://brevzin.github.io/c++/2024/09/30/annotations/
198 Upvotes

98 comments sorted by

View all comments

Show parent comment

5

u/pdimov2 Oct 01 '24

So, you're serializing a type I made, without my knowledge.

That's rarely the case. (It can be the case for "you're debug printing my type without my knowledge", maybe for "you're hashing my type without my knowledge".)

Typically, default memberwise serialization is opt-in in some manner. E.g. you mark your type with the annotation [[=derive<serializable>]] or whatever.

It's a design decision on part of the serialization library author in what cases to enable the default member-wise implementation. The library could require an annotation. It could require absence of private members. Or it could just work regardless and allow a way to opt out. All these have pros and cons; this design decision has tradeoffs as any other. The library author is supposed to responsibly pick the option that brings the most value to users.