How do I prevent boost::serialize from treating a pointer as an object
and serializing the object. I have written the specialization of
save/load for the object pointer.
I get the following errors when I attempt to compile the program below...
boost/serialization/access.hpp: In static member function 'static void
boost::serialization::access::serialize(Archive&, T&, unsigned int)
[with Archive = boost::archive::binary_oarchive, T = X]':
boost/serialization/serialization.hpp:81: instantiated from 'void
boost::serialization::serialize(Archive&, T&, unsigned int) [with
Archive = boost::archive::binary_oarchive, T = X]'
boost/serialization/serialization.hpp:140: instantiated from 'void
boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with
Archive = boost::archive::binary_oarchive, T = X]'
boost/archive/detail/oserializer.hpp:151: instantiated from 'void
boost::archive::detail::oserializer::save_object_data(boost::archive::detail::basic_oarchive&, const
void*) const [with Archive = boost::archive::binary_oarchive, T = X]'
s3.cpp:53: instantiated from here
boost/serialization/access.hpp:109: error: 'class X' has no member
named 'serialize'
==============================================================================================
#include <fstream>
#include <sstream>
#include <cstddef>
#include
#include
#include "boost/serialization/split_free.hpp"
using namespace std;
class X {
X(const X& other);
X& operator=(const X& other);
public :
static X* get(int id);
X() : id(0) {}
int id;
};
// Non-intrusive serialization (pretend I am not able to modify class X)
// I was hoping serialization would use this specialization
// archive X by id
template<class Archive>
void save(Archive& ar, X*& x, unsigned int version) {
int id = x->id;
ar & id;
}
// load X by lookup
template
void load(Archive& ar, X*& x, unsigned int version) {
int id;
ar & id;
x = X::get(id);
}
template
inline void serialize(Archive& ar, X*& t, const unsigned int version) {
split_free(ar, t, version);
}
int main()
{
X x;
std::ofstream ofs("junk");
boost::archive::binary_oarchive oa(ofs);
oa & x;
ofs.close();
return 0;
}