Why capture clause = working to update value in lambda | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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
2 Respostas
+ 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