The same is true of std::endl. std::endl is simply defined as << '\n' << std::flush; nothing more, nothing less. In all cases where endl gives you a “properly translated” newline, so does \n.
It’s controlled by whether the stream’s opened in text mode or binary mode. On Unix, they’re the same, but on Windows, text mode has line ending conversion.
std::endl provides zero portability benefits. C++ does have a portable newline abstraction, but it is called
\n
, not endl.No, there’s no guarantee that in every context \n is translated portably.
The same is true of std::endl. std::endl is simply defined as
<< '\n' << std::flush
; nothing more, nothing less. In all cases where endl gives you a “properly translated” newline, so does\n
.Ahhh, I see. Looks like the magic happens somewhere further down in iostream.
It’s controlled by whether the stream’s opened in text mode or binary mode. On Unix, they’re the same, but on Windows, text mode has line ending conversion.
Yeah it’s an artificial dichotomy based on a popular misconception of what std::endl is and how \n is interpreted.
Ultimately it does not ask about line endings, but about flushing, which is a completely orthogonal question.
Thank you two for demonstrating the image in the post so well.