0

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 =?

14th Apr 2021, 8:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Answers
+ 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.
14th Apr 2021, 8:57 AM
Shadow
Shadow - avatar
0
I have also work using lambda. I was working on a project called custom tshirts. Here is the URL of it: https://www.customtshirts.ae/tshirt-embroidery. I face many problems in the beginning as I was totally new in it. But thanks to my friend who helpmed a lot in this.
2nd May 2024, 11:34 AM
Custom Tshirts
0
I am pretty much aware of this error myself. Back in 2019 when I was working on my client's project called Woven Labels Maker in Canada, I faced this problem a lot of times. Here is the link to that project https://www.wovenlabels.ca/ and as you can see it's working totally fine now. Thanks to this forum I would say that I was able to find the answer to this problem.
9th Aug 2024, 12:23 PM
Woven Labels
0
Using [=] in a lambda captures this by value, allowing modification of member variables. For non-member variables, [=] only captures copies, so they remain unmodified. I noticed it too when I was working for my clients project https://www.wikiwriters.co.uk/
6th Nov 2024, 8:43 AM
John Kalvin Smith