mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-13 17:56:02 +00:00
Redesigned virtual call mechanism and user-facing syntax (breaking change!)
Sergey Lyskov pointed out that the trampoline mechanism used to override
virtual methods from within Python caused unnecessary overheads when
instantiating the original (i.e. non-extended) class.
This commit removes this inefficiency, but some syntax changes were
needed to achieve this. Projects using this features will need to make a
few changes:
In particular, the example below shows the old syntax to instantiate a
class with a trampoline:
class_<TrampolineClass>("MyClass")
.alias<MyClass>()
....
This is what should be used now:
class_<MyClass, std::unique_ptr<MyClass, TrampolineClass>("MyClass")
....
Importantly, the trampoline class is now specified as the *third*
argument to the class_ template, and the alias<..>() call is gone. The
second argument with the unique pointer is simply the default holder
type used by pybind11.
This commit is contained in:
@@ -82,15 +82,11 @@ void runExample12Virtual(Example12 *ex) {
|
||||
}
|
||||
|
||||
void init_ex12(py::module &m) {
|
||||
/* Important: use the wrapper type as a template
|
||||
argument to class_<>, but use the original name
|
||||
to denote the type */
|
||||
py::class_<PyExample12>(m, "Example12")
|
||||
/* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
|
||||
.alias<Example12>()
|
||||
/* Important: indicate the trampoline class PyExample12 using the third
|
||||
argument to py::class_. The second argument with the unique pointer
|
||||
is simply the default holder type used by pybind11. */
|
||||
py::class_<Example12, std::unique_ptr<Example12>, PyExample12>(m, "Example12")
|
||||
.def(py::init<int>())
|
||||
/* Copy constructor (not needed in this case, but should generally be declared in this way) */
|
||||
.def(py::init<const PyExample12 &>())
|
||||
/* Reference original class in function definitions */
|
||||
.def("run", &Example12::run)
|
||||
.def("run_bool", &Example12::run_bool)
|
||||
|
||||
Reference in New Issue
Block a user