【问题标题】:Does = in a capture list of a lambda capture the this pointer= 在 lambda 的捕获列表中是否捕获 this 指针
【发布时间】:2017-10-24 22:12:30
【问题描述】:

我目前有这样的东西

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?
        };

有什么建议吗?

【问题讨论】:

  • 是的....确实如此。
  • @ChrisDrew 我认为与 = 它仅按值捕获封闭范围中的所有变量。所以这意味着这也包括在内?

标签: c++ c++11 lambda


【解决方案1】:

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

Lambda 捕获

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

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

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

当前对象(*this)可以被隐式捕获,如果任一捕获 默认存在。如果隐式捕获,它总是被 参考,即使捕获默认为 =。

【讨论】:

  • odr 代表什么?
  • @Rajeshwar odr 代表 一个定义规则
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多