It turns out that we can't get away with the register_type<> workaround, at
least if we want to support anything we've serialized with earlier versions.
The register_type<> approach produces incompatible archives to those
produced with BOOST_CLASS_EXPORT.
This all comes about because the class type is registered in the archive the
first time it is seen. For the register_type<> case, it is marked as 'seen'
immediately, so it is never written to the archive..
IMO I don't believe the choice of how the class is registered should affect
the archive contents, but addressing this issue could be tricky at this
stage - to ensure it is backwards compatible, for sure. The archive would
have to be examined to see if the class were there or not.
If it's too much to address this issue, then, at least, the documentation
should be updated to make it clear that once the choice is made...that's it.
Anyway, FWIW, here's the test code to demonstrate:
#include
#include
#include
#include
#include <sstream>
struct B
{
virtual ~B() {}
template <typename A>
void serialize(A &a, const unsigned int i)
{}
};
typedef boost::shared_ptr<B> BP;
struct D : B
{
template <typename A>
void serialize(A &a, const unsigned int i)
{
a & boost::serialization::make_nvp("B",
boost::serialization::base_object<B>(*this));
}
};
BOOST_CLASS_EXPORT(D)
void
serialize_export_out_register_in()
{
std::string data;
{
BP bp(new D);
std::ostringstream str;
boost::archive::xml_oarchive arch(str);
arch & boost::serialization::make_nvp("b", bp);
data = str.str();
}
{
BP bp;
std::istringstream str(data);
boost::archive::xml_iarchive arch(str);
arch.register_type<D>(); // Pre-register
arch & boost::serialization::make_nvp("b", bp); // Stream error
}
}
void
serialize_register_out_export_in()
{
std::string data;
{
BP bp(new D);
std::ostringstream str;
boost::archive::xml_oarchive arch(str);
arch.register_type<D>(); // Pre-register
arch & boost::serialization::make_nvp("b", bp);
data = str.str();
}
{
BP bp;
std::istringstream str(data);
boost::archive::xml_iarchive arch(str);
arch & boost::serialization::make_nvp("b", bp); // Segment fault
}
}
Chard.
"Steven Watanabe" wrote in message
news:499B3A58.4090006@providere-consulting.com...
AMDG
Robert Ramey wrote:
I would like to change it back. but before I do, I would like
to hear other opinions.
Adding an anonymous namespace should not cause
any code that works now to break.
In Christ,
Steven Watanabe