3

The definition and application of boost::bind are clearly outlined in the boost website, yet I hardly could find what is the benefit of using it over using a normal function call? Or to put it simply in which scenarios it might come in handy?

b3hn4m
  • 75
  • 4
  • Look up function "currying". You should find a lot of examples. – Brady Dean Sep 14 '21 at 00:26
  • 1
    Are you using modern C++? Prior to C++11, `boost::bind` was incredibly handy as we didn't have lambdas. With the introduction of lambdas in C++11 onwards, there is less need for `boost::bind` (though there may still be some purpose) – Tas Sep 14 '21 at 00:37
  • I personally found it useful for simplifying random number generation. You gotta do something like `myDistribution(randomEngine)` to get a random number, why not just bind the two? get a simple `rng()`. – fortytoo Sep 14 '21 at 00:43
  • 3
    @spitconsumer A lambda can do that, too. In fact, there is hardly ever a good reason to use `(boost|std)::bind()` over a lambda in modern C++. – Remy Lebeau Sep 14 '21 at 00:44
  • I was checking to see if there is a duplicate, and instead I found the complementary question, if I understand the point of both questions. This question starts from knowing *how* to use `bind` and asks *when* it would be handy. [The purpose of Function Binding](https://stackoverflow.com/questions/27314972) starts from a situation when `bind` is handy and asks how to use it. – JaMiT Sep 14 '21 at 04:41
  • You can find an answer to your question at [Callback functions in C++](https://stackoverflow.com/questions/2298242) even though that does not ask the same thing (not a duplicate). – JaMiT Sep 14 '21 at 04:43

2 Answers2

3

Sometimes you have a set of arguments that you are going to pass to the function, but you wish to call the function later without needing to pass the arguments that are already known. One reason to need this may be because the call may need to conform to an interface that doesn't allow those arguments. This is typical in the (functor style) "callback" idiom.

That situation can be solved by defining a class that stores the arguments as members, and defines function call operator overload that delegates to the original function and passes the arguments stored as members.

boost::bind is a structured way to represent such "argument binding" without needing to define the class yourself. The standard library used to have std::bind1st and std::bind2nd which were more limited, less generic forms of bind.

boost::bind is rarely needed anymore since it was introduced to the standard library as std::bind in C++11, and furthermore lambdas were introduced in C++11 and improved in C++14 and they have largely obsoleted bind.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I think the first paragraph is not exactly clear. This answer might be improved by pointing out that this is a _functional_ construct comparable to _currying_: applying only some arguments to a function to get back a new function/object that needs only the remaining, unsupplied arguments in order to invoke it. – Dúthomhas Sep 14 '21 at 05:08
  • @Dúthomhas Curring is a similar concept, but it's not what `boost::bind` is. The abstract concept is *partial function application*. While it is useful to know, knowing the name of a concept doesn't necessarily help understand what it is used for or even understand what it is. – eerorika Sep 14 '21 at 05:13
  • Yes, they both work differently to perform the same conceptual feat. Giving it a name supplies further useful googling. If you don't like my suggestion, perhaps a more concrete example to explain it to a newbie? – Dúthomhas Sep 14 '21 at 05:24
1

bind provides a way to take a function or a function object with a certain arity and transform it to another function with lesser arity by precisely binding one or more arguments. And you can do it in place.

bind and functions don't have a good comparison. bind is more comparable to simple lambdas that call a function and fix certain parameters in their implementation.

The big difference between boost::bind and a modern lambda is that the bind object has a certain degree of instrospection associated with it that the lambda doesn't have.

For example you could in principle recover the original function and reconstruct what is the argument bound. In a lambda everything is private, even the simplest implementation.

In other words, the result of boost::bind is an "expression" and the type has well defined pattern (e.g. boost::bind_t<...> or something, and that can be matched in a template function argument). Lambdas instead are each their own unknowable sui generis type.

Admittedly, few people maybe interested in the difference, but it is there and I played with it once or twice to implement a symbolic system (for derivatives).

I can't say the same about std::bind because the object returned is unspecified by the standard and it could be more difficult to reconstruct the full bind "expression".

alfC
  • 14,261
  • 4
  • 67
  • 118