Online Book Reader

Home Category

Professional C__ - Marc Gregoire [229]

By Root 1319 0
32);

Code snippet from FunctionObjects\bind.cpp

The output is as follows:

func(32, Test)

There is a small issue with binding parameters in combination with overloaded functions. Suppose you have the following two overloaded functions called overloaded(). One accepts an integer and the other accepts a floating point number:

void overloaded(int num) {}

void overloaded(float f) {}

Code snippet from FunctionObjects\bind.cpp

If you want to use bind() with these overloaded functions, you need to explicitly specify which of the two overloads you want to bind. The following will not compile:

auto f3 = bind(overloaded, placeholders::_1); // ERROR

Code snippet from FunctionObjects\bind.cpp

If you want to bind the parameters of the overloaded function accepting a floating point argument, you need the following syntax:

auto f4 = bind((void(*)(float))overloaded, placeholders::_1); // OK

Code snippet from FunctionObjects\bind.cpp

Another example of the bind() function is to use the find_if() algorithm to find the first element in a sequence that is greater than or equal to 100. To solve this problem earlier in this chapter, you wrote a function perfectScore() and passed a function pointer to it to find_if(). Now that you know about the comparison functors, it seems as if you should be able to implement a solution using the greater_equal class template.

The problem with greater_equal is that it takes two parameters, whereas find_if() passes only one parameter to its callback predicate each time. You need the ability to specify that find_if() should use greater_equal, but should pass 100 as the second argument each time. That way, each element of the sequence will be compared against 100. This can be accomplished with bind(). The following code uses bind() to bind the second parameter of greater_equal to a fixed value of 100:

// Code for inputting scores into the vector omitted, similar as earlier.

auto end = myVector.end();

auto it = find_if(myVector.begin(), end,

bind(greater_equal(), placeholders::_1, 100));

if (it == end) {

cout << "No perfect scores" << endl;

} else {

cout << "Found a \"perfect\" score of " << *it << endl;

}

Code snippet from FunctionObjects\Binders.cpp

Pre-C++11 bind2nd and bind1st

If your compiler does not support bind(), you need to use a method like bind2nd(). Unlike bind(), bind2nd() only works with binary functions and only allows you to bind the second argument. The following example shows that the bind2nd() method takes a function and a second argument. Because of bind2nd(), the function is then called with one argument passed in by find_if() and one argument derived from the bind2nd().

// Code for inputting scores into the vector omitted, similar as earlier.

vector::iterator end = myVector.end();

vector::iterator it = find_if(myVector.begin(),end,

bind2nd(greater_equal(), 100));

if (it == end) {

cout << "No perfect scores" << endl;

} else {

cout << "Found a \"perfect\" score of " << *it << endl;

}

Code snippet from FunctionObjects\Binders.cpp

The bind2nd() function in this code binds the value 100 as the second parameter to greater_equal. The result is that find_if() compares each element against 100 with greater_equal.

There is also an equivalent bind1st() function that binds an argument to the first parameter of a binary function. For example, the following finds the first value less than 100 using bind1st(), because if the result of bind1st() is called with argument x, it returns the value of 100 > x:

find_if(v.begin(), v.end(), bind1st(greater(), 100));

Compare this to the following, using bind2nd(), which finds the first value greater than 100, because if the result of bind2nd() is called with argument x, it returns the value of x > 100:

find_if(v.begin(), v.end(), bind2nd(greater(), 100));

Both bind2nd() and bind1st() have been deprecated by C++11. Use lambda expressions or bind() instead.

Negators

The negators are functions similar to the binders but they simply complement the result of a predicate. For example,

Return Main Page Previous Page Next Page

®Online Book Reader