[Serialization] shared_ptr to const object

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

AMDG elizabeta petreska wrote:
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>?
The problem is that you are creating a temporary when
you do the cast. When you deserialize, the temporary
is modified, instead of mac. The following seems to
work.
#include

Thank you for the reply.
This works!!! , but it creates a temporary. I was thinking how to cast the
constness away, without making temporary object. I gess know at least I have
one possible solution, althgough not complete happy with it :)
On 12/19/09, Steven Watanabe
AMDG
elizabeta petreska wrote:
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>?
The problem is that you are creating a temporary when you do the cast. When you deserialize, the temporary is modified, instead of mac. The following seems to work.
#include
#include #include #include #include <fstream> #include 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;
BOOST_SERIALIZATION_SPLIT_MEMBER()
template<class Archive> void save(Archive & ar, const unsigned int /*version*/) const { using boost::serialization::make_nvp; ar & make_nvp("mac", mac); } template<class Archive> void load(Archive & ar, const unsigned int /*version*/) { using boost::serialization::make_nvp; boost::shared_ptr<B> temp; ar & make_nvp("mac", test); mac = temp; } };
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::xml_oarchive oa(ofs); oa & BOOST_SERIALIZATION_NVP(aa); }
BC raa; { boost::archive::xml_iarchive ia(ifs); ia & BOOST_SERIALIZATION_NVP(raa); }
}
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

AMDG elizabeta petreska wrote:
Thank you for the reply. This works!!! , but it creates a temporary. I was thinking how to cast the constness away, without making temporary object. I gess know at least I have one possible solution, althgough not complete happy with it :)
You can't avoid the temporary. Actually, I think the serialization library should handle this automatically, since its perfectly safe. In Christ, Steven Watanabe

Hello again
I just want to say that I managed to solve this problem without creating a
temporary , thanks to this thread :
http://old.nabble.com/(de)serializing-map%3Cconst-A*%3E-to5538474.html#a5596...http://old.nabble.com/%28de%29serializing-map%3Cconst-A*%3E-to5538474.html#a...
And here it is what I have got in my problem :
boost::shared_ptr<const TT> jj(new TT(5));
{
boost::archive::xml_oarchive oa(ofs);
oa & BOOST_SERIALIZATION_NVP(jj);
}
boost::shared_ptr<const TT> rjj;
{
void* xptr=&rjj;
boost::shared_ptr<TT> *xxptr;
xxptr=static_cast< boost::shared_ptr<TT> * > (xptr);
boost::archive::xml_iarchive ia(ifs);
ia & BOOST_SERIALIZATION_NVP(*xxptr);
}
On Sun, Dec 20, 2009 at 3:59 PM, Steven Watanabe
AMDG
elizabeta petreska wrote:
Thank you for the reply. This works!!! , but it creates a temporary. I was thinking how to cast the constness away, without making temporary object. I gess know at least I have one possible solution, althgough not complete happy with it :)
You can't avoid the temporary. Actually, I think the serialization library should handle this automatically, since its perfectly safe.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

AMDG elizabeta petreska wrote:
I just want to say that I managed to solve this problem without creating a temporary , thanks to this thread : http://old.nabble.com/(de)serializing-map%3Cconst-A*%3E-to5538474.html#a5596...http://old.nabble.com/%28de%29serializing-map%3Cconst-A*%3E-to5538474.html#a...
And here it is what I have got in my problem :
boost::shared_ptr<const TT> jj(new TT(5));
{ boost::archive::xml_oarchive oa(ofs); oa & BOOST_SERIALIZATION_NVP(jj); }
boost::shared_ptr<const TT> rjj;
{ void* xptr=&rjj; boost::shared_ptr<TT> *xxptr; xxptr=static_cast< boost::shared_ptr<TT> * > (xptr); boost::archive::xml_iarchive ia(ifs); ia & BOOST_SERIALIZATION_NVP(*xxptr); }
This is undefined behavior. I would strongly advise against it if there is a reasonable alternative. In Christ, Steven Watanabe

Steven Watanabe wrote:
AMDG
elizabeta petreska wrote:
I just want to say that I managed to solve this problem without creating a temporary , thanks to this thread : http://old.nabble.com/(de)serializing-map%3Cconst-A*%3E-to5538474.html#a5596...http://old.nabble.com/%28de%29serializing-map%3Cconst-A*%3E-to5538474.html#a...
And here it is what I have got in my problem :
boost::shared_ptr<const TT> jj(new TT(5));
{ boost::archive::xml_oarchive oa(ofs); oa & BOOST_SERIALIZATION_NVP(jj); }
boost::shared_ptr<const TT> rjj;
{ void* xptr=&rjj; boost::shared_ptr<TT> *xxptr; xxptr=static_cast< boost::shared_ptr<TT> * > (xptr); boost::archive::xml_iarchive ia(ifs); ia & BOOST_SERIALIZATION_NVP(*xxptr); }
This is undefined behavior. I would strongly advise against it if there is a reasonable alternative.
In Christ, Steven Watanabe
Note that for better or worse, the serialization library does a lot of this kind of thing internally. If fact, a number of key facilities (e.g. export, usage of DLLS, etc depend on behavior which is technically undefined, but necessary to implement them. Just getting them to work at all was a huge challenge. I doubt that the above is any more risky than many usages of the library itself. Robert Ramey
participants (3)
-
elizabeta petreska
-
Robert Ramey
-
Steven Watanabe