title: Pass Templated iterator for STL Container
body: <p>For an exercise for my C++ class (which hasn't covered Boost yet), I am having trouble writing a templated method to accept two iterators for summing numeric values in an STL container.<br>
  Consider the following example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iterator&gt;
  #include &lt;vector&gt;
  
  template&lt;typename T&gt;
  double Sum(const T &amp; c) {
      return 42.0;    // implementation stubbed
  }
  
  // need help writing this method signature to accept two iterators
  template&lt;typename T&gt;
  double Sum(const typename T::const_iterator &amp; begin,
             const typename T::const_iterator &amp; end) {
      return 43.0;    // another implementation stub
  }
  
  int main() {
      std::vector&lt;double&gt; v;
      v.push_back(3.14);
      v.push_back(2.71);
      v.push_back(1.61);    // sums to 7.46
  
      std::cout &lt;&lt; Sum(v) &lt;&lt; ' '              // line 23
                &lt;&lt; Sum(v.begin(), v.end())    // line 24
                &lt;&lt; '\n';
  }
  </code></pre>
  
  <p>I expect this code to output <code>42 43</code>, but it fails to compile.<br>
  The error g++ gives me is:</p>
  
  <pre><code>test_exercise2.cpp: In function ‘int main()’:
  test_exercise2.cpp:24: error: no matching function for call to ‘Sum(__gnu_cxx::__normal_iterator&lt;double*, std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;double*, std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt;)’
  </code></pre>
  
  <p>If I comment out line 24, I get <code>42</code> as the output, as expected.<br>
  I get the same error message whether or not the second templated method is present or not, so for some reason, it's not able to resolve the call on line 24 to the second method I wrote.</p>
  
  <p><strong>What signature must I have for the method that accepts two iterators?</strong></p>
  
  <hr>
  
  <p>The reason why I'm stuck on this is because I need to support summing over the second element of <code>std::map&lt;K, V&gt;</code>. This will require two more overloads to call <code>-&gt;second</code> instead of dereferencing the iterator:<br>
  1. <code>template&lt;typename K, typename V&gt; double Sum(const std::map&lt;K, V&gt; &amp; m);</code> (I'm okay with this one)<br>
  2. and another one involving iterators over the map.</p>
  
  <p>I feel like I'll be able to write the methods for <code>std::map</code> if I can figure out how to specify the passing of iterators for <code>std::list</code> and <code>std::map</code>. I'm okay with solutions that use template-templates.</p>
  
  <hr>
  
  <p>EDIT: The precise wording of problem (omitting non-contributory sentences).<br>
  The containers from "the previous exercise" were <code>std::vector&lt;double&gt;</code>, <code>std::list&lt;double&gt;</code>, <code>std::map&lt;std::string, double&gt;</code>.</p>
  
  <blockquote>
    <p>Create a template function called Sum() that accepts the template
    argument T as input and returns a double. The template argument will
    be a container.</p>
    
    <ul>
    <li>In the implementation get an iterator (T::const_iterator) for the end. Then create a loop that iterates the container T and adds all
    values. Finally return the sum.</li>
    <li>In the main program, call the Sum() function for the different container from the previous exercise.</li>
    </ul>
    
    <p>The Sum() function created calculates the sum of the complete
    container. Also create a Sum() function that calculates the sum
    between two iterators. The function then uses the template argument
    for the iterator type and accepts two iterators, the start and end
    iterator.</p>
  </blockquote>