title: C++11 g++ strangeness (using defaulted template, lambda & remove_if)
body: <p>I have encountered some really weird g++ behaviour and would appreciate some help.
  So I would like to present three almost similar pieces of code:</p>
  
  <pre><code>class Foo 
  {
      public:
  
      template&lt;typename T = char&gt;
      bool Moo(std::function&lt;bool()&gt; f = [](){ return true; }) const
      {
          std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
          str2.erase(std::remove_if(str2.begin(), str2.end(),
                          [](char x){return true;}), str2.end());
          // just to do something
          f = 0;
          return f();
      };
  };
  
  int main(int argc, char **args) 
  {
      Foo* myFoo = new Foo();
      return myFoo-&gt;Moo&lt;&gt;();
  }
  </code></pre>
  
  <p>This will produce 3 errors:</p>
  
  <ul>
  <li>default argument for template parameter for class enclosing ‘Foo::Moo(std::function) const::__lambda1’ [](char x){return true;})</li>
  <li>no matching function for call to ‘Foo::Moo()’</li>
  <li>template argument deduction/substitution failed</li>
  </ul>
  
  <p>Now if we change the parameter of Moo to a trivial type <em>or</em> if we take out the lambda inside the function body (by taking out the whole line with "str.erase") the code compiles without errors!</p>
  
  <p>Changing the parameter to trivial type:</p>
  
  <pre><code>class Foo 
  {
      public:
  
      template&lt;typename T = char&gt;
      bool Moo(bool f = true) const
      {
          std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
          str2.erase(std::remove_if(str2.begin(), str2.end(),
                          [](char x){return true;}), str2.end());
          // just to do something
          f = 0;
          return f;
      };
  };
  
  int main(int argc, char **args) 
  {
      Foo* myFoo = new Foo();
      return myFoo-&gt;Moo&lt;&gt;();
  }
  </code></pre>
  
  <p>Removing the line with "str.erase":</p>
  
  <pre><code>class Foo 
  {
      public:
  
      template&lt;typename T = char&gt;
      bool Moo(std::function&lt;bool()&gt; f = [](){ return true; }) const
      {
          std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
          // just to do something
          f = 0;
          return f();
      };
  };
  
  int main(int argc, char **args) 
  {
      Foo* myFoo = new Foo();
      return myFoo-&gt;Moo&lt;&gt;();
  }
  </code></pre>
  
  <p>So what is going on here? Why does the combination of the "remove_if-lambda" in the function body ("str.erase" line) and the "defaulted-lambda function" in the function parameter list produce errors regarding the "defaulted template parameter" in the function header?</p>