Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2005-06-10 10:52:12


void_cast_register should be necessary only in the most unusual cases. It
should happen automatically as part of the "base_object" wrappers call.
Also it is used void_cast_register<Derived, Base> which is the opposite
you've of what you've done below. So I would remove it from your example.

The export should make up for this.

There are number of code paths related to borland 5.64 and borland 5.51. I
don't know anything about borland 6. void_cast/export/template
instantiation are areas which many compilers have exhibited unexpected
behavior. So its quite possible that the code as is can't accomodate BC 6.
As far as I know - we've never seen borland 6 or tested it.

So if you want to use any part of boost with a new compiler whose ancestor
has a history of special requirements, you should be prepared to run the
test suite for that compiler. Please do this and post the results.

Robert Ramey

Richard Jennings wrote:
> I am using Boost 1.32.0 with Borland C++ Builder 6 and attempting to
> serialize an object via a pointer to a base class but I am getting a
> runtime exception of "unregistered void cast".
>
> Having read the page in the documentation on Runtime Casting, I
> thought I should be able to use
> boost::serialization::void_cast_register<Base, Derived>(0,0); to
> associate the base and derived classes, but I can't get it to work.
> If I use the alternate method (to serialize the base class and
> provide a do-nothing serialize method in the base class) then it
> works fine. Any ideas what's wrong?
>
> The program below demonstrates the problem. Class A uses the
> serialize base class method and that's ok while class B uses the
> void_cast_register method and an exception is thrown when it's
> serialized:
> -----
> #include <iostream>
> #include <boost/filesystem/fstream.hpp>
> #include <boost/filesystem/path.hpp>
> #include <boost/archive/xml_oarchive.hpp>
> #include <boost/serialization/export.hpp>
> #include <boost/serialization/base_object.hpp>
>
> #if defined(_DEBUG)
> # pragma link "libboost_serialization-bcb-mt-d-1_32.lib"
> #else
> # pragma link "libboost_serialization-bcb-mt-1_32.lib"
> #endif
>
>
> class Base_c
> {
> public:
> virtual void SomeMethod() = 0;
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {}
> };
> BOOST_IS_ABSTRACT(Base_c)
>
> class A : public Base_c
> {
> public:
> A() : m_Id("A"){};
> void SomeMethod(){};
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {
> /* boost::serialization::void_cast_register<Base_c, A>(0,0); */
> ar & boost::serialization::make_nvp("Base",
> boost::serialization::base_object<Base_c>(*this));
> ar & BOOST_SERIALIZATION_NVP(m_Id);
> }
> std::string const m_Id;
> };
> BOOST_CLASS_EXPORT(A);
>
> class B : public Base_c
> {
> public:
> B() : m_Id("B"){};
> void SomeMethod(){};
>
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {
> boost::serialization::void_cast_register<Base_c, B>(0,0);
> /* ar & boost::serialization::make_nvp("Base",
> boost::serialization::base_object<Base_c>(*this)); */
> ar & BOOST_SERIALIZATION_NVP(m_Id);
> }
> std::string const m_Id;
> };
> BOOST_CLASS_EXPORT(B)
>
> main(int, char**)
> {
>
> boost::filesystem::ofstream
> OfStream(boost::filesystem::path("D:\\Temp\\SerialisationText.xml",
> boost::filesystem::native));
> boost::archive::xml_oarchive Archive(OfStream);
>
> try
> {
> Base_c *pBase, *pBase2;
> pBase = new A();
> pBase2 = new B();
>
> Archive & boost::serialization::make_nvp("AByBasePointer", pBase);
> std::cout << "Serialised A ok\n";
> Archive & boost::serialization::make_nvp("BByBasePointer",
> pBase2); std::cout << "Serialised B ok\n";
> }
> catch (std::exception& E)
> {
> std::cout << "Exception: " << E.what();
> }
> }
> -----
> The output is:
> Serialised A ok
> Exception: unregistered void cast


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net