[C++-sig] preventing a copy of the vector when using the vector_indexing_suite

David Abrahams dave at boost-consulting.com
Wed Aug 10 19:30:36 CEST 2005


Scott McKay <skottmckay at gmail.com> writes:

> Is there a way to set the policy for the vector_indexing_suite to not
> create a copy of the vector itself (not the internal elements) in
> python?
>
> I have a vector being held by the vector indexing suite defined like...
>
> class_< std::vector< smart_ptr<X> > > ( "vector_X" )
>        .def( vector_indexing_suite< std::vector< smart_ptr<X> > >() )
> ;
>
> I then have a python extension of a c++ class, which has a method that
> takes a reference to std::vector< smart_ptr<X> > and populates it by
> calling the c++ parent class.
>
> If I do something like the following...
>
> class TestExtendedFactory( x.BasicFactory ):
>     def __init__( self ):
>         super( TestExtendedFactory, self ).__init__()
>
>     def generate( self, vectorOfSmartPtrXs ):
>         # first generate with our parent
>         super( TestExtendedFactor, self ).generate( vectorOfSmartPtrXs )
>         ...
>
> the entries added by the call to BasicFactory.generate() show up just
> fine in python, but don't make it back to the original c++ caller of
> TestExtendedFactory.generate as it is using a copy of the vector not
> the original one.

You've left out a lot of detail, but if you want to pass something
from C++ to Python by reference, you need to wrap it in boost::ref( )
Otherwise, Boost.Python will take the safe route and pass it by value.

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list