More about Binders
In previous chapter we briefly introduced binders and how they can be used in generic algorithms. With binders, we can convert a binary function object into a unitary function object. Among them, bind1st() binds the first parameter of operator()
into a fixed value, while bind2st() binds the second parameter into a fixed value.
The following example sorts an array from largest to smallest. Then we want to insert 48 inside the array and keep the array sorted. bind1st() is used here to bind greater with 48 in order to find the first element smaller than 48.
What is the underlying principle of a binder? Here we try to implement bind1st() ourselves. First we write our own my_find_if() function, which takes two iterators and a function object for the criterion. Then in the function, we call operator()
of the function object on every elements within the range until we find the proper one. Apparently, the function object here should be unitary since it only takes one parameter.
Now let's implement mybind1st(). In general, this function takes a binary function object and returns a unitary one. Let's name the returned unitary function object as _mybind1st. Here we don't implement it directly, but encapsulate once with template. This enables the type deduction of template function so that we don't need to pass in template parameters explicitly.
Next is the _mybind1st class, which takes a binary function object and a value to be fixed as member variables. Then the operator()
takes one parameter, and simple returns the binary function object with the first parameter fixed to the value.
As we can see, the underlying implementation of bind1st() and bind2nd() is simply a multi-layer encapsulation of function objects. However, their limitation is obvious: they are only for binary objects. What if the function object is more complicated, for example with multiple parameters? Fortunately, C++ 11 introduces two classes std::function and std::bind, which is more convenient and powerful.
Last updated