It makes sense that C++ avoids unnecessary copying or object creation whenever possible, that's pretty much C++'s M.O.
Old habits die hard? It used to always create a temporary right?
Temporaries could still be elided in some cases before, but the semantics were still understood in terms of temporary objects.
Now some forms of elision are mandatory and elision+RVO semantics are understood as objects being directly created into the final named location.
I find that understanding how memory is layed out in executable, how the C works in terms of stack, symbols etc is the introductory knowledge I had to obtain to even think about C++. Not sure what's there now, because I saw recently some great compiler warnings, but I'm pretty sure that I did convert a prvalue to a pointer reference (&) at least once in my life and later failed with memory problems, but no compiler errors
C++ needs those extra categories, they exist for good reasons. But it is more complex.
Similarly just because a compiler may optimize a prvalue away doesn't change the fact that a prvalue by definition of the language is a temp.
dzdt•6d ago
There is a pretty good stack overflow post that quuxplusone linked below. How they explain it:
quuxplusone•4d ago
Now, if you don't know what "move semantics" is, then "lvalues can't be moved from" isn't terribly helpful, and if you do then it's tautological, so I'm not saying you're wrong to criticize. :) But in a C++ context, "move" does have a single specific meaning — the one he's using properly if opaquely-to-non-C++ers.
Conscat•1h ago
cjensen•1h ago
But here's the gist: sometimes you have an object you want to copy, but then abandon the original. Maybe it's to return an object from a function. Maybe it's to insert the object into a larger structure. In these cases, copying can be expensive and it would be nice if you could just "raid" the original object to steal bits of it and construct the "copy" out of the raided bits. C++11 enabled this with rvalue references, std::move, and rvalue reference constructors.
This added a lot of "what the hell is this" to C++ code and a lot of new mental-model stuff to track for programmers. I understand why it was all added, but I have deep misgivings about the added complexity.
[1] https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues...
neonz80•24m ago