Online Book Reader

Home Category

Professional C__ - Marc Gregoire [228]

By Root 1403 0
require comparison callbacks, for which the predefined comparators come in handy.

Logical Function Objects

C++ provides function object classes for the three logical operations: logical_not (operator!), logical_and (operator&&), and logical_or (operator||).

Logical operations deal only with the values true and false. In the original STL, there was no provision for dealing with bitwise operations on integer-type values. C++11 has bitwise function objects (covered in the next section).

Logical functors can for example be used to implement an allTrue() function that checks if all the Boolean flags in a container are true. This can be implemented as follows:

bool allTrue(const vector& flags)

{

return accumulate(flags.begin(), flags.end(), true,

logical_and());

}

Code snippet from FunctionObjects\LogicalFunctors.cpp

Similarly, the logical_or functor can be used to implement an anyTrue() function that returns true if there is at least one Boolean flag in a container true:

bool anyTrue(const vector& flags)

{

return accumulate(flags.begin(), flags.end(), false,

logical_or());

}

Code snippet from FunctionObjects\LogicalFunctors.cpp

Bitwise Function Objects

C++11 adds function objects for all the bitwise operations: bit_and (operator&), bit_or (operator|), and bit_xor (operator^). These bitwise functors can for example be used together with the transform() algorithm (discussed later in this chapter) to perform bitwise operations on all elements in a container.

Function Object Adapters

When you try to use the basic function objects provided by the standard, it often feels as if you’re trying to put a square peg into a round hole. For example, you can’t use the less function object with find_if() to find an element smaller than some value because find_if() passes only one argument to its callback each time instead of two. The function adapters attempt to rectify this problem and others. They provide a modicum of support for functional composition, or combining functions together to create the exact behavior you need.

Binders

Binders can be used to bind parameters of functions to certain values. C++11 introduces std::bind(), which is very flexible and is discussed in the following section. The section afterwards explains the pre-C++11 bind2nd() and bind1st() adapters, which you have to use if your compiler does not yet support std::bind().

std::bind

std::bind() allows you to bind arguments of a function in a flexible way. You can bind function arguments to fixed values and you can even rearrange function arguments in a different order. It is best explained with an example.

Suppose you have a function called func() accepting two arguments:

void func(int num, const string& str)

{

cout << "func(" << num << ", " << str << ")" << endl;

}

Code snippet from FunctionObjects\bind.cpp

The following code demonstrates how you can use bind() to bind the second argument of the func() function to a fixed value, str. The result is stored in f1(). The C++11 auto keyword is used to remove the need to specify the exact return type which can become complicated. Arguments that are not bound to specific values should be specified as _1, _2, _3 and so on. These are defined in the std::placeholders namespace. In the definition of f1(), the _1 specifies where the first argument of f1() needs to go when func() is called. After this, f1() can be called with just a single integer argument as follows:

string str = "abc";

auto f1 = bind(func, placeholders::_1, str);

f1(16);

Code snippet from FunctionObjects\bind.cpp

The output should be:

func(16, abc)

bind() can also be used to rearrange the arguments as shown in the following code. The _2 specifies where the second argument of f2() needs to go when func() is called. In other words, the f2() binding means that the first argument to f2() will become the second argument to the function func() and the second argument to f2() will become the first argument to the function func().

auto f2 = bind(func, placeholders::_2, placeholders::_1);

f2("Test",

Return Main Page Previous Page Next Page

®Online Book Reader