diff --git a/presentations/rust-traits-c++-on-sea-2025/serialization.md b/presentations/rust-traits-c++-on-sea-2025/serialization.md new file mode 100644 index 0000000..5aee853 --- /dev/null +++ b/presentations/rust-traits-c++-on-sea-2025/serialization.md @@ -0,0 +1,36 @@ +## Example of subtyping as subclassing: Typical Serialization in C++ + +```c++ +#include + +// Infrastructure: +namespace user { + +struct TypeRegistry { + template + std::uint64_t id() const; +}; + +extern TypeRegistry g_registry; + +struct ISerialize { + virtual std::ostream &serialize(std::ostream &) const; + virtual std::size_t length() const; +}; + +template +struct SerializeWrapper: ISerialize { + T value_; + virtual std::ostream &serialize(std::ostream &to) const override { + return + to << g_registry.id() << ':' << value_; + } + virtual std::size_t length() const { + std::ostringstream temporary; + serialize(temporary); + return temporary.str().length(); + } +}; + +} +```