fix: can't bind method with pointer argument.

This commit is contained in:
lisong 2025-04-26 11:09:05 +08:00
parent 0528f394e3
commit 7b303eee2b
2 changed files with 11 additions and 2 deletions

View File

@ -280,7 +280,7 @@ void invoke(Fn&& fn,
if constexpr(is_member_function_pointer) { if constexpr(is_member_function_pointer) {
// helper function to unpack the arguments to call the member pointer // helper function to unpack the arguments to call the member pointer
auto unpack = [&](class_type_t<underlying_type>& self, auto&... args) { auto unpack = [&](class_type_t<underlying_type>& self, auto&&... args) {
return (self.*fn)(args...); return (self.*fn)(args...);
}; };

View File

@ -45,6 +45,12 @@ public:
std::string stringfy() const { std::string stringfy() const {
return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")"; return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")";
} }
void set_pointer(Point* p) {
this->x = p->x;
this->y = p->y;
this->z = p->z;
}
}; };
struct Line { struct Line {
@ -60,7 +66,8 @@ TEST_F(PYBIND11_TEST, class) {
.def_readwrite("x", &Point::x) .def_readwrite("x", &Point::x)
.def_readwrite("y", &Point::y) .def_readwrite("y", &Point::y)
.def_property("z", &Point::get_z, &Point::set_z) .def_property("z", &Point::get_z, &Point::set_z)
.def("stringfy", &Point::stringfy); .def("stringfy", &Point::stringfy)
.def("set_pointer", &Point::set_pointer);
py::exec(R"( py::exec(R"(
p = Point() p = Point()
@ -74,6 +81,8 @@ p.x = 10
p.y = 20 p.y = 20
p.z = 30 p.z = 30
assert p.stringfy() == '(10, 20, 30)' assert p.stringfy() == '(10, 20, 30)'
p.set_pointer(Point(4,5,6))
assert p.stringfy() == '(4, 5, 6)'
)"); )");
py::class_<Line> line(m, "Line"); py::class_<Line> line(m, "Line");