dimanche 19 avril 2015

this pointer in lambda-capture by reference


struct CL
{
int i;
void fnc()
{
[&this](){i=1;}; // (1) error
[&](){i=1;}; // (2) ok
}
};


Here is 1st case which doesn't work, but second case does. Why is that?

I see Standard 5.1.2/1:



capture:

identifier

& identifier

this



(i.e. &this doesn't exist)


And 5.1.2/15:



An entity is captured by reference if it is implicitly or explicitly captured but not captured by copy



In second case "this" doesn't captured by copy according 5.1.2/14:



An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly captured with a capture that does not include an &



But how can I capture "this" pointer by value explicitly? Or is it possible only implicitly with default-capture & ?


Aucun commentaire:

Enregistrer un commentaire