This is the mail archive of the egcs@egcs.cygnus.com mailing list for the EGCS project. See the EGCS home page for more information.


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

Re: dynamic_cast behavior differs from standard?



> > Keep in mind that I don't actually like the way dynamic cast is
> > defined by the standard.  However, this is a question about what what
> > the standard requires, not what it should have required.
> 
> The place to ask such question is on comp.std.c++, where you're likely
> to get answers from the people who actually wrote the standard.

It seems to me that David is actually right, and g++ is wrong, and
that he really found a bug.

In C++, a dynamic_cast must fail (i.e. return 0 or throw) if it
downcasts from a private base subobject. This is clearly indicated by
the wording David cited, as well as by the example in
[expr.dynamic.cast]/9. The example David presented is a reasonable
test case.

There is also a bug in the example in the standard, the line
     bp = dynamic_cast<B*>(&d); // fails
is ill-formed, instead of giving a failed dynamic_cast.

It seems, in tinfo.cc, the check

void* __class_type_info::
dcast (const type_info& desired, int is_public, void *objptr,
       const type_info *sub, void *subptr) const
{
  if (*this == desired)
    return objptr;

is a bit too optimistic: we must also check whether subptr is a public
base subobject of objptr. I'm not exactly sure how to do that, but it
seems all the pieces are there.

Regards,
Martin