
On 2013-01-17 16:35, Daniel Mitchell wrote:
Hi Robert, thanks for your reply. Unfortunately I couldn't find any of the demos to which you refer, but I did find a 2005 boost-users thread on the subject:
http://thread.gmane.org/gmane.comp.lib.boost.user/13244/
In that discussion you said you did not have a general solution. I'm just wondering if anything has changed since then e.g maybe C++11 makes a solution possible.
On Jan 17, 2013, at 2:01 PM, "Robert Ramey" <ramey@rrsd.com> wrote:
Daniel Mitchell wrote:
Hi everyone, has any progress has been made on a general solution for this problem? Given classes like these,
struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() = default; };
template<typename T> struct derived : base { template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & data; } T data; };
is it possible to serialize the derived type (with T unknown) via a base pointer? Sticking a call to register_type<derived> in derived::serialize doesn't seem to do it.
There are several demos in the examples directory which show how to do this.
Robert Ramey
I don't totally understand your original issue, but does this possibly do what you are wanting ?? #include <fstream> #include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/serialization/access.hpp> #include <boost/serialization/export.hpp> struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() { } }; BOOST_CLASS_EXPORT_KEY(base); BOOST_CLASS_EXPORT_IMPLEMENT(base); template<typename T> struct derived : base { derived() { } derived(int v) : data(v) { } template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & boost::serialization::base_object<base>(*this); ar & data; } T data; }; BOOST_CLASS_EXPORT_KEY(derived<int>); BOOST_CLASS_EXPORT_IMPLEMENT(derived<int>); int main() { base* b = new derived<int>(99); std::ofstream o("test.out", std::ios::binary); if (o.good()) { boost::archive::binary_oarchive oa(o); oa & b; } o.close(); delete b; std::ifstream i("test.out", std::ios::binary); if (i.good()) { base* b; boost::archive::binary_iarchive ia(i); ia & b; std::cout << static_cast<derived<int>*>(b)->data << std::endl; } i.close(); }