Lambda Capture Block

Can someone please give an example as to what the difference between the following?

1
2
 auto lambda1 = [ this]{ /*...*/ };
 auto lambda2 = [*this]{ /*...*/ };


Apparently the latter captures a copy of the current object while the former captures the object itself, but I cant wrap my head around when this would be useful.
You're exactly right.

This is generally useful if you plan to change the source object before calling the lambda, which you want to make sure operates on the object as it was before its changes. In short, when would you use postfix ++ over prefix ++? A similar rationale applies here.

Alternatively, if your program is multithreaded and swallowing the cost of a copy is preferable over holding some lock for too long (assuming the computation in the lambda is quite expensive), that might be another scenario to do this.

-Albatross
Topic archived. No new replies allowed.