This is admittedly an insignificant question, which is mainly due to curiosity. Suppose we have:
int x = 5; int&& xref = std::move(x); std::cout << "Before assignment x: " << x << std::endl; std::cout << "Before assignment xref: " << xref << std::endl; xref = 10; std::cout << "After assignment x: " << x << std::endl; std::cout << "After assignment xref: " << xref << std::endl;
Expected Result:
// Before assignment x: 5 // Before assignment xref: 5 // After assignment x: 10 // After assignment xref: 10
It makes sense. std::move converts x to the value of x and allows you to bind its memory to xref and change its contents accordingly. Now let's say that we have the following:
int&& xref = 5; std::cout << "Before assignment xref: " << xref << std::endl; xref = 10; std::cout << "After assignment xref: " << xref << std::endl; int x = 5; std::cout << "After assignment x: " << x << std::endl;
The output is intuitive:
// Before assignment xref: 5 // After assignment xref: 10 // After assignment x: 5
This is a common sense. We expect to be able to bind the constant literal 5 to xref , because 5 is the value of prvalue. We also expect xref be volatile. We also expect that the value of the constant literal 5 not modified (as shown somewhat pedantically in the last two lines of the above fragment).
So my question is: what exactly is going on here? As C ++ knows, do not change the value of the constant literal 5 , but keeping a sufficient identifier for xref to know that it has been changed to 10 by assignment. Is a new variable created when assigning xref when it is bound to a constant literal? This question never arose in C ++ 03, since only const references could be related to rvalues.
source share