For example, there is a method:
void foo( A* p )
{
auto l = [=](){ /* Do Something using p */ };
// Use l ...
}
How I should to capture the pointer: by reference or by value? Inside lambda p is not changed, just used its methods.
The fact that it is a pointer doesn't affect your decision about ref or value capture. Since a pointer is small enough and you don't want to write it, you can capture by value.
I usually use the following rule:
So a pointer that doesn't change should be captured by value. If the compiler detects that there are no writes, it might optimize a capture by reference and pass by value anyway, but I'm not sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With