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);
“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.
An lvalue reference can bind to an lvalue, but not to an rvalue.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With