The reason why you cannot use "[&]" in this case and have to use "[=]" instead is because you are capturing the local variable "nod" by value in the lambda function. \n\nThe lambda function is being passed as an argument to the scheduleOnce method, and it captures the variable "nod" from the surrounding scope. Using "[=]" means that you are capturing all variables from the surrounding scope by value, including "nod". \n\nIf you were to use "[&]", it would mean capturing all variables by reference, which includes "nod". However, the scheduleOnce method may not preserve the reference to "nod" beyond its execution, which would lead to undefined behavior. \n\nBy using "[=]", you are ensuring that a copy of "nod" is made and used within the lambda function, preventing any potential issues with the reference.

C++ scheduleOnce Lambda Capture: Understanding [=] vs. [&]

原文地址: https://www.cveoy.top/t/topic/fiJO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录