Re: [gtkmm] GCC 3.3.1 breaks gtkmm

On Thursday 17 July 2003 7:27 pm, Barnaby Gray wrote:
> On Thu, Jul 17, 2003 at 10:03:56AM +0200, Erich Schubert wrote:
> > > > > >
> > >
> > > Please help me to make sense of this. Are you saying that the
> > > "static_cast chain" problem in gnome bug 117494 already has a solution
> > > for debian? If so, I would like to know what it is.
> >
> > I posted it in the bug report already.
> > I havn't tested it yet, but i think a reinterpret_cast is what you want.
> > It compiles fine that way, and my own applications work (but they aren't
> > using trees)
> No, reinterpret_case is almost never sure the right solution. Checking
> Stroustrup reveals the problem is that static_cast can't be used on
> polymorphic classes (see pp.413 for 3rd edition). static_cast is dumb,
> and doesn't understand RTTI, so can't correctly downcast polymorphic
> classes - a dynamic_cast must be used for this case.

I do not think this is right.  I think you are confusing virtual inheritance 
where multiple inheritance is involved and polymorphism (virtual methods).  
You can use static_cast for polymorphic classes (but the "goodness" of the 
cast is not checked, so should only be used where you know the origin of the 
object concerned).  dynamic_cast is required to the exclusion of static_cast 
only where you have multiple inheritance involving a virtual base class 
(which in addition and to add to the confusion must also have a virtual 
method so that it has the necessary type information available, as with all 
dynamic_casts).  Incidentally, if a dynamic_cast will work, the standard also 
allows a C style cast to be used with multiple inheritance.

Unless both the following are satisfied:

(a) the cast is _from_ a pointer or reference which is a pointer or reference 
to a _virtual_ base class, and

(b) the cast is up the inheritance heirarchy to a derived class which inherits 
the base class virtually from more than one parent,

then the use of static_cast is valid C++ code and the problem is a gcc bug and 
not a gtkmm bug.  I have not examined the code in question to check whether 
that is the case.

I agree that if it is a gtkmm bug, we should not be trying to get round it by 
using reinterpret_cast, because whilst the result will technically be 
compilable C++, the pointer will be unusable where casting from  a virtual 
base clase having multiple inheritance.  And if it is a gcc bug (as I 
strongly suspect since otherwise the gtkmm code should never have worked) we 
should await a fix to gcc rather than using a hack like reinterpret_cast.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]