I would like to use polymorphism to have a vector of std::function<void(Base&)>
that take in a base class as a parameter, and fill the vector with std::function
with a similar signature that instead take in a derived class as a parameter.
#include <functional>
#include <vector>
#include <stdio.h>
class Base
{
public:
virtual ~Base() = default;
};
class Derived : public Base
{
public:
Derived(int x, int y) : a(x), b(y) {}
int a, b;
};
void DerivedFunction(Derived& d)
{
printf("A: %d, B: %d\n", d.a, d.b);
}
using FunctionSignature = std::function<void(Base&)>; // Changing Base& to Derived& compiles fine.
static std::vector<FunctionSignature> myVector;
int main()
{
FunctionSignature fn = [](Derived& d){ printf("A: %d, B: %d\n", d.a, d.b); };
myVector.push_back(fn); // error
myVector.push_back(std::forward<FunctionSignature>(fn)); // error
myVector.push_back(std::bind(&DerivedFunction, std::placeholders::_1)); // error
return 0;
}
What would be the correct way to push_back DerivedFunction into the vector?
Godbolt link: https://godbolt.org/z/b6Taqoqs8