title: Different definitions of the same lambda
body: <p>I've recently opened <a href="https://stackoverflow.com/questions/48241935/lambdas-captured-variable-is-reset">this</a> discussion, from which the following problem comes from. This question is a sort of continuous of that discussion. Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  std::function&lt;int(void)&gt; mylambda(int starter){
      return [starter]() mutable {
          return ++starter;
      };
  }
  
  void tester_wrapper(const std::function&lt;int(void)&gt;&amp; cb, int counter){
      if (counter == 10)
          return;
      else{
          cout &lt;&lt; cb() &lt;&lt; endl;
          tester_wrapper(cb, counter + 1);
      }
  }
  
  void tester(const std::function&lt;int(void)&gt;&amp; cb){
      tester_wrapper(cb, 0);
  }
  
  int main()
  {
      auto getNum = mylambda(1);
  
      tester(getNum);
      tester(getNum);
  }
  </code></pre>
  
  <p>In this case the code does what I expected, more specifically it prints all number from 2 to 21. However if my <code>main</code> function was like this:</p>
  
  <pre><code>int main()
  {
      auto getNum = ([](int starter) {
          return [starter]() mutable {
              return ++starter;
          };
      })(1);
  
      tester(getNum);
      tester(getNum);
  }
  </code></pre>
  
  <p>Then the output would be numbers from 2 to 11 repeated twice. I can't explain myself why it produces this output even if the only difference between the 2 pieces of code is where and how the <code>mylambda</code> function is defined.</p>