当前位置:网站首页>Lambda expression
Lambda expression
2022-06-09 08:33:00 【A struggling Xiaobai 1】
1. Introduce
Lambda The expression is an anonymous function , That is, functions without names
yes c++11 Newly added features , Pay attention to whether the compiler supports C++11, The aim is to simplify programming , The underlying implementation can be understood as a functor
2. grammar
[capture list] (parameter list) mutable -> return type { function body }
capture list: Capture list , It must not be omitted
params list: parameter list , Can be passed by value or by reference , It can be omitted
mutable: optional , It can be omitted
ret: Return value , If the compiler can automatically derive the return value type , You can also omit
function body: The body of the function , It must not be omitted
To sum up, the simplest Lambda An expression is shaped like [] {} The appearance of
3. Capture list
[] Don't capture any variables
[&] Capture all variables in the external scope , And use it as a reference in the function body ( Capture by reference )
[=] Capture all variables in the external scope , And use it as a copy in the function weight ( Capture by value )
[=,&foo] Capture all variables in the external scope by value , And capture by reference foo Variable
[&,=foo] Capture all variables in the external scope by reference , And capture by value foo Variable
[bar] Capture by value bar Variable , At the same time, other variables are not captured
[this] Capture... In the current class this The pointer , Let the expression have the same access rights as the current class member function .
notes 1: The capture list is passed by value to capture the constancy , To add mutable The copied copy can only be modified , Capture by reference is not constant , Parameter lists are also not constant .
notes 2: The capture list can only capture local variables in the current scope , Local variables or nonlocal variables outside the scope will report errors .
notes 3:lambda Cannot assign values between expressions , Even if it looks the same type .
4. Use
Law 1: First define a function object to accept anonymous function objects , Then call the function
auto f1 = []() { cout << "Hello world!"; };
f1();
Law 2: Use directly while defining
[]() {cout << "Hello C++!"; } ();Some use of capture lists , Follow the instructions to simply type a few codes , You can understand !
边栏推荐
- 使用闪回数据库(FLASHBACK DATABASE)和还原点(RESTORE POINT)
- The philosophical thought of the process of suffering
- 关于#oracle#的问题:为啥按照晚上教程链接DBMS还是无法输出结果
- 同花顺在上面开户安全吗?靠谱吗?
- 【TeXstudio】【3】较为完整的论文排版模板与bib文件引用方法
- 【TeXstudio】【1】分式dfrac,tfrac Undefined control squence错误
- 2022-2028 global nonlinear node detector industry survey and trend analysis report
- Flyway management database
- extends和super的使用
- 自制编译器学习1:Cb编译器的使用
猜你喜欢

Openinfra summit 2022 𞓜 Android cloud users stand out and are shortlisted for the super user Award

SQL:体育馆的人流量(连续日期案例通用解决方案)

华云数据入选IDC《工业软件与工业互联网发展趋势》云基础设施代表厂商

Compilation_ Common instructions

Huayun data was selected as the representative manufacturer of cloud infrastructure in IDC development trends of industrial software and industrial Internet

Self made compiler learning 3: introduction to flex and bison

Blow up the idea artifact in use recently

【TeXstudio】【3】较为完整的论文排版模板与bib文件引用方法

Nacos二次开发
![Nacos 启动报错[db-load-error]load jdbc.properties error](/img/e0/e511da6cd6821ffb315d118a2feae9.png)
Nacos 启动报错[db-load-error]load jdbc.properties error
随机推荐
Flyway management database
boost1.62.0编译静态库fPIC链接问题
Self made compiler learning 4: using flex
RMAN备份概念_关于RMAN控制文件和服务器参数文件的自动备份
leetcodeSQL:1070. Product sales analysis III
Boot1.62.0 compilation static library FPIC link problem
flyWay管理数据库
extends和super的使用
ELK集群设置账号密码认证
Compilation_ Common instructions
Laravel cross database query
ELK+Filebeat 部署安装
Sql: employees exceeding manager's income
Redlock red lock security debate (Part 1)
自制编译器学习1:Cb编译器的使用
SQL: 销售分析III
搞明白 left join、right join和join的区别
Anatomy of illusory rendering system (15) - XR topic
华为机试,HJ6质数因子
48-OIDC OP配置信息端点