-
This does not compile. m.def("create_2d", [](size_t rows, size_t cols, bool integral) {
if (integral) {
int *data = new int[rows * cols];
for (size_t i = 0; i < rows * cols; ++i) data[i] = (int)i;
// Delete 'data' when the 'owner' capsule expires
nb::capsule owner(data, [](void *p) noexcept { delete[] (int *)p; });
return nb::ndarray<nb::numpy, int, nb::ndim<2>>(
/* data = */ data,
/* shape = */ {rows, cols},
/* owner = */ owner);
} else {
float *data = new float[rows * cols];
for (size_t i = 0; i < rows * cols; ++i) data[i] = (float)i;
// Delete 'data' when the 'owner' capsule expires
nb::capsule owner(data, [](void *p) noexcept { delete[] (float *)p; });
return nb::ndarray<nb::numpy, float, nb::ndim<2>>(
/* data = */ data,
/* shape = */ {rows, cols},
/* owner = */ owner);
}
});
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
That's a C++ error that is unrelated to nanobind. The following does not compile:
with a similar error message:
|
Beta Was this translation helpful? Give feedback.
-
If you write a C++ template instead, see https://nanobind.readthedocs.io/en/latest/functions.html#function-templates |
Beta Was this translation helpful? Give feedback.
Oh, maybe you want to return an
nb::object
? If so, usenb::cast
.https://nanobind.readthedocs.io/en/latest/api_core.html#casting
return nb::cast(nb::ndarray<nb::numpy, float, nb::ndim<2>>(.....))