Andrew Stitcher wrote:
On Tue, 2008-06-03 at 12:10 -0400, Alan Conway wrote:
...
I hope that someone could give some solution, otherwise, I will have to switch to the GNU compiler.

Don't do that, make the inheritance public.

No please don't do that

It's clear from googling around that the issue of dynamic_cast to private bases has confused lots of people, several compiler vendors and the C++ standards comittee. Even if it is technically legal (which I suspect it is not) we should not rely on dynamic_cast to private bases in our code.

We're not doing that at all.

Private inheritance is involved, but the case is simple and not one that
should be a problem.

We start with an AsynchIO (privately inherited from DispatchHandle as
it's not a DispatchHandle it is only implemented in terms of a
DispatchHandle to get the correct lifetime semantics.). The AsynchIO
object is passed as a DispatchHandle to the Dispatcher - this is the
only place where casting through the private inheritance is involved.

This DispatchHandle (which is publically inherited from PollerHandle) is
passed to the Poller as a PollerHandle.

Sometime later the PollerHandle is reconstituted from an epoll_wait and
passed back to the Dispatcher which then needs to cast it to a
DispatchHandle - this is what is failing, as we have a PollerHandle that
we know is a DispatchHandle (as we got it from one) but dynamic_cast
fails.

dynamic_cast operates on the run-time type of the object which is AsyncIO, and AsyncIO is not convertible to DispatchHandler, so I'm not surprised the cast fails.

I can understand that there might be a bug in the dynamic_cast
implementation, but as far as I can tell there should be no failure
here.

I'm wondering now if another cast might succeed.

A static cast would succeed, since the types are indeed statically related. It is only the run-time type that prohibits the cast.

In fact if compiled with NDEBUG boost::polymorphic_downcast *is* a static cast. The intent of boost::polymorphic_downcast is to use dynamic casting to catch errors in debug builds and more efficient static casts in production.

(Que the great NDEBUG debate... ;)

Reply via email to