Boost.Python: How to expose std::unique_ptr

Viewed 4134

I am fairly new to boost.python and trying to expose the return value of a function to python.

The function signature looks like this:

 std::unique_ptr<Message> someFunc(const std::string &str) const;

When calling the function in python, I get the following error:

TypeError: No to_python (by-value) converter found for C++ type: std::unique_ptr<Message, std::default_delete<Message> >

My function call in python looks like this:

a = mymodule.MyClass()
a.someFunc("some string here") # error here

I tried to expose the std::unique_ptr but just cant get it to work.. Does someone know how to properly expose the pointer class? Thanks!

Edit: I tried the following:

class_<std::unique_ptr<Message, std::default_delete<Message>>, bost::noncopyable ("Message", init<>())

;

This example compiles, but I still get the error mentioned above. Also, I tried to expose the class Message itself

class_<Message>("Message", init<unsigned>())

        .def(init<unsigned, unsigned>()) 
        .def("f", &Message::f)
;
4 Answers
Related