• jecxjo@midwest.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    21 days ago

    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.

  • SpaceNoodle@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    22 days ago

    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.

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

          • zenforyen@feddit.org
            link
            fedilink
            arrow-up
            1
            ·
            22 days 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.

            • AnyOldName3@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              21 days ago

              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.

    • vapeloki@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      22 days ago

      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

        • ClemaX@lemm.ee
          link
          fedilink
          arrow-up
          1
          ·
          22 days ago

          It is the stream itself that is buffered, so the terminal does not handle the contents until the stream is flushed.

    • Hellfire103@lemmy.caOP
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      22 days ago

      Instead of this:

      cout << "Hello world.\n";
      

      You can do this:

      cout << "Hello world." << endl;
      
      • Daedskin@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        21 days ago

        The fact that you used the namespace for cout but not for endl inordinately bothers me