Discussion: Will It Move?
Understand why moving from a const object in C++ still results in copying rather than moving. Learn about lvalues, rvalues, std::move, and how defaulted move constructors handle const members by using the copy constructor. This lesson deepens your grasp of object initialization nuances in C++.
Run the code
Now, it’s time to execute the code and observe the output.
The WillItMove has a const member. We can’t move from a const object, so why could we still move from objectWithConstMember?
Understanding the output
In this puzzle, we first initialize WillItMove objectWithConstMember;. Then, on line 17, we initialize a new WillItMove moved{std::move(objectWithConstMember)};. Since WillItMove has no copy constructor, the only alternative to initialize moved is the move constructor. But how can the move constructor perform a move operation from the const Member constMember?
The move constructor
Let’s start digging from the outside in.
First, what does std::move(objectWithConstMember) do? The ...