Compared with std::bind, lambda is not just more elegant, but also has better performance.
struct A
{
void test() const { std::cout << "test" << std::endl; }
~A() { std::cout << "~A" << std::endl; }
int i;
};
struct Test
{
void test(A m) const { m.test(); }
};
void test(A m) { m.test(); }
int main()
{
Test t;
{
A m;
std::bind(&Test::test, &t, m)();
}
{
A m;
[=](){t.test(m);}();
}
{
A m;
std::bind(&test, m)();
}
{
A m;
[=](){test(m);}();
}
return 0;
}
# gcc 4.6.2 test ~A ~A ~A ~A ~A test ~A ~A ~A test ~A ~A ~A test ~A ~A ~A
# gcc 4.5.1 ~A test ~A ~A ~A ~A ~A ~A test ~A ~A ~A ~A test ~A ~A ~A ~A test ~A ~A ~A
No comments:
Post a Comment