title: How to get the value type from an output iterator?
body: <p>Let's say that I have a C container (e.g., <code>MyContainer</code>) with contained objects stored as <code>void*</code> pointers. The only way to iterate through the elements of this container is via two interface functions:</p>
  
  <ol>
  <li><code>getFirstElem(MyContainer const&amp;, void*)</code>: Outputs the first element of the container.</li>
  <li><code>getNextElem(MyContainer const&amp;, void*)</code>: Outputs the next element of the container.</li>
  </ol>
  
  <p>I want to code a generic function that iterates through the elements of this C container via the interface functions mentioned above and copy their values into a C++ container (e.g. <code>std::vector</code>).</p>
  
  <p>What I've done so far:</p>
  
  <pre><code>template&lt;typename OutputIterator&gt;
  void
  copy_container(MyContainer const &amp;cont, OutputIterator first) {
    typename std::iterator_traits&lt;OutputIterator&gt;::value_type elem;
    if(getFirstElem(cont, &amp;elem)) {
      do {
        *first = elem;
        ++first;
      } while(getNextElem(cont, &amp;elem))    
    }
  }
  </code></pre>
  
  <p>The above example works OK with normal iterators. However, it fails to compile with output iterators (e.g., <code>copy_container(cont, std::back_inserter(myvector));</code>).</p>
  
  <p>The reason is that <code>std::iterator_traits::value_type</code> results in <code>void</code> in cases where the argument type is an output iterator.</p>
  
  <p><strong>Is there a way to make this generic function work for output iterators as well?</strong></p>
  
  <p>I know that in C++11 it could be done by using <code>decltype</code> (e.g., <code>decltype(*first)</code>), but I'm particularly interested in pre-C++11 solutions since I use an old C++ compiler (gcc v4.4.7).</p>