-
|
Hi! Thanks for the great library. I'm looking into porting my library's bindings from pybind11 --> nanobind, but it makes light use of multiple inheritance. The docs list multiple inheritance as an intentionally-not supported feature to keep complexity down (understood, makes sense!). I'm wondering what exactly the restrictions are around multiple inheritance, but having trouble finding more info. Are there more details anywhere? For example, if my classes uses simple interface-style multiple inheritance: class ChildClass : public ParentClass, public Serializable {
// ...
};Does this mean I am totally unable to use nanobind with that class in any way? Or perhaps it just means that I just cannot bind methods which are provided by multiple bases? Or merely that I can't create Python class hierarchies intermingling Python and bound-C++ classes with multiple inheritance? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi Nick, when you bind classes with a parent–child relationship (via multiple In standard C++, that is not guaranteed once you have multiple or virtual inheritance. Converting To keep complexity down, nanobind intentionally does not implement general support for these adjusted pointers. Instead, it relies on the common ABI convention (GCC/Clang/MSVC-style) that the first non-virtual base subobject starts at the same address as the most-derived object. For your example class ChildClass : public ParentClass, public Serializable {
// ...
};things will work as expected if
In that case, What this means in terms of bindings:
There is an unmerged branch of nanobind with a small change that permits multiple inheritance on the Python side (for Python classes that multiply inherit from C++-backed types). Let me know if that would be required for your project. |
Beta Was this translation helpful? Give feedback.
Hi Nick,
when you bind classes with a parent–child relationship (via multiple
nb::class_<>calls), nanobind assumes that conversion betweenChildClassandParentClasscan be done using an address-preserving cast: it treats aChildClass*as aParentClass*with the same pointer value.In standard C++, that is not guaranteed once you have multiple or virtual inheritance. Converting
ChildClass*toParentClass*may require pointer adjustment, and the correct way to do that in all cases is to use the language-defined conversions (static_cast/dynamic_cast), which are free to change the pointer value to point at the appropriate base subobject.To keep complexity down, nanobind intentionally doe…