How to bind numpy arrays in-place when given as STL vectors? #145
-
I have C++ header to bind to Python:
And I have done following wrapper:
Which compiles but gives segmentation fault. There must be some other means to use the nb:ndarray for std::vector but I couldn't find this on documentation? I have previously used Cython for similar with memoryviews but I'm new to pybind or nanobind. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You cannot just reference-cast the
I suggest that you review the nanobind documentation, including how to iterate over the contents of an ndarray. You will likely have to update the interface of your |
Beta Was this translation helpful? Give feedback.
You cannot just reference-cast the
nb::ndarray
to astd::vector<uint8_t>
. They are different types and have an incompatible layout. Use astatic_cast
if you feel unsure. This would have resulted in an error message in this case.std::vector
can never be used as a view of some other pre-existing data, since it wants to own the underlying storage.I suggest that you review the nanobind documentation, including how to iterate over the contents of an ndarray. You will likely have to update the interface of your
bit2byte
functions. Another option is to not use anndarray
but usebind_vector.h
to expose this type. Again, please refer to the documentation for detail.