Lambda expressions in C++ (C++ 中的 lambda 表达式)
Lambda expressions in C++ {C++ 中的 lambda 表达式}
- 1. Parts of a lambda expression (lambda 表达式的各个部分)
- 1.2. Parameter list (Optional)
- References
lambda /ˈlæm.də/:the 11th letter of the Greek alphabet (希腊语字母表的第 11 个字母)
https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp
1. Parts of a lambda expression (lambda 表达式的各个部分)
This illustration shows the parts of lambda syntax:
-
capture clause
(Also known as thelambda-introducer
in the C++ specification) -
parameter list
Optional (可选) (Also known as thelambda declarator
) -
mutable specification
Optional (可选) -
exception-specification
Optional (可选) -
trailing-return-type
Optional (可选) -
lambda body
1.2. Parameter list (Optional)
Lambdas can both capture variables and accept input parameters.
lambda 既可以捕获变量,也可以接受输入参数。
A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function.
参数列表 (在标准语法中称为 lambda 声明符) 是可选的,它在大多数方面类似于函数的参数列表。
#include <stdio.h>int main(void) {auto add_func = [](const int first, const int second) {return first + second;};const int result = add_func(2, 3);printf("result = %d\n", result);return 0;
}
result = 5
请按任意键继续. . .
lambda could own default arguments.
#include <stdio.h>int main(void) {auto add_func = [](const int first, const int second = 12) {return first + second;};const int result = add_func(3);printf("result = %d\n", result);return 0;
}
result = 15
请按任意键继续. . .
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/