r/ProgrammerHumor • • Oct 10 '26

Meme theyAreLovingIt

Post image
2.1k Upvotes

83 comments sorted by

View all comments

Show parent comment

1

u/conundorum Oct 11 '26

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.

1

u/Hohenheim_of_Shadow Oct 11 '26

That's kinda my point? Pass by Move is a complex memory operation that is rightfully niche is C++.

Pass by Move is the default in Rust, which means you have to deal with it more.