O'Reilly logo

Extended STL, Volume 1: Collections and Iterators by Matthew Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 35. Intermezzo: Proscribing Fatuous Output Iterator Syntax Using the Dereference Proxy Pattern

 

If knowledge can create problems, it is not through ignorance that we can solve them.

 
 --Isaac Asimov

As was discussed in the last chapter, std::ostream_iterator supports the required semantics of an output iterator by using a simple implementation trick, whereby the dereference operator (operator *()) returns an instance to the object and an assignment operator (operator =()) taking the assigned type is defined. (See Listing 34.5 to see how it is implemented for stlsoft::ostream_iterator.) Consider the following code:

1  std::ostream_iterator<int>  iter(std::cout);
2
3  *iter = 10;  // Sensible

Line 3 is equivalent to:

iter.operator *().operator =(10); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required