Topic: STL semantics


Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/06/26
Raw View
A typical implementation of STL copy:
 while (first != last) *result++ = *first++;
It has been argued that use of postfix ++ is inefficient and that it was
coded that way because the compiler should be able to optimize it.  But:
 while (first != last) *result = *first, ++result, ++first;
does not have the same semantics.  For interactive istream_iterators, the
latter is what is desired.  Using the former, the output is always one
step behind the input as the following shows.

#include <iostream.h>
#include <algo.h>
struct S { int a, b; };
istream& operator>> (istream& is, S& p) {
 cerr << "Enter two integers ";
 return is >> p.a >> p.b;
 }
ostream& operator<< (ostream& os, S const& p) {
 return os << p.a << " + " << p.b << " = " << p.a + p.b << endl;
 }
int main () {
 copy(istream_iterator<S>(cin), istream_iterator<S>(),
   ostream_iterator<S>(cout));
 }

Has or will anything be placed in the (draft) standard to assure expected
semantics in general?  Copy is just one example of many.

John
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]