title: How to use C++0x lambdas local variables for std::fill()?
body: <p>So I was trying to test a lambda accessing local variables in the scope in which it is used, based roughly on a simple example by Bjarne on the C++0x FAQS page at:
  <a href="http://www2.research.att.com/~bs/C++0xFAQ.html#lambda" rel="nofollow">http://www2.research.att.com/~bs/C++0xFAQ.html#lambda</a></p>
  
  <p>When I try this simple test code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  //Test std::fill() with C++0x lambda and local var
  void f (int v) { 
      vector&lt;int&gt; indices(v);
      int count = 0;
      fill(indices.begin(), indices.end(), [&amp;count]() {
          return ++count;
      });
  
      //output test indices
      for (auto x : indices) {
          cout &lt;&lt; x &lt;&lt; endl;
      }
  }
  
  int main() {
      f(50);
  }
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&amp;) [with _ForwardIterator = __gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int&gt; &gt;, _Tp = f(int)::&lt;lambda()&gt;]'
  </code></pre>
  
  <p>I'm supposing this errmsg indicates the std::fill() signature requires a const Type&amp; to use for the new value element assignment.</p>
  
  <p>But if I'm to be able to use the fill() for this purpose, as indicated by Bjarne's example, won't I need to use a reference '[&amp;count]' inside the lambda capture clause to be able to reassign the original indices element value with the incrementing count var via the 'return ++count;' lambda statement block?</p>
  
  <p>I admit I don't quite understand all about these lambdas just yet! :)</p>