Repository navigation
Does order of defining classes matter? #6009
|
Suppose you are declaring classes. Sometimes a method of one class returns another class. I wonder, is it needed for the class that is e.g. returned, to be defined before the method that uses it, or is the order irrelevant? As an example: This seems to work fine, despite Can I rely on the order of class definitions being irrelevant, as far as return types and argument types go? |
Replies: 1 comment 1 reply
|
The order is irrelevant as long as all classes are registered before any of the bound methods is actually invoked from Python — which, for the pattern in your snippet (everything inside one The mechanism:
Inside a single Where it does start to matter:
For your |
The order is irrelevant as long as all classes are registered before any of the bound methods is actually invoked from Python — which, for the pattern in your snippet (everything inside one
PYBIND11_EMBEDDED_MODULE/PYBIND11_MODULE), is automatically the case.The mechanism:
py::class_<T>(m, "T")registersTin pybind11's internal type registry (a hash map keyed bystd::type_index).library.def("FindBook", &Library::FindBook, ...)does not resolveBookat registration time. It only stores enough type info to convert the return value later, at call time.library.FindBook(123)and pybind11 needs to hand back aBook*, it looks uptypeid(Book)in the registry. IfB…