• unalivejoy@lemm.ee
      link
      fedilink
      English
      arrow-up
      12
      ·
      3 months ago

      Just because the box says something is flushable doesn’t mean you should flush it.

    • mmddmm@lemm.ee
      link
      fedilink
      arrow-up
      8
      ·
      3 months ago

      It’s a very C++ thing that the language developers saw the clusterfuck that is stream flushing on the kernel and decided that the right course of action was to create another fucking layer of hidden inconsistent flushing.

  • pelya@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    3 months ago

    printf is superior and more concise, and snprintf is practically the only C string manipulation function that is not painful to use.

    Try to print a 32-bit unsigned int as hexadecimal number of exactly 8 digits, using cout. You can do std::hex and std::setw(8) and std::setfill('0') and don’t forget to use std::dec afterwards, or you can just, you know, printf("%08x") like a sane person.

    Just don’t forget to use -Werror=format but that is the default option on many compilers today.

    C++23 now includes std::print which is exactly like printf but better, so the whole argument is over.

  • Oinks@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    6
    ·
    edit-2
    3 months ago

    I am very sorry to remind everyone about the existence of Visual Basic, but it has:

    • VbCrLf
    • VbNewLine
    • ControlChars.CrLf
    • ControlChars.NewLine
    • Environment.NewLine
    • Chr(13) & Chr(10)

    And I know what you’re asking: Yes, of course all of them have subtly different behavior, and some of them only work in VB.NET and not in classic VB or VBA.

    The only thing you can rely on is that “\r\n” doesn’t work.

  • ulterno@programming.dev
    link
    fedilink
    English
    arrow-up
    4
    ·
    edit-2
    3 months ago

    Simple. \n when you just want a newline.
    endl when you need to flush at the moment.

    Useful in case you are printing a debug output right before some function that might do bed stuff to buffers.


    Edit: I wrote println instead of endl somehow. Guess I need more downtime

    • Croquette@sh.itjust.works
      link
      fedilink
      arrow-up
      2
      ·
      3 months ago

      I just learned that in Python, it’s fucking terrible. Python is a fucking mess and my next script will be in a different language.

      • qaz@lemmy.world
        link
        fedilink
        English
        arrow-up
        3
        ·
        3 months ago

        Perhaps TS is not a terrible language for shell scripts after all

        • Croquette@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          3 months ago

          I prefer strongly typed languages. Using bytes isn’t intuitive.

          Transforming certain data types into other data types is often not straightforward.

          The identation is the worst though. Let me format the code however I want.

  • Sibbo@sopuli.xyz
    link
    fedilink
    arrow-up
    2
    ·
    3 months ago

    Wasn’t this {fmt} library merged into STL now? Does this solve this issue?

    Anyways, there was also a constant that is the OS line ending without a flush, right?

  • r00ty@kbin.life
    link
    fedilink
    arrow-up
    2
    ·
    3 months ago

    Maybe c# has similar. There’s \r\n or \n like c++ and Environment.NewLine.

    Probably it’s similar in that Environment.NewLine takes into account the operating system in use and I wonder if endl in c++ does the same thing?

    • edinbruh@feddit.it
      link
      fedilink
      arrow-up
      5
      ·
      3 months ago

      Unix needed only \n because it had complex drivers that could replace \n with whatever sequence of special characters the printer needed. Also, while carriage return is useful, they saw little use for line feed

      On dos (which was intended for less powerful hardware than unix) you had to actually use the correct sequence which often but not always was \r\n (because teleprinters used that and because it’s the “most correct” one).

      Now that teleprinters don’t exist, and complex drivers are not an issue for windows, and everyone prefers to have a single \n, windows still uses \r\n, for backward compatibility.