在 C + + 11之前,我经常使用 boost::bind
或者 boost::lambda
。bind
部分使其成为标准库(std::bind
) ,另一部分成为核心语言(C + + lambdas)的一部分,使得 lambdas 的使用变得更加容易。现在,我很少使用 std::bind
,因为我几乎可以用 C + + lambdas 做任何事情。我能想到的 std::bind
有一个有效的用例:
struct foo
{
template < typename A, typename B >
void operator()(A a, B b)
{
cout << a << ' ' << b;
}
};
auto f = bind(foo(), _1, _2);
f( "test", 1.2f ); // will print "test 1.2"
C + + 14等价于
auto f = []( auto a, auto b ){ cout << a << ' ' << b; }
f( "test", 1.2f ); // will print "test 1.2"
简短得多。(在 C + + 11中,由于自动参数的原因,这还不能正常工作。)std::bind
击败 C + + lambdas 的替代方案还有其他有效的用例吗? 或者 std::bind
对于 C + + 14来说是多余的吗?