Boost logo

Boost Users :

Subject: [Boost-users] serialization of a ptr
From: Jonathan Bishop (jbishop.rwc_at_[hidden])
Date: 2011-10-31 16:36:39


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<Archive,
T>::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 <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#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<class Archive, class T>
void load(Archive& ar, X*& x, unsigned int version) {
  int id;
  ar & id;
  x = X::get(id);
}

template<class Archive, class T>
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;
}


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