Why does std :: move () work in C ++?

The following is a snippet of code:

int i=0;
int&&k=std::move(i);

In c ++ primer move

template <typename T>
typename remove_reference<T>::type &&move(T&& t)
{return static_cast<typename remove_reference<T>::type&&>(t);}

As far as I know, this pattern std::movesubtracts a function like

int&& move(int& t){return static_cast<int&&>(t);}

As a comparison and to develop my question, consider an example:

 int test(int k){k=66;return k;}
 int k;
 int a=test(k);

The above code will be compiled as:

int temp;//the temporary object
temp=k;
int a=temp;

Similarly, I think that the first code fragment will be compiled as:

int&& temp=0;
int&& k=temp;//oop!temp is an lvalue!

Which seems wrong because it tempis an lvalue, did I get something wrong?

+6
source share
1 answer

Similarly, I think that the first code fragment will be compiled as:

int&& temp=0;
int&& k=temp;//oop!temp is an lvalue!

Perhaps you confuse the type with the category of values .

++ ( , , ..) : .

int&&k=std::move(i); int&& k=temp; . std::move rvalue-, std::move rvalue ( x), rvalue-reference. , temp , lvalue, , int, int&, int&& .. Lvalue rvalue-reference.

lvalue:

lvalue:

  • ,...

xvalue (rvalue):

xvalue:

  • , rvalue , std::move(x);
  • ...
  • , static_cast<char&&>(x);
+5

All Articles