Shouldn't advance be defined for ostream_iterator?

Neal D. Becker nbecker@hns.com
Tue Feb 3 15:37:00 GMT 2004


On Tuesday 03 February 2004 10:21 am, Jonathan Wakely wrote:
> On Tue, Feb 03, 2004 at 10:07:09AM -0500, Neal D. Becker wrote:
> > ostream_iterator has operator++ (nop), but no generic advance.  I believe
> > advance should be defined:
> >
> > namespace std {
> >   template<class T, class charT, class traits, typename dist_t>
> >   inline void advance (ostream_iterator<T,charT,traits>, dist_t) {}
> > };
>
> std::advance() requires an InputIterator, whereas std::ostream_iterator
> is a model of the OutputIterator concept, and the description from
> http://www.sgi.com/tech/stl/OutputIterator.html says:
>
>   [3] Assignment through an Output Iterator x is expected to alternate
>   with incrementing x, and there must be an assignment through x before
>   x is ever incremented. Any other order of operations results in
>   undefined behavior. That is: {*x = t; ++x; *x = t2; ++x} is
>   acceptable, but {*x = t; ++x; ++x; *x = t2;} is not.
>
> In other words, std::advance(x, 2) is not acceptable.
>

Thanks for the reply.  Yes, I understand your argument, but the premise is 
that since ostream_iterator is a model of output_iterator, and since the 
requirements for output_iterator are X, therefore...

Problem is the premise.  It's true that ostream_iterator is considered a model 
of output_iterator, but that is really too restrictive.  In fact, isn't it 
true that defining operator+= and advance for ostream_iterator would be 
perfectly harmless?  Don't you agree that if operator++ is a nop, that 
logically operator+= should be also?

I can demonstrate code where it would be useful.
- 
Please AVOID sending me WORD, EXCEL or POWERPOINT attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html



More information about the Libstdc++ mailing list