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

98

u/alfps Feb 25 '19

std::endl is a fine convention for interactive console i/o.

In particular it helps ensure that all relevant output has been presented when a program crashes.

Not using endl and so misinterpreting what the output says about where the crash occurred, is a common beginner's problem.

endl is not fine for efficient text file i/o, but then iostreams are not fine for that in the first place.

So, the advice to ditch endl is ill-conceived, in both mentioned cases, and I can't think of more.

9

u/VirtueBot Feb 25 '19

I didn't think the author was trying to deny any use cases for flushing the stream, just that std::flush should be used instead of endl in those case.

5

u/jcelerier ossia score Feb 26 '19

The mere thought of typing << '\n' << std::flush; makes me want to slice my wrists

7

u/anders1234 C++ since MSC 6.0 Feb 26 '19

It seems more readable (at least for me) writing
std::cout << "hello world" << std::endl;

than

std::cout << "hello world\n" << std::flush;

​

but maybe I am just a n00b.

2

u/VirtueBot Feb 26 '19

oooooo this talk of slicing wrists is making me hot for std::flush!

fwiw I would write endl in that case too lol. But if it's "text\n" << std:: flush that sounds more appealing to me after reading the article.

Otoh maybe take a break before the next time you write to the console (;