views:

42

answers:

2

What is going on?

#include <iostream>
#include <iterator>
#include <sstream>

int main() {
    std::basic_stringbuf<unsigned char> buf;
    std::basic_istream<unsigned char> stream(&buf);
    // the next line throws std::bad_cast on g++ 4.4
    std::istream_iterator<unsigned char, unsigned char> it(stream);
}

I've tried stream.write(some_array, sizeof(some_array) before constructing the iterator, to no avail.

Thanks.

A: 

shouldnt it be:

std::istream_iterator<unsigned char> it(stream);
inf.ig.sh
That doesn't compile. ("no matching function for call to std::istream_iterator...")
Pedro d'Aquino
+1  A: 

It throws from sentry object's constructor where it checks the ctype facet on the stream (it needs it so it can skip whitespace), which happens to be NULL because it's not defined for unsigned chars.

Do you need to handle whitespace on that stream? If not, change to

std::istreambuf_iterator<unsigned char> it(stream);
Cubbi
Yes, this does it. Alternatively, I guess `stream.unsetf(std::ios::skipws)` has the same effect. Thanks.
Pedro d'Aquino