Serialization with polymorphic_*_archive and BOOST_CLASS_EXPORT
Hi,
I'm having trouble serializing through the a pointer to the base as shown
below.
If I manually register class B with template register_type<B>() it works,
but I
would rather use the BOOST_CLASS_EXPORT macros since that is more
convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated
version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
Anyone know how to get around that problem?
Thanks!
-- wang
Example:
#include
Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either. Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.? Though its beside the point, I would also make your example more portable wit the fillowing changes: ... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); } struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); } BOOST_CLASS_EXPORT(B) int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get()); assert(*x == *y); return 0; }
Thanks for your swift reply!
In my example with the non-templated serialize with polymorphic_iarchive
the statement BOOST_CLASS_EXPORT(B) fails to compile. It compiles
fine without that statement, but fails to serialize with asserion:
Assertion failed: NULL != bpos_ptr, file
include\boost\archive\detail\oserializer.hpp, line 418
If I manully register B with register_type it works.
I'm using VC8 and boost 1.33.1 and I've attached the error message from
compiling.
-- Anders
1>Compiling...
1>_main.cpp
1>include\boost\serialization\access.hpp(109) : error C2664: 'void
B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' :
cannot convert parameter 1 from 'boost::archive::text_oarchive' to
'boost::archive::polymorphic_iarchive &'
1> include\boost\serialization\serialization.hpp(81) : see reference
to function template instantiation 'void
boost::serialization::access::serialize
Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either.
Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.?
Though its beside the point, I would also make your example more portable wit the fillowing changes:
... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); }
struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... };
template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); }
BOOST_CLASS_EXPORT(B)
int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
----- Original Message -----
From: "Robert Ramey"
Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either.
Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.?
Though its beside the point, I would also make your example more portable wit the fillowing changes:
... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); }
struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... };
template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); }
BOOST_CLASS_EXPORT(B)
int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
Anders Wang Kristensen wrote:
Thanks for your swift reply!
In my example with the non-templated serialize with polymorphic_iarchive the statement BOOST_CLASS_EXPORT(B) fails to compile. It compiles fine without that statement, but fails to serialize with asserion: Assertion failed: NULL != bpos_ptr, file include\boost\archive\detail\oserializer.hpp, line 418 If I manully register B with register_type it works.
I'm using VC8 and boost 1.33.1 and I've attached the error message from compiling.
-- Anders
Try the following:
int main(int argc, const char* argv[]){ boost::shared_ptr<const A> a(new B); // Note "const" boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
Also I strongly recommend you used templated serialize functions. Robert Ramey
1>Compiling... 1>_main.cpp 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::text_oarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize
(Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(152) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(145) : while compiling class template member function 'void boost::archive::detail::oserializer ::save_object_data(boost::archive::detail::basic_oarchive &,const void *) const' 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(163) : see reference to class template instantiation 'boost::archive::detail::oserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(162) : while compiling class template member function 'const boost::archive::detail::basic_oserializer &boost::archive::detail::pointer_oserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_oarchive 1> ] 1> include\boost\archive\detail\oserializer.hpp(515) : see reference to class template instantiation 'boost::archive::detail::pointer_oserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_oarchive 1> ] 1> include\boost\serialization\export.hpp(86) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer &boost::archive::detail::instantiate_pointer_oserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(82) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::o::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::o' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::archive::detail::known_archive_types::type, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(132) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::archive::detail::known_archive_types::type, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(131) : while compiling class template member function 'boost::archive::detail::export_generator ::export_generator(void)' 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(177) : see reference to class template instantiation 'boost::archive::detail::export_generator ' being compiled 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(176) : while compiling class template member function 'const boost::archive::detail::export_generator *boost::archive::detail::export_instance ::not_abstract::invoke(void)' 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(192) : see reference to class template instantiation 'boost::archive::detail::export_instance ::not_abstract' being compiled 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(211) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> boost::archive::detail::export_instance_invoke (void)' being compiled 1> with 1> [ 1> _Ty1=const boost::archive::detail::export_generator *, 1> _Ty2=const boost::archive::detail::guid_initializer<B> *, 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(210) : while compiling class template member function 'std::pair<_Ty1,_Ty2> boost::archive::detail::export_archives
::non_empty_archive_list::invoke(void)' 1> with 1> [ 1> _Ty1=const boost::archive::detail::export_generator *, 1> _Ty2=const boost::archive::detail::guid_initializer<B> *, 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(225) : see reference to class template instantiation 'boost::archive::detail::export_archives
::non_empty_archive_list' being compiled 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> c:\users\awk\desktop\work\code\src\tests\scenegraph_regression\_main.cpp(129) see reference to function template instantiation 'std::pair<_Ty1,_Ty2> boost::archive::detail::export_archives_invoke(B
&,boost::archive::detail::known_archive_types::type &)' being compiled 1> with 1> [ 1> _Ty1=const boost::archive::detail::export_generator
*, 1> _Ty2=const boost::archive::detail::guid_initializer<B> * 1> ] 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::text_iarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize
(Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(164) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(157) : while compiling class template member function 'void boost::archive::detail::iserializer ::load_object_data(boost::archive::detail::basic_iarchive &,void *,const unsigned int) const' 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(175) : see reference to class template instantiation 'boost::archive::detail::iserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(174) : while compiling class template member function 'const boost::archive::detail::basic_iserializer &boost::archive::detail::pointer_iserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_iarchive 1> ] 1> include\boost\archive\detail\iserializer.hpp(546) : see reference to class template instantiation 'boost::archive::detail::pointer_iserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_iarchive 1> ] 1> include\boost\serialization\export.hpp(78) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_iserializer &boost::archive::detail::instantiate_pointer_iserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(74) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::i::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::i' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::mpl::list5boost::archive::text_iarchive,boost::archive::xml_oarchive,boost::archive::x..., 1> T=B 1> ] 1> include\boost\serialization\export.hpp(122) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::mpl::list5boost::archive::text_iarchive,boost::archive::xml_oarchive,boost::archive::x..., 1> T=B 1> ] 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::xml_oarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(152) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(145) : while compiling class template member function 'void boost::archive::detail::oserializer ::save_object_data(boost::archive::detail::basic_oarchive &,const void *) const' 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(163) : see reference to class template instantiation 'boost::archive::detail::oserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(162) : while compiling class template member function 'const boost::archive::detail::basic_oserializer &boost::archive::detail::pointer_oserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_oarchive 1> ] 1> include\boost\archive\detail\oserializer.hpp(515) : see reference to class template instantiation 'boost::archive::detail::pointer_oserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_oarchive 1> ] 1> include\boost\serialization\export.hpp(86) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer &boost::archive::detail::instantiate_pointer_oserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(82) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::o::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::o' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::mpl::list4boost::archive::xml_oarchive,boost::archive::xml_iarchive,boost::archive::po..., 1> T=B 1> ] 1> include\boost\serialization\export.hpp(122) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::mpl::list4boost::archive::xml_oarchive,boost::archive::xml_iarchive,boost::archive::po..., 1> T=B 1> ] 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::xml_iarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(164) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(157) : while compiling class template member function 'void boost::archive::detail::iserializer ::load_object_data(boost::archive::detail::basic_iarchive &,void *,const unsigned int) const' 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(175) : see reference to class template instantiation 'boost::archive::detail::iserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(174) : while compiling class template member function 'const boost::archive::detail::basic_iserializer &boost::archive::detail::pointer_iserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_iarchive 1> ] 1> include\boost\archive\detail\iserializer.hpp(546) : see reference to class template instantiation 'boost::archive::detail::pointer_iserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_iarchive 1> ] 1> include\boost\serialization\export.hpp(78) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_iserializer &boost::archive::detail::instantiate_pointer_iserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(74) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::i::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::i' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::mpl::list3boost::archive::xml_iarchive,boost::archive::polymorphic_oarchive,boost::arc..., 1> T=B 1> ] 1> include\boost\serialization\export.hpp(122) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::mpl::list3boost::archive::xml_iarchive,boost::archive::polymorphic_oarchive,boost::arc..., 1> T=B 1> ] ----- Original Message ----- From: "Robert Ramey"
Newsgroups: gmane.comp.lib.boost.user Sent: Tuesday, November 07, 2006 2:42 AM Subject: Re: Serialization with polymorphic_*_archiveandBOOST_CLASS_EXPORT Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either.
Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.?
Though its beside the point, I would also make your example more portable wit the fillowing changes:
... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); }
struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... };
template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); }
BOOST_CLASS_EXPORT(B)
int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
----- Original Message ----- From: "Robert Ramey"
Newsgroups: gmane.comp.lib.boost.user Sent: Tuesday, November 07, 2006 2:42 AM Subject: Re: Serialization with polymorphic_*_archiveandBOOST_CLASS_EXPORT Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either.
Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.?
Though its beside the point, I would also make your example more portable wit the fillowing changes:
... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); }
struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... };
template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); }
BOOST_CLASS_EXPORT(B)
int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
Didn't help.. same error.
I guess I'll switch to the templated version then.
The reason I preferred the non-template version was that the same code
worked with archives
of different kinds. To get the same behaviour with the template version
wouldn't you have to
put the implementation of serialize in the hpp file (ugly), or put it in the
cpp file and explicitly
instantiate the templates (only works for those archives used as template
params)?
-- Anders
"Robert Ramey"
Anders Wang Kristensen wrote:
Thanks for your swift reply!
In my example with the non-templated serialize with polymorphic_iarchive the statement BOOST_CLASS_EXPORT(B) fails to compile. It compiles fine without that statement, but fails to serialize with asserion: Assertion failed: NULL != bpos_ptr, file include\boost\archive\detail\oserializer.hpp, line 418 If I manully register B with register_type it works.
I'm using VC8 and boost 1.33.1 and I've attached the error message from compiling.
-- Anders
Try the following:
int main(int argc, const char* argv[]){ boost::shared_ptr<const A> a(new B); // Note "const" boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
Also I strongly recommend you used templated serialize functions.
Robert Ramey
1>Compiling... 1>_main.cpp 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::text_oarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize
(Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(152) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(145) : while compiling class template member function 'void boost::archive::detail::oserializer ::save_object_data(boost::archive::detail::basic_oarchive &,const void *) const' 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(163) : see reference to class template instantiation 'boost::archive::detail::oserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(162) : while compiling class template member function 'const boost::archive::detail::basic_oserializer &boost::archive::detail::pointer_oserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_oarchive 1> ] 1> include\boost\archive\detail\oserializer.hpp(515) : see reference to class template instantiation 'boost::archive::detail::pointer_oserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_oarchive 1> ] 1> include\boost\serialization\export.hpp(86) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer &boost::archive::detail::instantiate_pointer_oserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(82) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::o::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::o' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::text_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::archive::detail::known_archive_types::type, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(132) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::archive::detail::known_archive_types::type, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(131) : while compiling class template member function 'boost::archive::detail::export_generator ::export_generator(void)' 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(177) : see reference to class template instantiation 'boost::archive::detail::export_generator ' being compiled 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(176) : while compiling class template member function 'const boost::archive::detail::export_generator *boost::archive::detail::export_instance ::not_abstract::invoke(void)' 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(192) : see reference to class template instantiation 'boost::archive::detail::export_instance ::not_abstract' being compiled 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(211) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> boost::archive::detail::export_instance_invoke (void)' being compiled 1> with 1> [ 1> _Ty1=const boost::archive::detail::export_generator *, 1> _Ty2=const boost::archive::detail::guid_initializer<B> *, 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(210) : while compiling class template member function 'std::pair<_Ty1,_Ty2> boost::archive::detail::export_archives
::non_empty_archive_list::invoke(void)' 1> with 1> [ 1> _Ty1=const boost::archive::detail::export_generator *, 1> _Ty2=const boost::archive::detail::guid_initializer<B> *, 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> include\boost\serialization\export.hpp(225) : see reference to class template instantiation 'boost::archive::detail::export_archives
::non_empty_archive_list' being compiled 1> with 1> [ 1> T=B, 1> ASeq=boost::archive::detail::known_archive_types::type 1> ] 1> c:\users\awk\desktop\work\code\src\tests\scenegraph_regression\_main.cpp(129) see reference to function template instantiation 'std::pair<_Ty1,_Ty2> boost::archive::detail::export_archives_invoke(B
&,boost::archive::detail::known_archive_types::type &)' being compiled 1> with 1> [ 1> _Ty1=const boost::archive::detail::export_generator
*, 1> _Ty2=const boost::archive::detail::guid_initializer<B> * 1> ] 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::text_iarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize
(Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(164) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(157) : while compiling class template member function 'void boost::archive::detail::iserializer ::load_object_data(boost::archive::detail::basic_iarchive &,void *,const unsigned int) const' 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(175) : see reference to class template instantiation 'boost::archive::detail::iserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(174) : while compiling class template member function 'const boost::archive::detail::basic_iserializer &boost::archive::detail::pointer_iserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_iarchive 1> ] 1> include\boost\archive\detail\iserializer.hpp(546) : see reference to class template instantiation 'boost::archive::detail::pointer_iserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::text_iarchive 1> ] 1> include\boost\serialization\export.hpp(78) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_iserializer &boost::archive::detail::instantiate_pointer_iserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(74) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::i::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::i' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::text_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::mpl::list5boost::archive::text_iarchive,boost::archive::xml_oarchive,boost::archive::x..., 1> T=B 1> ] 1> include\boost\serialization\export.hpp(122) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::mpl::list5boost::archive::text_iarchive,boost::archive::xml_oarchive,boost::archive::x..., 1> T=B 1> ] 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::xml_oarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(152) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(145) : while compiling class template member function 'void boost::archive::detail::oserializer ::save_object_data(boost::archive::detail::basic_oarchive &,const void *) const' 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(163) : see reference to class template instantiation 'boost::archive::detail::oserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\archive\detail\oserializer.hpp(162) : while compiling class template member function 'const boost::archive::detail::basic_oserializer &boost::archive::detail::pointer_oserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_oarchive 1> ] 1> include\boost\archive\detail\oserializer.hpp(515) : see reference to class template instantiation 'boost::archive::detail::pointer_oserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_oarchive 1> ] 1> include\boost\serialization\export.hpp(86) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer &boost::archive::detail::instantiate_pointer_oserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(82) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::o::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::o' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_oarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::mpl::list4boost::archive::xml_oarchive,boost::archive::xml_iarchive,boost::archive::po..., 1> T=B 1> ] 1> include\boost\serialization\export.hpp(122) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::mpl::list4boost::archive::xml_oarchive,boost::archive::xml_iarchive,boost::archive::po..., 1> T=B 1> ] 1>include\boost\serialization\access.hpp(109) : error C2664: 'void B::serialize(boost::archive::polymorphic_iarchive &,const unsigned int)' : cannot convert parameter 1 from 'boost::archive::xml_iarchive' to 'boost::archive::polymorphic_iarchive &' 1> include\boost\serialization\serialization.hpp(81) : see reference to function template instantiation 'void boost::serialization::access::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\serialization.hpp(140) : see reference to function template instantiation 'void boost::serialization::serialize (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(164) : see reference to function template instantiation 'void boost::serialization::serialize_adl (Archive &,T &,const unsigned int)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(157) : while compiling class template member function 'void boost::archive::detail::iserializer ::load_object_data(boost::archive::detail::basic_iarchive &,void *,const unsigned int) const' 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(175) : see reference to class template instantiation 'boost::archive::detail::iserializer
' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\archive\detail\iserializer.hpp(174) : while compiling class template member function 'const boost::archive::detail::basic_iserializer &boost::archive::detail::pointer_iserializer ::get_basic_serializer(void) const' 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_iarchive 1> ] 1> include\boost\archive\detail\iserializer.hpp(546) : see reference to class template instantiation 'boost::archive::detail::pointer_iserializer ' being compiled 1> with 1> [ 1> T=B, 1> Archive=boost::archive::xml_iarchive 1> ] 1> include\boost\serialization\export.hpp(78) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_iserializer &boost::archive::detail::instantiate_pointer_iserializer (Archive *,T *)' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(74) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::i::invoke(void)' 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(105) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ::i' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(89) : while compiling class template member function 'void boost::archive::detail::export_impl::archive ::instantiate(void)' 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(116) : see reference to class template instantiation 'boost::archive::detail::export_impl::archive ' being compiled 1> with 1> [ 1> Archive=boost::archive::xml_iarchive, 1> T=B 1> ] 1> include\boost\serialization\export.hpp(115) : while compiling class template member function 'void boost::archive::detail::export_impl::for_each_archive ::instantiate(void)' 1> with 1> [ 1> ASeq=boost::mpl::list3boost::archive::xml_iarchive,boost::archive::polymorphic_oarchive,boost::arc..., 1> T=B 1> ] 1> include\boost\serialization\export.hpp(122) : see reference to class template instantiation 'boost::archive::detail::export_impl::for_each_archive ' being compiled 1> with 1> [ 1> ASeq=boost::mpl::list3boost::archive::xml_iarchive,boost::archive::polymorphic_oarchive,boost::arc..., 1> T=B 1> ] ----- Original Message ----- From: "Robert Ramey"
Newsgroups: gmane.comp.lib.boost.user Sent: Tuesday, November 07, 2006 2:42 AM Subject: Re: Serialization with polymorphic_*_archiveandBOOST_CLASS_EXPORT Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either.
Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.?
Though its beside the point, I would also make your example more portable wit the fillowing changes:
... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); }
struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... };
template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); }
BOOST_CLASS_EXPORT(B)
int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
----- Original Message ----- From: "Robert Ramey"
Newsgroups: gmane.comp.lib.boost.user Sent: Tuesday, November 07, 2006 2:42 AM Subject: Re: Serialization with polymorphic_*_archiveandBOOST_CLASS_EXPORT Anders Wang Kristensen wrote:
Hi,
I'm having trouble serializing through the a pointer to the base as shown below. If I manually register class B with template register_type<B>() it works, but I would rather use the BOOST_CLASS_EXPORT macros since that is more convenient.
Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.
I don't think that's true. Maybe you mean it assums you are using member serialize functions - but that won't be true either.
Basically I think your example should function. Where does it fail? Does it throw and exception? where? does the assert fail?, etc.,etc. Also what compiler, os, version of boost etc.?
Though its beside the point, I would also make your example more portable wit the fillowing changes:
... struct A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... }; template<class Archive> void A::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_NVP(data); }
struct B : public A { template<class Archive> void serialize(Archive & ar, const unsigned int file_version); ... };
template<class Archive> void B::serialize(Archive & ar, const unsigned int file_version){ ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); ar & BOOST_SERIALIZATION_NVP(foo); }
BOOST_CLASS_EXPORT(B)
int main(int argc, const char* argv[]){ boost::shared_ptr<A> a(new B); boost::shared_ptr<A> a1; std::stringstream ss; { boost::archive::polymorphic_text_oarchive oa(ss); //oa.template register_type<B>(); oa << BOOST_SERIALIZATION_NVP(a); } try{ boost::archive::polymorphic_text_iarchive ia(ss); //ia.template register_type<B>(); ia >> BOOST_SERIALIZATION_NVP(a1); } catch(...){ ... } B* x = dynamic_cast(a.get()); B* y = dynamic_cast(b.get());
assert(*x == *y); return 0; }
participants (2)
-
Anders Wang Kristensen
-
Robert Ramey