Lecture 3 - Slide 2 : 27 |
C++11 supports lambda expressions, as known from functional programming languages
auto f1 = [](int x)->int{return x + 1;}; // a function that returns x + 1. auto f2 = [m](int x){return x + m;}; // can read m from local context. auto f3 = [=](int x){return x + m + n;}; // can read all local variables from local context. auto f4 = [&m](){++m;}; // access to m by reference - can write. auto f5 = [&](){++m; n--;}; // access to all variables in local context by ref. auto f6 = [&]{++m;}; // the empty parameter list is implicit