Serialization, multiple inheritance, bad cast at smart_cast.hpp:202
Dear all I use the serialization library of boost, and I am very satisfied with it except one annoying issue. It turns out, that in many cases (not always!!!) when there is a multiple inheritance structure in the class hierarchy I am trying to serialize I get a std::bad_cast exception at line 202 in smart_cast.hpp. The corresponding boost code is: template<class U> static T cast(U * u){ T tmp = dynamic_cast<T>(u); #ifndef NDEBUG if ( tmp == 0 ) throw std::bad_cast(); #endif return tmp; } I have debugged it, and I could not really find out what the problem was. For example I have a class hierarchy similar to this: class A class B : public A class C : public virtual B class D : public virtual B class E : public C, public D so, a classic diamond inheritance which the serialization lib is supposed to be able to tackle with. Now, if I have a piece of code like this boost::serialization::xml_oarchive oa(somestream); A *a = new E(); oa << BOOST_SERIALIZATION_NVP(a); it throws exception at the aforementioned line. Examining the template substitutions, template class T is substituted with const B* and template class U is substituted with const A. So, the program should do a downcast on a const A* to const B* which is ought to be possible. The funny thing is, that the error does not always appear! I could put together a simple example code demonstrating this effect, but I am not sure that attachments to this mail get through the list server, so anybody kind enough to take a look at this problem can ask me to send him/her the example code. I have tested the example code both with Intel C++ compiler v8.1 and G++, so I concluded this may not be a compiler error but rather some boost flaw. Any help is warmly welcome and thanked in advance! Cheers Zoltan
Zoltan Szatmary wrote:
Dear all
I use the serialization library of boost, and I am very satisfied with it except one annoying issue. It turns out, that in many cases (not always!!!) when there is a multiple inheritance structure in the class hierarchy I am trying to serialize I get a std::bad_cast exception at line 202 in smart_cast.hpp. The corresponding boost code is:
template<class U> static T cast(U * u){ T tmp = dynamic_cast<T>(u); #ifndef NDEBUG if ( tmp == 0 ) throw std::bad_cast(); #endif return tmp; }
I have debugged it, and I could not really find out what the problem was. For example I have a class hierarchy similar to this:
class A class B : public A class C : public virtual B class D : public virtual B class E : public C, public D
so, a classic diamond inheritance which the serialization lib is supposed to be able to tackle with. Now, if I have a piece of code like this
boost::serialization::xml_oarchive oa(somestream); A *a = new E(); oa << BOOST_SERIALIZATION_NVP(a);
it throws exception at the aforementioned line. Examining the template substitutions, template class T is substituted with const B* and template class U is substituted with const A. So, the program should do a downcast on a const A* to const B* which is ought to be possible.
Your example does indicate whether A has any pure virtual functions or not. The dynamic down cast will only function if A is polymorphic - that is has at least one virtual function. Soooo, I would expect this work if A has at least one virtual function and I would expect it to fail if has no virtual function. Double check this and get back to us.
The funny thing is, that the error does not always appear! I could put together a simple example code demonstrating this effect, but I am not sure that attachments to this mail get through the list server, so anybody kind enough to take a look at this problem can ask me to send him/her the example code. I have tested the example code both with Intel C++ compiler v8.1 and G++, so I concluded this may not be a compiler error but rather some boost flaw.
Any help is warmly welcome and thanked in advance!
Cheers
Zoltan
participants (2)
-
Robert Ramey
-
Zoltan Szatmary