title: Compiler error while passing function objects
body: <p>Say you would like to make iterative modifications to the elements of a container object and do something with the elements at each iteration. The code I came up with to do something like this:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  template &lt;class InputIterator, class Operation, class Callback&gt;
  void transformAndTraverse(InputIterator begin, InputIterator end,  Operation op,  Callback cb) {
    InputIterator start = begin;
    while (begin != end) {
      op(begin);
      cb(start, end);
      ++begin;
    }
  }
  
  template &lt;class InputIterator&gt;
  struct plotFnObject {
    void operator()(InputIterator begin, InputIterator end) {
      while (begin &lt; end) {
        std::cout &lt;&lt; *begin &lt;&lt; " ";
        ++begin;
      }
      std::cout &lt;&lt; std::endl;
    }
  };
  
  template &lt;class InputIterator&gt;
  struct addTwoFnObject {
    void operator()(InputIterator idata) { *idata += 2; }
  };
  
  int main(int argc, char **argv) {
    typedef std::vector&lt;int&gt;::iterator vec_iter;
    std::vector&lt;int&gt; vec;
    vec.push_back(0);
    vec.push_back(0);
    vec.push_back(0);
  
  #ifndef DEMO_COMPILE_ERROR
    // the below works
    transformAndTraverse(vec.begin(), vec.end(), addTwoFnObject&lt;vec_iter&gt;(), plotFnObject&lt;vec_iter&gt;());
  #else
    // but this generates compilation errors
    addTwoFnObject&lt;vec_iter&gt; addtwo();
    plotFnObject&lt;vec_iter&gt; plot();
    transformAndTraverse(vec.begin(), vec.end(), addtwo, plot);
  #endif
  }
  </code></pre>
  
  <p>The trouble is that I get some difficult to understand compiler errors with the second form:</p>
  
  <pre><code>$ g++ testtemplate.cpp -DDEMO_COMPILE_ERROR
  testtemplate.cpp: In function 'void transformAndTraverse(InputIterator, InputIterator, Operation, Callback) [with InputIterator = __gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int&gt; &gt;, Operation = addTwoFnObject&lt;__gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int&gt; &gt; &gt; (*)(), Callback = plotFnObject&lt;__gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int&gt; &gt; &gt; (*)()]':
  testtemplate.cpp:44:60:   instantiated from here
  testtemplate.cpp:8:5: error: too many arguments to function
  testtemplate.cpp:9:5: error: too many arguments to function
  
  $ g++ testtemplate.cpp
  $
  </code></pre>
  
  <p>Why is the compiler OK with creating the function objects in-line, but not with creating them out of line and passing them?</p>
  
  <pre><code>$ g++ --version
  g++ (GCC) 4.5.3
  Copyright (C) 2010 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  </code></pre>