Hello
I am using boost 1.38.0
How to serialize shared_ptr member which points to const object.
The following compiles, but is not working i.e the restored boost_shared_ptr does not point to anything.
I tried to use const_pointer_cast to cast the const away. I suspect that this is the problematic part.
I think the question here is , how should I cast away the const correctly for boost::shared_ptr<const T>?
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/utility.hpp>
#include <fstream>
#include <boost/serialization/shared_ptr.hpp>
class B
{
public:
B() { }
B(int a):ma(a) { }
int ma;
template<class Archive>
void serialize(Archive & ar, const unsigned int
version)
{
using boost::serialization::make_nvp;
ar & make_nvp("ma",ma);
}
};
class BC
{
public:
BC() { }
BC(boost::shared_ptr<B>& t):mac(t) { }
boost::shared_ptr<const B> mac;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
using boost::serialization::make_nvp;
ar & make_nvp("mac", boost::const_pointer_cast<B> (mac));
}
};
int main()
{
std::ofstream ofs(L"C:\\MyTest.xml");
std::ifstream ifs(L"C:\\MyTest.xml");
boost::shared_ptr<B> bb(new B(4));
BC aa(bb);
{
boost::archive::text_oarchive oa(ofs);
oa & BOOST_SERIALIZATION_NVP(aa);
}
BC raa;
{
boost::archive::text_iarchive ia(ifs);
ia & BOOST_SERIALIZATION_NVP(raa);
}
}