Reputation: 223
So, this provides the intended output:
void f(std::string&& s)
{
s += " plus extra";
}
int main(void)
{
std::string str = "A string";
f( std::move(str) );
std::cout << str << std::endl;
return 0;
}
A string plus extra
That is, it works when I run it on Ideone, but is it UB? Adding extra string initializations before and after the call to f
didn't change anything.
Upvotes: 17
Views: 1153
Reputation: 17999
std::move
doesn't move anything. It indicates that an object may be "moved from", by casting its argument to an rvalue.
Your code is valid and there is no move operation performed.
You would get the behavior where the state of str
is unspecified after calling f()
, if you move-construct another string object from s
. The move-constructor performs the actual move operation.
Example:
std::vector<std::string> sv;
void f(std::string&& s)
{
s += " plus extra";
sv.push_back(std::move(s)); // move s (str) into a new object
}
int main(void)
{
std::string str = "A string";
f(std::move(str));
std::cout << str << std::endl; // output: empty string
std::cout << sv.back() << std::endl; // output: "A string plus extra"
return 0;
}
Upvotes: 10
Reputation: 726539
The code is valid, because there no actual moving is performed. Here is how you could make it invalid:
string f(std::string&& s) {
std::string res(std::move(s));
res += " plus extra";
return res;
}
The state of str
after this call would be valid, but unspecified. This means that you could still assign a new value to str
to put it back into a valid state, but you wouldn't be able to output it without invoking unspecified behavior (demo). See this Q&A for specifics on the moved-from state.
Upvotes: 9
Reputation: 41760
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.
Upvotes: 3
Reputation: 218750
It is valid, not UB.
It is also horribly obfuscated code. std::move(s)
is nothing but a cast to rvalue. By itself it does not actually generate any code at all. Its only purpose is to turn an lvalue into an rvalue so that client code can overload on lvalue/rvalue expressions (of string
in this case).
You should pass by lvalue-reference for this case:
void f(std::string& s)
{
s += " plus extra";
}
...
f( str );
Or alternatively, pass by value and return a new string:
std::string f(std::string s)
{
s += " plus extra";
return s;
}
...
str = f( std::move(str) );
Upvotes: 18