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
![]() | More forms - mutable and explicit return type. |
![]() | An immediate call of a lambda expression. |
![]() | Lambda expressions and recursive calls. |
![]() | A function that returns a function - closure - problems. |
![]() | Same in Scheme - where this kind of stuff works out of the box. |
![]() | A function that returns a function - OK. |
![]() | Get some experience with lambda expressions in C++ |