Really, in C++, you should rarely need to use std::move unless you're explicitly changing an object's memory address, handling pointers yourself, working with things that are expensive to copy, or are explicitly preventing copy construction/assignment. The general rule is that if you get the copy & the destructor for free, then you get the move for free, so you only need move operators if you manually defined copy operators and/or destructors. (On the logic that if the class is complex enough to need hand-crafted copying and/or destruction, then it's complex enough to need hand-crafted moving, too.) And copy elision & RVO/NRVO (different kinds of return value optimisation) change a lot of would-be copies into moves during compilation...
The only issue is that it's never explained that you can use std::move to turn any pass-by-copy into a pass-by-move, more than anything else. If people knew that, then we'd see it a lot more often.
View all comments
Show parent comment
1
u/conundorum Oct 11 '26
Really, in C++, you should rarely need to use
std::moveunless you're explicitly changing an object's memory address, handling pointers yourself, working with things that are expensive to copy, or are explicitly preventing copy construction/assignment. The general rule is that if you get the copy & the destructor for free, then you get the move for free, so you only need move operators if you manually defined copy operators and/or destructors. (On the logic that if the class is complex enough to need hand-crafted copying and/or destruction, then it's complex enough to need hand-crafted moving, too.) And copy elision & RVO/NRVO (different kinds of return value optimisation) change a lot of would-be copies into moves during compilation...The only issue is that it's never explained that you can use
std::moveto turn any pass-by-copy into a pass-by-move, more than anything else. If people knew that, then we'd see it a lot more often.