I am trying to bind std::function
with derived argument. Function I want to bind looks like this:
void Application::myFunction(Derived *derived) { }
Function I am trying to pass into this function (but bound) looks like this:
void Storage::register(int number, std::function<void(Base *base)>) { }
And I am doing this (this
keyword in this context is Application
):
myStorage->register(0, std::bind(&Application::myFunction, this, std::placeholders::_1);
But that gives me error:
error: no matching function for call (Storage::register...)
Any idea what am I doing wrong? Thank you!
This does not work because std::function<void(Base*)>
does not guarantee that it will always be called with a Derived*
. You cannot do this anymore than you can call Application::myFunction
with a Base*
directly.
Depending on what it is you want to achieve, make it a std::function<void(Derived *)>
or have myFunction
work with Base*
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With