I have the following (simplified) code in my current project:
#include <iostream> #include <string> #include <functional> #include <vector> class Test{ public: Test() = default; Test(const Test& other) = delete; Test& operator=(const Test& other) = delete; Test(Test&& other) = default; Test& operator=(Test&& other) = default; void setFunction(){ lambda = [this](){ a = 2; }; } int callAndReturn(){ lambda(); return a; } private: std::function<void()> lambda; int a = 50; }; int main() { Test t; t.setFunction(); std::vector<Test> elements; elements.push_back(std::move(t)); std::cout << elements[0].callAndReturn() << std::endl; }
When I run it, instead of the expected value, the value 50 will be printed. I assume that this is because the lambda function captures the current this pointer. After the move operation, the this pointer changes, and the function writes the incorrect a .
Now my question is: Is there a way to change the linked lambda link to the new Test to print the value 2?
c ++ lambda move
Overblade
source share