• marcos@lemmy.world
    link
    fedilink
    arrow-up
    20
    ·
    11 hours ago

    That’s how the C++ code should have looked all the time. And the amount of people that get surprised and complain about this is just more evidence that nobody should write C++. Ever.

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        5
        ·
        6 hours ago

        I guess, if you come from garbage-collected languages, you might be used to not needing the ampersands, because everything is automatically a reference in those…

        • XPost3000@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          6 hours ago

          Yeah this is how it was for me when I first started C++, I was use to any object beyond a simple 3D vector to always be passed by reference

          And then I read a C++ book my uncle gave me during a flight and realized that there isn’t any syntax for passing a parameter by copy, so obviously that’d have to be the default behavior and I’ve been passing by reference ever since

          • Ephera@lemmy.ml
            link
            fedilink
            English
            arrow-up
            3
            arrow-down
            1
            ·
            5 hours ago

            Oh wow, what the hell. I’m not actually familiar with C++ (just with Rust which gets similar reactions with the ampersands), but that’s insane that it just copies shit by default. I guess, it comes from a time when people mostly passed primitive data types around the place. But yeah, you won’t even notice that you’re copying everything, if it just does it automatically.

            And by the way, Rust did come up with a third meaning for passing non-references: It transfers the ownership of the object, meaning no copy is made and instead, the object is not anymore allowed to be used in the scope that passed it on.
            That’s true, except for data types which implement the Copy trait/interface, which is implemented mostly for primitive data types, which do then get treated like C++ apparently treats everything.

  • Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    31
    ·
    14 hours ago

    The rule of thumb I always tell people is that they should generally put owned data into struct fields and references into function parameters.

    • calcopiritus@lemmy.world
      link
      fedilink
      arrow-up
      11
      ·
      13 hours ago

      Good rule of thumb. As long as it’s not followed blindly of course.

      Structs with lifetimes are often quite convenient. Especially for performance.

    • harrowhawk@lemmy.sdf.org
      link
      fedilink
      arrow-up
      7
      arrow-down
      3
      ·
      14 hours ago

      I like this rule of thumb and it goes nicely with the “if you have more than 3 arguments to a function, consider making a struct to pass the arguments in” rule of thumb because

      You can have the struct use a named lifetime (or several) for the different parameters so it doesn’t own them!

      Best of both worlds.