📜 ⬆️ ⬇️

Lambda expressions are now in C ++

image
Many programming languages ​​allow you to create on-the-fly local non-named functions within expressions. These languages ​​include C # 3.0, Python, ECMAScript, and virtually all functional programming languages ​​(for example, Haskell and Scheme). Such functions are usually referred to as lambda functions that are widely used.

C ++ 0x is the unofficial name of the new C ++ language standard, which is planned to replace the existing standard (ISO / IEC 14882), which was published in 1998 and updated in 2003. The predecessors are also known as C ++ 98 and C + +03. The new standard will add several additions to the language and expand the standard C ++ library. One of the language extensions is the possibility of using lambda expressions. I am very pleased that today this opportunity has been implemented for VisualStudio 2010 and GCC.
imageimage

Lambda expressions - a programming technique that combines the advantages of function pointers and functional objects, avoiding inconvenience. Like functional objects, lambda expressions allow storing states, but their compact syntax, unlike functional objects, does not require a class declaration. Lambda expressions allow you to write more compact code and avoid mistakes, rather than using functional objects.

I recall that functional objects are ordinary objects with an overloaded “()” operator. Thus, from the point of view of syntax, they are ordinary functions.
')
First, let's look at an example using a function pointer. Imagine that we need to fill the array with numbers from 0 to 19, one of the options for filling the array using the current standard is given below.
image

The disadvantage of this solution is that we cannot track the state of our “value” variable outside the function.
Now we implement the same task with the help of a functional object. An implementation option is given below.
image
This technique works great and allows you to track the state of the variable “n”, but it requires a class declaration.

And now let's see how easy it is to perform the same operation using a lambda expression.
image

Syntax lambda expression:
image
1) Mask of variables
2) List of parameters
3) Change the parameter passed by value
4) Exception specification
5) Return Type
6) Body lambda expression

Variable mask
Lambda expression can access almost any variable and use it inside the body. The snapshot determines how the parameters are obtained by the lambda expression body Access to variables preceded by an ampersand (&) is provided by reference, and before which there is no ampersand, respectively, by value. An empty mask ([]) means that the body of the expression does not have access to the variables.

The standard mask determines whether we access the variable by reference or by value. By setting “&”, all values ​​will be obtained by reference. Using the same “=”, variables will be obtained by value. For example, if the body of a lambda expression gets access to the external variable “total” by reference and variable “factor” by value, the mask should be written as follows:
image
Also, you can use a lambda expression inside a class method. By passing the “this” pointer, you can access the methods and members of this class.

Parameter list
The parameter list of the lambda expression contains a list of parameters for the function, which have the following restrictions:
1) Parameter list cannot contain default values.
2) Cannot contain unnamed parameters
3) A limited number of parameters
image
Also, a lambda expression can take another lambda expression as a parameter. On the other hand, the parameter list is optional elements, and you can write an expression, not including it.
image
Change parameter passed by value
The specification allows the body to allow lambda expressions and replace the variables obtained by value.
image
Exception specification
Lambda expressions can include an exception specification that allows the body to not create an exception.
image
Return type
The specification defines the return type of lambda expression. You can not use this construct if the body of the expression has only one “return” statement or does not return values ​​at all. If the body contains only one “return” statement, the compiler will set the return type of the lambda expression identical to the type of the “return” statement.
image
Body lambda expression
Identical restrictions are imposed on the body of an expression, as in a code block or method.

This feature is already available in VisualStudio 2010 and GCC, I think it will help you to make your code even better.

Source: https://habr.com/ru/post/65798/


All Articles