According to C++ standard:
Copy elision is one of the two allowed forms of optimization, alongside allocation elision and extension, (since C++14)
And if I recall correctly, copy elision is only allowed in return, throw, catch, and yield statement.
In that case, for code like this:
void func(type x)
{
auto y = bar();
// do something with y
x.foo = y; // Can this be optimized?
return;
}
Is it theoretically permitted by the standard for compiler to use move constructor on lvalue y
(potentially by converting it to xvalue implicitly) suppose the compiler is smart enough to figure out that the conversion is safe/equivalent (unlike the corner case mentioned in this answer).