// Test case for flushing a filtering_ostream based on the // back_inserter example in // // augmented by a transparent output filter simplified from the // example given in // . // // Flushing a filtering_ostream using the flush() member function // stopped working in Boost 1.44. The string the back_inserter writes // to stays empty. A valid workaround seems to call strict_sync(), as // it leads to the expected result. The member function sync() // documented in // // does not seem to exist. // #include #include #include #include #include #include #include namespace io = boost::iostreams; // Simple output filter forwarding just forwarding the put chars to // the sink. struct transparent_output_filter { typedef char char_type; typedef io::output_filter_tag category; template bool put(Sink& snk, char c) { return io::put(snk, c); } }; int main(int argc, char *argv[]) { using namespace std; string result; io::filtering_stream out; // Transparent filter just forward the characters to the sink. If it // is left out from the filter chain, out.flush() works correctly // and result has the expected value. out.push(transparent_output_filter()); // Sink appends characters to string result. out.push(io::back_inserter(result)); // Write test string to filtering_ostream. out << "Hello World!"; // The flush only works in Boost 1.44 if there's only the sink // device in the chain. Until Boost 1.43 this used to work. out.flush(); // The documented sync() member function leads to a compile error // "‘class // boost::iostreams::filtering_stream, std::allocator, // boost::iostreams::public_>’ has no member named ‘sync’". //out.sync(); // Calling strict_sync() instead of flush() or sync() results in the // characters to be appended to the result string also in Boost 1.44. //out.strict_sync(); // Check result. cout << "result = \"" << result << "\"" << endl; assert(result == "Hello World!"); return 0; }