title: Why doesn't my custom iterator work with the STL copy?
body: <p>I wrote an OutputIterator for an <a href="https://stackoverflow.com/questions/1723515/insert-into-an-stl-queue-using-stdcopy/1723585#1723585">answer to another question</a>. Here it is:</p>
  
  <pre><code>#include &lt;queue&gt;
  
  using namespace std;
  
  template&lt; typename T, typename U &gt;
  class queue_inserter {
      queue&lt;T, U&gt; &amp;qu;  
  public:
      queue_inserter(queue&lt;T,U&gt; &amp;q) : qu(q) { }
      queue_inserter&lt;T,U&gt; operator ++ (int) { return *this; }
      queue_inserter&lt;T,U&gt; operator * () { return *this; }
      void operator = (const T &amp;val) { qu.push(val); }
  };
  
  template&lt; typename T, typename U &gt;
  queue_inserter&lt;T,U&gt; make_queue_inserter(queue&lt;T,U&gt; &amp;q) {
      return queue_inserter&lt;T,U&gt;(q);
  }    
  </code></pre>
  
  <p>This works great for this little copy function:</p>
  
  <pre><code>template&lt;typename II, typename OI&gt;
  void mycopy(II b, II e, OI oi) {
      while (b != e) { *oi++ = *b++; }
  }
  </code></pre>
  
  <p>But it doesn't work at all for the STL <code>copy</code> from <code>algorithms</code>. Here are the wonderful C++ errors I get:</p>
  
  <pre><code>i.cpp:33: error: specialization of ‘template&lt;class _Iterator&gt; struct std::iterator_traits’ in different namespace
  /usr/include/c++/4.0.0/bits/stl_iterator_base_types.h:127: error:   from definition of ‘template&lt;class _Iterator&gt; struct std::iterator_traits’
  /usr/include/c++/4.0.0/bits/stl_algobase.h: In function ‘_OI std::__copy_aux(_II, _II, _OI) [with _II = int*, _OI = queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt;]’:
  /usr/include/c++/4.0.0/bits/stl_algobase.h:335:   instantiated from ‘static _OI std::__copy_normal&lt;true, false&gt;::copy_n(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, _OI = queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt;]’
  /usr/include/c++/4.0.0/bits/stl_algobase.h:387:   instantiated from ‘_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = __gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, _OutputIterator = queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt;]’
  i.cpp:53:   instantiated from here
  /usr/include/c++/4.0.0/bits/stl_algobase.h:310: error: no type named ‘value_type’ in ‘struct std::iterator_traits&lt;queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt; &gt;’
  /usr/include/c++/4.0.0/bits/stl_algobase.h:315: error: no type named ‘value_type’ in ‘struct std::iterator_traits&lt;queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt; &gt;’
  /usr/include/c++/4.0.0/bits/stl_algobase.h:315: error: ‘__value’ is not a member of ‘&lt;declaration error&gt;’
  /usr/include/c++/4.0.0/bits/stl_algobase.h:335:   instantiated from ‘static _OI std::__copy_normal&lt;true, false&gt;::copy_n(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, _OI = queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt;]’
  /usr/include/c++/4.0.0/bits/stl_algobase.h:387:   instantiated from ‘_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = __gnu_cxx::__normal_iterator&lt;int*, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, _OutputIterator = queue_inserter&lt;int, std::deque&lt;int, std::allocator&lt;int&gt; &gt; &gt;]’
  i.cpp:53:   instantiated from here
  /usr/include/c++/4.0.0/bits/stl_algobase.h:317: error: ‘__simple’ is not a valid template argument for type ‘bool’ because it is a non-constant expression
  /usr/include/c++/4.0.0/bits/stl_algobase.h:317: error: ‘copy’ is not a member of ‘&lt;declaration error&gt;’
  </code></pre>
  
  <p>Here is the driver:</p>
  
  <pre><code>int main() {
      vector&lt;int&gt; v;
      v.push_back( 1 );
      v.push_back( 2 );
      queue&lt;int&gt; q;
      copy( v.begin(), v.end(), make_queue_inserter(q) );
      while (q.size() &gt; 0) {
          cout &lt;&lt; q.front() &lt;&lt; endl;
          q.pop();
      }
  }
  </code></pre>
  
  <p><strong>Why in the world is it specializing <code>iterator_traits</code>. What's wrong with my iterator? Can't I just write my own simple iterators?</strong></p>