title: In C++11 lambda syntax, heap-allocated closures?
body: <p>C++11 lambdas are great!</p>
  
  <p>But one thing is missing, which is how to safely deal with mutable data.</p>
  
  <p>The following will give bad counts after the first count:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;functional&gt;
  #include &lt;memory&gt;
  
  std::function&lt;int(void)&gt; f1()
  {
      int k = 121;
      return std::function&lt;int(void)&gt;([&amp;]{return k++;});
  }
  
  int main()
  {
      int j = 50;
      auto g = f1();
      printf("%d\n", g());
      printf("%d\n", g());
      printf("%d\n", g());
      printf("%d\n", g());
  }
  </code></pre>
  
  <p>gives,</p>
  
  <pre><code>$ g++-4.5 -std=c++0x -o test test.cpp &amp;&amp; ./test
  121
  8365280
  8365280
  8365280
  </code></pre>
  
  <p>The reason is that after <code>f1()</code> returns, <code>k</code> is out of scope but still on the stack.  So the first time <code>g()</code> is executed <code>k</code> is fine, but after that the stack is corrupted and <code>k</code> loses its value.</p>
  
  <p>So, the only way I've managed to make safely returnable closures in C++11 is to allocate closed variables explicitly on the heap:</p>
  
  <pre><code>std::function&lt;int(void)&gt; f2()
  {
      int k = 121;
      std::shared_ptr&lt;int&gt; o = std::shared_ptr&lt;int&gt;(new int(k));
      return std::function&lt;int(void)&gt;([=]{return (*o)++;});
  }
  
  int main()
  {
      int j = 50;
  auto g = f2();
      printf("%d\n", g());
      printf("%d\n", g());
      printf("%d\n", g());
      printf("%d\n", g());
  }
  </code></pre>
  
  <p>Here, <code>[=]</code> is used to ensure the shared pointer is <em>copied</em>, not referenced, so that memory handling is done correctly: the heap-allocated copy of <code>k</code> should be freed when the generated function <code>g</code> goes out of scope.  The result is as desired,</p>
  
  <pre><code>$ g++-4.5 -std=c++0x -o test test.cpp &amp;&amp; ./test
  121
  122
  123
  124
  </code></pre>
  
  <p>It's pretty ugly to refer to variables by dereferencing them, but it should be possible to use references instead:</p>
  
  <pre><code>std::function&lt;int(void)&gt; f3()
  {
      int k = 121;
      std::shared_ptr&lt;int&gt; o = std::shared_ptr&lt;int&gt;(new int(k));
      int &amp;p = *o;
      return std::function&lt;int(void)&gt;([&amp;]{return p++;});
  }
  </code></pre>
  
  <p>Actually, this oddly gives me,</p>
  
  <pre><code>$ g++-4.5 -std=c++0x -o test test.cpp &amp;&amp; ./test
  0
  1
  2
  3
  </code></pre>
  
  <p>Any idea why?  Maybe it's not polite to take a reference of a shared pointer, now that I think about it, since it's not a tracked reference.  I found that moving the reference to inside the lambda causes a crash,</p>
  
  <pre><code>std::function&lt;int(void)&gt; f4()
  {
      int k = 121;
  std::shared_ptr&lt;int&gt; o = std::shared_ptr&lt;int&gt;(new int(k));
      return std::function&lt;int(void)&gt;([&amp;]{int &amp;p = *o; return p++;});
  }
  </code></pre>
  
  <p>giving,</p>
  
  <pre><code>g++-4.5 -std=c++0x -o test test.cpp &amp;&amp; ./test
  156565552
  /bin/bash: line 1: 25219 Segmentation fault      ./test
  </code></pre>
  
  <p>In any case, it would be nice if there was a way to automatically make safely returnable closures via heap allocation.  For example, if there was an alternative to <code>[=]</code> and <code>[&amp;]</code> that indicated that variables should be heap allocated and referenced via references to shared pointers.  My initial thought when I learned about <code>std::function</code> was that it creates an object encapsulating the closure, therefore it could provide storage for the closure environment, but my experiments show that this doesn't seem to help.</p>
  
  <p>I think safely returnable closures in C++11 are going to be paramount to using them, does anyone know how this can be accomplished more elegantly?</p>