By Tod

2011-03-30 21:04:52 8 Comments

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?


@ildjarn 2011-03-30 21:22:45

Reposting from my comment:

(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.


  • 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!

@dreamlax 2011-03-30 21:25:32

It's only really an issue then if your program performs a lot of stream-based I/O.

@Matteo Italia 2011-03-30 21:27:01

AFAIK 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.

@Bo Persson 2011-03-30 22:42:43

Are 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!

@ildjarn 2011-03-30 22:58:48

@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 2011-03-30 23:01:01

@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.

@John 2011-09-23 21:39:24

Learned something new today. Thank you!

@David Rodríguez - dribeas 2011-10-28 07:23:47

@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.

@ildjarn 2012-03-29 00:23:40

@David : Sure, if you need flushing, use endl; however, most people who use endl do not need flushing.

Related Questions

Sponsored Content

40 Answered Questions

10 Answered Questions

11 Answered Questions

[SOLVED] What does the explicit keyword mean?

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2363081 View
  • 4243 Score
  • 1 Answer
  • Tags:   c++ c++-faq

28 Answered Questions

15 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

12 Answered Questions

[SOLVED] C++: "std::endl" vs "\n"

8 Answered Questions

[SOLVED] What is The Rule of Three?

Sponsored Content