Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any difference between static cast to rvalue reference and std::move

The description for static cast says

If new_type is an rvalue reference type, static_cast converts the value of expression to xvalue. This type of static_cast is used to implement move semantics in std::move.(since C++11)

Does this confirm that the following are equivalent ?

(A)

X x1; X x2 = static_cast<X&&>(x1);  

(B)

X x1; X x2 = std::move(x1); 
like image 568
Aditya Sihag Avatar asked Jun 17 '13 07:06

Aditya Sihag


People also ask

What is the difference between lvalue and rvalue references?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable.

Can Lvalue bind to rvalue reference?

An lvalue reference can bind to an lvalue, but not to an rvalue.


1 Answers

Yes there is a very important difference: std::move documents what you want to do. In addition the cast is prone to writing errors like a forgotten & or wrong type X.

As it can be seen, std::move is even less to type.

like image 107
Jan Herrmann Avatar answered Sep 28 '22 07:09

Jan Herrmann