I have a function that takes a std::shared_ptr, and I want to pass an object of Derived type to this function from python. Here\'s my class definitions:
stru
Boost.Python needs to be aware that a smart pointer to Derived
can be converted to a smart pointer to AbstractBase
. This can be accomplished by either of the following:
boost::shared_ptr
. Boost.Python has code to handle implicit conversions between boost::shared_ptr
s when their element_type
are hierarchical.Register an implicit conversion from std::shared_ptr<Derived>
to std::shared_ptr<AbstractBase>
via boost::python::implicitly_convertible. std::shared_ptr
meets the concept requirements for the implicitly_convertible
, so it only requires registering the conversion in the module definition:
implicitly_convertible<std::shared_ptr<Derived>, // Source
std::shared_ptr<AbstractBase> >(); // Target