Binding STL containers containing custom types #111
-
Thank you for making this library! I am taking my first steps in it right now (without having much pybind11 experience), and I am still trying to understand some of the basic concepts. Right now, I am trying to port a pybind11 extension to nanobind. It's almost a drop-in replacement of typedef std::map<std::string, Foo> FooMap; Now I guess I could get this to work by adding the But say I do not care about a custom PS: I am in fact registering my |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can simply include The main disadvantage of such an approach is performance when dealing with large maps. Every time you call a method that takes such a map as an input, all elements will be converted. Perhaps that's what you will need to do anyway, and then it does not matter. But if the function only accesses, say, 1 element in a map with millions of entries, then the There really isn't anything magic in |
Beta Was this translation helpful? Give feedback.
You can simply include
nanobind/stl/map.h
and either bind yourFoo
type usingnb::class_<>
or implement a custom caster for it. This should be enough to get an automatic bidirectional conversion between the STL type anddict[str, Foo]
.The main disadvantage of such an approach is performance when dealing with large maps. Every time you call a method that takes such a map as an input, all elements will be converted. Perhaps that's what you will need to do anyway, and then it does not matter. But if the function only accesses, say, 1 element in a map with millions of entries, then the
py::bind_map
approach from pybind11 will be superior.There really isn't anything magic in
py::bind_map
tha…