I must admit I am not familiar with this boost package, but I copied and compiled the code, which produced the same result as the OP mentioned
Realizing that we are using polymorphism, I added a public: virtual ~A(){}; in class A. Also, oa.register_type<B>(); is added in main according to the document, and output became:
A!
B!
According to the specification, a class is a polymorphic class only when declares or
inherits a virtual function. For non-polymorphic classed, maybe polymorphism just does not work.
EDIT:
Putting the BOOST_CLASS_EXPORT(B); in B.cpp instead of B.h seems to solve this problem of redefinition.
EDIT:
Checked the expansion result of BOOST_CLASS_EXPORT(B) (reformatted):
namespace boost {
namespace serialization {
template<> struct guid_defined<B> : boost::mpl::true_ {};
template<> inline const char * guid<B>(){ return "B"; }
}
}
namespace boost {
namespace archive {
namespace detail {
namespace { // NOTE
template<> struct init_guid< B > {
static guid_initializer< B > const & g;
};
guid_initializer< B > const & init_guid< B >::g = ::boost::serialization::singleton< guid_initializer< B > >::get_mutable_instance().export_guid();
}
}
}
}
And for the line marked with NOTE: for boost 1.42 used an anonymous namespace is used, which will be no problem if it is put into multiple cpp files or put into a header file (tested in ubuntu with g++, using the boost package come along with Ubuntu). However, in boost 1.48, namespace extra_detail is used, which would cause problems when put into multiple cpp files (tested in windows with VS2010, using boost downloaded from homepage).