#include #include #include #include #include #include #include class FixedBuffer : public std::streambuf { std::array data_{}; protected: int_type overflow(int_type c = traits_type::eof()) override { if (traits_type::eq_int_type(c, traits_type::eof())) return traits_type::not_eof(c); return traits_type::eof(); } public: FixedBuffer() { setp(data_.data(), data_.data() + data_.size()); } FixedBuffer(const FixedBuffer&) = delete; FixedBuffer& operator=(const FixedBuffer&) = delete; std::string text() const { return std::string(pbase(), pptr()); } }; int main() { FixedBuffer buffer; std::ostream out(&buffer); out.imbue(std::locale::classic()); out << 42 << '!'; assert(out.good()); assert(buffer.text() == "42!"); out.put('?'); assert(out.bad()); assert(buffer.text() == "42!"); }