I was listening to a google talk by Andrei Alexandrescu on the D programming language when he threw out a one liner about the "endl" fiasco. I just thought endl was the preferred way to signify the end of a line and flush the buffer for a stream. Why is it considered a fiasco? Should I not be using it in my code?
-
1@ildjarn: You are 100% right. I knew that it flushes the buffers and still lost ~75 points in [CEOI 2008](http://ceoi2008.de/en/welcome) for that! Bad habits! – Yakov Galka Mar 30 '11 at 21:15
-
1@ildjarn My teacher insists we use `std::endl` for everything rather than `\n`, which I agree is rather strange. But then again, we're just writing simple console programs. But then again again, he seems to think initializing a (POD) variable in a loop is a potential performance issue... – Maxpm Mar 30 '11 at 21:17
-
1I added the link, also I found the link on http://erdani.com/ – Tod Mar 30 '11 at 21:24
-
@Tod Thats Alexandrescus home page :) – Tom Mar 30 '11 at 21:26
2 Answers
(I assume) He just means that many, especially new, C++ programmers use std::endl
blindly instead of '\n'
for newline, flushing unnecessarily frequently and potentially making the performance of their program abysmal.
I.e., most people are taught that std::endl
is the canonical way to insert a newline into a stream even though it is very rarely necessary or appropriate to flush it.
It is some people's opinion (*cough*) that std::endl
shouldn't even be in the standard, as it is so rarely appropriate and not a significant typing savings over '\n' << std::flush
anyway.
TL;DR(s):
- In the end,
std::endl
buys you nothing except usually worse performance and usually more typing. - It has its uses, but they are considerably rarer than its frequency of use in most large codebases would suggest, therefore...
- Its utility is highly questionable and its ubiquity is absurd – a fiasco indeed!

- 27,591
- 48
- 66
- 103

- 62,044
- 9
- 127
- 211
-
2It's only really an issue then if your program performs a lot of stream-based I/O. – dreamlax Mar 30 '11 at 21:25
-
55AFAIK some people use it thinking that it's the "platform neutral" way to insert a newline, without knowing that `\n` **is** the newline in C, and that `\n`->platform specific line terminator conversion is handled by the stream. – Matteo Italia Mar 30 '11 at 21:27
-
7Are we talking premature optimization here? It is nice to have a simple to use end-line thingy to use for your output. Especially if you are learning the language and don't immediately see what '\n' would do for output. After you have learned to profile your code, you can consider other options! – Bo Persson Mar 30 '11 at 22:42
-
12@Bo Persson : The larger issue (in my mind) is that if there were no `std::endl`, then people would be taught to use `'\n'` from the beginning, and as `'\n'` is shorter to type then `std::endl` (and the same as `endl`) I would say it's simple to use too. :-] In the end, `std::endl` buys you nothing except worse performance and usually more typing. – ildjarn Mar 30 '11 at 22:58
-
12@Bo Persson : I guess another way of phrasing it would be, the existence and ubiquity of `std::endl` is a premature pessimization more-so than using `'\n'` is a premature optimization. – ildjarn Mar 30 '11 at 23:01
-
2
-
6@ildjarn: it buys you nothing except worse performance, more typing *and* flushing. I have been bitten by it, adding traces to a program that was crashing to detect when/where and being confused because unless you flush, absence of the message in the output does not mean that the trace was not reached, it could have been reached and not flushed. – David Rodríguez - dribeas Oct 28 '11 at 07:23
-
4@David : Sure, if you need flushing, use `endl`; however, most people who use `endl` do _not_ need flushing. – ildjarn Mar 29 '12 at 00:23
-
1
-
1@Raildex it doesn't *just* flush. It's the "make sure this line reaches a terminal / log file" operator – Caleth Nov 04 '21 at 09:13
To add to the already great answer, this issue can be extended to the 'iostream' fiasco.
Using iostream in general has a performance penalty because it is designed to be synced up with the stdio streams (printf
and scanf
) so that you can use a mix of isotream and stdio if so desired.
If you are only going to be using one you can get a performance benefit by calling std::ios::sync_with_stdio(false)
. I believe, however, the unsynced streams are no longer thread safe...

- 600
- 6
- 12