r/cpp • • Mar 29 '19

Don’t Use std::endl

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

27 comments sorted by

47

u/capn_bluebear Mar 29 '19

This again...the message is way too extreme: there's no problem using `std::endl` outside of performance-critical code (which, in my personal experience, is 99.9% of std::endl usage). _Definitely_ use `std::endl` if a newline+flush is what you want.

If for some reason you need your std:: printing to go faster, definitely avoid flushing at every newline.

15

u/konanTheBarbar Mar 29 '19

Yeah I think the emphasis should be on not using std::endl for file streams. I observed some pretty bad performance regression in that case, but not so much for cout.

18

u/F-J-W Mar 29 '19

The problem is that std::endl is premature pessimization in the vast majority of cases, making it impossible to see whether the flush is intentional.

<< '\n' << std::flush completely avoids that problem and should thus always be preferred.

3

u/Gotebe Mar 30 '19

What do you mean "impossible"?!

If the author of the code knows what endl does, then it us, otherwise it isn't - simple!

13

u/F-J-W Mar 30 '19

The vast majority of people do not know however and it is very hard to know if the author knows (or knew when they wrote the code). That is the opposite of simple.

-1

u/Gotebe Mar 31 '19

Why is it very hard? I can ask him, can't I?

8

u/F-J-W Mar 31 '19

You keep making my points for me: If you have to ask whether this was intentional you have long left the realms of readable and self-explaining code.

Also: Not every other author sits next to you in an office that both of you work in every day. Maybe you are looking at a sporadically maintained FLOSS-project or the author is on vacation, making this into a potentially multi-week-endeavor until you get your answer.

→ More replies (1)

2

u/patatahooligan Mar 31 '19

Asking the author is not a viable solution. They might not remember, they might not work where you are anymore or you might have not gotten the code from someone in person to begin with. Information that is not in the code or in the documentation is almost guaranteed to be lost eventually.

→ More replies (1)

1

u/Obvious_Presentation Mar 31 '19

Aren't most tools line buffered so shouldn't '\n' be equivalent to endl?

4

u/F-J-W Mar 31 '19

It's common on Linux (and IIRC different on for example Windows) but even here std::cout << "foo\n" is measurably faster than std::cout << "foo" << std::endl because you still safe write-operations by combining them.

2

u/Osbios Mar 29 '19

There also is the issue with output from multiple threads. Mixing up parts of things you intended to output as a single string over multiple lines.

3

u/kalmoc Mar 30 '19

On cout that will happen regardless, because the cout stream object doesn't buffer by default.

1

u/Osbios Mar 30 '19

Interesting, did not know that. But makes sense. Every single << is a function call.

0

u/[deleted] Mar 30 '19

[deleted]

1

u/kalmoc Mar 30 '19

You are mixing stream buffering on the OS level (it maybe c level) and buffering in the c++ upstream objects.

16

u/jbandela Mar 29 '19

My opinion is that it is not a big deal. Most programs that use std::endl are toy ones where the output performance isn't a big deal.

It it does turn out the the flushing is a major bottleneck, then that is a teachable moment, to go over various options such as using "\n", switching to a binary format that may be more compact, or not using iostreams.

In addition, when teaching beginners, std::endl flushing may prevent them from being surprised by output that seems to occur later than when they did it.

Basically, std::endl won't matter for beginners and learners, and if it does matter there are bigger issues to consider than flushing vs non-flushing.

1

u/patatahooligan Mar 31 '19

In addition, when teaching beginners, std::endl flushing may prevent them from being surprised by output that seems to occur later than when they did it.

That's an even more valuable lesson, though. Debugging through std::cout is common for whatever reason and it is inherently broken.

15

u/OlivierTwist Mar 29 '19

Let me guess without reading: because std::endl flushes the stream?

Well, that is a feature, not a bug.

9

u/boredcircuits Mar 29 '19

Eventually fmtlib will be standardized and we'll forget all about this nonsense.

1

u/NotWorthTheRead Mar 30 '19

I’m sure my grandkids will tell me all about it in the nursing home.

(I love C++ but timely integration is not something they value.)

0

u/mili42 Mar 30 '19

formatting tools are not that useful in practice anyway, as simply needs to concat string and numbers.

It is actually better to flush [in my opinion], as normal people expect to see something in their terminal when using std::cout

3

u/[deleted] Mar 29 '19

I'm fine with it, I sometimes forget to flush in C and most of the time it's exactly what I want

3

u/HappyFruitTree Mar 29 '19

I think this is a good idea, but I also think that it doesn't really matter in most cases so I hope this will not be another of those things that we are going to tell beginners to stop doing out of the blue when they ask for help on a totally unrelated problem.

1

u/[deleted] Apr 01 '19

I use "\n" because "hi\n" is a lot more comfortable to type than "hi" << std::endl

I usually don't think that typing is a strong argument but I'm this case the technicalities simply don't matter for 99.9% of the cases.

I call bike shedding

1

u/[deleted] Apr 14 '19

If performance is that much of a concern you shouldn't be using std::iostream at all.

1

u/[deleted] May 26 '22

How 'bout we just educate ourselves on what std::endl does and use it when we should/want?