当 "this" 被 lambda 捕获时,是否必须显式使用它?

When "this" is captured by a lambda, does it have to be used explicitly?

我发现在 lambda 中捕获 this 的例子明确地使用了它;例如:

capturecomplete = [this](){this->calstage1done();};

但似乎也可以隐式使用它;例如:

capturecomplete = [this](){calstage1done();};

我在 g++ 中测试了它,并编译了它。

这是标准的 C++ 吗? (如果是,是哪个版本),还是某种形式的扩展?

它是 completely standard,自从在 C++11 中引入了 lambdas 以来就一直如此。

你不需要在那里写this->

这是标准的,自从 C++11 添加了 lambda 以来一直如此。根据 cppreference.com:

For the purpose of name lookup, determining the type and value of the this pointer and for accessing non-static class members, the body of the closure type's function call operator is considered in the context of the lambda-expression.

struct X {
    int x, y;
    int operator()(int);
    void f()
    {
        // the context of the following lambda is the member function X::f
        [=]()->int
        {
            return operator()(this->x + y); // X::operator()(this->x + (*this).y)
                                            // this has type X*
        };
    }
};