Move Semantic
Explore how move semantic in Modern C++ enhances resource management by prioritizing efficient resource transfer over copying. Understand the role of std::move, move constructors, and move assignment operators in STL containers and user-defined classes. Learn the rules governing automatically generated special methods to write optimized, maintainable embedded code.
We'll cover the following...
We'll cover the following...
Containers of the standard template library (STL) can have non-copyable elements. The copy semantic is the fallback for the move semantic. Let’s learn more about the move semantic.
std::move
The function std::move moves its resource.
- The function needs the header
<utility>. - The function converts the type of its argument into a rvalue reference.
- The compiler applies move semantic to the rvalue reference.
std::moveis under the hood astatic_castto an rvalue reference.
static_cast<std::remove_reference<decltype(arg)>::type&&>(arg);
- What is happening here?
decltype(arg): deduces the type of the argumentstd::remove_reference<....>removes all references from the type of the argumentstatic_cast<....&&>adds two references to the type
...