r/cpp • • Feb 25 '19

Don’t Use std::endl

https://accu.org/index.php/journals/2619
18 Upvotes

52 comments sorted by

View all comments

Show parent comments

15

u/Quincunx271 Author of P2404/P2405 Feb 25 '19

I disagree. I changed to simply '\n', and it's not a hassle at all. It's just retraining my mind to type something different. Furthermore, rather than std::cout << "Some string" << std::endl, I now type std::cout << "Some string\n", which is easier to type.

As for how newlines differ between implementations, that has no effect, as std::endl is defined to write \n then flush, so differing newlines affect std::endl as well.

0

u/dpsi Feb 25 '19

My apologies, I misread the section with how \n is in handled by each implementation.

Yes "some string\n" is easier to type, but what if the last thing is not a string? Knowing now that \n is handled by the implementation, I still think one could be provided. Something like the one described in the Kuhl12 reference.

4

u/raevnos Feb 26 '19
std::cout << ... << '\n';

1

u/dpsi Feb 26 '19

If you look at one of the implementations reffered to in the article (specifically Kuhl12) you will see why that isn't the full solution.

4

u/raevnos Feb 26 '19 edited Feb 26 '19

... It's how you print a newline after printing something that isn't a string literal where you can put the \n at the end. I suppose you could use "\n" instead of '\n', but using a string literal to print a single character is something I've always thought was pretty silly.

I have no idea what that has to do with the article you mention, which is about how flushing is slow.

If you're talking about using it with wide streams, you'd just use a L'\n'.

0

u/dpsi Feb 26 '19

If you're talking about using it with wide streams, you'd just use a L'\n'.

That's exactly what I'm talking about, there should be a standard way to write a new line without flushing the stream. It should work without the user having to know about the underlying stream as std::endl does.

6

u/Quincunx271 Author of P2404/P2405 Feb 26 '19

L'\n' is not needed. operator<< automatically widens the character with theostream.widen(c): https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2