boostorg / python

Boost.org python module
http://boostorg.github.io/python
Boost Software License 1.0
472 stars 201 forks source link

boost::python fails to correctly handle constness of wrapped classes #205

Open rhaschke opened 6 years ago

rhaschke commented 6 years ago

I want to wrap C++ code that only provides const access to a class Foo:

struct Foo {};
std::shared_ptr<const Foo> producer();

Wrapping Foo as usual:

BOOST_PYTHON_MODULE(foo) {
    boost::python::class_<Foo, std::shared_ptr<Foo>, boost::noncopyable>("Foo", boost::python::no_init);
    boost::python::def("producer", &producer);
}

compiles fine, but complains at runtime - obviously - about a missing type converter: No to_python (by-value) converter found for C++ type: std::shared_ptr<Foo const>

Alternatively trying:

BOOST_PYTHON_MODULE(foo) {
    // const holder
    boost::python::class_<Foo, std::shared_ptr<const Foo>, boost::noncopyable>("Foo", boost::python::no_init);
    // const class + holder
    boost::python::class_<const Foo, std::shared_ptr<const Foo>, boost::noncopyable>("Foo", boost::python::no_init);
}

yields compile-time errors about constness issues. Isn't there a solution available that respects constness?

allopislozano commented 1 year ago

Same problem here , was any solution found?

rhaschke commented 1 year ago

I switched to pybind11, which is a more modern and faster wrapper.