title: Passing functions as argument to template function problem
body: <p>I am trying to create a template function which has 5 parameters, four of which should be iterators and the fifth should be a function. What the function should do it should essentially transform each element from the block between first and second iterators passed as arguments and copy them between the third and fourth iterator passed as arguments, using the fifth parameter(function) to transform the elements. The return value is the sum of all elements of the first block. </p>
  
  <p>Here is what I came up with</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  #include &lt;vector&gt;
  #include &lt;deque&gt;
  using namespace std;
  
  int funk(int a) {
      return a*=2;
  }
  
  template &lt;typename IterType1, typename IterType2&gt;
  auto MyFunction(IterType1 p1,
                  IterType1 p2,
                  IterType2 p3,
                  IterType2 p4,
                  decltype(p1[0]) CriteriaFunction(decltype(p1[0])))
  -&gt; decltype(p1[0])
  {
  
      auto temp= remove_reference&lt;decltype (p1[0])&gt;::type;
      while(p1 != p2) {
          temp+=*p1;
  
          *p3++ = CriteriaFunction(*p1);
          p1++;
  
      }
      p4=++p3;
  
      return temp;
  }
  
  int main()
  {
      vector&lt;int&gt; v1 = {1,2,3,4,5};
      deque&lt;double&gt; d1(5);
  
      auto it1 = v1.begin();
      auto it2 = v1.end();
      auto it3 = d1.begin();
      auto it4 = d1.end();
  
      cout &lt;&lt; MyFunction(it1, it2, it3, it4, funk);
      return 0;
  </code></pre>
  
  <p>Now, I am not sure if my template function will actually work the way I wrote it but I will change it up later. The problem is that this will not even compile, the error is:</p>
  
  <p><code>main.cpp|48|error: invalid user-defined conversion from 'main()::&lt;lambda(int)&gt;' to 'int&amp; (*)(int&amp;)' [-fpermissive]|</code></p>
  
  <p>Now, I am familiar with passing functions as arguments to other functions, but what if the function the template is receiving <strong>depends on the typename arguments of the template</strong>. Is my syntax wrong? How can I fix this?</p>