EDIT: Consider 2 following examples:
std::string x;
{
std::string y = "extremely long text ...";
...
x = y; // *** (1)
}
do_something_with(x);
struct Y
{
Y();
Y(const Y&);
Y(Y&&);
... // many "heavy" members
};
struct X
{
X(Y y) : y_(std::move(y)) { }
Y y_;
}
X foo()
{
Y y;
...
return y; // *** (2)
}
In both examples y on lines (1) and (2) is near end of its lifetime and is about to be destroyed. It seems obvious that it can be treated as an rvalue and be moved in both cases. In (1) its contents can be moved into x and in (2) into temp instance of X().y_.
My questions are:
1) Will it be moved in either of the above examples? (a) If yes, under what standard provision. (b) If no, why not? Is that an omission in the standard or is there another reason that I am not thinking of?
2) If the above answer is NO. In the first example I can change (1) to x = std::move(y) to force the compiler to perform the move. What can I do in the second example to indicate to the compiler that y can be moved? return std::move(y)?
NB: I am purposely returning an instance of Y and not X in (2) to avoid (N)RVO.