|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2005-07-10 15:29:00
Addresses of primitive types are generally not tracked by default. So
replacing A with a char would not give a desired result. This is described
in the manual under the section of seriailzation traites/tracking and
serialization of pointers.
A solution would be to make a "trackable char" using STRONG_TYPE or some
other sort of wrapper to distinguish a char from a your own chartype.
Note that if you make a few small changes in your program - and are using
current CVS version (1.33 - real soon now) you will an error message to
alert you to this problem. Here is my updated version of your program.
Ivan Rachev wrote:
> Hi Martin,
>
> If class A from the example is replaced by char, how should this
> situation be handled? Code follows at the end.
>
> Thanks,
> Ivan
>
>
>
>
> template <typename T>
> struct DynamicArray
> {
> int Size;
> T* Element;
>
> friend class boost::serialization::access;
> template<class Archive>
> void save(Archive & ar, const unsigned int version) const
> {
> ar & Size;
> for (int i = 0; i < Size; ++i)
> ar & Element[i];
> }
> template<class Archive>
> void load(Archive & ar, const unsigned int version)
> {
> ar & Size;
> assert(Size >=0);
> if (Size > 0)
> {
> Element = new T[Size];
> for (int i = 0; i < Size; ++i)
> ar & Element[i];
> }
> }
> BOOST_SERIALIZATION_SPLIT_MEMBER()
> };
>
> int main(int argc, char* argv[])
> {
> DynamicArray<char> ArrayOfAs, ArrayOfAs2;
> ArrayOfAs.Element = new char[5];
> ArrayOfAs.Size = 5;
> char* secondElement = &ArrayOfAs.Element[1];
> char* secondElement2;
>
> std::stringstream stream;
> {
> boost::archive::text_oarchive oa(stream);
> oa & ArrayOfAs;
> oa & secondElement;
>
> }
>
> {
> boost::archive::text_iarchive ia(stream);
> ia & ArrayOfAs2;
> ia & secondElement2;
>
> }
>
> assert(secondElement2 == &ArrayOfAs2.Element[1]);
>
> delete [] ArrayOfAs.Element;
> delete [] ArrayOfAs2.Element;
> }
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net