you are viewing a single comment's thread
view the rest of the comments
[–] 46 points 1 year ago* (1 child)

If I'm writing C++, I'm usually optimizing for portability over performance, in which case I would prefer std::endl as it would yield the best results regardless of platform; it also keeps the end-of-line character out of other strings, making code just a little cleaner.

\n is for when I'm done pretending that anything that isn't Unix-like is OK, or I'm counting the cycles of every branch instruction.

  • source
  • hideshow 2 child comments
  • [–] 33 points 1 year ago (2 children)

    std::endl provides zero portability benefits. C++ does have a portable newline abstraction, but it is called \n, not endl.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 8 points 1 year ago (1 child)

    No, there's no guarantee that in every context \n is translated portably.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 16 points 1 year ago (2 children)

    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.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 3 points 1 year ago

    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.

  • source
  • parent