#include <ostream> // see [ostream.syn]
namespace std {
template<class charT, class traits, class Allocator>
class basic_syncbuf;
using syncbuf = basic_syncbuf<char>;
using wsyncbuf = basic_syncbuf<wchar_t>;
template<class charT, class traits, class Allocator>
class basic_osyncstream;
using osyncstream = basic_osyncstream<char>;
using wosyncstream = basic_osyncstream<wchar_t>;
}
namespace std {
template<class charT, class traits, class Allocator>
class basic_syncbuf : public basic_streambuf<charT, traits> {
public:
using char_type = charT;
using int_type = typename traits::int_type;
using pos_type = typename traits::pos_type;
using off_type = typename traits::off_type;
using traits_type = traits;
using allocator_type = Allocator;
using streambuf_type = basic_streambuf<charT, traits>;
// [syncstream.syncbuf.cons], construction and destruction
explicit basic_syncbuf(streambuf_type* obuf = nullptr)
: basic_syncbuf(obuf, Allocator()) {}
basic_syncbuf(streambuf_type*, const Allocator&);
basic_syncbuf(basic_syncbuf&&);
~basic_syncbuf();
// [syncstream.syncbuf.assign], assignment and swap
basic_syncbuf& operator=(basic_syncbuf&&);
void swap(basic_syncbuf&);
// [syncstream.syncbuf.members], member functions
bool emit();
streambuf_type* get_wrapped() const noexcept;
allocator_type get_allocator() const noexcept;
void set_emit_on_sync(bool) noexcept;
protected:
// [syncstream.syncbuf.virtuals], overridden virtual functions
int sync() override;
private:
streambuf_type* wrapped; // exposition only
bool emit_on_sync{}; // exposition only
};
// [syncstream.syncbuf.special], specialized algorithms
template<class charT, class traits, class Allocator>
void swap(basic_syncbuf<charT, traits, Allocator>&,
basic_syncbuf<charT, traits, Allocator>&);
}basic_syncbuf(streambuf_type* obuf, const Allocator& allocator);
basic_syncbuf(basic_syncbuf&& other);
~basic_syncbuf();
basic_syncbuf& operator=(basic_syncbuf&& rhs) noexcept;
void swap(basic_syncbuf& other) noexcept;
bool emit();
streambuf_type* get_wrapped() const noexcept;
allocator_type get_allocator() const noexcept;
void set_emit_on_sync(bool b) noexcept;
namespace std {
template<class charT, class traits, class Allocator>
class basic_osyncstream : public basic_ostream<charT, traits> {
public:
using char_type = charT;
using int_type = typename traits::int_type;
using pos_type = typename traits::pos_type;
using off_type = typename traits::off_type;
using traits_type = traits;
using allocator_type = Allocator;
using streambuf_type = basic_streambuf<charT, traits>;
using syncbuf_type = basic_syncbuf<charT, traits, Allocator>;
// [syncstream.osyncstream.cons], construction and destruction
basic_osyncstream(streambuf_type*, const Allocator&);
explicit basic_osyncstream(streambuf_type* obuf)
: basic_osyncstream(obuf, Allocator()) {}
basic_osyncstream(basic_ostream<charT, traits>& os, const Allocator& allocator)
: basic_osyncstream(os.rdbuf(), allocator) {}
explicit basic_osyncstream(basic_ostream<charT, traits>& os)
: basic_osyncstream(os, Allocator()) {}
basic_osyncstream(basic_osyncstream&&) noexcept;
~basic_osyncstream();
// [syncstream.osyncstream.assign], assignment
basic_osyncstream& operator=(basic_osyncstream&&) noexcept;
// [syncstream.osyncstream.members], member functions
void emit();
streambuf_type* get_wrapped() const noexcept;
syncbuf_type* rdbuf() const noexcept { return &sb ; }
private:
syncbuf_type sb; // exposition only
};
}basic_osyncstream(streambuf_type* buf, const Allocator& allocator);
basic_osyncstream(basic_osyncstream&& other) noexcept;
~basic_osyncstream();
basic_osyncstream& operator=(basic_osyncstream&& rhs) noexcept;
void emit();
{
osyncstream bout(cout);
bout << "Hello," << '\n'; // no flush
bout.emit(); // characters transferred; cout not flushed
bout << "World!" << endl; // flush noted; cout not flushed
bout.emit(); // characters transferred; cout flushed
bout << "Greetings." << '\n'; // no flush
} // characters transferred; cout not flushed
streambuf_type* get_wrapped() const noexcept;
{
osyncstream bout1(cout);
bout1 << "Hello, ";
{
osyncstream(bout1.get_wrapped()) << "Goodbye, " << "Planet!" << '\n';
}
bout1 << "World!" << '\n';
}
produces the uninterleaved output