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,

相关推荐
CYRUS_STUDIO8 分钟前
Frida Hook Native:jobjectArray 参数解析
android·c++·逆向
榆榆欸8 分钟前
4.Socket类、InetAddr类、Epoll类实现模块化
linux·c++·tcp/ip
..过云雨16 分钟前
11. 【C++】模板进阶(函数模板特化、类模板全特化和偏特化、模板的分离编译)
开发语言·c++
BC橡木1 小时前
C++ IO流
c++
m0_555762901 小时前
struct 中在c++ 和c中用法区别
java·c语言·c++
月亮有痕迹诶1 小时前
【C++】智能指针
开发语言·c++·c++11
王禄DUT2 小时前
化学方程式配平 第33次CCF-CSP计算机软件能力认证
开发语言·c++·算法
wuqingshun3141592 小时前
蓝桥杯 XYZ
数据结构·c++·算法·职场和发展·蓝桥杯
DreamByte2 小时前
C++菜鸟教程 - 从入门到精通 第五节
开发语言·c++·算法
长流小哥2 小时前
可视化开发:用Qt实现Excel级动态柱状图
开发语言·c++·qt·ui