Currently I'm trying to convert a py::dict
into its counterpart C++
s std::map
. Trying to use the
automatic conversion like this fails :
#include <pybind11/stl.h>
namespace py = pybind11;
using namespace py::literals;
...
py::dict py_kwargs = py::dict("number1"_a = 5, "number2"_a = 42);
auto cpp_kwargs = py_kwargs.cast<std::map<int, int>>();
with an exception stating :
Unable to cast Python instance of type <class 'dict'> to C++ type 'std::map<int,int,std::less<int>,std::allocator<std::pair<int const ,int> > >'
What am I missing here?
Also on a side note, how should I go about the situation , where the python dictionary has different key types, for example like :
py::dict py_kwargs = py::dict("name"_a = "World", "number"_a = 42);
How should I go about the conversion in such cases?