Note that there are some explanatory texts on larger screens.

plurals
  1. POIterators and templates
    text
    copied!<p>I'm trying to implement a function which allows the user to input some type of begin and end iterator, then all perform some operation on the data. However, the function has to be generic enough it should work for many types of iterators (for example <code>std::vector::iterator</code>, <code>std::string::iterator</code>, <code>std::iterator</code>, etc.). The only limit is that the iterators have to be at least <code>forward_iterator_tag</code> capabilities.</p> <p>My function prototype looks like this:</p> <pre><code>template &lt;class key_type, class data_type&gt; std::shared_ptr&lt;data_type&gt; remove(std::iterator&lt;std::forward_iterator_tag, key_type&gt; key_start, std::iterator&lt;std::forward_iterator_tag, key_type&gt; key_end); </code></pre> <p>However, this limits me to using specifically a <code>forward_iterator_tag</code> iterator, so trying to call the function like this:</p> <pre><code>remove&lt;char, char&gt;(std::iterator&lt;std::random_access_iterator_tag, char&gt;(), std::iterator&lt;std::random_access_iterator_tag, char()); </code></pre> <p>will fail because the compiler can't convert a <code>std::iterator&lt;std::random_access_iterator_tag,...&gt;</code> to a <code>std::iterator&lt;std::forward_access_iterator_tag,...&gt;</code>. Additionally, this method does not work for string iterators, vector iterators, or other stl iterators.</p> <p>Does someone know how the stl implements the containers/strings to accept iterators from each other? For example, this compiles correctly:</p> <pre><code>std::string a = "hello"; std::vector&lt;char&gt; v(a.begin(), a.end()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload