Der Quellcode ist sehr einfach und selbsterklärend. Die Frage ist im Kommentar enthalten.
#include <iostream>
#include <functional>
using namespace std;
using namespace std::tr1;
struct A
{
A()
{
cout << "A::ctor" << endl;
}
~A()
{
cout << "A::dtor" << endl;
}
void foo()
{}
};
int main()
{
A a;
/*
Performance penalty!!!
The following line will implicitly call A::dtor SIX times!!! (VC++ 2010)
*/
bind(&A::foo, a)();
/*
The following line doesn't call A::dtor.
It is obvious that: when binding a member function, passing a pointer as its first
argument is (almost) always the best way.
Now, the problem is:
Why does the C++ standard not prohibit bind(&SomeClass::SomeMemberFunc, arg1, ...)
from taking arg1 by value? If so, the above bind(&A::foo, a)(); wouldn't be
compiled, which is just we want.
*/
bind(&A::foo, &a)();
return 0;
}