title: Compilation error with lambda capturing templated parameters
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7512224/is-there-a-way-to-pass-template-arguments-to-a-function-on-an-object-when-the-ob">Is there a way to pass template arguments to a function on an object when the object type is a template argument?</a>  </p>
  </blockquote>
  
  
  
  <p>I have been learning how to use lambdas in C++ lately when I came across the following problem.</p>
  
  <p>When I write a lambda which captures a templated variable (by reference or by copy, doesn't matter), I get a weird compilation error, not related to the lambda itself. The test code is as follows:</p>
  
  <pre><code>// templated.cpp  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  template&lt;class T, class Function&gt;
  Function for_each_index(const T&amp; v, Function f)
  {
      for (unsigned int i = 0; i &lt; v.size(); ++i) { f(i); }
      return f;
  }
  
  class Test
  {
  public:
      template&lt;class T&gt;
      T f() { return 1; }
  };
  
  template&lt;class T&gt;
  void fun(T&amp; v)
  {
      for_each_index(v, [&amp;v] (unsigned int i) {
          v[i].f&lt;float&gt;();
      });
  }
  
  int main()
  {
      std::vector&lt;Test&gt; v;
      v.push_back(Test());
      v.push_back(Test());
      v.push_back(Test());
      fun(v);
      return 0;
  }
  </code></pre>
  
  <p>Compilation of the above code fails with the following message:</p>
  
  <pre><code>$ g++ -Wall templated.cpp -std=c++0x
  templated.cpp: In lambda function:
  templated.cpp:23:16: error: expected primary-expression before ‘float’
  templated.cpp:23:16: error: expected ‘;’ before ‘float’
  </code></pre>
  
  <p>However, if I move the lambda to <code>main</code> (no template there), it compiles just fine:</p>
  
  <pre><code>// simple.cpp
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  template&lt;class T, class Function&gt;
  Function for_each_index(const T&amp; v, Function f)
  {
      for (unsigned int i = 0; i &lt; v.size(); ++i) { f(i); }
      return f;
  }
  
  class Test
  {
  public:
      template&lt;class T&gt;
      T f() { return 1; }
  };
  
  int main()
  {
      std::vector&lt;Test&gt; v;
      v.push_back(Test());
      v.push_back(Test());
      v.push_back(Test());
      for_each_index(v, [&amp;v] (unsigned int i) {
          v[i].f&lt;float&gt;();
      });
      return 0;
  }
  </code></pre>
  
  <p>I use <code>for_each_index</code> here as I am using a similar "iterating tool" in my own code, from which the above error comes from (the original code does some heavy image processing, this code is a proof of concept). However, it servers a second purpose here - to avoid putting templated variable in lambda arguments.</p>
  
  <p>So, is this a compiler bug? Or am I doing something very wrong? If so, what possible workaround could you suggest?</p>
  
  <p>FYI, I am using a standard Ubuntu-issue <code>g++</code>, version <code>4.6.3</code>.</p>