Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pre/post increment operator on OutputIterator

I read in an article on OutputIterator that we only can dereference it as lvalue. My question is about operator++ which increments the iterator by one position.

So,

*it++ = t 

would be

{*it = t; ++it; }

https://www.sgi.com/tech/stl/OutputIterator.html

Now, I am assuming that operator++ would be overloaded in such a fashion that it would increment OutputIterator by one position.

ostream_iterator is also an OutputIterator and implements all the requirements of OutputIterator.

Then why is operator++ implemented as shown below in ostream_iterator?

ostream_iterator<T,charT,traits>& operator++() { return *this; }
ostream_iterator<T,charT,traits>& operator++(int) { return *this; }

http://www.cplusplus.com/reference/iterator/ostream_iterator/

That shows operator++ does nothing.

Does dereferencing the output operator assign a new value and advance it by one position? Without using operator++?

If so, then why do we need to implement operator++?

like image 731
YADAV Avatar asked May 19 '26 15:05

YADAV


1 Answers

The use of this operator is formal. An OutputIterator must support the ++-operator regardless if it has any effect.

like image 85
FPK Avatar answered May 22 '26 06:05

FPK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!