Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 738333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:49:28+00:00 2026-05-14T07:49:28+00:00

The STL commonly defines an output iterator like so: template<class Cont> class insert_iterator :

  • 0

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 two std::string s.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:49:28+00:00Added an answer on May 14, 2026 at 7:49 am

    The real value type of the iterator could well be the iterator itself. operator* may easily just return a reference to *this because the real work is done by the assignment operator. You may well find that *it = x; and it = x; have exactly the same effect with output iterators (I suppose special measures might be taken to prevent the latter from compiling).

    As such, defining the real value type would be just as useless. Defining it as a void, on the other hand, can prevent errors like:

     typename Iter::value_type v = *it; //useless with an output iterator if it compiled
    

    I suppose this is just the limit of the concept of output iterators: they are objects which “abuse” operator overloading, so as to appear pointerlike, whereas in reality something completely different is going on.

    Your problem is interesting, though. If you want to support any container, then the output iterators in question would probably be std::insert_iterator, std::front_insert_iterator and std::back_insert_iterator. In this case you could do something like the following:

    #include <iterator>
    #include <vector>
    #include <string>
    #include <map>
    #include <iostream>
    
    //Iterator has value_type, use it
    template <class T, class IterValue>
    struct value_type
    {
        typedef IterValue type;
    };
    
    //output iterator, use the container's value_type
    template <class Container>
    struct value_type<Container, void>
    {
        typedef typename Container::value_type type;
    };
    
    template <class T, class Out>
    void parse_aux(Out out)
    {
        *out = typename value_type<T, typename Out::value_type>::type("a", "b");
    }
    
    template <template <class> class Out, class T>
    void parse(Out<T> out)
    {
        parse_aux<T>(out);
    }
    
    //variadic template in C++0x could take care of this and other overloads that might be needed
    template <template <class, class> class Out, class T, class U>
    void parse(Out<T, U> out)
    {
        parse_aux<T>(out);
    }
    
    int main()
    {
        std::vector<std::pair<std::string, std::string> > vec;
        parse(std::back_inserter(vec));
        std::cout << vec[0].first << ' ' << vec[0].second << '\n';
    
        std::map<std::string, std::string> map;
        parse(std::inserter(map, map.end()));
        std::cout << map["a"] << '\n';
    
        //just might also support normal iterators
        std::vector<std::pair<std::string, std::string> > vec2(1);
        parse(vec2.begin());
        std::cout << vec2[0].first << ' ' << vec2[0].second << '\n';
    }
    

    It would still only get you this far. I suppose one could take this further, so it can also manage, say, a std::ostream_iterator<printable_type>, but at some point it would get so complex that it takes a god to decipher the error messages, should something go wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the STL library some containers have iterators and it is commonly held that
Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter
The STL vector template defines element accessors as both const and non-const variants, for
In STL/Boost, is there a ready-made output iterator that only counts the number of
In the STL almost all containers have an erase function. The question I have
I have a C++ STL set with a custom ordering defined. The idea was
Why does the C++ STL not provide any tree containers, and what's the best
If you have an STL vector which has been resized, is it safe to
I have two STL containers that I want to merge, removing any elements that
I'm fairly new to the STL, so I was wondering whether there are any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.