Online Book Reader

Home Category

Professional C__ - Marc Gregoire [225]

By Root 1504 0
x)

{

return [=]{return 2*x;};

}

Code snippet from Lambdas\multiplyBy2Lambda.cpp

The body of this function creates a lambda expression that captures the variables from the enclosing scope by value and returns an integer, which is two times the value passed to multiplyBy2Lambda(). The return type of the multiplyBy2Lambda() function is function, which is a function accepting no arguments and returning an integer. The lambda expression defined in the body of the function exactly matches this prototype. The variable x is captured by value and thus a copy of the value of x is bound to the x in the lambda expression before the lambda is returned from the function.

The preceding function can be called as follows:

function fn = multiplyBy2Lambda(5);

cout << fn() << endl;

Code snippet from Lambdas\multiplyBy2Lambda.cpp

You can use the C++11 auto keyword to make this much easier:

auto fn = multiplyBy2Lambda(5);

cout << fn() << endl;

Code snippet from Lambdas\multiplyBy2Lambda.cpp

The output will be 10.

The multiplyBy2Lambda() example captures the variable x by value, [=]. Suppose the function is rewritten to capture the variable by reference, [&], as follows. This will not work as explained after the code:

function multiplyBy2Lambda(int x)

{

return [&]{return 2*x;};

}

The lambda expression captures x by reference. However, the lambda expression will be executed later in the program, not anymore in the scope of the multiplyBy2Lambda() function at which point the reference to x is not valid anymore!

Lambda Expressions as Parameters

You can write your own functions that accept lambda expressions as parameters. This can for example be used to implement callbacks. The following code implements a testCallback() function that accepts a vector of integers and a callback function. The implementation will iterate over all the elements in the given vector and will call the callback function for each element. The callback function accepts the current element in the vector as an int argument and returns a Boolean. If the callback returns false, the iteration is stopped.

void testCallback(const vector& vec,

const function& callback)

{

for (auto i : vec) {

// Call callback. If it returns false, stop iteration.

if (!callback(i))

break;

// Callback did not stop iteration, so print value

cout << i << " ";

}

cout << endl;

}

Code snippet from Lambdas\callback.cpp

The testCallback() function can be tested as follows. First a vector with 10 elements is created and the generate() algorithm is used to fill in those 10 elements. The generate() algorithm requires an iterator range and will replace the values in that range with the values returned from the function given as third parameter. The generate() algorithm is explained in more detail later in this chapter. The code then outputs all the values of the vector using the for_each() algorithm in combination with a lambda. The for_each() algorithm will call the function given as third parameter for each value in the given iterator range. The last line calls the testCallback() function with a small lambda expression as callback function. This lambda expression returns true for values that are less than 6.

vector vec(10);

int index = 0;

generate(vec.begin(), vec.end(), [&index]{return ++index;});

for_each(vec.begin(), vec.end(), [](int i){cout << i << " ";});

cout << endl;

testCallback(vec, [](int i){return i<6;});

Code snippet from Lambdas\callback.cpp

The output of this example is as follows:

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5

Examples

This section gives a few examples that use STL algorithms in combination with lambda expressions.

count_if

The following example uses the count_if() algorithm to count the number of elements in the given vector that satisfy a certain condition. The condition is given in the form of a lambda expression, which captures the variables in its enclosing scope by value. This makes the variable value available in the body of the lambda. Had the capture clause been the empty capture

Return Main Page Previous Page Next Page

®Online Book Reader