ValueFlow: avoid various unnecessary copies by firewave · Pull Request #7583 · cppcheck-opensource/cppcheck
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It matches the pattern we currently use all over the code.
I havent really seen this pattern in the code. We usually use a const ref when we only want to read the values.
Although the explicit rvalue move makes the code more explicit it also uglifies it.
Using a non-const ref makes the code harder to read. It looks like the intention is not to read the value but to modify it non-locally. The rvalue makes the intention clear that it wants to move the value, so it works just like a non-reference where mutations are local since it has unique ownership.
const T& x = yshows it will only readxandyand there is no mutations.T x = yshows it will not mutateyand any mutations toxare localT& x = yshows it will mutatexandy, so mutations are not local.T&& x = std::move(y)showsxhas unique ownership ofyand any mutations are local sinceywill not be used later. So you can essentially read it asT x = y.
Writing T& x = y when you are really doing T&& x = std::move(y) requires more time to read the scope of everything to understand what is really being mutated which is why it makes it harder to read.