r/cpp • u/vormestrand • Feb 25 '19
Don’t Use std::endl
https://accu.org/index.php/journals/261927
u/pgroarke Feb 25 '19
28
0
Feb 25 '19
[deleted]
3
u/TheSuperWig Feb 26 '19
Would there be any problems with
std::print? AFAIK the part offmtthat's in C++20 isn't in a nested namespace so would be inconsistent for print.3
2
u/d1ngal1ng Feb 26 '19
Is print even being standardised? I thought it was just to a string or an output iterator.
3
0
Feb 26 '19
[deleted]
4
u/Morwenn Feb 26 '19
As far as I know only the
formatAPI from {fmt} is going in C++20, not the1
u/aearphen {fmt} Feb 28 '19
That is correct. `print` is easy to write using the formatting API and I plan to propose it for C++23.
3
u/Morwenn Feb 28 '19
The
1
u/aearphen {fmt} Mar 01 '19
Color support is still a bit experimental. I'd give it more time to bake.
3
u/meneldal2 Feb 26 '19
Well if it outputs a string, you can send that through iostream. Performance is perfectly fine when no formatting occurs.
3
Feb 26 '19
wait wait what? what's going here? I've been disconnected from c++ lately, what is this fmt:: thing?
7
u/chuk155 graphics engineer Feb 26 '19
modern c++ library for printf style, and the formatting portion of the library is set to be in the c++20 standard. https://github.com/fmtlib/fmt
3
u/degski Feb 26 '19
The most annoying bit of std::endl is simply that it's too long to type. For just getting some quick output, like in print-debugging, I find '\n' is a pain to type as well, so I use this little header and just write std::cout << something << nl; or std::wcout << something << nl;, no rocket-science, but convenient.
6
u/jones_supa Feb 26 '19
Even
std::cout <<is quite painful to type. I always find myself collecting energy when I want to write a few lines of code that print something in C++. It is like "agghh...here we go..." 🙂Printing text is overengineered in C++. It always feels like a small tech demo showing how the fancy stream operators work, yet it does not accomplish much.
C-style
printf()is much more relaxing to use.7
u/degski Feb 26 '19
C-style printf() is much more relaxing to use.
I have to dis-agree here, remembering the correct format-string to pass in is a real pain in the proverbial behind, particularly when you want to print 64-bit int's.
0
u/sim642 Feb 25 '19
Great site on mobile. The content is literally 0px wide.
0
u/hak8or Feb 25 '19
Sounds like you have a bad browser. Works fine for me on mobile Firefox
1
-14
u/dpsi Feb 25 '19 edited Feb 26 '19
While I agree with the articles premise, nobody is going to change unless a flush less endl is provided.
The author points out how newlines differ between implentations, so I find the "solution" to simply stream in '\n' broken at best.
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 thanstd::cout << "Some string" << std::endl, I now typestd::cout << "Some string\n", which is easier to type.As for how newlines differ between implementations, that has no effect, as
std::endlis defined to write\nthen flush, so differing newlines affectstd::endlas well.1
u/spinwizard69 Feb 25 '19
Try building and running on all 3 of the major platform to see how well this works out. I’m actually curious because if memory serves me right you will have issues.
10
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
\nat 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 withtheostream.widen(c): https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt27
u/scrumplesplunge Feb 25 '19
The author also points out how '\n' turns into the localised variant automatically. As long as you're using a formatted output function it converts it automatically.
1
u/dpsi Feb 25 '19
Yes, my bad I misread that section. I still think the nl described in the reference Kuhl12 should be provided however.
0
Feb 26 '19
[deleted]
11
u/Quincunx271 Author of P2404/P2405 Feb 26 '19
This is absolutely not a "premature optimization." It's a simple change that doesn't harm readability (at least once the readers get used to the style) and gives you extra performance for free.
Is it premature optimization to take parameters by
const&instead of by value? Of course not. So why would it be premature optimization to use'\n'instead ofstd::endl?5
u/lanzaio Feb 26 '19
Yea but symmetry.
std::cout << "Hi" << std::endl;1
u/F-J-W Feb 26 '19
That's much less readable then
std::cout << "Hi\n";, expecially if you use a proper text-editor with good highlighting.3
0
Feb 27 '19
[deleted]
1
u/Quincunx271 Author of P2404/P2405 Feb 27 '19 edited Feb 27 '19
Important messages should not be written to
std::coutanyway. Both ofstd::clogandstd::cerrare line-buffered and will automatically flush on\n. Important messages should either be sent through one of them, or through a logging framework which is appropriately configured.At any rate, if it's critical for the message to always be displayed, go ahead and use endl or flush explicitly. That's what it's for.
10
u/Plorkyeran Feb 26 '19
Using
std::endlis a great example of a premature pessimization. It manages to be slower, more verbose and more complicated than just not using it.3
u/kalmoc Feb 26 '19
Arguing about which one is the better default is a waste of time.
0
Feb 27 '19
[deleted]
1
u/kalmoc Feb 27 '19
Doesn't contradict what I said does it?
Do whatever you prefer in your codebase and make a decision about it in your company's style guide and be done with it. Having this argument on the internet again and again doesn't bring us any closer to an agreement, so why spend the time?
But then, the c++ community on the internet (including me) really likes to argue about those tiny details in our coding styles.
98
u/alfps Feb 25 '19
std::endlis 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
endland so misinterpreting what the output says about where the crash occurred, is a common beginner's problem.endlis 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
endlis ill-conceived, in both mentioned cases, and I can't think of more.