I have a Boost.python .cpp that looks like this
BOOST_PYTHON_MODULE(my_module)
{
class_<MyCustomClass>("MyCustomClass")
.def("getDict",
&MyCustomClass::getDict, // This returns std::map<std::string, std::string>
bp::return_value_policy<bp::return_by_value>()
;
}
When I try to call MyCustomClass().getDict(), I get this error:
TypeError: No to_python (by-value) converter found for C++ type: std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >
It looks like Boost doesn't attempt to make this mapping for you by default. How do you let Boost know that it's okay to make that conversion?
At the moment I'm getting around the issue by having MyCustomClass::getDict return a boost::python::dict but, because this API is meant to be used by both C++ and Python users, it'd be much preferred to return the native std::map<std::string, std::string> for the C++ API and do a proper convert in the Python bindings.
I tried adding .def(map_indexing_suite<std::map<std::string, std::string> >()) to the class_ definition but it had no effect. I still get the same TypeError, as shown above.