From cppreference :
To search for a name, determine the type and value of this pointer, and to access non-stationary members of the class, the operator of the call to the closure function is considered in the context of a lambda expression.
In the body of the method panel (int x), the token “x” refers to the method argument, not to a member of the class. The item is hidden. [NOTE: this is, of course, bad coding practice, but not illegal]
If you said x = 6 , where you have a lambda definition, you expect the local variable x (i.e. the by-value-value argument for the method) to be changed, not the x member, right?
So the only question is, can a local variable be implicitly captured by a lambda? I would say that compilers are pretty straightforward, explaining that this is not possible.
cppreference also clarifies this statement:
A variable can be used without capture if it does not (that is, it is not a local variable or it is static or stream local) or if it is not used in the lambda body.
Note: odr-used means that you need to know the address of the variable, not just its value. Assigning a value to a variable counts as an odr use.
Consider this code:
class Foo { int george; void bar(int washington) { int martha = washington; washington = 7; int jefferson = washington; int adams = martha; george = 6; [this, jefferson]() -> void { this->george = 15;
Dale wilson
source share