wjakob / nanobind

nanobind: tiny and efficient C++/Python bindings
BSD 3-Clause "New" or "Revised" License
2.14k stars 161 forks source link

[BUG]: Get std::bad_cast when a callback return a Python complex type velue #587

Closed luckymark closed 1 month ago

luckymark commented 1 month ago

Problem description

Get std::bad_cast when a callback return a Python complex type velue

Reproducible example code

test.py:
def callback():
    return complex(1.0,2.0)

def test_foo():
    foo(callback)

bind.cpp:
#include <nanobind/nanobind.h>
#include  <nanobind/stl/complex.h>
#include <nanobind/stl/function.h>

void foo(std::function<std::complex<double>()> func) {
    auto result = func();
}

NB_MODULE(lib_ext, module) {
    module.def("foo", &foo);
}
wjakob commented 1 month ago

Cannot reproduce. I think that there is a difference between the issue reported and the code to demonstrate it.

luckymark commented 1 month ago

I'am so sorry! After checking my code, it turned out that I had an idiot mistake. Thank you for your help