std::move doesn't move anything! (contrary to it's name). It is exactly equivalent to a static_cast to an rvalue
reference type.
That it, it is just a cast to rvalue- more specifically to an xvalue, as opposed to a prvalue. And it is also true that having a cast named move sometimes confuses people. However the intent of this naming is not to confuse, but rather to make your code more readable.
Using the xvalue
, we can trigger the right overload and hence, we can use std::swap in such overloads to take the ownership of another object (but aren't required).
For example, a move constructor of a linked list might copy the pointer to the head of the list and store nullptr
in the argument instead of allocating and copying individual nodes.
why does it still have 2 as its value
As mentioned std::move
doesn't move and the real job of swapping/moving the resources is being performed by the overloads like move constructor and move assignment. std::move
tasks is just to cast so that compiler can call the right overload (for example, move constructor in favor of copy constructor) and the actual moving of resources has to be defined by the software developer in their respective overloads. Since, fundamental types like int
doesn't have any move constructor, the statement int c = std::move(a);
merely copies the value of a
to c
.
Try this:
#include <iostream>
#include <utility>
void hello(int& a)
{
std::cout << "LVALUE" << std::endl;
}
void hello(int&& a)
{
std::cout << "RVALUE" << std::endl;
}
int main(void)
{
int a = 8;
hello(a);
hello(std::move(a));
return 0;
}