Boost logo

Boost Users :

From: Zoltan Szatmary (szaki_at_[hidden])
Date: 2005-03-10 12:47:20


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


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