[C++-sig] trouble using vector of shared_ptr using boost python

Jim Bosch talljimbo at gmail.com
Fri Jan 13 17:15:27 CET 2012


On 01/13/2012 03:13 AM, helferthomas at free.fr wrote:
> Hi,
>
> In the following test, we wrap a class "A" (and the class shared_ptr<A>  using boost python facilites) and a function returning a vector of shared_ptr of this class (see test.cxx). The class "A" provides a display method. Creating an object of type "A" and calling the display method just works as expected.
>
> The trouble (see test.py) is that calling the "display" method when iterating over the elements of the vector (of shared_ptr...) leads to the following message :
>
> Traceback (most recent call last):
>    File "test.py", line 5, in<module>
>      i.display()
> Boost.Python.ArgumentError: Python argument types in
>      A.display(A)
> did not match C++ signature:
>      display(A {lvalue})
>
> Can anybody tell me if I misused the library ?
>

It looks like vector_indexing_suite isn't smart enough to figure out it 
shouldn't return a proxy object for a vector of shared_ptr.

This snippet should solve your problem:

class_<vector<shared_ptr<A> > >("AVector")
   .def(vector_indexing_suite<vector<shared_ptr<A> >, true >());
                                                      ^^^^

That sets the vector_indexing_suite's NoProxy template parameter to 
"true".  I believe those proxies only exist to keep the container's 
elements from becoming dangling references in Python, which isn't 
necessary for containers of shared_ptr.

Jim


More information about the Cplusplus-sig mailing list