C++11 adds a lot of features, lambda expressions are one of them, if you want to understand the c++11 full features, it is recommended to go here, here, here, and see here. This article, as the last blog in May, will introduce the lambda expression of c++11.
Lambda expressions are available in many languages, such as Python,java 8. Lambda expressions make it easy to construct anonymous functions, and if you have a large number of small functions in your code that are typically called only once, you might want to re-construct them as lambda expressions.
The c++11 lambda expression specification is as follows:
[ ] Capture ( params ) mutable Exception attribute -> ret { body } |
(1) |
|
[ ] Capture ( ) params -> ret { body } |
(2) |
|
[ ] Capture ( ) params { body } |
(3) |
|
[ ] Capture { Body } |
(4) |
|
which
- (1) is a complete lambda expression form,
- (2) A lambda expression of type Const, which cannot be changed to a value in the Capture ("capture") list.
- (3) A lambda expression with a return value type is omitted, but the return type of the lambda expression can be pushed by the following rules:
- If a lambda code block contains a return statement, the return type of the lambda expression is determined by the return type of the return statement.
- If there is no return statement, a void f (...) function is similar.
- The argument list is omitted, similar to the parameterless function f ().
The mutable modifier describes the code in the body of a lambda expression to modify the captured variable and to access the Non-const method of the captured object.
Exception indicates whether the lambda expression throws an exception ( noexcept
), and what exception is thrown, similar to void F() throw(X, Y).
attribute used to declare properties.
In addition,capture specifies a list of external variables visible within the code of a lambda expression within the visible domain scope, as explained below:
[a,&b]
A variable is captured in the form of a value, and B is captured as a reference.
[this]
Captures the this pointer in a value manner.
[&]
Captures all external automatic variables in a reference manner.
[=]
All external automatic variables are captured in a value manner.
[]
No external variables are captured.
In addition, theparams specifies the parameters of the lambda expression.
An example of a specific c++11 lambda expression:
#include <vector> #include <iostream> #include <algorithm> #include <functional> int main () { std::vector<int> c {1,2,3,4,5,6,7}; int x = 5; C.erase (Std::remove_if (C.begin (), C.end (), [x] (int n) {return n < x;}), C.end ()); Std::cout << "C:"; for (auto i:c) { std::cout << i << '; } Std::cout << ' \ n '; The type of a closure cannot is named, but can is inferred with auto auto func1 = [] (int i) {return i+4;}; std::cout << "func1:" << func1 (6) << ' \ n '; Like all callable objects, closures can is captured in std::function //(this may incur unnecessary overhead)
std::function<int (int) > FUNC2 = [] (int i) {return i+4;}; Std::cout << "Func2:" << func2 (6) << ' \ n ';}
c++11 lambda expression