Q: When should it be used? A: You should use std::move if you want to call functions that support move semantics with an argument which is not an rvalue (temporary expression).
Std::move is not faster than straight up copying.
std::move. std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.
std::move is totally unnecessary when returning from a function, and really gets into the realm of you -- the programmer -- trying to babysit things that you should leave to the compiler.
Yes, *this
is always an lvalue, no matter how a member function is called, so if you want the compiler to treat it as an rvalue, you need to use std::move
or equivalent. It has to be, considering this class:
struct A {
void gun() &; // leaves object usable
void gun() &&; // makes object unusable
void fun() && {
gun();
gun();
}
};
Making *this
an rvalue would suggest that fun
's first call to gun
can leave the object unusable. The second call would then fail, possibly badly. This is not something that should happen implicitly.
This is the same reason why inside void f(T&& t)
, t
is an lvalue. In that respect, *this
is no different from any reference function parameter.
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