BWG*_*BWG 25 c++ memory lambda
这个例子怎么可能有效呢?它打印6:
#include <iostream>
#include <functional>
using namespace std;
void scopeIt(std::function<int()> &fun) {
int val = 6;
fun = [=](){return val;}; //<-- this
}
int main() {
std::function<int()> fun;
scopeIt(fun);
cout << fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
完成调用6后存储的值在哪里scopeIt?如果我[=]用a 替换[&]它,它打印0而不是6.
Dre*_*ann 20
It is stored within the closure, which - in your code - is then stored within std::function<int()> &fun.
A lambda generates what's equivalent to an instance of a compiler generated class.
This code:
[=](){return val;}
Run Code Online (Sandbox Code Playgroud)
Generates what's effectively equivalent to this... this would be the "closure":
struct UNNAMED_TYPE
{
UNNAMED_TYPE(int val) : val(val) {}
const int val;
// Above, your [=] "equals/copy" syntax means "find what variables
// are needed by the lambda and copy them into this object"
int operator() () const { return val; }
// Above, here is the code you provided
} (val);
// ^^^ note that this DECLARED type is being INSTANTIATED (constructed) too!!
Run Code Online (Sandbox Code Playgroud)
Cor*_*lks 16
C++中的Lambdas实际上只是"匿名"结构函子.所以当你写这个:
int val = 6;
fun = [=](){return val;};
Run Code Online (Sandbox Code Playgroud)
编译器将其转换为:
int val = 6;
struct __anonymous_struct_line_8 {
int val;
__anonymous_struct_line_8(int v) : val(v) {}
int operator() () const {
return val; // returns this->val
}
};
fun = __anonymous_struct_line_8(val);
Run Code Online (Sandbox Code Playgroud)
然后,std::function通过类型擦除存储该仿函数.
当您使用[&]而不是[=],它将结构更改为:
struct __anonymous_struct_line_8 {
int& val; // Notice this is a reference now!
...
Run Code Online (Sandbox Code Playgroud)
所以现在对象存储对函数对象的引用,该val对象在函数退出后变为悬空(无效)引用(并且您得到未定义的行为).
所谓的闭包类型(lambda表达式的类类型)具有每个捕获实体的成员.这些成员是按值捕获的对象,以及通过引用捕获的引用.它们使用捕获的实体进行初始化,并在闭包对象(此lambda指定的闭包类型的特定对象)中独立存在.
与值捕获相对应的未命名成员使用闭包类型的内部进行val初始化val和访问operator(),这很好.闭包对象可能很容易被复制或移动多次,直到发生这种情况,这也很好 - 闭包类型具有隐式定义的移动和复制构造函数,就像普通类一样.
然而,通过参考捕获时,在调用时被隐式执行左值到右值转换fun在main诱导未定义的行为作为该基准部件称为已经被销毁的对象 -即,我们使用的是悬空参考.
| 归档时间: |
|
| 查看次数: |
3113 次 |
| 最近记录: |