Move Semantics Without the Folklore
There is a reported interview question on what std::move does to an rvalue, and the language-semantics assessments reach for the same area. It is fertile ground for interviewers because the folklore version, "move is faster than copy", is close enough to be useful and wrong in every specific case where it matters.
std::move does not move
std::move(x) is a cast. It produces an rvalue reference to x and generates no instructions at all. Nothing is moved by it, nothing is modified, and if no move constructor or move assignment operator is subsequently selected, nothing happens at all.
What it does is change overload resolution. Passing an lvalue selects the copy constructor; passing the result of std::move selects the move constructor if the type has one, and the copy constructor if it does not. So std::move is a request, and the type decides whether to honour it.
The rest of this lesson is for subscribers
Unlock every lesson in Systems Programming for Trading, and every other premium course.
Subscribe to continueTest your knowledge
Keep reading Systems Programming for Trading
19 lessons in this course, and every other premium course, on one subscription.
- Every lesson in every course, with the worked examples and interactive simulators
- Graded questions on every lesson, with explanations for the wrong answers as well as the right one
- The trainers, timed assessments and brainteaser library that go with them