Lambda functions (since C++11)

From cppreference.com
< cpp‎ | language

Constructs a closure: an unnamed function object capable of capturing variables in scope.

Contents

[edit] Syntax

[ capture-list ] ( params ) mutable(optional) exception attribute -> ret { body } (1)
[ capture-list ] ( params ) -> ret { body } (2)
[ capture-list ] ( params ) { body } (3)
[ capture-list ] { body } (4)

1) Full declaration

2) Declaration of a const lambda: the objects captured by copy cannot be modified.

3) Omitted trailing-return-type: the return type of the closure's operator() is determined according to the following rules:

  • otherwise, the return type is void
(until C++14)

The return type is deduced from return statements as if for a function whose return type is declared auto.

(since C++14)

4) Omitted parameter list: function takes no arguments, as if the parameter list was ()

[edit] Explanation

mutable - allows body to modify the parameters captured by copy, and to call their non-const member functions
exception - provides the exception specification or the noexcept clause for operator() of the closure type
attribute - provides the attribute specification for operator() of the closure type
capture-list - a comma-separated list of zero or more captures, see below.

A list of symbols can be passed as follows:

  • [a,&b] where a is captured by value and b is captured by reference.
  • [this] captures the this pointer by value
  • [&] captures all automatic variables mentioned in the body of the lambda by reference
  • [=] captures all automatic variables mentioned in the body of the lambda by value
  • [] captures nothing
params - The list of parameters, as in named functions, except that default arguments are not allowed. if auto is used as a type of a parameter, the lambda is a generic lambda (since C++14)
ret - Return type. If not present it's implied by the function return statements ( or void if it doesn't return any value)
body - Function body

The lambda expression constructs an unnamed prvalue temporary object of unique unnamed non-union non-aggregate type, known as closure type, which is declared (for the purposes of ADL) in the smallest block scope, class scope, or namespace scope that contains the lambda expression. The closure type has the following members:

ClosureType::operator()(params)

ret operator()(params) const { body }
(the keyword mutable was not used)
ret operator()(params) { body }
(the keyword mutable was used)
template<template-params>
ret operator()(params) { body }
(since C++14)
(generic lambda)

Executes the body of the lambda-expression, when invoked. When accessing a variable, accesses its captured copy (for the entities captured by copy), or the original object (for the entities captured by reference). Unless the keyword mutable was used in the lambda-expression, the function-call operator is const-qualified and the objects that were captured by copy are non-modifiable from inside this operator(). The function-call operator is never volatile-qualified and never virtual.

For every parameter in params whose type is specified as auto, an invented template parameter is added to template-params, in order of appearance. The invented template parameter may be a parameter pack if the corresponding function member of params is a function parameter pack.

// generic lambda, operator() is a template with two parameters
auto glambda = [](auto a, auto&& b) { return a < b; };
bool b = glambda(3, 3.14); // OK
// generic lambda, operator() is a template with one parameter
auto vglambda = [](auto printer) {
    return [=](auto&&... ts) { // generic lambda, ts is a parameter pack
        printer(std::forward<decltype(ts)>(ts)...);
        return [=]() { printer(ts...); }; // capture-less lambda
    };
};
auto p = vglambda( [](auto v1, auto v2, auto v3) {
                          std::cout << v1 << v2 << v3;
                      } );
auto q = p(1, 'a', 3.14); // outputs 1a3.14
q();                      // outputs 1a3.14
(since C++14)

the exception specification exception on the lambda-expression applies to the function-call operator or operator template.

For the purpose of name lookup, determining the type and value of the this pointer and for accessing non-static class members, the body of the closure type's function call operator is considered in the context of the lambda-expression.

struct X {
   int x, y;
   int operator()(int);
   void f() {
     // the context of the following lambda is the member function X::f
     [=]()->int {
         return operator()(this->x + y); // X::operator()(this->x + (*this).y)
                                         // this has type x*
         };
    }
};

Dangling references

If an entity is captured by reference, implicitly or explicitly, and the function call operator of the closure object is invoked after the entity's lifetime has ended, undefined behavior occurs. The C++ closures do not extend the lifetimes of the captured references.

ClosureType::operator ret(*)(params)()

typedef ret(*F)(params);
operator F() const;
(capture-less non-generic lambda)
template<template-params> using fptr_t = /*see below*/;

template<template-params>

operator fptr_t<template-params>() const;
(since C++14)
(capture-less generic lambda)

This user-defined conversion function is only defined if the capture list of the lambda-expression is empty.

