Wednesday 23 November 2011

C++11 Lambdas


Lambdas are unnamed function object classes which define a function call operator



lambda-introducer
The [] is the lambda-introducer for the stateless lambda, and it tells the compiler that a lambda expression is beginning


lambda-parameter-declaration
The (int n) is the lambda-parameter-declaration, which tells the compiler what the unnamed function object class's function call operator should take. This syntatically consists of: ( lambda-parameter-declaration-listopt ) mutableopt  exception-specificationopt lambda-return-type-clauseopt

lambda-compound-statement
The { cout << n << " "; } is the lambda-compound-statement which serves as the body of the unnamed function object class's function call operator.  By default, the unnamed function object class's function call operator returns void. 


lambda-return-type-clause: an optional return type
If a lambda's compound-statement is { return expression; } , then the lambda's return type will be automatically deduced to be the type of expression
eg:
    transform(v.begin(), v.end(), front_inserter(d), [](int n) { return n * n * n; });

If you don't start the lambda-compound-statement with return, you must explicitly state the return type, with -> type


eg:
    transform(v.begin(), v.end(), front_inserter(d), [](int n) -> double {
        if (n % 2 == 0) {
            return n * n * n;
        } else {
            return n / 2.0; 
        }
    });


Passing state into the lambda
If you want to have state passed into your lambda, you must declare your local variables
You can have stateful lambdas too, and this is accomplished through "capturing" local variables.  The empty lambda-introducer [] says "I am a stateless lambda".  But within the lambda-introducer, you can specify a capture-list:



eg:
    v.erase(remove_if(v.begin(), v.end(), [x, y](int n) { return x < n && n < y; }), v.end());


The compound-statement { return x < n && n < y; } serves as the body of the function call operator within that class.  Although the compound-statement is lexically within the scope of main(), it is conceptually outside the scope of main(), so you can't use local variables from main() without capturing them within the lambda.
Note that
(a) the captured copies can't be modified within the lambda, because by default the function call operator is const
(b) some objects are expensive to copy
(c) updates to the local variables will not be reflected in the captured copies (You can clearly see that the captures are "by value")


Passing all state into the lambda
You can also "capture everything by value".  The syntax for this is the lambda-introducer [=] (the capture-default = is supposed to make you think of assignment or copy-initialization Foo foo = bar;)

eg:
    v.erase(remove_if(v.begin(), v.end(), [=](int n) { return x < n && n < y; }), v.end());


When the compiler sees x and y mentioned within the lambda, it captures them from the surrounding scope by value.



Modifying the captured state

By default, a lambda's function call operator is const, but you can make it non-const by saying mutable:

eg:
    for_each(v.begin(), v.end(), [=](int& r) mutable {
        const int old = r;

        r *= x * y;

        x = y;
        y = old;
    });

Modifying external state

Capture by reference.  The syntax for doing this is the lambda-introducer [&x, &y]


eg:

    for_each(v.begin(), v.end(), [&x, &y](int& r) {
        const int old = r;

        r *= x * y;

        x = y;
        y = old;
    });


Passing all state into the lambda by reference

You can also "capture everything by reference".  The syntax for this is the lambda-introducer [&]




Modifying both the captured and external state

You can capture by reference and make the lambda mutable


Mix capture by value and capture by reference
You can specify some (or all: '=') parameters by value, and some by reference
eg:
    for_each(v.begin(), v.end(), [=, &sum, &product](int& r) mutable {
        sum += r;

        if (r != 0) {
            product *= r;
        }

        const int old = r;

        r *= x * y;

        x = y;
        y = old;
    });

The opposite lambda-introducer [&, x, y] would produce exactly the same result (capture everything by reference, except x and y by value).

Note that the lambda expression syntax only allows you to capture local variables. Global or class member variables are not allowed.
Note that this is a local variable, so you can pass this to a lambda - and that allows you to implicitly access member variables (without having to dereference this)
Passing everything by value [=] will implicitly capture this.

eg:
    for_each(v.begin(), v.end(), [this](int n) {
            cout << "If you gave me " << n << " toys, I would have " << n + m_toys << " toys total." << endl;
        });

Nullary lambdas
If you want a nullary lambda (taking no arguments), you can elide the lambda-parameter-declaration entirely.

eg:
    generate_n(back_inserter(v), 10, [&] { return i++; });

Note that if you want to say mutable or -> ReturnType, you need empty parentheses between that and the lambda-introducer. (you can't elide the arguments).

Storing lambdas
You can store lambdas using the auto construct


eg:
    auto g = [](int n) { cout << n * n * n << " "; };
    g(5);

You can also store lambdas in a matching std::tr1::function object, but this comes with overhead


eg:
    std::tr1::function<void (int)> g = [](int n) { cout << n * n * n << " "; };

No comments:

Post a Comment