Lambda expressions in C++ (C++ 中的 lambda 表达式)

Lambda expressions in C++ {C++ 中的 lambda 表达式}

  • [1. Parts of a lambda expression (Lambda 表达式的各个部分)](#1. Parts of a lambda expression (Lambda 表达式的各个部分))

    • [1.2. Parameter list (Optional)](#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:

  1. capture clause (Also known as the lambda-introducer in the C++ specification)

  2. parameter list Optional (可选) (Also known as the lambda declarator)

  3. mutable specification Optional (可选)

  4. exception-specification Optional (可选)

  5. trailing-return-type Optional (可选)

  6. 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,

相关推荐
Demon--hx21 小时前
[C++]迭代器失效问题
前端·c++
liulilittle21 小时前
C++ 计算当前时区偏移量秒数(GMT/UNIX偏移量)
linux·c++·unix
再睡一夏就好21 小时前
深入理解Linux程序加载:从ELF文件到进程地址空间的完整旅程
linux·运维·服务器·c++·学习·elf
lijiatu1008621 小时前
[C++] 上锁、解锁、获取锁、释放锁的区别
开发语言·c++
阿沁QWQ21 小时前
STL和string实现
开发语言·c++
乌萨奇也要立志学C++1 天前
【Linux】线程概念 线程与进程深度剖析:虚实内存转换、实现机制与优缺点详解
linux·c++
爱学习的小邓同学1 天前
数据结构 --- 二叉搜索树
数据结构·c++
Wild_Pointer.1 天前
高效工具实战指南:CMake构建工具
c++·软件构建
可峰科技1 天前
Apriltag_ros CMakeList.txt一句话导致其他包编译失败
c++
code bean1 天前
【C++ 】C++ 与 C#:using 关键字、命名空间及作用域解析符对比
开发语言·c++·c#