boost::python and shared_ptr

John Hunter jdhunter at
Tue Aug 21 14:55:37 CEST 2001

>>>>> "Alex" == Alex Martelli <aleax at> writes:

    Alex> What about adding (Python-side only maybe) another
    Alex> 'constructor' (factory, if you will) of SomeClass that's a
    Alex> template?  If your C++ accepts templated constructors,

    Alex> class SomeClass {
    Alex> ...
    Alex>   template <class Derived>
    Alex>   SomeClass(boost::shared_ptr<Derived>) {
    Alex>     // ... whatever ...
    Alex> }

I don't think I fully understand, but I'm intrigued.  Would this be
expected to work w/o the clone method I mentioned in the OP?  If not,
I don't see the advantage of this over what I have currently:

  typedef boost::shared_ptr<Base> PtrBase;
  SomeClass( PtrBase ) 

because the problem doesn't seem to be in the polymorphism ( I can use
my shared_ptr<Derived> in classes expecting a shared_ptr<Base>
polymorphically).  But to be perfectly honest, I don't know exactly
what the problem is.  I am assuming that the code freezing is due to
some reference counting problem that is arising with my use of the
clone method, and that this is causing a pointer to be deleted too
early, but I it could be many things.

But to get back to your example ... it makes me wonder if the example
you gave, or something similar ( a constructor that knows something
about how boost::python represents objects created in python and the
conversion functions to_python from_python ) could be used like

d = Derived()
s = SomeClass( d )  

w/o the use of the clone method.  In a nutshell, how to automagically
create a shared_ptr<Derived> from a Derived object constructed in
python.  Is your example expected do this?

BTW: I use gcc 2.95.2.  Does it support templated constructors?

Sorry for the meandering post.  

John Hunter

More information about the Python-list mailing list