Skip to content Skip to sidebar Skip to footer

Using Unique_ptr With Boost Python - Boost::shared_ptr Works But Unique_ptr Doesnt

This may be the same issue as Boost Python No to_python for std::unique_ptr However, i haven't seen a response and it's not clear if this is a 'boost-python' issue or due to my par

Solution 1:

Unfortunately, at the moment, it is not possible to expose a std::unique_ptr using boost::python, as it requires move semantics, and these are not supported by boost::python yet (more details here). Moreover, you should not derive from smart pointers, because it is not a good practice and they do not have virtual destructors (apart from many other reasons)

Saying that, you have quite a few other options, some of which are:

  • use std::auto_ptr as a data member, which is well supported by boost::python (assuming your class is made non-copyable). However, an auto_ptr cannot hold an array (it would not call the correct variant of delete()).
  • use boost::shared_ptr as a data member, and hide its use in the API. However, please note that boost::shared_ptrcannot not hold array data either, for the same reason as above.
  • wrap an std::vector<boost::shared_ptr<>>, which will work with boost::shared_ptr out of the box using vector_indexing_suite.
  • ... and many others.

However, there are many other ways, and I would personally recommend to redesign your code to use some of the more generally accepted coding and python-wrapping practices from boost::python examples.

Post a Comment for "Using Unique_ptr With Boost Python - Boost::shared_ptr Works But Unique_ptr Doesnt"