In Visual C++, a lambda expression—referred to as a lambda—is like an anonymous function that maintains state and can access the variables that are available to the enclosing scope. It does this by defining a class and constructing an object of that type. This article defines what lambdas are, compares them to other programming techniques, describes their advantages, and provides a basic example.
Many programming languages support the concept of an anonymous function, which is a function that has a body, but doesn't have a name. A lambda is a programming technique that's related to anonymous functions. A lambda implicitly defines a function object class and constructs a function object of that class type. For more information about function objects, see Function Objects.
As an introductory example of a lambda, the ISO C++ Standard shows one used in the context of a parameter passed to the std::sort() function:
This article explains how this expression works.
When you write code, you probably use function pointers and function objects to solve problems and perform calculations, especially when you use STL algorithms. Function pointers and function objects have advantages and disadvantages—for example, function pointers have minimal syntactic overhead but do not retain state within a scope, and function objects can maintain state but require the syntactic overhead of a class definition.
A lambda combines the benefits of function pointers and function objects and avoids their disadvantages. Like a function objects, a lambda is flexible and can maintain state, but unlike a function object, its compact syntax doesn't require a class definition. By using lambdas, you can write code that's less cumbersome and less prone to errors than the code for an equivalent function object.
The following examples compare the use of a lambda to the use of a function object. The first example uses a lambda to print to the console whether each element in a vectorobject is even or odd. The second example uses a function object to accomplish the same task.
This example uses a lambda that's embedded in the for_each function call to print to the console whether each element in a vector object is even or odd.
Sometimes a lambda would be too unwieldy to extend much further than the previous example. The next example uses a function object instead of a lambda, together with thefor_each function, to produce the same results as Example 1. Both examples store the count of even numbers in a vector object. To maintain the state of the operation, theFunctorClass class stores the m_evenCount variable by reference as a member variable. To perform the operation, FunctorClass implements the function-call operator,operator(). The Visual C++ compiler generates code that is comparable in size and performance to the lambda code in Example 1. For a basic problem like the one in this article, the simpler lambda design is probably better than the function-object design. However, if you think that the functionality might require significant expansion in the future, then use a function object design so that code maintenance will be easier.