What happens is undefined behavior.
In the call to bind() , an object will be returned that contains a copy of instance , so when called, func(0) will effectively call:
(instance->*(&type::func))(0);
Displaying an invalid pointer, as you would if there were instance delete d, this is undefined behavior. It will not throw an exception (although it is undefined, so it could, who knows).
Please note that there is no placeholder in your call:
std::function<foo(bar)> func = std::bind(type::func, instance, std::placeholders::_1);
Without this, you cannot call func(0) even with an unused instance.
Updating the sample code to better illustrate what happens:
struct foo{ int f; ~foo() { f = 0; } void bar(int i) { std::cout << i+f << std::endl; } };
With this added destructor, you can see the difference between copying the pointer (in f ) and copying the object pointed to (in g ):
foo* bar = new foo{42}; std::function<void(int)> f = std::bind(&foo::bar, bar, std::placeholders::_1); std::function<void(int)> g = std::bind(&foo::bar, *bar, std::placeholders::_1); f(100);
Barry source share