Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture "this" in a lambda function in lambda? [duplicate]

Tags:

c++

c++11

lambda

For example

class A
{
    void f() {}
    void g()
    {
        [this]() // Lambda capture this
        {
            f();
            A* p = this;
            [p]() // Workaround to let inner lambda capture this
            {
                p->f();
            };
        };
    }
};

Any better way to capture this in the inner lambda?

like image 421
user1899020 Avatar asked Mar 19 '13 21:03

user1899020


People also ask

How do you duplicate a lambda function?

When editing a lambda function, you can go Actions > Export Function > Download deployment package. This downloads a . zip file of the function. This duplicates the code and npm modules etc...

What does it mean to lambda capture this?

A lambda is a syntax for creating a class. Capturing a variable means that variable is passed to the constructor for that class. A lambda can specify whether it's passed by reference or by value.

Can lambda take two arguments?

A lambda function can take any number of arguments, but can only have one expression.


1 Answers

Just use [=], this is implicitly captured. If you have other variables which you don't wanna capture by copy, then just capture [this].

like image 54
Stephan Dollberg Avatar answered Oct 14 '22 16:10

Stephan Dollberg