bool std::operator==( std::istreambuf_iterator, std::istreambuf_iterator )
returns TRUE if both iterators are EOF or both are not. That means two
iterators at different places in the streambuf -- which, when dereferenced
produce different characters -- nevertheless compare as equal. This is
inconsistent with how other iterators work, and inconsistent with the pointer
model of iterators.
streambuf iterators are constructed from a stream, and could use the stream's
tellg/tellp to determine equality. (Two streams both at EOF should of course
continue to be equal to permit canonical use in e.g. std::copy.)
In the program below, two istreambuf_iterator objects repeatedly compare as
true even though one is incremented. I would expect no output, but the program
produces:
45 4c 46 01 01 01 00 00 00 00
IOW, two such iterators are always equal! Or, acutally, looking at the
operator==() code:
return (__thiseof && __beof || (!__thiseof && !__beof));
their equality is a function of their EOF status only.
== snip ==
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int
main( int argc, char *argv[] )
{
string name(argv[0]);
ifstream input( name.c_str() );
istreambuf_iterator<char> begin(input);
istreambuf_iterator<char> end(input);
for( int i=0; i < 10; i++ ) {
if( begin == ++end ) {
int data = *end;
cout << setw(2) << setfill('0')
<< hex << data << ' ';
}
}
return 0;
}
== pins ==
Unless two streambuf_iterators can be compared for equality within the stream,
it's impossible to use standard algorithms with them to process part of a file:
to work, one iterator must always be EOF.
--
Summary: streambuf_iterator compares equal when it should not
Product: gcc
Version: 4.0.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jklowden at schemamania dot org
GCC build triplet: i386--netbsdelf
GCC host triplet: i386--netbsdelf
GCC target triplet: i386--netbsdelf
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45725