[C++-sig] copy constructors and multiple instances
Hans Meine
hans_meine at gmx.net
Fri Sep 2 10:21:06 CEST 2011
Am Donnerstag, 1. September 2011, 22:52:56 schrieb Jim Bosch:
> Yup. With boost::shared_ptr, you don't even need to include it as a
> template argument of class_, if you instead add the line:
>
> boost::python::register_ptr_to_python< boost::shared_ptr<Scene> >();
>
> This allows you to return shared_ptr to Python, but because a
> boost::shared_ptr can use an arbitrary deleter, Boost.Python can convert
> any wrapped Scene object (even one that doesn't hold a
> shared_ptr<Scene>) into a shared_ptr<Scene> with correct reference
> counting.
>
> If you do include boost::shared_ptr<Scene> in the class_ template
> arguments, all Scene objects constructed in Python will be inside a
> shared_ptr, but because of the above, that isn't necessary unless you
> have functions that need to reset the shared_ptr itself.
Jim, this is the best write-up on the shared_ptr support I read _in all the
years_. The above matches my current mental model of the topic, but it took
me a long time to understand this. The custom deleter part (rarely mentioned
and probably not well known even among shared_ptr users) is absolutely crucial
information to people with a solid C++ (and Python) background trying to
understand BPL behavior in this respect. Looking forward to you improving the
docs. :-)
Best,
Hans
More information about the Cplusplus-sig
mailing list