• jecxjo@midwest.social
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 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.

    • lud@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      but the goal should always be to write as optimal as possible.

      Within reason.

      Over optimization is a curse on getting done.

  • Digital Mark@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    0
    ·
    3 days ago

    If you write to a text (as opposed to binary) stream, \n produces \n or \r\n (or \r if old enough) depending on platform just fine.

    Nobody should be using C++ anyway, but plenty of languages have silly system newline constants, which do nothing useful.

      • easily3667@lemmus.org
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 days ago

        It’s memory unsafe and it’s syntax is indistinguishable from the runes which summon cthulu.

        • thebestaquaman@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          2 days ago

          Memory unsafe C++ is a choice. With modern C++ you have no excuse for accessing raw pointers or arrays without range checking if memory safety is a priority.

            • thebestaquaman@lemmy.world
              link
              fedilink
              arrow-up
              0
              ·
              2 days ago

              As I said: There are tools in place in modern C++ that are designed to catch the errors you make. If you are using a raw pointer when you could have used a reference, or accessing an array without range checking, those are choices you’ve made. They may be valid choices in your use-case, but don’t go complaining that the language is “unsafe” when it gives you the option to code with guard rails and you choose to forgo them.

        • MrScottyTay@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          0
          ·
          edit-2
          2 days ago

          I think the memory stuff is pretty good nowadays. I’m sure I saw modern C++ can have a garbage collector. And the syntax is only runelike until you learn it, like any language really. As an industry C# developer I’ve recently taken up C++ as a hobby to better learn the workings of low level code and I’ve been enjoying it so far.

    • grandel@lemmy.ml
      link
      fedilink
      English
      arrow-up
      0
      ·
      6 hours ago

      I like that you added the absolute namespace identifier or whatever its called

  • SpaceNoodle@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    3 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
            0
            ·
            2 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
              0
              ·
              2 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.