Question

int main()
{
    int rx = 0;
    int ry = std::move(rx); //here is the point of my question

    int lx = 0;
    int ly = &lx; //(2) obviously doesn't compile

    std::cin.ignore();
}

I'm a little bit lost with this aspect of rvalue, I can't understand how we can't bind &&rx to ry, because std::move(rx) is a reference to a rvalue, so I believed that this kind of expression could only be bind to a reference type as is it he case for lvalue reference and illustrated in (2)

Was it helpful?

Solution

References != address-of operator.

int& ly = lx; // reference
int* ly = &lx; // pointer

OTHER TIPS

std::move obtains an rvalue reference to its argument and converts it to an xvalue. [1]

Which in turn can be copied to ry.

The expression int ry = std::move(rx); does not "bind" rx to ry. It tells the compiler that rx is no longer needed and that its contents can be moved to ry while at the same time invalidating rx.

This is especially useful when functions return by value:

std::vector<int> foo() {
    std::vector<int> v = {1,2,3,4};
    return v;
}
std::vector<int> u = foo();

At return v the compiler notice that v is no longer needed an that it can actually use it directly as u without doing a deep copy of the vector contents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top