Lambda函数捕获变量vs返回值
Lambda function capture a variable vs return value?
我正在学习c++11的新函数lambda函数,我有点困惑。我读到
[] Capture nothing (or, a scorched earth strategy?)
[&] Capture any referenced variable by reference
[=] Capture any referenced variable by making a copy
[=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference
[bar] Capture bar by making a copy; don't copy anything else
[this] Capture the this pointer of the enclosing class
我的问题是捕获变量到底意味着什么,返回值的区别是什么,如果你想捕获一个变量,你必须返回它对吗?
例如:[] () { int a = 2; return a; }
为什么不
int [] () { int a = 2; return a; }
您可以"捕获"属于封闭函数的变量。
void f() {
int a=1;
// This lambda is constructed with a copy of a, at value 1.
auto l1 = [a] { return a; };
// This lambda contains a reference to a, so its a is the same as f's.
auto l2 = [&a] { return a; };
a = 2;
std::cout << l1() << "n"; // prints 1
std::cout << l2() << "n"; // prints 2
}
如果需要,可以返回捕获的变量,也可以返回其他内容。返回值与捕获无关。
捕获列表用于将符号带入 lambda。返回值用于从 lambda返回值。
同样,lambdas使用尾随返回类型语法,如[] () -> int { int a=2; return a; }
。不过通常最好还是隐式推导
lambda函数的一个关键特性是它可以记住声明它的作用域中的内容:
#include <iostream>
#include <vector>
#include <functional>
auto f(int i, int j) {
int k = i * j;
return [=](){ std::cout << k << "n"; };
}
int main() {
std::vector<std::function<void(void)>> v;
v.push_back(f(2,2));
v.push_back(f(6, 7));
for (auto& l: v) {
l();
}
}
请注意,当我们调用lambda时,我们不需要传递任何东西给它。
lambda本质上是围绕函子对象的语法糖,而捕获是向函子添加成员变量的过程:
[](char* p){ *p = toupper(*p); }
struct Lambda1 {
int operator(char* p) const { *p = toupper(*p); }
};
int x = 42;
int y = 5;
[x, &y](int i){ return i * (x + y); }
struct Lambda2 {
int x_;
int& y_;
Lambda2(int x, int& y) : x_(x), y_(y) {}
int operator(int i) { return i*(x_ + y_); }
};
从lambda返回值是可选的,但是返回值是lambda的输出,而不是输入。
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- C++类函数返回值 VS 对本地数据进行操作
- VS 2010 中返回值的编译器优化
- 从函数返回值:引用vs结构
- 在函数外部返回值时使用move vs copy
- Lambda函数捕获变量vs返回值
- 原始函数指针vs boost::bind返回值