Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better cout a.k.a coutn;

Tags:

c++

Guys would it be difficult to write coutn which would basically place newline symbol at the end of the input. While working with console (that's all I can do at the moment) I'm finding very tedious to write '\n' every time I want the line to be a new line.
Or maybe it is already implemented?

like image 748
There is nothing we can do Avatar asked Jun 08 '10 13:06

There is nothing we can do


1 Answers

To circumvent the multiple injections on a single line, you could use a temporary object. This temporary object would add the '\n' in its destructor.

struct coutn {
    coutn(): os(cout) {}
    ~coutn() { os << '\n'; }
    template <typename T>
        coutn & operator<<(T const & x) { os << x; return *this; }
private:
    ostream &os;
};

coutn() << "Hello " << "World" << "!";

In the end, I'm wondering if this coutn is actually better?

like image 60
Didier Trosset Avatar answered Sep 19 '22 16:09

Didier Trosset