I have been using boost::serialization recently, and I am currently trying to get a better understanding of the design choices made in this library by experimenting with my own simplistic serializer.
And the first problem I am running into is const-correctness.
I am working on non-intrusive serialization, for I think this is the most conveninent. User code has to partially specialize a template like this:
template<class Archive, class Object> void serialize(Archive & a, Object & o) {
// ...
}
template<class Archive> void serialize(Archive & a, my_class & c) {
a & c.x;
a & c.y;
}
And the serialization mechanism will instanciate a version with load_archive and save_archive if needed. For saving, the operator & of the archive takes a const reference to an object, and the loading one takes non-const. But the problem is that the partial specialization cannot handle both const and non-const versions. And I don't want to require the two specializations to be written by the "user", because this is error-prone.
So for now, I am using const_cast to always send a volative reference to the serialize functions, but this doesn't look very clean. The operators in the archive classes are const-correct, but nothing prevents "user code" from modifying the object...
From what I have seen, boost also casts to non-const, but I was wondering if there was some other way around.











