Do=在lambda的捕获列表中捕获this指针

Does = in a capture list of a lambda capture the this pointer

本文关键字:this 指针 列表 lambda Do      更新时间:2023-10-16

我现在有这样的

void foo::setup()
{
//this->setSubTitleText("Summary");
button("ok")->onPress = [=](Mtype*)
{
this->bar(this); //Why is the this pointer being recognized here?
};
}

lambda的捕获子句中的=是否允许访问this指针。就我而言,是吗?我的印象是使用this指针,我需要像一样明确地捕捉它

button("ok")->onPress = [=,this](Mtype*)
{
this->bar(this); //Why is the this pointer being recognized here?
};

有什么建议吗?

我认为cppreference.com非常明确地声明了这一点:

Lambda捕获

捕获是零个或多个捕获的逗号分隔列表,可选地从捕获默认值开始。唯一的捕获默认值为

&(通过引用隐式捕获odr使用的自动变量)和

=(通过复制隐式捕获odr使用的自动变量)。

如果捕获默认值存在。如果隐式捕获,则始终由引用,即使捕获默认值为=