Replies: 1 comment 1 reply
-
Unlike pybind11, nanobind stores some data of its own in the trampoline instance (the
You have to expand
This should work fine. The only caveat is that nanobind doesn't support the variety of options pybind11 has for controlling whether constructing a T produces a trampoline instance or not. nanobind will create the trampoline instance if the instantiated type is a Python subclass or if constructing the base instance would be invalid (e.g. because the base is an abstract class), and will create a base instance otherwise. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the pybind11 docs, we're given an example of having inheritance between trampoline classes:
https://pybind11.readthedocs.io/en/latest/advanced/classes.html#combining-virtual-functions-and-inheritance
I tested a toy example in nanobind and it seems to work:
However, it seems like this pattern was deliberately left out of the nanobind docs. I'm also not sure how it interacts with the
NB_TRAMPOLINE
macros.Is this pattern no longer recommended? Are there any edge case differences between how pybind11/nanobind handles trampoline classes that would break it with a more complicated set up? Would it break how nanobind infers inheritance information?
Note that I am migrating a pybind11 project to nanobind which used this pattern extensively.
Side question: Our current pybind11 project uses trampoline classes to store
py::object
variables, which seems to work fine right now. Would any differences in nanobind break this?Edit: looking at the implementation of
NB_TRAMPOLINE
maybe you should instead do this?does this work?
Edit 2: it doesn't work, I don't know why. I tried a few different variants, the error messages make no sense to me.
Edit 3: I think this works? Had to learn about C++ behavior for unqualified names from dependent base class, and shadowing of redeclared class instance variables... Perhaps the
NB_TRAMPOLINE
macro can be changed to accommodate this pattern?Beta Was this translation helpful? Give feedback.
All reactions