std::move
is just casting your object to an rvalue reference. Since your function takes the reference and just do something with it, no ownership is taken here, so your string is still in a valid state and can be used safely.
I would not recommend using this in your code because it's misleading, as a lot of people would consider your string as invalid, because taking ownership is the primary use of rvalue reference, hence std::move
.
If you're really need to call this function this way, I would recommend writing this:
std::string myString{"a string"};
// leave a comment here to explain what you are doing.
f(static_cast<std::string&&>(myString));
However, please note that your example would be really different if the function f
took value instead of a reference. In that case, calling it with both std::move
or static_cast
would invalidate the string.