A function pointer can point to anything from a free function, a function object, a wrapper over a member function call.
However, the std::bind created functors can have state, as well as custom-created ones. Where that state is allocated, and who is deleting it?
Consider the below example - will the state ( the number 10) be deleted when the vector is deleted? Who know to call a deleter on the functor, and no deleter on the function pointer?
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
using namespace std::placeholders;
class Bar
{
public:
void bar(int x, int y) { cout << "bar" << endl; }
};
void foo(int baz){ cout << "foo" << endl; }
int main() {
typedef std::function<void(int)> Func;
std::vector<Func> funcs;
funcs.push_back(&foo); // foo does not have to be deleted
Bar b;
// the on-the-fly functor created by bind has to be deleted
funcs.push_back(std::bind(&Bar::bar, &b, 10, _1));
// bind creates a copy of 10.
// That copy does not go into the vector, because it's a vector of pointers.
// Where does it reside? Who deletes it after funcs is destroyed?
return 0;
}