title: How to write a c++ function what can return either iterator or reverse_iterator
body: <p>As far as I can tell in c++ there is no common base class that covers both iterator and reverse_iterator.</p>
  
  <p>The only suggestion I have seen so far is to get around this using templates (
  <a href="https://stackoverflow.com/questions/1448119/how-to-write-a-function-that-takes-an-iterator-or-collection-in-a-generic-way">How to write a function that takes an iterator or collection in a generic way?</a> )</p>
  
  <p>However this solution doesn't seem to work for me.</p>
  
  <pre><code>class MyClass
  {
      template&lt;typename Iter&gt; Iter* generate_iterator(...params...)
      {
          //returns either a vector::iterator or vector::reverse_iterator
      }
      template&lt;typename Iter&gt; void do_stuff(Iter *begin, Iter *end)
      {
          //does stuff between elements specified by begin and end
          //I would like this function to remain agnostic of which direction it is working in!
      }
      void caller()
      {
          //I would like this function to remain agnostic of which direction it is working in too...
             do_stuff(generate_iterator(blah),generate_iterator(foo));
      }
  };
  </code></pre>
  
  <p>In this case, generate_iterator() cannot be used as desired because the compiler complains "generate_iterator is not a member of class MyClass" presumably because I haven't specified it (which I can't in practice as caller should be agnostic of the iterator type).</p>
  
  <p>Can anyone help?  Thanks in advance!</p>
  
  <p><strong>edit</strong>: as Mark B pointed out generate_iterator must return a pointer - now corrected</p>
  
  <p><strong>update</strong>: just started using this <a href="http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/start_page.html" rel="nofollow noreferrer">http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/start_page.html</a> and it seems to work... </p>