[Boost-users] (de)serializing vector<const A*>

7 views
Skip to first unread message

Zoltan Szatmary

unread,
Jan 18, 2005, 2:40:09 PM1/18/05
to boost...@lists.boost.org
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 mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Robert Ramey

unread,
Jan 19, 2005, 1:21:32 AM1/19/05
to boost...@lists.boost.org
Look in the documentation under Reference/Class Serialization/const members

Robert Ramey

Zoltan Szatmary

unread,
Jan 19, 2005, 7:20:44 AM1/19/05
to boost...@lists.boost.org
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.

Robert Ramey

unread,
Jan 19, 2005, 12:20:02 PM1/19/05
to boost...@lists.boost.org
OK, I've looked at this more carefully and see no obvious fix.

My original intention was that serialization of a const object would
generate a compile error unless explicitly overridden. I your case this
isn't easy as the override would be inside the implementation of
serialization of collections. Fixing it here would conflict with my
original intent and permit un noticed alteration of const members.

I'll have to think about this some more.

As to your particular case, I did try variations on:

template<class Archive>
inline void load(
Archive & ar,
std::vector<const A *> &t,
const unsigned int /* file_version */
){
ar >> reinterpret_cast<vector<A *> >(t);
}

but wasn't successful. So for now the best I can suggest is to re-implement
the serializaton for your particular vector.

Robert Ramey

Zoltan Szatmary wrote:
> Dear Robert

>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
>



Reply all
Reply to author
Forward
0 new messages