Converting a dictionary to unordered_map (without copying)
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
I am trying to pass a py::dict to a funtion. Internally, inside C++, I want to use unordered_map.
However when I pass a dict of type (int : int), it gives an error : Unable to cast Python instance to C++ type (compile in debug mode for details). Need some help here. I do not want the copy thing to happed and thats why I am using opaque.
PYBIND11_MAKE_OPAQUE(std::unordered_map<int, int>);
namespace py = pybind11;
std::unordered_map<int, int> modify_map(py::dict dictionary) {
std::unordered_map<int, int> mapp = dictionary.cast<std::unordered_map<int , int>>();
// DO some stuff here
return mapp;
}
PYBIND11_MODULE(SYCL, m) {
m.def("mod", &modify_map);
py::bind_map<std::unordered_map<int, int>>(m, "IntMap");
}```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the modify_map function and the PYBIND11_MODULE definition shown in the issue, then read the relevant pybind11 dict casting and bind_map documentation or tests. Reproduce the debug-mode cast failure with the provided int-to-int example and determine whether the requested no-copy conversion is supported; done means a documented, verified resolution or a clearly scoped project change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100