C++17 will add copy capture of this object by value, with a capture specification of [*this].
How is this useful? How is it different than capturing this? Can't this already be achieved in C++14 with [tmp = *this]?
Bonus for explaining why P0018R3 uses [=, tmp = *this] instead of [tmp = *this] in their example. If they had used [tmp = *this], all the listed downsides of the C++14 solution would be eliminated.
How is it useful? It's useful when you need a copy of
*this- for example, when*thisitself is no longer valid by the time the lambda is evaluated.How is it different from capturing
this? It makes a copy of the object, so that when the lambda is evaluated, itsthispointer refers to the copy, rather than to the original object.Can it be achieved in C++14 with
[tmp = *this]? It can, but[*this]is more convenient, as code can be moved without prefixing member access withtmp.. Otherwise, especially with[=, tmp = *this], it's possible to accidentally refer to members of the original object when you meant to refer to the copy (particularly if you're in the habit of cut+paste programming).[=,*this]is a safer alternative in this case, as the original object is not accessible from inside the lambda's body (at least, not via thethispointer).