New course! Every coder should learn Generative AI!
Try a free lesson0
Why capture clause = working to update value in lambda
Hi Refer below code: https://code.sololearn.com/cA4a45A25A18/#cpp As = is non modifiable capture, why lambda function is able to modify value of member variable? Should it not be & instead of =?
1 Answer
+ 3
From cppreference.com:
The current object (*this) can be implicitly captured if either capture default is present. If implicitly captured, it is always captured by reference, even if the capture default is =.
https://en.cppreference.com/w/cpp/language/lambda
Since the current object is captured by reference, you can modify it. However, as of C++20, the implicit capture of this via = has been deprecated, but it still works. Now you might want to capture [=, this] instead to be more explicit.