Lambdas in C++

The new C++0x standard adds lambda expressions to the language. Visual Studio 2010 CTP is already supporting this new feature that brings functional techniques to C++ too.

What is a lambda expression? It’s basically a function. In F# it’s an anonymous function, in C# it’s an anonymous delegate; in C++ it’s in fact an anonymous function object. Whenever you create a new lambda function, the compiler creates a function object for you.

That is equivalent to:

Of course, the lambda functor can be more complicated, when the lambda function captures state from the local scope. However, that is beyond the scope of my post. I recommend that you read more about lambdas in C++ on the VC++ blog.

Question is, what are these lambdas good for? Well, they mostly come in handly with algorithms that take predicates (function objects) as arguments. I’ll try to give you some examples in this post.

Let’s first consider a filter function, that takes a sequence (vector of T) and a predicate that indicates what values should be filtered, and returns a new sequence. That would look like this:

We can use this filter function to extract the odds numbers from a sequence (vector).

You could see in the above example that a second lambda function is used for printing the numbers to the console.

Since the Filter function is a template function it could be used with other types too. In the next example we’ll see how to filter words that have at least 4 letters from a sequence of words.

Let’s consider a second example, a Find (template) function, that takes a sequence and a predicate (that checks a condition for an element), and returns the first element in the sequence for which the predicate returned true.

We’ll use this function to find the first element in a sequence that is greater than a given value.

If you input 4 for instance, it will return 5. If you input 10, an exception will be thrown. You can see that this time the lambda function is [min](int i){return i > min;}. This means that it captures by value the min variable from the local scope, so that it can compare each element with that given value.

The last example I’m going to show is an accumulator function (also known as aggregate or fold). This function takes a sequence of elements, a seed (or initial value) and a function that specifies how to aggregate the elements, and returns the aggregate.

First, we can use it to compute the sum of all elements in a sequence.

The first lambda function above sums the current element with the previous sum, which initially is given as 0. The result is 55. The second lambda function multiplies the current element with the previous product, which initially is 1. The result is 3628800.

But the Aggregate function can be used with other types too. Here is a last example with strings.

These were several examples of how lambda functions help us write more generic, and less verbose code. I suggest you read more about lambdas in C++ here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.