Output iterator's value_type

Posted by wilhelmtell on Stack Overflow See other posts from Stack Overflow or by wilhelmtell
Published on 2010-04-12T02:26:17Z Indexed on 2010/04/12 2:33 UTC
Read the original article Hit count: 354

Filed under:
|
|

The STL commonly defines an output iterator like so:

template<class Cont>
class insert_iterator
: public iterator<output_iterator_tag,void,void,void,void> {
    // ...

Why do output iterators define value_type as void? It would be useful for an algorithm to know what type of value it is supposed to output.

For example, a function that translates a URL query "key1=value1&key2=value2&key3=value3" into any container that holds key-value strings elements.

template<typename Ch,typename Tr,typename Out>
void parse(const std::basic_string<Ch,Tr>& str, Out result)
{
    std::basic_string<Ch,Tr> key, value;
    // loop over str, parse into p ...
        *result = typename iterator_traits<Out>::value_type(key, value);
}

The SGI reference page of value_type hints this is because it's not possible to dereference an output iterator. But that's not the only use of value_type: I might want to instantiate one in order to assign it to the iterator.

© Stack Overflow or respective owner

Related posts about c++

Related posts about iterator