Move Semantics Explained

Get introduced to move semantics and some important rules for handling resources inside a class.

Understanding Move Semantics

Move semantics is a concept introduced in C++11 that is quite hard to grasp, even by experienced programmers. Therefore, we will try to give you an in-depth explanation of how it works, when the compiler utilizes it, and, most importantly, why it is needed. Essentially, the reason C++ even has the concept of move semantics, whereas most other languages don’t, is a result of it being a value-based language, as discussed before. If C++ did not have move semantics built-in, the advantages of value-based semantics would get lost in many cases, and programmers would have to perform one of the following trade-offs:

  • Performing redundant deep-cloning operations with high-performance costs
  • Using pointers for objects like Java does, losing the robustness of value semantics
  • Performing error-prone swapping operations at the cost of readability

We do not want any of these, so let’s have a look at how move semantics help us.

Create a free account to view this lesson.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy