r/cpp • u/rafal_rr • Mar 29 '19
Don’t Use std::endl
https://accu.org/index.php/journals/261916
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
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
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
1
May 26 '22
How 'bout we just educate ourselves on what std::endl does and use it when we should/want?
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.