Lefsler
Lefsler

Reputation: 1768

Using std::move, and preventing further use of the data

I have been using c++11 for some time but I always avoided using std::move because I was scared that, while reading a library where the user does not have the access to the code, it would try to use the variable after I move it.

So basically something like

void loadData(std::string&& path);

Would not be enough to make the user understand that it will be moved. Is it expected that the use of && would imply that the data will be moved. I know that comments can be used to explain the use case, but a lot of people dont pay attention to that.

Is it safe to assume that when you see a && the data will be moved, or when should I use std::move and how to make it explicit from the signature.

Upvotes: 1

Views: 38

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473547

Is it expected that the use of && would imply that the data will be moved.

Generally speaking yes. A user cannot call loadData with an lvalue. They must provide a prvalue or an xvalue. So if you have a variable to pass, your code would generally look like loadData(std::move(variable)), which is a pretty good indicator of what you're doing from your side. forwarding could also be employed, but you'd still see it at the call site.

Indeed, generally speaking it is extremely rude to move from a parameter which is not an rvalue reference.

Upvotes: 2

Related Questions