[C++-sig] Boost.Python shared_ptr container gotchas

Roman Yakovenko roman.yakovenko at gmail.com
Tue Aug 1 11:16:08 CEST 2006


On 8/1/06, John Reid <j.reid at mail.cryst.bbk.ac.uk> wrote:
> Hi,
>
> I use boost::shared_ptr to manage lifetimes of objects. I've run into a
> couple of problems using these inside containers with Boost.Python. I
> thought I could share my experiences and perhaps get some useful
> feedback or help others avoid these problems.
>
> 1) Exposing a vector< shared_ptr< T > > through the indexing suite was
> giving me headaches. I couldn't work out why I couldn't access the
> elements from python. I kept getting a runtime error about no python
> class for boost::shared_ptr< T > even after I was registering the
> pointer type. It turns out that my problem was the vector indexing suite
> was trying to wrap a reference to the boost::shared_ptr< T > in a python
> object. I needed to set the NoProxy template parameter on
> vector_indexing_suite to true and that solved all those issues. Once I
> understood what was happening the answer was clear. Until that moment
> there was a lot of head-scratching. The documentation is factually
> comprehensive but I wonder whether it couldn't have more use cases that
> explain why certain things are necessary.

For next code:

struct item{};

std::vector< boost::shared_ptr< item > > get_items(){
    return std::vector< boost::shared_ptr< item > >();
}

pyplusplus will generate next registration code for the vector:

  bp::vector_indexing_suite< ::std::vector<boost::shared_ptr<item>,
std::allocator<boost::shared_ptr<item> > >, true >()

Do you see "true" at the end? :-)

pyplusplus tries to help a user as much as it can. It can serve as a
user guide and
as an inspector: http://tinyurl.com/mbd4r ( development version ).

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list