c++ - 是否 = 在 lambda 的捕获列表中捕获 this 指针

标签 c++ c++11 lambda

我现在有这样的东西

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 capture

The captures is a comma-separated list of zero or more captures, optionally beginning with the capture-default. The only capture defaults are

& (implicitly capture the odr-used automatic variables by reference) and

= (implicitly capture the odr-used automatic variables by copy).

The current object (*this) can be implicitly captured if either capture default is present. If implicitly captured, it is always captured by reference, even if the capture default is =.

关于c++ - 是否 = 在 lambda 的捕获列表中捕获 this 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46905567/

相关文章:

c++ - 在 makefile 中添加 c++11 以消除错误 to_string is not declared in this scope

java - java会允许使用功能接口(interface)作为方法吗?

c++ - 使用 Lambda/Template/SFINAE 自动化 try/catch-safeguarding trampoline 函数

c++ - 计算 vector 中相同字符串的更简单方法?

c++ - 原子比较

c++ - Qtableview搜索栏

c++ - 如何从注册表更改高级设置

c++ - 存储一组指针会导致未定义的行为吗?

android - 收到错误 : 'shared_ptr' in namespace 'std' does not name a type

c++ - 避免接受 lambda 的方法的 const/non-const 重复