title: how do I write a function that can iterate over a generic type in a typesafe way
body: <p>I want to write a function that can input an iterator over a given generic type in a typesafe way. One possible use case would be writing a function like accumulate/map/fold:</p>
  
  <pre><code>#include &lt;iterator&gt;
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  
  template &lt;typename V, typename K&gt;
  K accumulate(
        std::function&lt;K(K, V)&gt; accumulator,
        /* WHAT TYPE DO I PUT HERE */ it,
        /* WHAT TYPE DO I PUT HERE */ end,
        K initial) {
    K sum = initial;
    for (; it != end; ++it) {
      V item = *it;
      sum = accumulator(sum, item);
    }
    return sum;
  }
  </code></pre>
  
  <p>How can I do this in a way that the compiler will check types and all that good stuff?</p>
  
  <p>Previously asked <a href="https://stackoverflow.com/q/34301090/1078199">here</a></p>