std::string s = "y";
s = "x" std::move(s) "x";
Send(std::move(s));
Microsoft STL implementation checks for this, but is it mandated by the standard?
It looks more clean than insert append or two variables approach.
Note: I know I can do Send(std::move("x" std::move(s) "x"))
, but real code is not so simple.
CodePudding user response:
There's no self-move here. A self-move is something like this:
s = std::move(s);
which means
s.operator=(std::move(s));
In other words, when operator=
is called, this
points to the same string as the argument.
In your code, "x" std::move(s)
will be evaluated first, which returns a std::string
prvalue. In other words, not a reference to s
, although it's possible that the concatenation might "steal" the buffer from s
. Then the second
is evaluated, and another prvalue is returned. Finally, operator=
is called. At this point, the prvalue on the right-hand side has to be materialized, because it's bound to the rvalue reference argument of operator=
. So all we have is s
being assigned from a temporary object, which might have stolen s
's buffer. But no matter: s
is an object of a standard library type, which means that it is in a "valid but unspecified state". Since s
is in a valid state, and is not the same object as the RHS, there is no problem.