Boost logo

Boost Users :

From: Zoltan Szatmary (szaki_at_[hidden])
Date: 2005-01-19 07:20:44


Dear Robert

Thank you for answering.

I am sorry I was not very specific (though please don't think I would put the
question without RTFM first ;) )
The part of documentation you pointed to told me to do const_cast manually. It is fine
in case of const members of a class, but this time the const type is contained by a vector.
I could manually write the serialization code for my vector<const A*> type but I hoped I
could use the built-in vector serialization templates in vector.hpp
To be clear I copy here the piece of code I am talking about

#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;

class A
{
        friend class boost::serialization::access;
        template<class Archive>
        void serialize (Archive &ar, const unsigned int version)
        {
        }
};

/* NEITHER OF THESE HELPS
namespace boost { namespace serialization {
        template <class Archive>
        inline void serialize (Archive &ar, const A *p, const unsigned int version)
        {
                ar & const_cast<A*>(p);
        }
        template <class Archive>
        inline void serialize (Archive &ar, const A &p, const unsigned int version)
        {
                ar & const_cast<A&>(p);
        }
}}
*/

int main()
{
        A a;
        vector<const A*> v(1, &a);
        ofstream ofs ("vectorofconstptr");
        boost::archive::text_oarchive oa(ofs);
        oa << v;
        ofs.close();

// UNTIL THIS POINT EVERYTHING IS FINE
// BUT DESERIALIZATION FAILS TO COMPILE

        ifstream ifs("vectorofconstptr");
        boost::archive::text_iarchive ia(ifs);
        vector<const A*> p;
        ia >> p;
        ifs.close();
}

Please tell me if I am doing something wrong. I tried to look into the documentation, I have
even followed the serialization library code for a while, but eventually decided to ask.

Thank you in advance

Zoltan

>Look in the documentation under Reference/Class Serialization/const members

>Robert Ramey

>Zoltan Szatmary wrote:
>> Hello
>>
>> could you help please? what is the easiest way to (de)serialize a
>> member of type vector<const A*> (A being a base class)? Serializing
>> is fine, but deserialization fails to compile since the vector
>> deserialization code tries to deserialize a const A* pointer without
>> const_cast-ing it (I guess).
>>
>> Thank you in advance
>>
>> Zoltan


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