Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-04-24 17:06:50

"Darin Adler" <darin_at_[hidden]> wrote in message
> On Wednesday, April 24, 2002, at 01:26 PM, Fernando Cacciola wrote:
> > The conversion from Derived* to Base* might require a pointer fixup.
> > This
> > requires RTTI. C++ doesn't force the use of RTTI unless dynamic_cast<>
> > is
> > used, so the implicit conversion could go wrong in exactly the same
> > way a
> > C-style cast could go wrong if the conversion requires pointer value
> > adjustment.
> >
> > IOWs, the template constructor acts exactly like a C-style cast between
> > arbitrary pointers. If dynamic_cast<> isn't required in this situation,
> > then
> > when is it?
> Conversion from Derived* to Base* might require a pointer fixup, but it
> *never* requires RTTI.
In the example, I was thinking of 'Base' as any class from which Derived is
derived from, including intermediate virtual base classes involving, for
instance, multiple inheritance.

I know as a fact that at least some version of the Borland compiler (don't
remember which one), didn't get the fixup right without RTTI if 'base' was
not a root type and the inheritance wasn't strictly simple.
Maybe this was just a compiler bug, but I've always been under the
impression that static casts were *guaranteed* to yield the correct value
only when downcasting to a root type or without multiple and virtual

I could be wrong because my reading of the std doesn't help me much on this
"a static_cast is always guaranteed to yield the correct value (or fail to
compile), when Base is a sub-object of Derived in any possible hirearchical
configuration, including virtual and multiple base classes"
Is that what you are saying?
Is that the specified behaviour?

What worries me is: 5.2.9/5:

..."An lvalue of type ``cv1 B'', where B is a class type, can be cast to
type ``reference to cv2 D'', where D is a
class derived (10) from B, if a valid standard conversion from ``pointer to
D'' to ``pointer to B'' exists (4.10),
cv2 is the same cv­qualification as, or greater cv­qualification than, cv1,
and B is not a virtual base class of
D. The result is an lvalue of type ``cv2 D.'' If the lvalue of type ``cv1
B'' is actually a sub­object of an object
of type D, the lvalue refers to the enclosing object of type D. Otherwise,
the result of the cast is undefined."

Notice both: 'B is not a virtual base class' and 'Otherwise, the result of
the cast is undefined'

> The purpose of polymorphic_cast is to help for conversions from Base* to
> Derived*. These types of conversions are never done automatically. They
> always require some kind of cast.
Yes, I know.

> This is not a problem with Peter's proposal.
Which can be viewed as a feature since we can't have a shared_ptr<Derived>
from Base*, something that sounds good to me.

Fernando Cacciola
Sierra s.r.l.

Boost list run by bdawes at, gregod at, cpdaniel at, john at