开发者

Using boost IOStreams with std::ostream_iterator

I tried to use an array-device based stream and wantet to pass the stream to std::ostream_iterator or std::istream_iterator, but unfortunately, I get a compilation error with gcc 4.3.5.

Boost::IOStreams documentation states that the io::stream is either derived from std::basic_istream or std::basic_ostream or both (std::basic_iostream) dependent on the underlying device category. array device has seekable category, so I'd expect io::stream to derive from std::basic_iostream and be compatible with std::ostream_iterator or std::istream_iterator. But I unfortunately get a compilation error.

Here is the code snippet:

namespace io=boost::io;

typedef unsigned char byte;
typedef io::basi开发者_JAVA百科c_array<byte>  array_device;
typedef io::stream<array_device> array_stream;

byte my_buffer[256]={};

array_stream  ios_(my_buffer); 

std::istream_iterator<byte> in(ios_);

And the last line results in the error stating:

src/my_file.cpp: In member function 'void my_test_class::ctor::test_method()':
src/my_file.cpp:86: error: no matching function for call to 
'std::istream_iterator<unsigned char, char, std::char_traits<char>, int>::istream_iterator(my_test_class::<unnamed>::array_stream&)'


You're not supplying enough template arguments for std::istream_iterator -- the second argument is the underlying character type of the stream, which defaults to char, but the underlying character type of your stream is byte (unsigned char).

Changing

std::istream_iterator<byte> in(ios_);

to

std::istream_iterator<byte, byte> in(ios_);

should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