Hi guys, I just encountered a few problems regarding the usage of move() function.
1. Normally, the compiler forbids users to bind an lvalue(include an rvalue reference which is an lvalue itself) to an rvalue reference, such as the code below, which would generate a error.
However, when I used move() function to convert an lvalue to an rvalue reference, then surprisingly I managed to bind an converted rvalue reference to another one.
|
int &&a = 1, &&b = move(a);
|
why?
2. As shown in the code below, by using move()funciton, when I bound a converted lvalue to an rvalue reference, and then changed the value of the rvalue reference, the binding from lvalue was changed as well, which indicated that the rvalue reference was dependent on the lvalue.
1 2 3 4
|
int a = 1, &&b = move(a);
cout << a << " " << b << endl;
b = 2;
cout << a << " " << b << endl;
|
But when I bound some other rvalue reference that was converted from some random rvalue or lvalue to the rvalue reference, and then rebound an rvalue reference that was converted from an lvalue to the rvalue reference, then it showed that this time the rvalue reference and the lvalue were independent.
1 2 3 4 5
|
int a = 1, &&b = move(int(2));
b = move(a);
cout << a << " " << b << endl;
b = 2;
cout << a << " " << b << endl;
|
And what surprised me more was that I could even bind an lvalue to "b".
1 2 3 4 5
|
int a = 1, &&b = move(int(2));
b = a;
cout << a << " " << b << endl;
b = 2;
cout << a << " " << b << endl;
|
why?
These are all my questions, thanks!