Copy versus Move Semantic
Explore the fundamental differences between copy and move semantics in Modern C++. Understand how move operations transfer ownership to optimize resource use and improve performance, reducing costly copies. This lesson helps you grasp essential techniques for managing memory efficiently in embedded and critical systems development.
We'll cover the following...
A lot has been written on the advantages of the move semantic over the copy semantic. Rather than an expensive copy operation, we can use a cheap move operation. Let’s break than down further.
There is one subtle difference between copy and move semantic: if we create a new object based on an existing one, the copy semantic will copy the elements of the resource, while the move semantic will move the elements of the resource. Of course, copying is expensive, and moving is cheap, but there are additional serious consequences to this technique:
- With copy semantic, a std::bad_alloc will be thrown because the program is out of memory.
- The resource of the move operation