Note that there are some explanatory texts on larger screens.

plurals
  1. POOutput iterator's value_type
    primarykey
    data
    text
    <p>The STL commonly defines an output iterator like so:</p> <pre><code>template&lt;class Cont&gt; class insert_iterator : public iterator&lt;output_iterator_tag,void,void,void,void&gt; { // ... </code></pre> <p><strong>Why do output iterators define <code>value_type</code> as <code>void</code>?</strong> It would be useful for an algorithm to know what type of value it is supposed to output.</p> <p>For example, a function that translates a URL query <code>"key1=value1&amp;key2=value2&amp;key3=value3"</code> into any container that holds key-value strings elements.</p> <pre><code>template&lt;typename Ch,typename Tr,typename Out&gt; void parse(const std::basic_string&lt;Ch,Tr&gt;&amp; str, Out result) { std::basic_string&lt;Ch,Tr&gt; key, value; // loop over str, parse into p ... *result = typename iterator_traits&lt;Out&gt;::value_type(key, value); } </code></pre> <p>The <a href="http://www.sgi.com/tech/stl/value_type.html" rel="noreferrer">SGI reference page of <code>value_type</code></a> hints this is because it's not possible to dereference an output iterator. But that's not the only use of <code>value_type</code>: I might want to instantiate one in order to assign it to the iterator.</p> <p><strong>What alternative approach is there for constructing a value to output with the output iterator?</strong> Two approaches I considered:</p> <ul> <li>Accept a functor parameter that would return an object of the correct type. I still want to have a version of the algorithm that doesn't take that function object parameter though.</li> <li>Require that the output container holds <code>pair&lt;string,string&gt;</code>, or else a type convertible from that. I wonder if I can do without this requirement, perhaps allow any element that can construct from two <code>std::string</code> s.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    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