What exactly happens when we use rvalue links and how does std :: move work?

I am trying to understand rvalue reference and move semantics. In the following code, when I pass 10 to the Print function, it causes a reassignment of the rvalue reference value, which is expected. But what exactly is happening, where 10 will be copied (or where it is indicated from). Secondly, what does std::move actually do? Does the value 10 get from i and then pass it? Or is it an instruction for the compiler to use the rvalue link?

 void Print(int& i) { cout<<"L Value reference "<<endl; } void Print(int&& i) { cout<<"R Value reference "<< endl; } int main() { int i = 10; Print(i); //OK, understandable Print(10); //will 10 is not getting copied? So where it will stored Print(std::move(i)); //what does move exactly do return 0; } 

Thanks.

+6
source share
3 answers

But what exactly happens when this 10 is copied (or where it refers from)

A temporary value is created and the link is passed to the function. Time series are values, so you can bind them to rvalue values; therefore, a second overload is selected.

Secondly, what is actually std::move ?

It gives an rvalue reference to its argument. It is equivalent (by definition) to static_cast<T&&> .

Despite the name, it makes no movement; it just gives you a link that can be used to move the value.

+8
source

In case 10 , optimizations are likely to be involved that will change the actual implementation, but conceptually the following happens:

  • A temporary int is created and initialized with a value of 10 .

  • This temporary int is associated with the function parameter of the g-value reference.

Thus, conceptually there is no copying - the link will refer to a temporary one.

As for std::move() : there may be some complex bits related to links, etc., but basically it's just a reference to an r-value. std::move() doesn't actually std::move() anything. It just turns its argument into a r value, so it can be transferred from.

"Moving" is not really a specific operation. While it is convenient to think about moving, it is important to note the value of l-value vs. r-value.

"Movement" is usually implemented by move constructors, assignment operators, and functions that use r-value references (for example, push_back() ). It is their implementation that makes the movement a real movement โ€” that is, they are implemented so that they can โ€œstealโ€ r-value resources instead of copying them. This is because, as an r-value, it will no longer be available (or you promise the compiler).

This is why std::move() allows you to "move" - โ€‹โ€‹it turns its argument into an r-value, an alarm, "hey, compiler, I will no longer use this value l, you can let functions (for example, move ctors) examine it like r-value and steal from him. "

+9
source

std::move enter int into int&& via static_cast<int&&> . After all, if the type is class or struct , the move constructor, if defined (implicitly or explicitly), will be called instead of copy constructor / classical constructor .

-1
source

Source: https://habr.com/ru/post/954993/


All Articles