Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The real value type of the iterator could well be the iterator itself. <code>operator*</code> may easily just return a reference to <code>*this</code> because the real work is done by the assignment operator. You may well find that <code>*it = x;</code> and <code>it = x;</code> have exactly the same effect with output iterators (I suppose special measures might be taken to prevent the latter from compiling).</p> <p>As such, defining the real value type would be just as useless. Defining it as a <code>void</code>, on the other hand, can prevent errors like:</p> <pre><code> typename Iter::value_type v = *it; //useless with an output iterator if it compiled </code></pre> <p>I suppose this is just the limit of the concept of output iterators: they are objects which "abuse" operator overloading, so as to <em>appear</em> pointerlike, whereas in reality something completely different is going on.</p> <p>Your problem is interesting, though. If you want to support any container, then the output iterators in question would probably be <code>std::insert_iterator</code>, <code>std::front_insert_iterator</code> and <code>std::back_insert_iterator</code>. In this case you could do something like the following:</p> <pre><code>#include &lt;iterator&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;map&gt; #include &lt;iostream&gt; //Iterator has value_type, use it template &lt;class T, class IterValue&gt; struct value_type { typedef IterValue type; }; //output iterator, use the container's value_type template &lt;class Container&gt; struct value_type&lt;Container, void&gt; { typedef typename Container::value_type type; }; template &lt;class T, class Out&gt; void parse_aux(Out out) { *out = typename value_type&lt;T, typename Out::value_type&gt;::type("a", "b"); } template &lt;template &lt;class&gt; class Out, class T&gt; void parse(Out&lt;T&gt; out) { parse_aux&lt;T&gt;(out); } //variadic template in C++0x could take care of this and other overloads that might be needed template &lt;template &lt;class, class&gt; class Out, class T, class U&gt; void parse(Out&lt;T, U&gt; out) { parse_aux&lt;T&gt;(out); } int main() { std::vector&lt;std::pair&lt;std::string, std::string&gt; &gt; vec; parse(std::back_inserter(vec)); std::cout &lt;&lt; vec[0].first &lt;&lt; ' ' &lt;&lt; vec[0].second &lt;&lt; '\n'; std::map&lt;std::string, std::string&gt; map; parse(std::inserter(map, map.end())); std::cout &lt;&lt; map["a"] &lt;&lt; '\n'; //just might also support normal iterators std::vector&lt;std::pair&lt;std::string, std::string&gt; &gt; vec2(1); parse(vec2.begin()); std::cout &lt;&lt; vec2[0].first &lt;&lt; ' ' &lt;&lt; vec2[0].second &lt;&lt; '\n'; } </code></pre> <p>It would still only get you this far. I suppose one could take this further, so it can also manage, say, a <code>std::ostream_iterator&lt;printable_type&gt;</code>, but at some point it would get so complex that it takes a god to decipher the error messages, should something go wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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