[C++-sig] smart pointers, polymorphism

Dusty Leary dleary at ttlc.net
Wed Jul 9 23:54:27 CEST 2003


I am converting a semi-large api over to boost.python.

We have been using boost::intrusive_ptr for some time now.  (I can't
remember exactly why).  I tried switching before mailing the list.  With
both intrusive_ptr and shared_ptr, I am running into "conversion to base"

I have quite a few abstract interfaces.  Each one defines an overloaded
templated static "cast" function, to make it easy to cast references.  (e.g.
foo::bar::MyInterface::cast(someReference)), which handles using
dynamic_cast or boost::shared_dynamic_cast, etc...

With my previous home-grown python interfaces (stubs generated from an idl),
the binding for the cast function would just extract the anonymous pointer
from the PyObject (after verifying that the PyObject's type was one of our
interfaces), static_cast it to IObject* (even though it might not implement
IObject), then dynamic_cast it to the interface type (in this case,

I am not sure if static_casting it to some possibly unrelated polymorphic
type, and then dynamic_casting it to our type is valid.  (Although I was
sure it would always be a polymorphic type).  It worked, at least for my
current environment.

Anyway, I can't get the casting to work properly.
I am always getting "TypeError: bad argument for built-in operation."...
presumably, boost.python can't find a valid overload.

I have tried forcing all interfaces in the system to extend a common base
interface (IObject), with no luck.  (so, I define "static shared_ptr<T>
cast_iobject(IObject*)" in each interface class.  Even though I have "IFoo :
public IObject", a shared_ptr<IFoo> can't be passed into my cast function)

Either, I need to special case this function, accepting PyObjects directly
and doing the static_cast -> dynamic_cast thing like I did in my previous
bindings, or I need to somehow make the compiler and/or boost.python infer
the proper behaviour.  With all of the boost::python::(converter, holder,
pointee) stuff, that might be possible, but I can't make heads or tails of

What is the proper way?

More information about the Cplusplus-sig mailing list