title: Undefined Behavior with the C++0x Closure: I
body: <p>Consider the example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;     // std::function
  #include &lt;vector&gt;        // std::vector
  #include &lt;algorithm&gt;    // std::for_each
  
  int main(){
  
      auto adder = [](int x) {
          return [&amp;](int y) { 
              return x+=y; 
          }; 
      };
  
      std::vector &lt; std::function&lt;int(int)&gt; &gt; vec;
  
      vec.push_back(adder(1));
      vec.push_back(adder(10));
  
      std::for_each(vec.begin(), vec.end(), [](std::function&lt;int(int)&gt; f){std::cout &lt;&lt; f(33) &lt;&lt; " ";});
      std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>One expects the integers <strike> 34 and 43 </strike>  <strong>43</strong> and <strong>76</strong>, but instead gcc 4.6.0 produces "internal compiler error: Segmentation fault". What is wrong with the code?</p>
  
  <p>Edit: Several other examples are discussed <a href="https://stackoverflow.com/questions/5566198/undefined-behavior-with-the-c0x-closure-ii">here</a>.</p>