Hi, If see from the serialization documentation that if I serialize a pointer more than once, only one instance will appear in the archive. However, what about static member variables? Will they only be serialized once, even if I serialize multiple objects of the class type? e.g. class foo { private: int i; static int j; friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version) { ar & i; ar & j; // Will j be serialized multiple times, i.e. once per foo instance? } }; Thanks, Joseph -- http://www.cs.nyu.edu/~turian/
Objects that are tracked are tracked by address regardless of whether they are static variables or not. So the answer to your question is Yes - static pointers will be serialized only once - providing they point to tracked types. However, in your example, the static object isn't a pointer - its a static integer. Given that default behavior for primitive types (including integers) is not tracked, multiple copies of j will be stored/loaded. Robert Ramey Joseph Turian wrote:
Hi,
If see from the serialization documentation that if I serialize a pointer more than once, only one instance will appear in the archive.
However, what about static member variables? Will they only be serialized once, even if I serialize multiple objects of the class type?
e.g.
class foo { private: int i; static int j;
friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version) { ar & i; ar & j; // Will j be serialized multiple times, i.e. once per foo instance? } };
Thanks,
Joseph
Robert,
However, in your example, the static object isn't a pointer - its a static integer. Given that default behavior for primitive types (including integers) is not tracked, multiple copies of j will be stored/loaded.
How can I ensure, in the example I give, that the primitive static members will be stored/loaded only once? I see how to control the tracking behavior of classes. http://www.boost.org/libs/serialization/doc/traits.html#tracking How can I change the tracking of particular objects? Thank you for explaining that tracking means that two objects with the same address are never serialized twice. I didn't understand that from the documentation, but it's very useful. Is the performance overhead too high for me just to turn on tracking for all objects? Joseph -- http://www.cs.nyu.edu/~turian/
Joseph Turian wrote:
Robert,
However, in your example, the static object isn't a pointer - its a static integer. Given that default behavior for primitive types (including integers) is not tracked, multiple copies of j will be stored/loaded.
How can I ensure, in the example I give, that the primitive static members will be stored/loaded only once?
use BOOST_STRONGTYPE to wrap them in a typesafe wrapper and make sure the new wrapper uses the default tracking behavior.
I see how to control the tracking behavior of classes. http://www.boost.org/libs/serialization/doc/traits.html#tracking How can I change the tracking of particular objects?
You can't. You can only control it by the type of object. It would be possible to augment the library to assign tracking an other traits on a per object basis, but it doesn't seem that there has any been any real demand for it. The same effect can easily be obtained for specifice cases by usiing the BOOST_STRONGTYPE approach above. In practice the assigment of serialization traits to the type as opposed to the object seems to almost always work well.
Thank you for explaining that tracking means that two objects with the same address are never serialized twice.
Unless tracking is turned off by setting the corresponding serialization trait.
I didn't understand that from the documentation, but it's very useful.
Is the performance overhead too high for me just to turn on tracking for all objects?
I would expect so. and I think it would seem to be overkill to most of us. Robert Ramey
Joseph
participants (2)
-
Joseph Turian
-
Robert Ramey