A generic captureless lambda has user-defined conversion function template with the same invented template parameter list as the function-call operator template. If the return type is empty or auto, it is obtained by return type deduction on the function template specialization, which, in turn, is obtained by template argument deduction for conversion function templates.

void f1(int (*)(int)) { }
void f2(char (*)(int)) { }
void h(int (*)(int)) { } // #1
void h(char (*)(int)) { } // #2
auto glambda = [](auto a) { return a; };
f1(glambda); // OK
f2(glambda); // error: not convertible
h(glambda); // OK: calls #1 since #2 is not convertible
 
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
(since C++14)

The value returned by this conversion function is a pointer to a function with C++ language linkage that, when invoked, has the same effect as invoking the closure object's function call operator directly.

ClosureType::ClosureType()

ClosureType() = delete;
ClosureType(const ClosureType& ) = default;
ClosureType(ClosureType&& ) = default;

Closure types are not DefaultConstructible. The copy constructor and the move constructor are implicitly-declared and may be implicitly-defined according to the usual rules for implicit copy constructors and move constructors.

ClosureType::operator=(const ClosureType&)

ClosureType& operator=(const ClosureType&) = delete;

Closure types are not CopyAssignable.

ClosureType::~ClosureType()

~ClosureType() = default;

The destructor is implicitly-declared.

ClosureType::CapturedParam

T1 a;

T2 b;

...

If the lambda-expression captures anything by copy (either implicitly with capture clause [=] or explicitly with a capture that does not include the character &, e.g. [a, b, c]), the closure type includes unnamed non-static data members, declared in unspecified order, that hold copies of all entities that were so captured.

Those data members that correspond to captures without initializers are direct-initialized when the lambda-expression is evaluated. Those that correspond to captures with initializers are initialized as the initializer requires (could be copy- or direct-initialization). If an array is captured, array elements are direct-initialized in increasing index order. The order in which the data memebrs are initialized is the order in which they are declared (which is unspecified).

The type of each data member is the type of the corresponding captured entity, except if the entity has reference type (in that case, references to functions are captured as-is, and references to objects are captured as copies of the referenced objects).

For the entities that are captured by reference (with the default capture [&] or when using the character &, e.g. [&a, &b, &c]), it is unspecified if additional data members are declared in the closure type.

Lambda-expressions are not allowed in unevaluated expressions, template arguments, alias declarations, typedef declarations, and anywhere in a function (or function template) declaration except the function body and the function's default arguments.

[edit] Lambda capture

The capture-list is a comma-separated list of zero or more captures, optionally beginning with the capture-default. The only capture defaults are & (implicitly catch the odr-used automatic variables and this by reference) and = (implicitly catch the odr-used automatic variables and this by value).

The syntax of an individual capture in capture-list is

identifier (1)
identifier ... (2)
identifier initializer (3) (C++14)
& identifier (4)
& identifier ... (5)
& identifier initializer (6) (C++14)
this (7)
1) simple by-copy capture
2) by-copy capture that is a pack expansion
3) by-copy capture with an initializer
4) simple by-reference capture
5) by-reference capture that is a pack expansion
6) by-reference capture with an initializer
7) capture for the this pointer

If a capture-default is used, no other captures may use the same capture type. Any capture may appear only once.

struct S2 { void f(int i); };
void S2::f(int i) {
    [&, i]{ }; // OK
    [&, &i]{ }; // error: i preceded by & when & is the default
    [=, this]{ }; // error: this when = is the default
    [i, i]{ }; // error: i repeated
}

Only lamdbda-expressions defined at block scope may have a capture-default or captures without initializers. For such lambda-expression, the reaching scope is defined as the set of enclosing scopes up to and including the innermost enclosing function (and its parameters). This includes nested block scopes and the scopes of enclosing lambdas if this lambda is nested.

The identifier in any capture without an initializer (other than the this-capture) is looked up using usual unqualified name lookup in the reaching scope of the lambda. Such name is explicitly captured.

A capture with an initializer acts as if it declares and explicitly captures a variable declared with type auto, whose declarative region is the body of the lambda expression (that is, it is not in scope within its initializer), except that:

  • if the capture is by-copy, the non-static data member of the closure object is another way to refer to that auto variable.
  • if the capture is by-reference, the reference variable's lifetime ends when the lifetime of the closure object ends

This is used to capture move-only types with a capture such as x = std::move(x)

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

If a capture list has a capture-default and does not explicitly capture this or an automatic variable, captures it implicitly if

  • the body of the lambda odr-uses the variable or this
  • or the variable or this is named in a potentially-evaluated expression within an expression that depends on a generic lambda parameter
