jeudi 4 juin 2015

How to capture a single class data member in a lambda expression? [duplicate]

This question already has an answer here:

I know that you can capture class members by capturing the this pointer. However, this captures all class members. Is it possible to restrict which class members are captured?

I know the following doesn't work but is it possible to achieve?

class Foo
{
public:
    Foo() : mBar1(1), mBar2(2) {}

    void doBar()
    {
        auto test = [this->mBar1]()
            {
                std::cout << mBar1 << "\n";
                // Trying to access 'mBar2' here would fail to compile...
            };

        test();
    }

    int mBar1;
    int mBar2;
};

Caveat: This question is just to satisfy my curiosity.

Aucun commentaire:

Enregistrer un commentaire