r/cpp • • Mar 29 '19

Don’t Use std::endl

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

27 comments sorted by

View all comments

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.

-5

u/Gotebe Mar 31 '19

Niah.. You're inventing "your point" as you go. You said first that's "impossible to know". Now you're saying it's not readable.

And dig this: if you know what this does (I did) code is readable and it explained itself.

So... good luck with this, you have won (it's what you care about anyhow, so you can have it, I am not interested).

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.

0

u/Gotebe Apr 01 '19

So let me get this straight...

  • there is a performance problem (because if there wasn't, the point is moot)

  • one can't check whether changing to '\n' fixes it (because there's no tests? Because there's no performance targets?)

  • there's no documentation about this in a comment or whatever

  • there's nothing to see from code history (e.g. whether this part was written by an intern thereby making the probability of them nor knowing what endl does)

  • there's nobody to ask for all the reasons you evoke above.

Yeah, that is surely what OP called "impossible".

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.

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.

1

u/Osbios Mar 30 '19

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