void f(int, const int (&)[2] = {}) { } // #1
void f(const int&, const int (&)[1]) { } // #2
void test() {
    const int x = 17;
    auto g = [](auto a) {
                  f(x); // OK: calls #1, does not capture x
             };
    auto g2 = [=](auto a) {
                  int selector[sizeof(a) == 1 ? 1 : 2]{};
                  f(x, selector); // OK: is a dependent expression, so captures x
             };
}
(since C++14)

If the body of a lambda odr-uses an entity captured by copy, the member of the closure type is accessed. If it is not odr-using the entity, the access is to the original object:

void f(const int*);
void g() {
    const int N = 10;
    [=] { 
        int arr[N]; // not an odr-use: refers to g's const int N
        f(&N); // odr-use: causes N to be captured (by copy)
               // &N is the address of the closure object's member N, not g's N
    };
}

Within the body of a lambda, any use of decltype on any variable with automatic storage duration is as if it were captured and odr-used, even though decltype itself isn't an odr-use and no actual capture takes place:

void f3() {
    float x, &r = x;
    [=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
        decltype(x) y1; // y1 has type float
        decltype((x)) y2 = y1; // y2 has type float const& because this lambda
                               // is not mutable and x is an lvalue
        decltype(r) r1 = y1;   // r1 has type float& (transformation not considered)
        decltype((r)) r2 = y2; // r2 has type float const&
    };
}

Any entity captured by a lambda (implicitly or explicitly) is odr-used by the lambda-expression (therefore, Implicit capture by a nested lambda triggers implicit capture in the enclosing lambda).

All implicitly-captured variables must be declared within the reaching scope of the lambda.

If a lambda captures this, the nearest enclosing function must be a non-static member function.

If a generic lambda (or an instantiation of a generic capture-less lambda's conversion operator) ODR-uses this or any variable, it is captured by the lambda expression.

void f1(int i) {
    int const N = 20;
    auto m1 = [=]{
            int const M = 30;
            auto m2 = [i]{
                  int x[N][M]; // N and M are not odr-used (OK that they are not captured)
                  x[0][0] = i; // i is explicitly captured by m2
                               // and implicitly captured by m1
            };
     };
 
    struct s1 { // local class within f()
        int f;
        void work(int n) { // non-static member function
            int m = n*n;
            int j = 40;
            auto m3 = [this,m] {
                auto m4 = [&,j] { // error: j not captured by m3
                    int x = n; // error: n implicitly captured by m4
                               // but not captured by m3
                    x += m; // OK: m implicitly captured by m4
                            // and explicitly captured by m3
                    x += i; // error: i is outside of the reaching scope (which ends at work())
                    x += f; // OK: this captured implicitly by m4
                            // and explicitly by m3
                };
            };
       }
    };
}

If a lambda-expression appears in a default argument, it cannot explicitly or implicitly capture anything.

Members of anonymous unions cannot be captured.

If a nested lambda m2 captures something that is also captured by the immediately enclosing lambda m1, then m2's capture is transformed as follows:

  • if the enclosing lambda m1 captures by copy, m2 is capturing the non-static member of m1's closure type, not the original variable or this.
  • if the enclosing lambda m1 by reference, m2 is capturing the original variable or this.
#include <iostream>
int main() 
{
    int a = 1, b = 1, c = 1;
    auto m1 = [a, &b, &c]() mutable { 
      auto m2 = [a, b, &c]() mutable {
        std::cout << a << b << c;
        a = 4; b = 4; c = 4;
      };
      a = 3; b = 3; c = 3;
      m2(); 
    };
    a = 2; b = 2; c = 2;
    m1();                     // calls m2() and prints 123
    std::cout << a << b << c; // prints 234
}


[edit] Example

This example shows (a) how to pass a lambda to a generic algorithm and (b) how objects resulting from a lambda declaration can be stored in std::function objects.

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
 
int main()
{
    std::vector<int> c { 1,2,3,4,5,6,7 };
    int x = 5;
    c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
 
    std::cout << "c: ";
    for (auto i: c) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
 
    // the type of a closure cannot be named, but can be inferred with auto
    auto func1 = [](int i) { return i+4; };
    std::cout << "func1: " << func1(6) << '\n'; 
 
    // like all callable objects, closures can be captured in std::function
    // (this may incur unnecessary overhead)
    std::function<int(int)> func2 = [](int i) { return i+4; };
    std::cout << "func2: " << func2(6) << '\n'; 
}

Output:

c: 5 6 7
func1: 10
func2: 10

[edit] See also

auto specifier specifies a type defined by an expression (C++11)
(C++11)
wraps callable object of any type with specified function call signature
(class template)