First, ptr
is not invokable, so it cannot be the target of std::function<void(int)>
. The only objects that can be the target of std::function<void(int)>
are those that can be invoked with (int)
.
This is generally true of every std::shared_ptr
-- there are no invokable std::shared_ptr
s, so they are never the target
of a std::function
.
An object is invokable if you can go object( blah )
for some blah
.
target<X>()
only returns non-nullptr
if the invokable object stored is exactly of type X
. If you have stored a bind
or mem_fun
expression, this means there is no way practical to get it back out.
You can arrange so that your std::function
s are constructed in very particular ways using very particular types, and on that subset of std::function
s use target<X>()
to extract out the backing objects, but this is not generally a useful technique, and is instead intended for narrow special cases.