title: Capturing a Lambda's static in a Nested Lambda
body: <p>In <a href="https://stackoverflow.com/a/33282441/2642059">this answer</a> I use this code:</p>
  
  <pre><code>std::vector&lt;std::vector&lt;int&gt;&gt; imat(3, std::vector&lt;int&gt;(10));
  
  std::for_each(imat.begin(), imat.end(), [&amp;](auto&amp; i) {
      static auto row = 0;
      auto column = 0;
      std::transform(i.begin(), i.end(), i.begin(), 
          [&amp;](const auto&amp; /*j*/) {
              return row * column++; 
      }); 
  
      ++row; 
  });
  </code></pre>
  
  <p>But I notice some misbehavior in capturing <code>static auto row</code> depending upon the compiler.</p>
  
  <p><a href="http://coliru.stacked-crooked.com/a/fe3ef266452f39fe" rel="nofollow noreferrer">Clang 3.7.0 yields</a>:</p>
  
  <blockquote>
    <p>0 0   0   0   0   0   0   0   0   0<br>
    0   1   2   3   4   5   6   7   8   9<br>
    0   2   4   6   8   10  12  14  16  18</p>
  </blockquote>
  
  <p><a href="http://ideone.com/NTLmV2" rel="nofollow noreferrer">gcc 5.1.0 yields</a>:</p>
  
  <blockquote>
    <p>0 0   0   0   0   0   0   0   0   0<br>
    0   0   0   0   0   0   0   0   0   0<br>
    0   0   0   0   0   0   0   0   0   0</p>
  </blockquote>
  
  <p>And Visual Studio 2015 gives me a compile time error:</p>
  
  <blockquote>
    <p>An internal error has occurred in the compiler.</p>
  </blockquote>
  
  <p>If I change the capture nested capture to capture <code>row</code> explicitly I get the compiler error:</p>
  
  <blockquote>
    <p>identifier in capture must be a variable with automatic storage duration declared in the reaching scope of the lambda</p>
  </blockquote>
  
  <p>Am I allowed to capture a <code>static</code> in a nested lambda? It seems legit, but there are so many problems!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/33285103/capturing-a-lambdas-static-in-a-nested-lambda?noredirect=1#comment54371126_33285103">Fozi pointed out</a> that I <em>can</em> get Visual Studio 2015 to compile and give the same output as Clang 3.7.0 if I change the nested lambda's parameter type from <code>const auto&amp;</code> to <code>const int&amp;</code>. Which seems completely unrelated, but it works.</p>
  
  <p>This doesn't work if I try to capture <code>row</code> explicitly. In that case I still get the compiler error:</p>
  
  <blockquote>
    <p>identifier in capture must be a variable with automatic storage duration declared in the reaching scope of the lambda</p>
  </blockquote>
  
  <p>I've reported a Visual Studio 2015 bug here: <a href="https://connect.microsoft.com/VisualStudio/feedback/details/1930409/capturing-a-lambdas-static-in-a-nested-lambda" rel="nofollow noreferrer">https://connect.microsoft.com/VisualStudio/feedback/details/1930409/capturing-a-lambdas-static-in-a-nested-lambda</a></p>