Bloods vs Crips meme, captioned "Which side are you on? backslash N or end-ell"

top 50 comments

sorted by: hot top controversial new old
[–] 46 points 2 years ago* (7 children)

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 7 child comments
  • [–] 33 points 2 years ago (6 children)

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

  • source
  • parent
  • hideshow 6 child comments
  • [–] 8 points 2 years ago (4 children)

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

  • source
  • parent
  • hideshow 4 child comments
  • [–] 16 points 2 years ago (3 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 3 child comments
  • [–] 3 points 2 years 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
  • [–] 22 points 2 years ago (4 children)

    They aren't the same thing so the comparison is weird.

    endl has a flush which is important when doing something like embedded work or RTOS development. If i was doing multiple lines they all were \n until the last line when i actually want to push the buffer.

    Obviously depending on the tuning of the compiler's optimization multiple flushes could be reduced but the goal should always be to write as optimal as possible.

  • source
  • hideshow 4 child comments
  • [–] 20 points 2 years ago

    \n, because I ordered a newline, not a flush.

  • source
  • [–] 17 points 2 years ago (17 children)
  • [–] 40 points 2 years ago (3 children)

    std::endl is used in output streams in C++ to end the line, using the os specific line termination sequence, and flush the buffer.

    The later one is a performance issue in many cases, why the use of "\n" is considered preferred

  • source
  • parent
  • hideshow 3 child comments
  • [–] 2 points 2 years ago (2 children)

    Don’t most terminals flush the buffer on newline anyway?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 7 points 2 years ago (4 children)
  • [–] 16 points 2 years ago

    #define endl "\n"

  • source
  • [–] 12 points 2 years ago (1 child)
  • [–] 10 points 2 years ago (4 children)
  • [–] 9 points 2 years ago

    os.linesep

    Lol jk none of my stuff runs on Windows anyway

  • source
  • [–] 9 points 2 years ago (2 children)
  • [–] 6 points 2 years ago
    [–] 5 points 2 years ago

    Rebel side \0

  • source
  • [–] 5 points 2 years ago (2 children)

    Environment.NewLine might exist in C#

  • source
  • hideshow 2 child comments
  • load more comments (1 reply)
    [–] 5 points 2 years ago
    [–] 4 points 2 years ago

    Well, Java has System.lineSeparator so, maybe no?

  • source
  • [–] 3 points 2 years ago
    [–] 2 points 2 years ago

    Endl is faster to type

  • source
  • load more comments
    view more: next ›