-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Regression when binding alias functions #991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This seems like it was walking into undefined behavior in v2.1 ( I'm not sure about a simple way to do this. It strikes me that |
Edit: it would still need a public alias method that forwards to the protected version, of course, but that seems a reasonable limitation. |
I think the following might be the easiest way to do this (and with the least amount of trickery): class A {
protected:
int foo() { return 42; }
};
class PublicA : public A {
public:
using A::foo;
};
PYBIND11_MODULE(expose, m) {
py::class_<A>(m, "A")
.def(py::init<>())
.def("foo", &PublicA::foo);
} This works because The access mode change looks like a hack, but it should be fairly well defined in the language. This has the advantage of not messing around with alias instances at all. |
Huh, I didn't know that (I had thought |
I have a use case where
|
This should do the trick: class A {
public:
virtual ~A() = default;
protected:
virtual int foo() { return 42; }
};
class PublicA : public A {
public:
using A::foo;
};
class Trampoline : public A {
public:
int foo() override { PYBIND11_OVERLOAD(int, A, foo, ); }
};
PYBIND11_MODULE(cmake_example, m) {
py::class_<A, Trampoline>(m, "A")
.def(py::init<>())
.def("foo", &PublicA::foo);
} Again, |
There are a couple of regressions in current
master
versus 2.1 with the following:which was a nice trick to allow exposing protected base class methods to Python.
The first regression is that the above fails a static assertion because
Foo
isn't polymorphic. But I think that if usingpy::init_alias<>()
for constructors, or for aliases that have just simply inherited constructors and no member variables, a non-polymorphic class is actually okay.The second problem is that the above fails because of the
method_adapter
changes added in #855: the cast from the deducedPyFoo
member function pointer isn't castable to the baseFoo
member function pointer type.Admittedly this is a bit fragile; we could also solve them by providing a nicer way to binding protected methods, if someone can think of one.
The text was updated successfully, but these errors were encountered: