Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-03-10 00:23:05

David Abrahams wrote:
> In giving a presentation about the serialization library on Tuesday
> the students got very hung up on the const-correctness of output
> streaming using the usual single serialize(MyClass&, ... ) function.

I'm not sure I get this. I don't see any serialize functions or templates
that look like that. Free functions look like

template<class Archive, class T>
serialize(Archive &ar, T & t, ..

while member functions look like

template<class Archive>
void serialize(Archive &ar, const unsigned int version){

> Can you serialize const objects that way? Does the library cast away
> const?

The library can't cast away const because remove_const wasn't available
for msvc 6.0.

ar << t; // forces t to be const - for now

ar >> t; // prohbits t from being const

ar & t; // a couple layers down adds const if its a saving archive.

I'm not sure if the following refers to the questions raised in your
course, but I can make the following comment.

I suppose if one were clever enough, he might enforce the current
const rules with the & operator as well as the << and >> operators
depending on the archive type. It would certainly be more consistent
than the current scheme. I don't know if it would be possible to do
this, but given that this const checking has been the source of a few
vociferous complaints its not totally clear that it won't be removed
in the future. So I'm not all that motivated to invest time to investigate
this. Also, the current system provides and "escape hatch" for those
who object to the const rules.

> One could get the compiler to deduce constness appropriately if the
> object being serialized were specified as:
> template<class Archive, class GPSPosition>
> void serialize(Archive & ar, GPSPosition& g, const unsigned int
> version)

I don't see how this is different from the above (replacing
GPSPosition with T)

> but of course that doesn't overload very well; you'd need some other
> way to indicatethe type of the object to be serialized.
> How is this all related to


Robert Ramey

Boost list run by bdawes at, gregod at, cpdaniel at, john at