How to expose an opaque pointer in nanobind?

128 views Asked by At

I have a function in C++ that I am trying to expose in Python:

void f(T* t) {
  ...
}

and I am wrapping it like so:

m.def("f", f);

Nanobind doesn't like the pointer and complains with:

error: invalid use of incomplete type ‘struct T’ return nb_type_get(&typeid(Type), src.ptr(), flags, cleanup,

It seems I am trying to expose an opaque pointer, which in Boost.Python would be done like this:

BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(T);

and as far as I can see, the equivalent functionality in nanobind is:

NB_MAKE_OPAQUE(T);

The discussion here says to create a thin wrapper for T and it will work, but this doesn't seem to fix the issue.

Any help is much appreciated.

Thanks

1

There are 1 answers

0
got here On BEST ANSWER

this post explains that you create a nb::capsule to hold it, and then you pass capsule::data() which is the raw pointer to the function all within a lambda:

m.def("f", [](nb::capsule cap) { f((T*)cap.data()); });