4 ms·
Some basic things in the article appear to be factually wrong. > Then we ask us the following questions: > 1. When we passed Dave to show, did we create a cop
by nayuki 2y ago
Some basic things in the article appear to be factually wrong.
> Then we ask us the following questions:
> 1. When we passed Dave to show, did we create a copy?
> 2. If so, how do we avoid creating a copy?
> C++ example
> 1. Yes. You can insert cout << "Person record is at address " << &p << endl; before the call of show as well as the beginning of show. This reveals different memory addresses of the record.
Judging copies by the object's address is incorrect methodology. In both C++ and Rust, "moving" an object will still copy the struct fields, but will avoid copying any of the pointees (such as the variable-size array that the string owns).
> 2. Replace void show(Person person) with void show(Person& person). So only the function needs to change. The caller does not have to adapt to it.
Passing by reference is a different concept to moving. While the author used this approach for C++, they did not use the same approach for Rust. This is comparing apples to oranges.
- ajross 2y ago> In both C++ and Rust, "moving" an object will still copy the struct fields, but Most people consider a shallow copy a "copy", certainly a shallow copy isn't a "reference"! One of the big problems in this space is in fact the divergence of terminology that leads to arguments like this. The introduction of move semantics to C++ was a terrible, terrible mistake; not because it doesn't solve a real problem but because the language is objectively much worse now as a routine tool for general developers. People used to hack on code to implement features, now they get confused over and argue about how many "&" characters they need in a function signature. It was a problem that was best left unsolved, basically.
- webnrrd2k 2y agoRe: "problem that was best left unsolved" This is a good example of a hard-won life lesson... There might be a solution to a problem, but the solution is worse than the original problem. I semi-jokingly call this "the healing power of apathy". The reality of it is that, sometimes, there are problems in life where benign neglect is the best response.
- otabdeveloper4 2y ago> now they get confused Sounds like a skill issue. Maybe they should go shopping. Jokes aside though, yeah, move semantics is taught bad. Once you start using it (say, with a unique_ptr in a container) it will quickly start making sense.