OK - figured it out. As usual something really obvious in retrospect.
The serialization library presumes that each data type loaded will
be the same type that was saved.
In this case we're saving a type d2 and loading a type base.
In saving a type d2 - it's presumed that the type loaded
will also be type d2 so there is no need to include type
information in the archive.
If the type being saved is base, the library "realizes" that
this could be either d2 or d1 and save a tag in the archive
accordingly. This tag is set by the BOOST_CLASS_EXPORT
macro.
The tip-off is that when the archive is rendered as text, no
type name tag appears. The following modification to your
test fixes the problem. (I made some other changes in the
course of tracking down the issue - they are beside the
point addressed here)
Robert Ramey
int main(int argc, char **argv[]){
std::string data;
{
std::ostringstream archive_stream;
{
boost::archive::text_oarchive archive( archive_stream );
// create an instance of derived2
const derived2 d;
// create a pointer of the above cast to the base class.
const base * const b = & d;
// now serialize through the base class pointer.
archive << b;
}
data = archive_stream.str();
}
{
// deserialize
std::istringstream archive_stream(data);
boost::archive::text_iarchive archive( archive_stream );
// I don't know what derived class was serialized.
// note that we're loading a base * - just the same as we saved !!!
base* b;
try{
archive >> b;
}
catch( std::exception e ){
std::cout << e.what() << std::endl;
}
if( b->type() == 1 ){
derived1* d1 = static_cast