#include #include #include void runTest(bool doSeek) { namespace io = boost::iostreams; typedef io::restriction SlicedStreamDev; std::istringstream data; data.str("foo bar"); // Seek to start of "bar", and restrict to a single character int offset = 4; int length = 1; data.seekg(offset); io::stream restrictedStream(SlicedStreamDev(data, offset, length)); if (doSeek) restrictedStream.seekg(0); // Seek to beginning of restricted stream - should do nothing. // Should dump the entire contents of the restricted stream (ie, "b") to // the screen. This doesn't work if seekg(0) has been called above std::cout << restrictedStream.rdbuf() << "\n"; } int main() { runTest(false); runTest(true); // Here's part of what's happening behind the scenes: std::istringstream data; data.str("foo bar"); { // boost::iostreams::seek with std::streambuf calls the following, which // fails according to C++11 standard N3242, section 27.8.2.4 (table 130) std::streambuf::pos_type result = data.rdbuf()->pubseekoff(4, std::ios::cur, std::ios::in | std::ios::out); std::cout << "istringstream pubseekoff() result: " << result << "\n"; } { // The following also does not work in libstdc++, since istringstream // has no output pointer set in the streambuf, so passing std::ios::out // doesn't make sense (note implications for boost::iostreams::seek, // which passes (std::ios::in | std::ios::out) by default!) std::streambuf::pos_type result = data.rdbuf()->pubseekoff(4, std::ios::beg, std::ios::in | std::ios::out); std::cout << "pubseekoff() result: " << result << "\n"; } return 0; }