[C++-sig] Re: vector<foo*> -> list, preserving identity

Jacek Generowicz jacek.generowicz at cern.ch
Mon Dec 1 10:39:48 CET 2003

Raoul Gough <RaoulGough at yahoo.co.uk> writes:

> Jacek Generowicz <jacek.generowicz at cern.ch> writes:


> > How should I write a converter for the std::vector<foo*>, in order to
> > preserve the identity of the foos ?
> >
> > (In the library I am wrapping, the return type of makefoos is actually
> > const std::vector<const foo*>&)
> I'm not sure how you would handle this, except by switching to
> boost::shared_ptr instead of plain pointers. You would end up with
> something like this:
> std::vector<boost::shared_ptr<const foo> > makefoos(int n) {
>   // ...
> }

Unfortunately "makefoos" is part of a library over which I have no
control, so I cannot make it return shared pointers.

>   class_<foo, boost::shared_ptr<foo> >("foo", no_init)
>     .def("pointer", &foo::pointer);
>   class_<std::vector<boost::shared_ptr<foo const> > > ("foo_vector")
>     .def (SOMETHING<std::vector<boost::shared_ptr<foo const> >());
>   def ("makefoos", makefoos);
> }
> Where SOMETHING would use either the current indexing suite (available
> from CVS)

Once again, I am obliged to use the local offical Boost version
(1.30.2, at the moment), as that is what my users have. The Boost
version I use is beyond my control.

How does reference_existing_object work?  Using it as the return value
policy for functions returning foo* preserves identity just fine.
It's just when I get a whole vector of them and try to put them in a
Python list that I don't seem to manage to avoid copying the objects

More information about the Cplusplus-sig mailing list