title: How to use template type argument in lambda?
body: <p>Is it possible to use a template type argument from a surrounding template function within a local anonymous function? I'm pretty certain I can't declare a template lambda ...</p>
  
  <p>For example how would I go about doing something like this:</p>
  
  <pre><code>template &lt;typename T&gt;
  void TrimString(std::basic_string&lt;T&gt;&amp; str, const std::locale&amp; loc = std::locale())
  {
      // std::isspace as lambda unary predicate?
      auto fn = [&amp;loc](T c){ return std::use_facet&lt;std::ctype&lt;T&gt;&gt;(loc).is(std::ctype_base::space, c); };
      // trim right
      str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(fn)).base(), str.end());
      // trim left
      str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(fn)));
  }
  </code></pre>
  
  <p>Currently this generates the following error:</p>
  
  <pre><code>error C2039: 'argument_type' : is not a member of '`anonymous-namespace'::&lt;lambda0&gt;'
  </code></pre>
  
  <p>Which makes sense as the lambda has no clue about argument <code>T</code> from the surrounding template function.</p>
  
  <p>I use VS2010 and gcc 4.7 but I don't want to use boost.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Edit:</strong> It appears I was wrong in my assumption that the problem was the template argument itself. Rather it is the use of <code>std::not1</code> being compiled with the lambda function. Here is the more verbose error output:</p>
  
  <pre><code>error C2039: 'argument_type' : is not a member of '`anonymous-namespace'::&lt;lambda0&gt;'
   : see declaration of '`anonymous-namespace'::&lt;lambda0&gt;'
   : see reference to class template instantiation 'std::unary_negate&lt;_Fn1&gt;' being compiled
            with
            [
                _Fn1=`anonymous-namespace'::&lt;lambda0&gt;
            ]
   : see reference to function template instantiation 'void TrimString&lt;char&gt;(std::basic_string&lt;_Elem,_Traits,_Ax&gt; &amp;,const std::locale &amp;)' being compiled
            with
            [
                _Elem=char,
                _Traits=std::char_traits&lt;char&gt;,
                _Ax=std::allocator&lt;char&gt;
            ]
  </code></pre>
  
  <p>Do you need to explicitly declare the type for the argument if it's a function type? I'm not sure what I'm doing wrong still ...</p>
  
  <p><strong>Answers:</strong></p>
  
  <p>Option 1: If I don't use <code>std::not1</code> and instead negate the returned value in the lambda I get the same behavior without issue.</p>
  
  <pre><code>auto fn = [&amp;loc](T c){ return !std::use_facet&lt;std::ctype&lt;T&gt;&gt;(loc).is(std::ctype_base::space, c); };
  </code></pre>
  
  <p>Option 2: Since the lambda is not longer equivalent to how <code>std::isspace</code> would behave as a unary predicate a function object constructor cast also does the trick.</p>
  
  <pre><code>str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::function&lt;bool(T)&gt;(fn))).base(), str.end());
  </code></pre>