Your question is equivalent to asking why this doesn't work:
struct Integer
{
int value;
};
std::shared_ptr<int> p(new int(1));
std::shared_ptr<Integer> p2 = p;
It doesn't work because they're not the same type. Just because you can store a MyFunctor
in a std::function<void(const float&)>
doesn't mean that a pointer to one is convertible to a pointer to the other.
You want:
auto mf2_ptr = std::make_shared<std::function<void (const float &)>>( MyFunctor() );
a.run(mf2_ptr);
Now I discovered that a.run(...) compiles if MyFunctor inherits from std::function:
It compiles because now you can convert shared_ptr<MyFunctor>
to shared_ptr<function<void(const float&)>>
, but it won't work correctly. std::function::operator()()
is not virtual, so if you call the function it will call the base class' operator()
, but the base class doesn't point to anything and will throw std::bad_cast
.