First of all, there is a third option for your code:
bind(&A::foo, std::ref(a))();
Now, why are the options made by the default copy? I suppose, but this is just a wild hunch that it bind’s preferable for default behavior to be independent of the parameter lifetime: the binding result is a functor whose action can be delayed long before the parameters are destroyed.
Do you expect from the following code you get the default UB?
void foo(int i) { }
int main()
{
std::function<void ()> f;
{
int i = 0;
f = std::bind(foo, i);
}
f();
}
source
share