
Hi! I'm having some problems grasping how the serialization library works with respect to const/non-const data. Ex: Consider the following simple class that I would like to serialize: struct Simple { Simple() : member(0) {} Simple(int m) : member(m) {} int member; }; BOOST_CLASS_EXPORT(Simple); BOOST_SERIALIZATION_SPLIT_FREE(Simple); //BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never); namespace boost { namespace serialization { template<class Archive> void save(Archive & ar, const Simple & t, unsigned int version) { ar << t.member; } template<class Archive> void load(Archive & ar, Simple & t, unsigned int version) { ar >> t.member; } } } But when I do the serialization, strange things happen. Ex: const Simple s1(7); //Simple s1(7); boost::archive::text_oarchive oa(ss, f); oa << s1; The non-const alternative above doesn't work unless I do BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never); And, Simple * const s1 = new Simple(7); //Simple * s1 = new Simple(7); boost::archive::text_oarchive oa(ss, f); oa << s1; The non-const alternative above doesn't work even if I do BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never); I'm obviously missing something here, please help me out. Best regards, Anders Sundman

This is explained in the "rationale" section of the documentation. The issue is that tracking implements the behavior that only one copy of an object is serialized even though it mght be passed multiple times to the serialization library (e.g. as a pointer). The library takes the passing of a non-const to signal that the programmer might be changing this value while its being serialized and traps this with a compile time message. This avoids accidently creating archives which cannot be read in the future. Robert Ramey Anders Sundman wrote:
Hi!
I'm having some problems grasping how the serialization library works with respect to const/non-const data.
Ex:
Consider the following simple class that I would like to serialize:
struct Simple { Simple() : member(0) {} Simple(int m) : member(m) {} int member; }; BOOST_CLASS_EXPORT(Simple); BOOST_SERIALIZATION_SPLIT_FREE(Simple); //BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never);
namespace boost { namespace serialization { template<class Archive> void save(Archive & ar, const Simple & t, unsigned int version) { ar << t.member; } template<class Archive> void load(Archive & ar, Simple & t, unsigned int version) { ar >> t.member; } } }
But when I do the serialization, strange things happen.
Ex:
const Simple s1(7); //Simple s1(7); boost::archive::text_oarchive oa(ss, f); oa << s1;
The non-const alternative above doesn't work unless I do BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never);
And,
Simple * const s1 = new Simple(7); //Simple * s1 = new Simple(7); boost::archive::text_oarchive oa(ss, f); oa << s1;
The non-const alternative above doesn't work even if I do BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never);
I'm obviously missing something here, please help me out.
Best regards, Anders Sundman

Oh, now I get it. Thank you. // Anders Sundman Robert Ramey skrev:
This is explained in the "rationale" section of the documentation.
The issue is that tracking implements the behavior that only one copy of an object is serialized even though it mght be passed multiple times to the serialization library (e.g. as a pointer). The library takes the passing of a non-const to signal that the programmer might be changing this value while its being serialized and traps this with a compile time message. This avoids accidently creating archives which cannot be read in the future.
Robert Ramey
Anders Sundman wrote:
Hi!
I'm having some problems grasping how the serialization library works with respect to const/non-const data.
Ex:
Consider the following simple class that I would like to serialize:
struct Simple { Simple() : member(0) {} Simple(int m) : member(m) {} int member; }; BOOST_CLASS_EXPORT(Simple); BOOST_SERIALIZATION_SPLIT_FREE(Simple); //BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never);
namespace boost { namespace serialization { template<class Archive> void save(Archive & ar, const Simple & t, unsigned int version) { ar << t.member; } template<class Archive> void load(Archive & ar, Simple & t, unsigned int version) { ar >> t.member; } } }
But when I do the serialization, strange things happen.
Ex:
const Simple s1(7); //Simple s1(7); boost::archive::text_oarchive oa(ss, f); oa << s1;
The non-const alternative above doesn't work unless I do BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never);
And,
Simple * const s1 = new Simple(7); //Simple * s1 = new Simple(7); boost::archive::text_oarchive oa(ss, f); oa << s1;
The non-const alternative above doesn't work even if I do BOOST_CLASS_TRACKING(Simple, boost::serialization::track_never);
I'm obviously missing something here, please help me out.
Best regards, Anders Sundman
participants (2)
-
Anders Sundman
-
Robert Ramey