As mentioned in a similarly worded question (Why use bind over lambdas in c++14?) The answer was - no reason (and also mentioned why it would be better to use lambdas).
My question is - if in C++14 there was no longer a reason to use bind, why did the standards committee found it necessary to add std::bind_front in C++20?
Does it now have any new advantage over a lambda?
bind_frontbinds the first X parameters, but if the callable calls for more parameters, they get tacked onto the end. This makesbind_frontvery readable when you're only binding the first few parameters of a function.The obvious example would be creating a callable for a member function that is bound to a specific instance:
The
bind_frontversion is a lot less noisy. It gets right to the point, having exactly 3 named things:bind_front, the member function to be called, and the instance on which it will be called. And that's all that our situation calls for: a marker to denote that we're creating a binding of the first parameters of a function, the function to be bound, and the parameter we want to bind. There is no extraneous syntax or other details.By contrast, the lambda has a lot of stuff we just don't care about at this location. The
auto... argsbit, thestd::forwardstuff, etc. It's a bit harder to figure out what it's doing, and it's definitely much longer to read.Note that
bind_frontdoesn't allowbind's placeholders at all, so it's not really a replacement. It's more a shorthand for the most useful forms ofbind.