My top 5 C++11 language features #2 - lambda expressions
The countdown continues with my second favorite new C++ 11 language feature - lambda expressions. Lambda expressions have many uses and have been around in other programming languages (mostly dynamic languages) for a while. Derived from lambda calculus and procedural abstraction, lambda expressions in C++11 (and in other programming languages) are particularly well suited for creating anonymous functions.
The basic syntax to create an anonymous functions with a lambda expressions is:
[](parameters)-> return type {body}
The [] operator is called the capture operator, then an optional parameter list is provided within (), followed by an optional return type (C++11 can infer the return type in most cases), and finally the body within curly braces {}; The capture operator basically tells the compiler to "capture" variables within its current scope to make them available in the lambda expression body.
Here is a simple example of binding an lambda expression to a variable, using the complete syntax, that can be called later as a function.
auto f = [] (std:string& s) -> int {std::cout << s; return 0;};
f("hello lambdas");
Now, let’s go a little further with this and get back to anonymous functions. Let’s say I want to run a quick sorting algorithm on an array. The std::sort expects an initial position and final position in the container class (both in the form of std::RandomAccessIterator) and a comparison function with a proper signature. Before, lambda expressions for anonymous functions I would have had to write and declare a separate function with the correct signature. Now, I can just embed it into my sort call as an anyonymous function.
template <typename X> void sort_vector(std::vector<X>& v)
{
std::sort(v.begin(), v.end(), [] (const X& a, const X& b) {return a < b});
}
So, in addition to closures, lambda expressions enable many other cool features like functors and delegates with much less code and without a pre-processor. How do you envision using lambda expressions?
~/jt
Share This | Email this page to a friend
Posted by J T on November 15th, 2012 under C++, C++11, C++Builder, ISO C++, ednfront |3 Responses to “My top 5 C++11 language features #2 - lambda expressions”
Leave a Comment
You must be logged in to post a comment.


RSS Feed

November 18th, 2012 at 5:50 am
Typo in the syntax description, I think - surely it is
[](parameters)->return-type{body}
ie -> before return-type.
Also, the example is also a bit garbled, isn’t it? I’d expect
auto f = [] (std:string& s)-> int
{std::cout << s; return 0;};
Or am I missing something?
The std::sort() example omits the bool return type - is this allowed?
November 19th, 2012 at 5:42 pm
Good catch on the syntax description and example - fixed them both. Regarding the std::sort function, I believe your question is why did I not specify the return type of bool in that lambda expression? I could have but it is also allowed to not have to specify the return type if it can be inferred. So, in this case when the compiler can figure out a boolean is the result of a comparison operator it can figure it out. BTW, I did test this in our C++11 compiler and it works just fine.
November 21st, 2012 at 3:00 pm
Yes, I was asking if the bool return type was inferred. Ta.