In C ++ 11, the following works:
#include <iostream> struct myout_base { }; struct myout { bool alive; myout() : alive(true) { } myout(myout && rhs) : alive(true) { rhs.alive = false; } myout(myout const &) = delete; ~myout() { if (alive) std::cout << std::endl; } }; template <typename T> myout operator<<(myout && o, T const & x) { std::cout << x; return std::move(o); } template <typename T> myout operator<<(myout_base &, T const & x) { return std::move(myout() << x); } myout_base m_out; // like the global std::cout int main() { m_out << 1 << 2 << 3; }
With extra work, you can add a link to the actual output stream.
source share