Topic: Defect report: 'equivalence' for input iterators


Author: "Corwin Joy" <cjoy@houston.rr.com>
Date: 17 Dec 02 03:53:27 GMT
Raw View
 [Moderator's note: this defect report has been
 forwarded to the C++ committee. -moderator.]

In section 24.1.1 [lib.input.iterators] table 72 -
'Input Iterator Requirements' we have as a postcondition of *a:
"If a==b and (a, b) is in the domain of == then *a is equivalent to *b".

In section 24.5.3.5 [lib.istreambuf.iterator::equal] it states that
"istreambuf_iterator::equal returns true if and only if both
iterators are at end-of-stream, or neither is at end-of-stream,
**regardless of what streambuf object they use**."
(My emphasis).

The defect is that either 'equivalent' needs to be more precisely
defined or the conditions for equality in
24.5.3.5 [lib.istreambuf.iterator::equal] are incorrect.
(Or both).

Consider the following example:

 #include <iostream>
 #include <fstream>
 #include <iterator>
 using namespace std;

 int main() {
  ifstream file1("file1.txt"), file2("file2.txt");
  istreambuf_iterator<char> f1(file1), f2(file2);
  cout << "f1 == f2 : " << boolalpha << (f1 == f2) << endl;
  cout << "*f1 = " << *f1 << endl;
  cout << "*f2 = " << *f2 << endl;
  return 0;
 }

Now assuming that neither f1 or f2 are at the end-of-stream then
f1 == f2 by 24.5.3.5 [lib.istreambuf.iterator::equal]

However, it is unlikely that *f1 will give the same value as *f2 except
by accident.

For example,
If file1.txt = "abc" and file2.txt="def" then the output would look like

f1 == f2 : true
*f1 = a
*f2 = b

So what does *f1 'equivalent' to *f2 mean?  I think the standard should
be clearer on this point, or at least be explicit that this does not
mean that *f1 and *f2 are required to have the same value in the case
of input iterators.
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]