mercredi 26 avril 2017

Can lambda expression in the class scope capture this?

It seems that I can define lambda expressions that capture this in the class scope. As far as I read N4640 up to date working draft, I couldn't find the sentence that allows the behavior. I think that I'm missing something...

Here is an example:

#include <iostream>
#include <functional>

struct foo {
    std::function<void()> f1 = [this]{ ++i; };
    int i = 0;
};

int main() {
    foo a;
    foo const& cref = a;
    cref.f1();
    std::cout << a.i << std::endl;
}

Running demo. (g++ -std=c++11 pedantic) http://ift.tt/2oOnBVv

Aucun commentaire:

Enregistrer un commentaire