Move Semantics in C++11/14/17/20

C-Plus-Plus C++ 14 C++ 17 C++ 20

In this post we are going to understand what is Move Semantics in C++11/14/17/20.As we already understood what are lvalues and rvalues, also we understand what are lvalue and rvalue references, now we are ready to understand what is move semantics concept in C++11/14/17/20 and how to use it.

What is Move Semantics in C++11/14/17/20


As the name itself is suggesting, move semantics is just moving things around and rearranging them to achieve better performance. Things here mean variables, pointers, and memory associated with them.

The main purpose of using move semantics is to avoid copying data around and instead of creating copies we prefer to use the memory location to get pointed by references at runtime.

Where can we use move semantics


This is most important question to answer, where we can use it. This question will help us understand the need and then we will be able to better understand the concept.

  • The first rule is that you can use move semantic when you have a memory associated with an object, variable, etc.
  • The second rule is the object or variable that you want to use in your move, should support the move functions which will do the actual move for you in the background.

As you know the copying task is mainly managed by the copy constructor and copy assignment operator. so to achieve the functionality of moving data C++ 11 introduces the use of a move constructor and move assignment operator. move constructor and move assignment operator works on rvalue references that have an address associated with it.