Take the 2-minute tour ×

What is a lambda expression in C++11? When would I use one? What class of problem do they solve that wasn't possible prior to their introduction?

A few examples, and use cases would be useful.

share|improve this question
I don't understand why people voting for closing this. –  Nawaz Oct 2 '11 at 15:02
I think the question in this form cannot be answered without writing a whole article. However, it could be a valuable FAQ entry. –  pmr Oct 2 '11 at 15:05

3 Answers 3

up vote 427 down vote accepted

The problem


#include <algorithm>
#include <vector>

namespace {
  struct f {
    void operator()(int) {
      // do something

void func(std::vector<int>& v) {
  f f;
  std::for_each(v.begin(), v.end(), f);

If you only use f once and in that specific place it seems overkill to be writing a whole class just to do something trivial and one off.

In C++03 you might be tempted to write something like the following, to keep the functor local:

void func2(std::vector<int>& v) {
  struct {
    void operator()(int) {
       // do something
  } f;

however this is not allowed, f cannot be passed to a template function in C++03.

The new solution

C++11 introduces lambdas allow you to write an inline, anonymous functor to replace the struct f. For small simple examples this can be cleaner to read (it keeps everything in one place) and potentially simpler to maintain, for example in the simplest form:

void func3(std::vector<int>& v) {
  std::for_each(v.begin(), v.end(), [](int) { /* do something here*/ });

Lambda functions are just syntactic sugar for anonymous functors.

Return types

In simple cases the return type of the lambda is deduced for you, e.g.:

void func4(std::vector<double>& v) {
  std::transform(v.begin(), v.end(), v.begin(),
                 [](double d) { return d < 0.00001 ? 0 : d; }

however when you start to write more complex lambdas you will quickly encounter cases where the return type cannot be deduced by the compiler, e.g.:

        [](double d) {
            if (d < 0.0001) {
                return 0;
            } else {
                return d;

To resolve this you are allowed to explicitly specify a return type for a lambda function, using -> T:

        [](double d) -> double {
            if (d < 0.0001) {
                return 0;
            } else {
                return d;

"Capturing" variables

So far we've not used anything other than what was passed to the lambda within it, but we can also use other variables, within the lambda. If you want to access other variables you can use the capture clause (the [] of the expression), which has so far been unused in these examples, e.g.:

void func5(std::vector<double>& v, const double& epsilon) {
        [epsilon](double d) -> double {
            if (d < epsilon) {
                return 0;
            } else {
                return d;

You can capture by both reference and value, which you can specify using = and &:

  • [&epsilon] capture by reference
  • [&, epsilon] specify that the default way of capturing is by reference and what we want to capture
  • [=, &epsilon] capture by value by default, but for epsilon use reference instead

The generated operator() is const by default, with the implication that captures will be const when you access them by default. This has the effect that each call with the same input would produce the same result, however you can mark the lambda as mutable to request that the operator() that is produced is not const.

share|improve this answer
Does mutable make operator() non-const, or does it make the copies of the local variables mutable? Conclusion it simply makes the state of the lambda mutable, operator() remains const. –  Yakk Mar 13 '13 at 13:44
@Yakk you have been trapped. lambdas without a capture have an implicit conversion to function type pointers. the conversion function is const always... –  Johannes Schaub - litb Mar 31 '13 at 22:17
@JohannesSchaub-litb oh sneaky -- and it happens when you invoke () -- it is passed as a zero-argument lambda, but because () const doesn't match the lambda, it looks for a type conversion which allows it, which includes implicit-cast-to-function-pointer, and then calls that! Sneaky! –  Yakk Apr 1 '13 at 0:55
great explanation, thanks a lot! –  platzhersh Jan 16 at 20:02
Interesting - I originally thought that lambdas were anonymous functions rather than functors, and was confused about how captures worked. –  immibis Mar 9 at 1:39

What is a lambda function?

The C++ concept of a lambda function originates in the lambda calculus and functional programming. A lambda is an unnamed function that is useful (in actual programming, not theory) for short snippets of code that are impossible to reuse and are not worth naming.

In C++ a lambda function is defined like this

[]() { } // barebone lambda

or in all its glory

[]() mutable -> T { } // T is the return type, still lacking throw()

[] is the capture list, () the argument list and {} the function body.

The capture list

The capture list defines what from the outside of the lambda should be available inside the function body and how. It can be either:

  1. a value: [x]
  2. a reference [&x]
  3. any variable currently in scope by reference [&]
  4. same as 3, but by value [=]

You can mix any of the above in a comma separated list [x, &y].

The argument list

The argument list is the same as in any other C++ function.

The function body

The code that will be executed when the lambda is actually called.

Return type deduction

If a lambda has only one return statement, the return type can be omitted and has the implicit type of decltype(return_statement).


If a lambda is marked mutable (e.g. []() mutable { }) it is allowed to mutate the values that have been captured by value.

Use cases

The library defined by the ISO standard benefits heavily from lambdas and raises the usability several bars as now users don't have to clutter their code with small functors in some accessible scope.


In C++14 lambdas have been extended by various proposals.

Initialized Lambda Captures

An element of the capture list can now be initialized with =. This allows renaming of variables and to capture by moving. An example taken from the standard:

int x = 4;
auto y = [&r = x, x = x+1]()->int {
            r += 2;
            return x+2;
         }();  // Updates ::x to 6, and initializes y to 7.

and one taken from Wikipedia showing how to capture with std::move:

auto ptr = std::make_unique<int>(10); // See below for std::make_unique
auto lambda = [ptr = std::move(ptr)] {return *ptr;};

Generic Lambdas

Lambdas can now be generic (auto would be equivalent to T here if T were a type template argument somewhere in the surrounding scope):

auto lambda = [](auto x, auto y) {return x + y;};

Improved Return Type Deduction

C++14 allows deduced return types for every function and does not restrict it to functions of the form return expression;. This is also extended to lambdas.

share|improve this answer
Great and clear answer! –  Drazick May 26 '12 at 11:47
Your outlined, broken-down answer makes for very concise reading and easy understanding. +1 for well thought-through answer! –  David Peterson Feb 12 '13 at 18:29
I learned something today... thanks pmr for a day not wasted :) –  Dmitry Matveev May 23 at 10:37
I found this much more useful than the accepted answer. Thanks! –  DC_ Jun 23 at 12:20

Lambda expressions are typically used to encapsulate algorithms so that they can be passed to another function. However, it is possible to execute a lambda immediately upon definition:

[&](){ ...your code... }(); // immediately executed lambda expression

is functionally equivalent to

{ ...your code... } // simple code block

This makes lambda expressions a powerful tool for refactoring complex functions. You start by wrapping a code section in a lambda function as shown above. The process of explicit parameterization can then be performed gradually with intermediate testing after each step. Once you have the code-block fully parameterized (as demonstrated by the removal of the &), you can move the code to an external location and make it a normal function.

Similarly, you can use lambda expressions to initialize variables based on the result of an algorithm...

int a = []( int b ){ int r=1; while (b>0) r*=b--; return r; }(5); // 5!

As a way of partitioning your program logic, you might even find it useful to pass a lambda expression as an argument to another lambda expression...

[&]( std::function<void()> algorithm ) // wrapper section
   ...your wrapper code...
   ...your wrapper code...
([&]() // algorithm section
   ...your algorithm code...

Lambda expressions also let you create named nested functions*, which can be a convenient way of avoiding duplicate logic. Using named lambdas also tends to be a little easier on the eyes (compared to anonymous inline lambdas) when passing a non-trivial function as a parameter to another function. Note: don't forget the semicolon after the closing curly brace.

auto algorithm = [&]( double x, double m, double b ) -> double
   return m*x+b;

int a=algorithm(1,2,3), b=algorithm(4,5,6);

If subsequent profiling reveals significant initialization overhead for the function object, you might choose to rewrite this as a normal function.

share|improve this answer
Have you realized that this question was asked 1.5 years ago and that the last activity was almost 1 year ago? Anyway, you're contributing some interesting ideas I haven't seen before! –  Piotr99 Mar 1 '13 at 8:32
Thanks for the simultaneous define-and-execute tip! I think it's worth noting that that works in as a contidion for if statements: if ([i]{ for (char j : i) if (!isspace(j)) return false ; return true ; }()) // i is all whitespace, assuming i is an std::string –  Blacklight Shining Mar 2 '13 at 1:13
So the following is a legal expression: [](){}();. –  nobar Apr 13 '13 at 22:35
Ugh! Python's (lambda: None)() syntax is so much more legible. –  dan04 May 30 '13 at 3:28
@MarkLakata: A nested function can be declared inside another function, and can even reference the outer function's data if desired. –  nobar May 2 at 0:52

Your Answer


