Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions presentations/rust-traits-c++-on-sea-2025/serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Example of subtyping as subclassing: Typical Serialization in C++

```c++
#include <sstream>

// Infrastructure:
namespace user {

struct TypeRegistry {
template<typename T>
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<typename T>
struct SerializeWrapper: ISerialize {
T value_;
virtual std::ostream &serialize(std::ostream &to) const override {
return
to << g_registry.id<T>() << ':' << value_;
}
virtual std::size_t length() const {
std::ostringstream temporary;
serialize(temporary);
return temporary.str().length();
}
};

}
```