pybind / pybind11

Seamless operability between C++11 and Python

Home Page:https://pybind11.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG]: Docs bug fix/improvements for inherited overloaded virtual methods

deadlocklogic opened this issue · comments

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

master

Problem description

Consider the documentation: Inheritance
If we add a virtual function overload, then the compilation fails.

class Animal {
public:
    virtual std::string go() { return ""; } // Added virtual overload
    virtual std::string go(int n_times) = 0;
    virtual std::string name() { return "unknown"; }
};
class Dog : public Animal {
public:
    std::string go(int n_times) override {
        std::string result;
        for (int i=0; i<n_times; ++i)
            result += bark() + " ";
        return result;
    }
    virtual std::string bark() { return "woof!"; }
};

template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
public:
    using AnimalBase::AnimalBase; // Inherit constructors
    std::string go() override { PYBIND11_OVERRIDE(std::string, AnimalBase, go, ); } // Added virtual overload
    std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, AnimalBase, go, n_times); }
    std::string name() override { PYBIND11_OVERRIDE(std::string, AnimalBase, name, ); }
};
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
public:
    using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
    // Override PyAnimal's pure virtual go() with a non-pure one:
    std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, DogBase, go, n_times); }
    std::string bark() override { PYBIND11_OVERRIDE(std::string, DogBase, bark, ); }
};

Now this won't compile!
So since we are registering every virtual method for each class we should explicitly use the base class like the following:

template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
public:
    using AnimalBase::AnimalBase; // Inherit constructors
    std::string go() override { PYBIND11_OVERRIDE(std::string, Animal, go, ); }
    std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Animal, go, n_times); }
    std::string name() override { PYBIND11_OVERRIDE(std::string, Animal, name, ); }
};
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
public:
    using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
    // Override PyAnimal's pure virtual go() with a non-pure one:
    std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, Dog, go, n_times); }
    std::string bark() override { PYBIND11_OVERRIDE(std::string, Dog, bark, ); }
};

Now compilation succeeds.
Thanks.

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

Now this is more verbose, because every inherited class needs to be explicitly registered but compilation succeeds.
If you don't want to use this fix, you can at least mention that the suggested trampoline technique doesn't work when there is hidden virtual overloaded methods.
Thanks.