[BUG]: Pure virtual method overriding using mixin class gives: Tried to call pure virtual function …
Open
Nobody has claimed this yet.
triage
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.13.5
Problem description
I cant override pure virtual functions using mixin classes. Pybind does not realize that the function in question does exist but is provided by the mixin class.
Reproducible example code
struct M {
int f() { return 1; }
};
struct A {
virtual int f() = 0;
};
// Imagine a trampoline class AT for A here
PYBIND11_MODULE(example, m) {
pybind11::class_<M>(m, "Mixin")
.def("f", &M::f);
pybind11::class_<A, AT>(m, "Abstract")
.def("f", &A::f);
}
class Derived(M, A):
...
d = D()
d.foo()
The last statement throws "Tried to call pure virtual function". For example in plain Python this works as expected:
class A():
def f(self):
pass
class M:
def f(self):
return "mixed in"
class D(M, A):
pass
d = D()
print(d.f())
Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the C++ mixin, abstract class, and trampoline setup in the reproducible example, then run the corresponding Python multiple-inheritance case. Trace how the pure virtual call is dispatched; done when the mixin-provided f() implementation is used instead of raising the pure-virtual error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100