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

David Abrahams dave at boost-consulting.com
Mon Aug 15 16:36:46 CEST 2005


Scott McKay <skottmckay at gmail.com> writes:

>> 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.
>
> Sorry - was trying to keep it to the essential details. 
>
> I tried adding the boost:ref() call and that corrects the copy and
> provides a reference to the vector in the python code, however it
> fails when exiting from the python class' (TestExtendedFactory)
> generate method 

Actually it's exiting the call<Factory&>( ... ) where we have the
problem.

> with
>
> 'TypeError: No registered converter was able to extract a C++
> reference to type Factory from this Python object of type
> TestExtendedFactory'

>From the code you posted it seems pretty straightforward to me.  The
Python generate() call is creating a Python TestExtendedFactory
object, which is derived from the Python wrapper for BasicFactory, so
it contains a C++ BasicFactory.  Does it contain a C++ Factory object?
not unless BasicFactory is derived from Factory and, from
Boost.Python's POV, not unless its class_ names Factory as one of its
bases<...>.  You didn't show the declaration of BasicFactory, so it's
hard to say much about it.


> Do I need to register a custom converter, or is there some other call
> I'm missing to connect the python object with its base class'
> converters?
>
> Thanks for you help so far.
>
> S
>
> Here's a fuller picture:
>
> --- boost python code, largely created by Pyste
>
> --- Abstract base class for the Factory 
>
> struct Factory_Wrapper: Factory, wrapper< Factory >
> {
>     Factory_Wrapper(const Factory& p0):
>         Factory(p0) {}
>
>     Factory_Wrapper():
>         Factory() {}
>
>     Factory& generate(std::vector<smart_ptr<X>>& p0) {

P.S. is this actually the code you compiled?  All compilers I know of
will choke on the appearance of ">>" above without an intervening
space.

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




More information about the Cplusplus-sig mailing list