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.
What alternative approach is there for constructing a value to output with the output iterator? Two approaches I considered:
- 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.
- Require that the output container holds
pair<string,string>
, or else a type convertible from that. I wonder if I can do without this requirement, perhaps allow any element that can construct from twostd::string
s.