Tutorial by Examples

A lambda expression provides a concise way to create simple function objects. A lambda expression is a prvalue whose result object is called closure object, which behaves like a function object. The name 'lambda expression' originates from lambda calculus, which is a mathematical formalism invented...
For lambdas with a single return statement, or multiple return statements whose expressions are of the same type, the compiler can deduce the return type: // Returns bool, because "value > 10" is a comparison which yields a Boolean result auto l = [](int value) { return value &gt...
If you specify the variable's name in the capture list, the lambda will capture it by value. This means that the generated closure type for the lambda stores a copy of the variable. This also requires that the variable's type be copy-constructible: int a = 0; [a]() { return a; // Ok, 'a' ...
C++14 Lambdas can capture expressions, rather than just variables. This permits lambdas to store move-only types: auto p = std::make_unique<T>(...); auto lamb = [p = std::move(p)]() //Overrides capture-by-value of `p`. { p->SomeFunc(); }; This moves the outer p variable into th...
If you precede a local variable's name with an &, then the variable will be captured by reference. Conceptually, this means that the lambda's closure type will have a reference variable, initialized as a reference to the corresponding variable from outside of the lambda's scope. Any use of the v...
By default, local variables that are not explicitly specified in the capture list, cannot be accessed from within the lambda body. However, it is possible to implicitly capture variables named by the lambda body: int a = 1; int b = 2; // Default capture by value [=]() { return a + b; }; // OK;...
c++14 Lambda functions can take arguments of arbitrary types. This allows a lambda to be more generic: auto twice = [](auto x){ return x+x; }; int i = twice(2); // i == 4 std::string s = twice("hello"); // s == "hellohello" This is implemented in C++ by making the closur...
If a lambda's capture list is empty, then the lambda has an implicit conversion to a function pointer that takes the same arguments and returns the same return type: auto sorter = [](int lhs, int rhs) -> bool {return lhs < rhs;}; using func_ptr = bool(*)(int, int); func_ptr sorter_func = ...
A lambda expression evaluated in a class' member function is implicitly a friend of that class: class Foo { private: int i; public: Foo(int val) : i(val) {} // definition of a member function void Test() { auto lamb = [](Foo &foo, int val) ...
Lambda functions in C++ are syntactic sugar that provide a very concise syntax for writing functors. As such, equivalent functionality can be obtained in C++03 (albeit much more verbose) by converting the lambda function into a functor: // Some dummy types: struct T1 {int dummy;}; struct T2 {int ...
Let's say we wish to write Euclid's gcd() as a lambda. As a function, it is: int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } But a lambda cannot be recursive, it has no way to invoke itself. A lambda has no name and using this within the body of a lambda refers to a captured thi...
C++14 Parameter pack unpacking traditionally requires writing a helper function for each time you want to do it. In this toy example: template<std::size_t...Is> void print_indexes( std::index_sequence<Is...> ) { using discard=int[]; (void)discard{0,((void)( std::cout <&...

Page 1 of 1