C++ lambda ´this´ pointer invalidation after move operation : <p>I have the following (simplified) code in my current project:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;functional&gt;
#include &lt;vector&gt;


class Test{

public:

    Test() = default;
    Test(const Test&amp; other) = delete;
    Test&amp; operator=(const Test&amp; other) = delete;
    Test(Test&amp;&amp; other) = default;
    Test&amp; operator=(Test&amp;&amp; other) = default;



    void setFunction(){
       lambda = [this](){
           a = 2;
       };
    }  

    int callAndReturn(){
       lambda();
       return a;
    }

private:
    std::function&lt;void()&gt; lambda; 
    int a = 50;
};


int main()
{
  Test t;
  t.setFunction();
  std::vector&lt;Test&gt; elements;
  elements.push_back(std::move(t));
  std::cout &lt;&lt; elements[0].callAndReturn() &lt;&lt; std::endl;
}
</code></pre>

<p>When I run it, the value 50 is printed instead of the expected value 2. I suppose this happens because the lambda function captures the current <code>this</code> pointer. After the move operation the <code>this</code> pointer changes and the function writes to the wrong <code>a</code>.</p>

<p><strong>Now my question is:</strong> Is there a way to change the lambda's captured reference to the new <code>Test</code> so that the value 2 is printed?</p>
