简体   繁体   中英

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::basic_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM