Skip to content

How do I capture numpy and numpy-like buffer with template arguments using the same function? #5066

Closed Answered by anand-avinash
anand-avinash asked this question in Q&A
Discussion options

You must be logged in to vote

I found the solution to my query. It was simple actually! The key is to simply change the prototype of the lambda function and rely on the overloading within python module. Finally it would look like this:

auto lambda = []<typename dfloat>(py::buffer &phi) {
  py::buffer_info phi_info = phi.request();
  dfloat *phi_ptr = reinterpret_cast<dfloat *>(phi_info.ptr);
  my_func(phi_info.size, phi_ptr);
};

PYBIND11_MODULE(my_module, m) {
  m.def("my_func", [](py::array_t<double> &phi) {
    lambda.template operator()<double>(phi);
  });
  m.def("my_func", [](py::array_t<float> &phi) {
    lambda.template operator()<float>(phi);
  });

  m.def("my_func", [](foo::array_t<double> &phi) {
    lambda.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by anand-avinash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant