In C++ is the compiler allowed to determine that a variable is not used after a statement and therefore move from it even though it is an lvalue?
For example:
{
std::string s = "Hello";
myvector.push_back(s);
}
It seems that the compiler could tell that s is never used again and treat it as a temporary, eliminating the copy.
I've seen code at a large company littered with std::moves to improve efficiency. This seems really unsafe since you have to look at the whole function to tell whether a variable is still valid or not, and if you rearrange any lines you could accidentally blow everything up. In almost all of these cases the variable is never referred to after the move. It seems that these optimizations would be better left up to the compiler and only the logic left to us humans.