[QUESTJION]: PYBIND11_OVERLOAD_PURE Causing Error
Nobody has claimed this yet.
- 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?
master
Problem description
Hi,
I'm trying to bind the virtual function myfunc in class B. I have class A forward declared and the full definition of class A is in a separate file. I have the following error:
C:/msys64/home/lucas/CS 5850/testproject/test.cpp:21:9: required from here
C:/msys64/mingw64/include/c++/12.2.0/type_traits:1447:38: error: invalid use of incomplete type 'class A
test.cpp:21:9 is the line where PYBIND11_OVERLOAD_PURE locates.
If myfunc is not a virtual function, I can use A_wrapper to accomplish my goal. However, when myfunc becomes pure virtual, the macro PYBIND11_OVERLOAD_PURE seemed need to check full definition of A. How can I work around this?
Reproducible example code
#include <pybind11/pybind11.h>
namespace py = pybind11;
class A;
class B {
public:
virtual void myfunc(A* p) = 0;
};
struct A_wrapper {
A* ptr;
};
class PyB : B {
public:
void myfunc(A* p) override {
PYBIND11_OVERLOAD_PURE(void, B, myfunc, p)
}
};
PYBIND11_MODULE(mymodule, m) {
py::class_<A_wrapper>(m, "A_wrapper");
py::class_<B, PyB>(m, "B")
.def("myfunc", [](B &self, A_wrapper a) { self.myfunc(a.ptr); });
}
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 reproducer in test.cpp, especially the PYBIND11_OVERLOAD_PURE call at line 21, and review how the macro handles the forward-declared A type. Compare this behavior with the documented overload macros and determine whether a supported workaround or library change is possible for pure virtual methods using incomplete parameter types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100