My logic says the following: lambdas is just a piece of code with optional required links. In the case where you need to copy something (which usually happens for memory management purposes, for example, to copy shared_ptr), you still do not want the lambda to have its own state. This is a rather unusual situation.
I think that only the following two options seem to be "correct"
- Close some code using some local variables so you can "pass it"
- Same as above, just add memory management, because maybe you want to execute this part of the code asynchronously or something else and the creation area will disappear.
But when the lambda is "changeable", i.e. captures values that are not constants, which means that it actually maintains its own state. Meaning, every time you call a lambda , it can give a different result, which is not based on its actual closure, but again, on its internal state, which is kind of counter-intuitive in my mind that lambdas come from functional languages .
However, C ++, which is C ++, gives you the ability to get around this limitation by also making it a little ugly, just to make sure you know that you are doing something weird.
I hope it will be with you.
source share