I've read the difference between std::endl
and '\n'
is that std::endl
flushes the buffer and '\n'
doesn't. However, as far as I know stdout
on linux is line-buffered anyway, so does it mean that std::cout << ... << std::endl
is the same as std::cout << ... << '\n'
?
std::ostream os;
os << std::endl; // more concise
os << '\n' << std::flush; // more explicit about flushing
Those two lines have the exact same effect.
The manual flushing is often a waste of time:
If the output stream is line-buffered, which should be the case for std::cout
if it connects to an interactive terminal and the implementation can detect that, then printing \n
already flushes.
If the output stream is paired with an input stream you read from directly afterwards (std::cout
and std::cin
are paired), then reading already flushes.
If you nobody waits for the data to arrive at its destination, flushing is again just going to waste.