Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use std::bind?

Every time I need to use std::bind, I end up using a lambda instead. So when should I use std::bind? I just finished removing it from one codebase, and I found that lambdas were always simpler and clearer than std::bind. Isn't std::bind completely unnecessary? Shouldn't it be deprecated in the future? When should I prefer std::bind to lambda functions? (There has to be a reason that it got into the standard at the same time as lambdas.)

I've also noticed that more and more people are familiar with lambdas (so they know what lambdas do). However, a lot fewer people are familiar with std::bind and std::placeholders.

like image 396
gnzlbg Avatar asked Mar 24 '13 12:03

gnzlbg


People also ask

Why is std :: bind used?

std::bind. std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

Why BIND is used in C++?

Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs. Placeholders are namespaces which detect the position of a value in a function.

Is std :: bind deprecated?

Yes: std::bind should be replaced by lambda For almost all cases, std::bind should be replaced by a lambda expression. It's idiomatic, and results in better code.

Why is it important to bind a function to its arguments C++?

Bind function with the help of placeholders helps to manipulate the position and number of values to be used by the function and modifies the function according to the desired output.


2 Answers

Here's something you can't do with a lambda:

std::unique_ptr<SomeType> ptr = ...; return std::bind(&SomeType::Function, std::move(ptr), _1, _2); 

Lambdas can't capture move-only types; they can only capture values by copy or by lvalue reference. Though admittedly this is a temporary issue that's being actively resolved for C++14 ;)

"Simpler and clearer" is a matter of opinion. For simple binding cases, bind can take a lot less typing. bind also is focused solely on function binding, so if you see std::bind, you know what you're looking at. Whereas if you use a lambda, you have to look at the lambda implementation to be certain of what it does.

Lastly, C++ does not deprecate things just because some other feature can do what it does. auto_ptr was deprecated because it is inherently dangerous to use, and there is a non-dangerous alternative.

like image 85
Nicol Bolas Avatar answered Oct 18 '22 11:10

Nicol Bolas


You can create polymorphic objects with std::bind which you can't with lambdas, i.e. the call wrapper returned by std::bind can be invoked with different argument types:

#include <functional> #include <string> #include <iostream>  struct Polly {   template<typename T, typename U>     auto operator()(T t, U u) const -> decltype(t + u)     { return t + u; } };  int main() {   auto polly = std::bind(Polly(), std::placeholders::_1, "confusing");    std::cout << polly(4) << polly(std::string(" this is ")) << std::endl;     } 

I created this as a puzzle not an example of good code, but it does demonstrate polymorphic call wrappers.

like image 26
Jonathan Wakely Avatar answered Oct 18 '22 11:10

Jonathan Wakely