Before C++11 I used boost::bind
or boost::lambda
a lot. The bind
part made it into the standard library (std::bind
) the other part became part of the core language (C++ lambdas) and made the use of lambdas a lot easier. Nowadays, I hardly use std::bind
, since I can do almost anything with C++ lambdas. There's one valid use-case for std::bind
that I can think of:
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"
The C++14 equivalent for that would be
auto f = []( auto a, auto b ){ cout << a << ' ' << b; }
f( "test", 1.2f ); // will print "test 1.2"
Much shorter and more concise. (In C++11 this does not work yet because of the auto parameters.) Is there any other valid use case for std::bind
beating the C++ lambdas alternative or is std::bind
superfluous with C++14?
bind
wherever that made sense. – Pazbind
. Just useauto f = foo{};
– Halter